diff --git a/0004-median-of-two-sorted-arrays/NOTES.md b/0004-median-of-two-sorted-arrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0004-median-of-two-sorted-arrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp new file mode 100644 index 00000000..0d8a867c --- /dev/null +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + bool isPalindrome(int x) { + + if(x < 0 or (x % 10 == 0 and x != 0)) + return false; + + int rev = 0; + + while(rev < x) + { + rev = (rev * 10) + (x%10); + x /= 10; + } + + return (rev == x or rev/10 == x); + } +}; \ No newline at end of file diff --git a/0009-palindrome-number/NOTES.md b/0009-palindrome-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0009-palindrome-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0009-palindrome-number/README.md b/0009-palindrome-number/README.md new file mode 100644 index 00000000..135519db --- /dev/null +++ b/0009-palindrome-number/README.md @@ -0,0 +1,33 @@ +

9. Palindrome Number

Easy


Given an integer x, return true if x is a palindrome, and false otherwise.

+ +

 

+

Example 1:

+ +
Input: x = 121
+Output: true
+Explanation: 121 reads as 121 from left to right and from right to left.
+
+ +

Example 2:

+ +
Input: x = -121
+Output: false
+Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
+
+ +

Example 3:

+ +
Input: x = 10
+Output: false
+Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
+
+ +

 

+

Constraints:

+ + + +

 

+Follow up: Could you solve it without converting the integer to a string?
\ No newline at end of file diff --git a/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp b/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp new file mode 100644 index 00000000..7059bd67 --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp @@ -0,0 +1,33 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + + ListNode* fast = head, *slow = head; + + while(n--) + fast = fast->next; + + if(!fast) + return head->next; + + while(fast->next) + { + fast = fast->next; + slow = slow->next; + } + + slow->next = slow->next->next; + + return head; + } +}; \ No newline at end of file diff --git a/0019-remove-nth-node-from-end-of-list/NOTES.md b/0019-remove-nth-node-from-end-of-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0019-remove-nth-node-from-end-of-list/README.md b/0019-remove-nth-node-from-end-of-list/README.md new file mode 100644 index 00000000..b1ecfb61 --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/README.md @@ -0,0 +1,34 @@ +

19. Remove Nth Node From End of List

Medium


Given the head of a linked list, remove the nth node from the end of the list and return its head.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4,5], n = 2
+Output: [1,2,3,5]
+
+ +

Example 2:

+ +
Input: head = [1], n = 1
+Output: []
+
+ +

Example 3:

+ +
Input: head = [1,2], n = 1
+Output: [1]
+
+ +

 

+

Constraints:

+ + + +

 

+

Follow up: Could you do this in one pass?

+
\ No newline at end of file diff --git a/0022-generate-parentheses/0022-generate-parentheses.cpp b/0022-generate-parentheses/0022-generate-parentheses.cpp new file mode 100644 index 00000000..3d3bd40c --- /dev/null +++ b/0022-generate-parentheses/0022-generate-parentheses.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + + void generate(int openBracket, int closeBracket, int n, string ds, vector& answer) + { + if(openBracket == n and closeBracket == n) + { + answer.push_back(ds); + return; + } + + if(openBracket < n) + generate(openBracket+1, closeBracket, n, ds + '(', answer); + if(closeBracket < openBracket) + generate(openBracket, closeBracket+1, n, ds + ')', answer); + } + + vector generateParenthesis(int n) { + + vector answer; + + generate(0, 0, n, "", answer); + + return answer; + } +}; \ No newline at end of file diff --git a/0022-generate-parentheses/README.md b/0022-generate-parentheses/README.md new file mode 100644 index 00000000..154f576c --- /dev/null +++ b/0022-generate-parentheses/README.md @@ -0,0 +1,17 @@ +

22. Generate Parentheses

Medium


Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

+ +

 

+

Example 1:

+
Input: n = 3
+Output: ["((()))","(()())","(())()","()(())","()()()"]
+

Example 2:

+
Input: n = 1
+Output: ["()"]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= n <= 8
  • +
+
\ No newline at end of file diff --git a/0026-remove-duplicates-from-sorted-array/README.md b/0026-remove-duplicates-from-sorted-array/README.md new file mode 100644 index 00000000..ec027591 --- /dev/null +++ b/0026-remove-duplicates-from-sorted-array/README.md @@ -0,0 +1,52 @@ +

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.

+ +

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

+ +
    +
  • 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.
  • +
  • Return k.
  • +
+ +

Custom Judge:

+ +

The judge will test your solution with the following code:

+ +
int[] nums = [...]; // Input array
+int[] expectedNums = [...]; // The expected answer with correct length
+
+int k = removeDuplicates(nums); // Calls your implementation
+
+assert k == expectedNums.length;
+for (int i = 0; i < k; i++) {
+    assert nums[i] == expectedNums[i];
+}
+
+ +

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

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -100 <= nums[i] <= 100
  • +
  • nums is sorted in non-decreasing order.
  • +
+
\ No newline at end of file diff --git a/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp b/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp new file mode 100644 index 00000000..16bba9d0 --- /dev/null +++ b/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + + int binarySearchFirst(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int first = - 1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + first = mid; + end = mid-1; + } + else if(nums[mid] < target) + { + start = mid+1; + } + else + { + end = mid - 1; + } + } + + return first; + } + + int binarySearchEnd(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int last = -1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + last = mid; + start = mid+1; + } + else if(nums[mid] > target) + { + end = mid-1; + } + else + { + start = mid+1; + } + } + + return last; + } + + vector searchRange(vector& nums, int target) { + + int n = nums.size(); + + int first = -1, last = -1; + + first = binarySearchFirst(nums, target); + last = binarySearchEnd(nums, target); + + return {first, last}; + + } +}; \ No newline at end of file diff --git a/0034-find-first-and-last-position-of-element-in-sorted-array/README.md b/0034-find-first-and-last-position-of-element-in-sorted-array/README.md new file mode 100644 index 00000000..3ba57ee9 --- /dev/null +++ b/0034-find-first-and-last-position-of-element-in-sorted-array/README.md @@ -0,0 +1,27 @@ +

34. Find First and Last Position of Element in Sorted Array

Medium


Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

+ +

If target is not found in the array, return [-1, -1].

+ +

You must write an algorithm with O(log n) runtime complexity.

+ +

 

+

Example 1:

+
Input: nums = [5,7,7,8,8,10], target = 8
+Output: [3,4]
+

Example 2:

+
Input: nums = [5,7,7,8,8,10], target = 6
+Output: [-1,-1]
+

Example 3:

+
Input: nums = [], target = 0
+Output: [-1,-1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
  • nums is a non-decreasing array.
  • +
  • -109 <= target <= 109
  • +
+
\ No newline at end of file diff --git a/0040-combination-sum-ii/0040-combination-sum-ii.cpp b/0040-combination-sum-ii/0040-combination-sum-ii.cpp new file mode 100644 index 00000000..0e49d197 --- /dev/null +++ b/0040-combination-sum-ii/0040-combination-sum-ii.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + void helper(int idx, int target, vector ds, vector>& res, vector& candidates) + { + if(target == 0) + { + res.push_back(ds); + return; + } + + for(int i = idx; i < candidates.size(); ++i) + { + if(i != idx and candidates[i] == candidates[i-1]) + continue; + if(target - candidates[i] < 0) + break; + ds.push_back(candidates[i]); + helper(i+1, target-candidates[i], ds, res, candidates); + ds.pop_back(); + } + + } + + vector> combinationSum2(vector& candidates, int target) { + + vector ds; + vector> res; + + sort(candidates.begin(), candidates.end()); + + helper(0, target, ds, res, candidates); + + return res; + + } +}; \ No newline at end of file diff --git a/0040-combination-sum-ii/README.md b/0040-combination-sum-ii/README.md new file mode 100644 index 00000000..41ac5070 --- /dev/null +++ b/0040-combination-sum-ii/README.md @@ -0,0 +1,38 @@ +

40. Combination Sum II

Medium


Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

+ +

Each number in candidates may only be used once in the combination.

+ +

Note: The solution set must not contain duplicate combinations.

+ +

 

+

Example 1:

+ +
Input: candidates = [10,1,2,7,6,1,5], target = 8
+Output: 
+[
+[1,1,6],
+[1,2,5],
+[1,7],
+[2,6]
+]
+
+ +

Example 2:

+ +
Input: candidates = [2,5,2,1,2], target = 5
+Output: 
+[
+[1,2,2],
+[5]
+]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= candidates.length <= 100
  • +
  • 1 <= candidates[i] <= 50
  • +
  • 1 <= target <= 30
  • +
+
\ No newline at end of file diff --git a/0041-first-missing-positive/0041-first-missing-positive.cpp b/0041-first-missing-positive/0041-first-missing-positive.cpp new file mode 100644 index 00000000..285acdfb --- /dev/null +++ b/0041-first-missing-positive/0041-first-missing-positive.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int firstMissingPositive(vector& nums) { + + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + if(nums[i] <= 0) + nums[i] = n+1; + } + + for(int i = 0; i < n; ++i) + { + if(abs(nums[i])-1 >= 0 and abs(nums[i])-1 < n) + { + if(nums[abs(nums[i])-1] >= 0) + nums[abs(nums[i])-1] = -nums[abs(nums[i])-1]; + } + } + + for(int i = 0; i < n; ++i) + { + if(nums[i] >= 0) + return i+1; + } + + return n+1; + } +}; \ No newline at end of file diff --git a/0041-first-missing-positive/README.md b/0041-first-missing-positive/README.md new file mode 100644 index 00000000..8c35883a --- /dev/null +++ b/0041-first-missing-positive/README.md @@ -0,0 +1,34 @@ +

41. First Missing Positive

Hard


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

+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,0]
+Output: 3
+Explanation: The numbers in the range [1,2] are all in the array.
+
+ +

Example 2:

+ +
Input: nums = [3,4,-1,1]
+Output: 2
+Explanation: 1 is in the array but 2 is missing.
+
+ +

Example 3:

+ +
Input: nums = [7,8,9,11,12]
+Output: 1
+Explanation: The smallest positive integer 1 is missing.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp new file mode 100644 index 00000000..6ea2c60c --- /dev/null +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int trap(vector& height) { + + int n = height.size(); + + int i = 0, j = n-1, ans = 0; + + int leftMax = 0, rightMax = 0; + + while(i < j) + { + leftMax = max(leftMax, height[i]); + rightMax = max(rightMax, height[j]); + + ans += min(leftMax, rightMax) - min(height[i], height[j]); + + if(height[i] <= height[j]) + ++i; + else + --j; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0042-trapping-rain-water/NOTES.md b/0042-trapping-rain-water/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0042-trapping-rain-water/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0042-trapping-rain-water/README.md b/0042-trapping-rain-water/README.md new file mode 100644 index 00000000..9d727dd1 --- /dev/null +++ b/0042-trapping-rain-water/README.md @@ -0,0 +1,25 @@ +

42. Trapping Rain Water

Hard


Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

+ +

 

+

Example 1:

+ +
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
+Output: 6
+Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
+
+ +

Example 2:

+ +
Input: height = [4,2,0,3,2,5]
+Output: 9
+
+ +

 

+

Constraints:

+ +
    +
  • n == height.length
  • +
  • 1 <= n <= 2 * 104
  • +
  • 0 <= height[i] <= 105
  • +
+
\ No newline at end of file diff --git a/0048-rotate-image/README.md b/0048-rotate-image/README.md new file mode 100644 index 00000000..c415d5c4 --- /dev/null +++ b/0048-rotate-image/README.md @@ -0,0 +1,26 @@ +

48. Rotate Image

Medium


You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

+ +

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [[7,4,1],[8,5,2],[9,6,3]]
+
+ +

Example 2:

+ +
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
+Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
+
+ +

 

+

Constraints:

+ +
    +
  • n == matrix.length == matrix[i].length
  • +
  • 1 <= n <= 20
  • +
  • -1000 <= matrix[i][j] <= 1000
  • +
+
\ No newline at end of file diff --git a/0049-group-anagrams/README.md b/0049-group-anagrams/README.md new file mode 100644 index 00000000..3da623eb --- /dev/null +++ b/0049-group-anagrams/README.md @@ -0,0 +1,24 @@ +

49. Group Anagrams

Medium


Given an array of strings strs, group the anagrams together. You can return the answer in any order.

+ +

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

+ +

 

+

Example 1:

+
Input: strs = ["eat","tea","tan","ate","nat","bat"]
+Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
+

Example 2:

+
Input: strs = [""]
+Output: [[""]]
+

Example 3:

+
Input: strs = ["a"]
+Output: [["a"]]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= strs.length <= 104
  • +
  • 0 <= strs[i].length <= 100
  • +
  • strs[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0054-spiral-matrix/0054-spiral-matrix.cpp b/0054-spiral-matrix/0054-spiral-matrix.cpp new file mode 100644 index 00000000..3397eda9 --- /dev/null +++ b/0054-spiral-matrix/0054-spiral-matrix.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + vector spiralOrder(vector>& matrix) { + + int n = matrix.size(); + int m = matrix[0].size(); + + int top = 0, left = 0, right = m-1, bottom = n-1, dir = 0; + vector ans; + + while(top <= bottom and left <= right) + { + if(dir == 0) + { + for(int i = left; i <= right; ++i) + ans.push_back(matrix[top][i]); + ++top; + } + + else if (dir == 1) + { + for(int i = top; i <= bottom; ++i) + ans.push_back(matrix[i][right]); + --right; + } + + else if(dir == 2) + { + for(int i = right; i >= left; --i) + ans.push_back(matrix[bottom][i]); + --bottom; + } + + else if(dir == 3) + { + for(int i = bottom; i >= top; --i) + ans.push_back(matrix[i][left]); + ++left; + } + + dir = (dir + 1) % 4; + } + + return ans; + } +}; \ No newline at end of file diff --git a/0054-spiral-matrix/NOTES.md b/0054-spiral-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0054-spiral-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0054-spiral-matrix/README.md b/0054-spiral-matrix/README.md new file mode 100644 index 00000000..afec7a07 --- /dev/null +++ b/0054-spiral-matrix/README.md @@ -0,0 +1,25 @@ +

54. Spiral Matrix

Medium


Given an m x n matrix, return all elements of the matrix in spiral order.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [1,2,3,6,9,8,7,4,5]
+
+ +

Example 2:

+ +
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
+Output: [1,2,3,4,8,12,11,10,9,5,6,7]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 10
  • +
  • -100 <= matrix[i][j] <= 100
  • +
+
\ No newline at end of file diff --git a/0057-insert-interval/NOTES.md b/0057-insert-interval/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0057-insert-interval/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0058-length-of-last-word/0058-length-of-last-word.cpp b/0058-length-of-last-word/0058-length-of-last-word.cpp new file mode 100644 index 00000000..16428111 --- /dev/null +++ b/0058-length-of-last-word/0058-length-of-last-word.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int lengthOfLastWord(string s) { + + int i = s.size()-1; + int len = 0; + + while(i >= 0 and s[i] == ' ') + --i; + + while(i >= 0 and s[i] != ' ') + ++len, --i; + + return len; + + } +}; \ No newline at end of file diff --git a/0058-length-of-last-word/NOTES.md b/0058-length-of-last-word/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0058-length-of-last-word/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0058-length-of-last-word/README.md b/0058-length-of-last-word/README.md new file mode 100644 index 00000000..f5d2873a --- /dev/null +++ b/0058-length-of-last-word/README.md @@ -0,0 +1,35 @@ +

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.

+ +

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

+ +

 

+

Example 1:

+ +
Input: s = "Hello World"
+Output: 5
+Explanation: The last word is "World" with length 5.
+
+ +

Example 2:

+ +
Input: s = "   fly me   to   the moon  "
+Output: 4
+Explanation: The last word is "moon" with length 4.
+
+ +

Example 3:

+ +
Input: s = "luffy is still joyboy"
+Output: 6
+Explanation: The last word is "joyboy" with length 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of only English letters and spaces ' '.
  • +
  • There will be at least one word in s.
  • +
+
\ No newline at end of file diff --git a/0067-add-binary/NOTES.md b/0067-add-binary/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0067-add-binary/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0069-sqrtx/0069-sqrtx.cpp b/0069-sqrtx/0069-sqrtx.cpp new file mode 100644 index 00000000..e22f88c4 --- /dev/null +++ b/0069-sqrtx/0069-sqrtx.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int mySqrt(int x) { + + long long start = 1, end = x; + + int ans = 0; + + while(start <= end) + { + long long mid = (start + end) >> 1; + + long long val = mid * 1LL * mid; + + if(val <= x) + { + ans = mid; + start = mid+1; + } + else + { + end = mid-1; + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0069-sqrtx/NOTES.md b/0069-sqrtx/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0069-sqrtx/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0069-sqrtx/README.md b/0069-sqrtx/README.md new file mode 100644 index 00000000..3b33c2e0 --- /dev/null +++ b/0069-sqrtx/README.md @@ -0,0 +1,30 @@ +

69. Sqrt(x)

Easy


Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

+ +

You must not use any built-in exponent function or operator.

+ +
    +
  • For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
  • +
+ +

 

+

Example 1:

+ +
Input: x = 4
+Output: 2
+Explanation: The square root of 4 is 2, so we return 2.
+
+ +

Example 2:

+ +
Input: x = 8
+Output: 2
+Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= x <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0070-climbing-stairs/0070-climbing-stairs.cpp b/0070-climbing-stairs/0070-climbing-stairs.cpp new file mode 100644 index 00000000..fbaea400 --- /dev/null +++ b/0070-climbing-stairs/0070-climbing-stairs.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int climbStairs(int n) { + + if(n == 1) + return 1; + + int prev2 = 1, prev = 1, res = 1; + + for(int i = 2; i <= n; ++i) + { + res = prev2 + prev; + prev = prev2; + prev2 = res; + } + + return res; + } +}; \ No newline at end of file diff --git a/0070-climbing-stairs/README.md b/0070-climbing-stairs/README.md new file mode 100644 index 00000000..c864654b --- /dev/null +++ b/0070-climbing-stairs/README.md @@ -0,0 +1,31 @@ +

70. Climbing Stairs

Easy


You are climbing a staircase. It takes n steps to reach the top.

+ +

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 2
+Explanation: There are two ways to climb to the top.
+1. 1 step + 1 step
+2. 2 steps
+
+ +

Example 2:

+ +
Input: n = 3
+Output: 3
+Explanation: There are three ways to climb to the top.
+1. 1 step + 1 step + 1 step
+2. 1 step + 2 steps
+3. 2 steps + 1 step
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 45
  • +
+
\ No newline at end of file diff --git a/0073-set-matrix-zeroes/README.md b/0073-set-matrix-zeroes/README.md new file mode 100644 index 00000000..574d6249 --- /dev/null +++ b/0073-set-matrix-zeroes/README.md @@ -0,0 +1,36 @@ +

73. Set Matrix Zeroes

Medium


Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

+ +

You must do it in place.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
+Output: [[1,0,1],[0,0,0],[1,0,1]]
+
+ +

Example 2:

+ +
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
+Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[0].length
  • +
  • 1 <= m, n <= 200
  • +
  • -231 <= matrix[i][j] <= 231 - 1
  • +
+ +

 

+

Follow up:

+ +
    +
  • A straightforward solution using O(mn) space is probably a bad idea.
  • +
  • A simple improvement uses O(m + n) space, but still not the best solution.
  • +
  • Could you devise a constant space solution?
  • +
+
\ No newline at end of file diff --git a/0076-minimum-window-substring/0076-minimum-window-substring.cpp b/0076-minimum-window-substring/0076-minimum-window-substring.cpp new file mode 100644 index 00000000..c0755a6f --- /dev/null +++ b/0076-minimum-window-substring/0076-minimum-window-substring.cpp @@ -0,0 +1,71 @@ +class Solution { +public: + string minWindow(string s, string t) { + + int n = s.size(); + + map freq, curr; + + for(auto& ele : t) + { + ++freq[ele]; + } + + string ans; + + int i = 0, j = 0; + + while(j < n) + { + ++curr[s[j]]; + + bool ok = true; + + for(auto&[ch, f] : freq) + { + if(curr[ch] < f) + { + ok = false; + break; + } + } + + if(ok) + { + while(true) + { + if(freq.find(s[i]) == freq.end()) + { + --curr[s[i]]; + ++i; + } + else if(curr[s[i]] > freq[s[i]]) + { + --curr[s[i]]; + ++i; + } + else + break; + + } + + if(ans.empty()) + { + ans = s.substr(i, j - i + 1); + } + else{ + string curr = s.substr(i, j - i + 1); + if(curr.size() < ans.size()) + { + ans = curr; + } + } + } + ++j; + } + + + + return ans; + } +}; \ No newline at end of file diff --git a/0076-minimum-window-substring/README.md b/0076-minimum-window-substring/README.md new file mode 100644 index 00000000..0f0823a6 --- /dev/null +++ b/0076-minimum-window-substring/README.md @@ -0,0 +1,40 @@ +

76. Minimum Window Substring

Hard


Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

+ +

The testcases will be generated such that the answer is unique.

+ +

 

+

Example 1:

+ +
Input: s = "ADOBECODEBANC", t = "ABC"
+Output: "BANC"
+Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
+
+ +

Example 2:

+ +
Input: s = "a", t = "a"
+Output: "a"
+Explanation: The entire string s is the minimum window.
+
+ +

Example 3:

+ +
Input: s = "a", t = "aa"
+Output: ""
+Explanation: Both 'a's from t must be included in the window.
+Since the largest window of s only has one 'a', return empty string.
+
+ +

 

+

Constraints:

+ +
    +
  • m == s.length
  • +
  • n == t.length
  • +
  • 1 <= m, n <= 105
  • +
  • s and t consist of uppercase and lowercase English letters.
  • +
+ +

 

+

Follow up: Could you find an algorithm that runs in O(m + n) time?

+
\ No newline at end of file diff --git a/0078-subsets/0078-subsets.cpp b/0078-subsets/0078-subsets.cpp new file mode 100644 index 00000000..3a971dec --- /dev/null +++ b/0078-subsets/0078-subsets.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + void helper(int idx, vector& ds, vector& nums, vector>& res) + { + if(idx == nums.size()) + { + res.push_back(ds); + return; + } + + ds.push_back(nums[idx]); + helper(idx+1, ds, nums, res); + ds.pop_back(); + helper(idx+1, ds, nums, res); + } + + vector> subsets(vector& nums) { + + vector> res; + vector ds; + + helper(0, ds, nums, res); + + return res; + } +}; \ No newline at end of file diff --git a/0078-subsets/NOTES.md b/0078-subsets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0078-subsets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0078-subsets/README.md b/0078-subsets/README.md new file mode 100644 index 00000000..226b26bb --- /dev/null +++ b/0078-subsets/README.md @@ -0,0 +1,26 @@ +

78. Subsets

Medium


Given an integer array nums of unique elements, return all possible subsets (the power set).

+ +

The solution set must not contain duplicate subsets. Return the solution in any order.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: nums = [0]
+Output: [[],[0]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 10
  • +
  • -10 <= nums[i] <= 10
  • +
  • All the numbers of nums are unique.
  • +
+
\ No newline at end of file diff --git a/0079-word-search/README.md b/0079-word-search/README.md new file mode 100644 index 00000000..1480ac27 --- /dev/null +++ b/0079-word-search/README.md @@ -0,0 +1,37 @@ +

79. Word Search

Medium


Given an m x n grid of characters board and a string word, return true if word exists in the grid.

+ +

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

+ +

 

+

Example 1:

+ +
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
+Output: true
+
+ +

Example 2:

+ +
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
+Output: true
+
+ +

Example 3:

+ +
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • m == board.length
  • +
  • n = board[i].length
  • +
  • 1 <= m, n <= 6
  • +
  • 1 <= word.length <= 15
  • +
  • board and word consists of only lowercase and uppercase English letters.
  • +
+ +

 

+

Follow up: Could you use search pruning to make your solution faster with a larger board?

+
\ No newline at end of file diff --git a/0084-largest-rectangle-in-histogram/README.md b/0084-largest-rectangle-in-histogram/README.md new file mode 100644 index 00000000..8d5fb02f --- /dev/null +++ b/0084-largest-rectangle-in-histogram/README.md @@ -0,0 +1,25 @@ +

84. Largest Rectangle in Histogram

Hard


Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.

+ +

 

+

Example 1:

+ +
Input: heights = [2,1,5,6,2,3]
+Output: 10
+Explanation: The above is a histogram where width of each bar is 1.
+The largest rectangle is shown in the red area, which has an area = 10 units.
+
+ +

Example 2:

+ +
Input: heights = [2,4]
+Output: 4
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= heights.length <= 105
  • +
  • 0 <= heights[i] <= 104
  • +
+
\ No newline at end of file diff --git a/0085-maximal-rectangle/NOTES.md b/0085-maximal-rectangle/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0085-maximal-rectangle/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0088-merge-sorted-array/0088-merge-sorted-array.cpp b/0088-merge-sorted-array/0088-merge-sorted-array.cpp new file mode 100644 index 00000000..f5bd7797 --- /dev/null +++ b/0088-merge-sorted-array/0088-merge-sorted-array.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + + int i = m-1, j = n-1, k = n+m-1; + + while(i >= 0 and j >= 0) + { + if(nums1[i] > nums2[j]) + nums1[k--] = nums1[i--]; + else + nums1[k--] = nums2[j--]; + } + + while(i >= 0) + nums1[k--] = nums1[i--]; + while(j >= 0) + nums1[k--] = nums2[j--]; + + } +}; \ No newline at end of file diff --git a/0088-merge-sorted-array/NOTES.md b/0088-merge-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0088-merge-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0088-merge-sorted-array/README.md b/0088-merge-sorted-array/README.md new file mode 100644 index 00000000..edd52f0d --- /dev/null +++ b/0088-merge-sorted-array/README.md @@ -0,0 +1,46 @@ +

88. Merge Sorted Array

Easy


You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

+ +

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

+ +

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
+Output: [1,2,2,3,5,6]
+Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
+The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
+
+ +

Example 2:

+ +
Input: nums1 = [1], m = 1, nums2 = [], n = 0
+Output: [1]
+Explanation: The arrays we are merging are [1] and [].
+The result of the merge is [1].
+
+ +

Example 3:

+ +
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
+Output: [1]
+Explanation: The arrays we are merging are [] and [1].
+The result of the merge is [1].
+Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
+
+ +

 

+

Constraints:

+ +
    +
  • nums1.length == m + n
  • +
  • nums2.length == n
  • +
  • 0 <= m, n <= 200
  • +
  • 1 <= m + n <= 200
  • +
  • -109 <= nums1[i], nums2[j] <= 109
  • +
+ +

 

+

Follow up: Can you come up with an algorithm that runs in O(m + n) time?

+
\ No newline at end of file diff --git a/0090-subsets-ii/0090-subsets-ii.cpp b/0090-subsets-ii/0090-subsets-ii.cpp new file mode 100644 index 00000000..f2522e66 --- /dev/null +++ b/0090-subsets-ii/0090-subsets-ii.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + void helper(int idx, vector ds, vector& nums, vector>& res) + { + res.push_back(ds); + + for(int i = idx; i < nums.size(); ++i) + { + if(i > idx and nums[i] == nums[i-1]) + continue; + ds.push_back(nums[i]); + helper(i+1, ds, nums, res); + ds.pop_back(); + } + } + + + vector> subsetsWithDup(vector& nums) { + + sort(nums.begin(), nums.end()); + + vector> res; + vector ds; + + helper(0, ds, nums, res); + + return res; + + } +}; \ No newline at end of file diff --git a/0090-subsets-ii/NOTES.md b/0090-subsets-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0090-subsets-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0090-subsets-ii/README.md b/0090-subsets-ii/README.md new file mode 100644 index 00000000..49bedbe0 --- /dev/null +++ b/0090-subsets-ii/README.md @@ -0,0 +1,20 @@ +

90. Subsets II

Medium


Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

+ +

The solution set must not contain duplicate subsets. Return the solution in any order.

+ +

 

+

Example 1:

+
Input: nums = [1,2,2]
+Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
+

Example 2:

+
Input: nums = [0]
+Output: [[],[0]]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 10
  • +
  • -10 <= nums[i] <= 10
  • +
+
\ No newline at end of file diff --git a/0091-decode-ways/0091-decode-ways.cpp b/0091-decode-ways/0091-decode-ways.cpp new file mode 100644 index 00000000..ce5333e9 --- /dev/null +++ b/0091-decode-ways/0091-decode-ways.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + + int helper(int idx, int n, string& s, vector& dp) + { + if(idx == n) + return 1; + + if(s[idx] == '0') + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int first = 0, second = 0; + + if(idx + 2 <= n) + { + string curr = s.substr(idx, 2); + + if(stoi(curr) >= 1 and stoi(curr) <= 26) + { + first = helper(idx + 2, n, s, dp); + } + } + + int ch = s[idx] - '0'; + + if(ch >= 1 and ch <= 26) + second = helper(idx + 1,n, s, dp); + + return dp[idx] = first + second; + } + + int numDecodings(string s) { + + int n = s.size(); + + vector dp(n+1, -1); + + return helper(0, n, s, dp); + + } +}; \ No newline at end of file diff --git a/0091-decode-ways/NOTES.md b/0091-decode-ways/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0091-decode-ways/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0091-decode-ways/README.md b/0091-decode-ways/README.md new file mode 100644 index 00000000..84e177d9 --- /dev/null +++ b/0091-decode-ways/README.md @@ -0,0 +1,51 @@ +

91. Decode Ways

Medium


A message containing letters from A-Z can be encoded into numbers using the following mapping:

+ +
'A' -> "1"
+'B' -> "2"
+...
+'Z' -> "26"
+
+ +

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:

+ +
    +
  • "AAJF" with the grouping (1 1 10 6)
  • +
  • "KJF" with the grouping (11 10 6)
  • +
+ +

Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".

+ +

Given a string s containing only digits, return the number of ways to decode it.

+ +

The test cases are generated so that the answer fits in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: s = "12"
+Output: 2
+Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
+
+ +

Example 2:

+ +
Input: s = "226"
+Output: 3
+Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
+
+ +

Example 3:

+ +
Input: s = "06"
+Output: 0
+Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s contains only digits and may contain leading zero(s).
  • +
+
\ No newline at end of file diff --git a/0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp b/0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp new file mode 100644 index 00000000..e4855cf8 --- /dev/null +++ b/0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector getRow(int rowIndex) { + + vector> v(rowIndex); + + if(rowIndex == 0) + return {1}; + else if(rowIndex == 1) + return {1, 1}; + else + { + v.push_back({1}); + v.push_back({1, 1}); + + for(int i = 2; i <= rowIndex; ++i) + { + vector curr; + curr.push_back(1); + + for(int j =1; j < v.back().size(); ++j) + { + curr.push_back(v.back()[j] + v.back()[j-1]); + } + + curr.push_back(1); + + v.push_back(curr); + } + return v.back(); + } + + } +}; \ No newline at end of file diff --git a/0119-pascals-triangle-ii/NOTES.md b/0119-pascals-triangle-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0119-pascals-triangle-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0119-pascals-triangle-ii/README.md b/0119-pascals-triangle-ii/README.md new file mode 100644 index 00000000..2c0ffdfb --- /dev/null +++ b/0119-pascals-triangle-ii/README.md @@ -0,0 +1,25 @@ +

119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

+ +

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

+ +

 

+

Example 1:

+
Input: rowIndex = 3
+Output: [1,3,3,1]
+

Example 2:

+
Input: rowIndex = 0
+Output: [1]
+

Example 3:

+
Input: rowIndex = 1
+Output: [1,1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= rowIndex <= 33
  • +
+ +

 

+

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

+
\ No newline at end of file diff --git a/0137-single-number-ii/NOTES.md b/0137-single-number-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0137-single-number-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0143-reorder-list/0143-reorder-list.cpp b/0143-reorder-list/0143-reorder-list.cpp new file mode 100644 index 00000000..1795ab34 --- /dev/null +++ b/0143-reorder-list/0143-reorder-list.cpp @@ -0,0 +1,50 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + void reorderList(ListNode* head) { + + if(!head or !head->next) + return; + + ListNode* slow = head, *fast = head, *first = nullptr; + + while(fast and fast->next) + { + first = slow; + slow = slow->next; + fast = fast->next->next; + } + + first->next = nullptr; + + ListNode* prev = nullptr; + + while(slow) + { + ListNode* nex = slow->next; + slow->next = prev; + prev = slow; + slow = nex; + } + + while(head) + { + ListNode* nex = head->next; + head->next = prev; + ListNode* nex2 = prev->next; + if(nex) + prev->next = nex; + head = nex; + prev = nex2; + } + } +}; \ No newline at end of file diff --git a/0143-reorder-list/README.md b/0143-reorder-list/README.md new file mode 100644 index 00000000..fa1bf802 --- /dev/null +++ b/0143-reorder-list/README.md @@ -0,0 +1,33 @@ +

143. Reorder List

Medium


You are given the head of a singly linked-list. The list can be represented as:

+ +
L0 → L1 → … → Ln - 1 → Ln
+
+ +

Reorder the list to be on the following form:

+ +
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
+
+ +

You may not modify the values in the list's nodes. Only nodes themselves may be changed.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4]
+Output: [1,4,2,3]
+
+ +

Example 2:

+ +
Input: head = [1,2,3,4,5]
+Output: [1,5,2,4,3]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 5 * 104].
  • +
  • 1 <= Node.val <= 1000
  • +
+
\ No newline at end of file diff --git a/0148-sort-list/0148-sort-list.cpp b/0148-sort-list/0148-sort-list.cpp new file mode 100644 index 00000000..65a81cfc --- /dev/null +++ b/0148-sort-list/0148-sort-list.cpp @@ -0,0 +1,55 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + ListNode* merge(ListNode* l1, ListNode* l2) + { + if(!l1) + return l2; + + if(!l2) + return l1; + + if(l1->val < l2->val) + { + l1->next = merge(l1->next, l2); + return l1; + } + else + { + l2->next = merge(l1, l2->next); + return l2; + } + } + + ListNode* sortList(ListNode* head) { + + if(!head or !head->next) + return head; + + ListNode* slow = head, *fast = head, *prev = nullptr; + + while(fast and fast->next) + { + prev = slow; + slow = slow->next; + fast = fast->next->next; + } + + prev->next = nullptr; + + ListNode* l1 = sortList(head); + ListNode* l2 = sortList(slow); + + return merge(l1, l2); + } +}; \ No newline at end of file diff --git a/0148-sort-list/README.md b/0148-sort-list/README.md new file mode 100644 index 00000000..e56bc9c9 --- /dev/null +++ b/0148-sort-list/README.md @@ -0,0 +1,32 @@ +

148. Sort List

Medium


Given the head of a linked list, return the list after sorting it in ascending order.

+ +

 

+

Example 1:

+ +
Input: head = [4,2,1,3]
+Output: [1,2,3,4]
+
+ +

Example 2:

+ +
Input: head = [-1,5,3,4,0]
+Output: [-1,0,3,4,5]
+
+ +

Example 3:

+ +
Input: head = []
+Output: []
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [0, 5 * 104].
  • +
  • -105 <= Node.val <= 105
  • +
+ +

 

+

Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?

+
\ No newline at end of file diff --git a/0150-evaluate-reverse-polish-notation/NOTES.md b/0150-evaluate-reverse-polish-notation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0150-evaluate-reverse-polish-notation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0160-intersection-of-two-linked-lists/README.md b/0160-intersection-of-two-linked-lists/README.md new file mode 100644 index 00000000..bf03bc16 --- /dev/null +++ b/0160-intersection-of-two-linked-lists/README.md @@ -0,0 +1,64 @@ +

160. Intersection of Two Linked Lists

Easy


Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

+ +

For example, the following two linked lists begin to intersect at node c1:

+ +

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

+ +

Note that the linked lists must retain their original structure after the function returns.

+ +

Custom Judge:

+ +

The inputs to the judge are given as follows (your program is not given these inputs):

+ +
    +
  • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
  • +
  • listA - The first linked list.
  • +
  • listB - The second linked list.
  • +
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
  • +
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
  • +
+ +

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

+ +

 

+

Example 1:

+ +
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
+Output: Intersected at '8'
+Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
+From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
+- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
+
+ +

Example 2:

+ +
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
+Output: Intersected at '2'
+Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
+From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
+
+ +

Example 3:

+ +
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
+Output: No intersection
+Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
+Explanation: The two lists do not intersect, so return null.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes of listA is in the m.
  • +
  • The number of nodes of listB is in the n.
  • +
  • 1 <= m, n <= 3 * 104
  • +
  • 1 <= Node.val <= 105
  • +
  • 0 <= skipA < m
  • +
  • 0 <= skipB < n
  • +
  • intersectVal is 0 if listA and listB do not intersect.
  • +
  • intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
  • +
+ +

 

+Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory?
\ No newline at end of file diff --git a/0162-find-peak-element/0162-find-peak-element.cpp b/0162-find-peak-element/0162-find-peak-element.cpp new file mode 100644 index 00000000..b9badb4f --- /dev/null +++ b/0162-find-peak-element/0162-find-peak-element.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int findPeakElement(vector& arr) { + + int n = arr.size(); + + if(n == 1) + return 0; + if(arr[0] > arr[1]) + return 0; + if(arr[n-1] > arr[n-2]) + return n-1; + + int low = 1, high = n-2; + + while(low <= high) + { + int mid = low + (high - low) / 2; + + if(arr[mid] > arr[mid+1] and arr[mid] > arr[mid-1]) + return mid; + else if(arr[mid] > arr[mid-1]) + low = mid+1; + else + high = mid-1; + } + + return -1; + } +}; \ No newline at end of file diff --git a/0162-find-peak-element/NOTES.md b/0162-find-peak-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0162-find-peak-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0162-find-peak-element/README.md b/0162-find-peak-element/README.md new file mode 100644 index 00000000..96c32bd2 --- /dev/null +++ b/0162-find-peak-element/README.md @@ -0,0 +1,30 @@ +

162. Find Peak Element

Medium


A peak element is an element that is strictly greater than its neighbors.

+ +

Given a 0-indexed 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.

+ +

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

+ +

You must write an algorithm that runs in O(log n) time.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1]
+Output: 2
+Explanation: 3 is a peak element and your function should return the index number 2.
+ +

Example 2:

+ +
Input: nums = [1,2,1,3,5,6,4]
+Output: 5
+Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • nums[i] != nums[i + 1] for all valid i.
  • +
+
\ No newline at end of file diff --git a/0165-compare-version-numbers/0165-compare-version-numbers.cpp b/0165-compare-version-numbers/0165-compare-version-numbers.cpp new file mode 100644 index 00000000..14ab26a7 --- /dev/null +++ b/0165-compare-version-numbers/0165-compare-version-numbers.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int compareVersion(string version1, string version2) { + + int i = 0, j= 0, n1 = version1.length(), n2 = version2.length(), num1 = 0, num2 = 0; + + while(i < n1 || j < n2) + { + while(i < n1 && version1[i] != '.') + { + num1 = num1*10 + (version1[i] - '0'); + ++i; + } + while(j < n2 && version2[j] != '.') + { + num2 = num2 * 10 + (version2[j] - '0'); + ++j; + } + + + if(num1 > num2) + return 1; + + if(num1 < num2) + return -1; + + ++i, ++j; + + num1 = 0, num2 = 0; + + } + return 0; + + } +}; \ No newline at end of file diff --git a/0165-compare-version-numbers/README.md b/0165-compare-version-numbers/README.md new file mode 100644 index 00000000..2e07f72d --- /dev/null +++ b/0165-compare-version-numbers/README.md @@ -0,0 +1,49 @@ +

165. Compare Version Numbers

Medium


Given two version numbers, version1 and version2, compare them.

+ +
    +
+ +

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.

+ +

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.

+ +

Return the following:

+ +
    +
  • If version1 < version2, return -1.
  • +
  • If version1 > version2, return 1.
  • +
  • Otherwise, return 0.
  • +
+ +

 

+

Example 1:

+ +
Input: version1 = "1.01", version2 = "1.001"
+Output: 0
+Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
+
+ +

Example 2:

+ +
Input: version1 = "1.0", version2 = "1.0.0"
+Output: 0
+Explanation: version1 does not specify revision 2, which means it is treated as "0".
+
+ +

Example 3:

+ +
Input: version1 = "0.1", version2 = "1.1"
+Output: -1
+Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= version1.length, version2.length <= 500
  • +
  • version1 and version2 only contain digits and '.'.
  • +
  • version1 and version2 are valid version numbers.
  • +
  • All the given revisions in version1 and version2 can be stored in a 32-bit integer.
  • +
+
\ No newline at end of file diff --git a/0169-majority-element/0169-majority-element.cpp b/0169-majority-element/0169-majority-element.cpp new file mode 100644 index 00000000..b2296cda --- /dev/null +++ b/0169-majority-element/0169-majority-element.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int majorityElement(vector& nums) { + + int cnt = 1, num1 = nums[0], n = nums.size(); + + for(int i = 1; i < n; ++i) + { + if(num1 != nums[i]) + --cnt; + else + ++cnt; + + if(cnt == 0) + { + cnt = 1; + num1 = nums[i]; + } + } + + return num1; + } +}; \ No newline at end of file diff --git a/0169-majority-element/NOTES.md b/0169-majority-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0169-majority-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0169-majority-element/README.md b/0169-majority-element/README.md new file mode 100644 index 00000000..0159f3e8 --- /dev/null +++ b/0169-majority-element/README.md @@ -0,0 +1,23 @@ +

169. Majority Element

Easy


Given an array nums of size n, return the majority element.

+ +

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

+ +

 

+

Example 1:

+
Input: nums = [3,2,3]
+Output: 3
+

Example 2:

+
Input: nums = [2,2,1,1,1,2,2]
+Output: 2
+
+

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 5 * 104
  • +
  • -109 <= nums[i] <= 109
  • +
+ +

 

+Follow-up: Could you solve the problem in linear time and in O(1) space?
\ No newline at end of file diff --git a/0189-rotate-array/0189-rotate-array.cpp b/0189-rotate-array/0189-rotate-array.cpp new file mode 100644 index 00000000..3795c8f9 --- /dev/null +++ b/0189-rotate-array/0189-rotate-array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + void rotate(vector& nums, int k) { + + int n = nums.size(); + + k %= n; + + reverse(nums.begin(), nums.begin() + (n - k)); + + reverse(nums.begin() + (n-k), nums.end()); + + reverse(nums.begin(), nums.end()); + + } +}; \ No newline at end of file diff --git a/0189-rotate-array/NOTES.md b/0189-rotate-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0189-rotate-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0200-number-of-islands/0200-number-of-islands.cpp b/0200-number-of-islands/0200-number-of-islands.cpp new file mode 100644 index 00000000..29d1a177 --- /dev/null +++ b/0200-number-of-islands/0200-number-of-islands.cpp @@ -0,0 +1,99 @@ +class DSU{ + public: + int N; + vector parent, size; + + DSU(int n) + { + N = n; + parent.resize(N); + size.resize(N, 1); + for(int i = 0; i < N; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } +}; + +class Solution { +public: + int numIslands(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1 ,0}; + + DSU dsu(n*m + 1); + + set ls; + + int island = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == '1') + { + ls.insert((i*m) + j); + for(int k = 0; k < 4; ++k) + { + int cx = dx[k] + i; + int cy = dy[k] + j; + + if(cx >= 0 and cy >= 0 and cx < n and cy < m and grid[cx][cy] == '1') + { + int node = (i * m) + j; + int adjNode = (cx * m) + cy; + + if(!dsu.isSame(node, adjNode)) + { + dsu.unionBySize(node, adjNode); + } + } + } + } + } + } + + for(auto& node : ls) + { + if(dsu.findParent(node) == node) + ++island; + } + + return island; + + } +}; \ No newline at end of file diff --git a/0200-number-of-islands/NOTES.md b/0200-number-of-islands/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0200-number-of-islands/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0200-number-of-islands/README.md b/0200-number-of-islands/README.md new file mode 100644 index 00000000..f0398d0f --- /dev/null +++ b/0200-number-of-islands/README.md @@ -0,0 +1,37 @@ +

200. Number of Islands

Medium


Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

+ +

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

+ +

 

+

Example 1:

+ +
Input: grid = [
+  ["1","1","1","1","0"],
+  ["1","1","0","1","0"],
+  ["1","1","0","0","0"],
+  ["0","0","0","0","0"]
+]
+Output: 1
+
+ +

Example 2:

+ +
Input: grid = [
+  ["1","1","0","0","0"],
+  ["1","1","0","0","0"],
+  ["0","0","1","0","0"],
+  ["0","0","0","1","1"]
+]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 300
  • +
  • grid[i][j] is '0' or '1'.
  • +
+
\ No newline at end of file diff --git a/0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp b/0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp new file mode 100644 index 00000000..b92b413c --- /dev/null +++ b/0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int rangeBitwiseAnd(int left, int right) { + + int rightShift = 0; + + while(right != left) + { + left >>= 1; + right >>= 1; + ++rightShift; + } + + return right << rightShift; + + } +}; \ No newline at end of file diff --git a/0201-bitwise-and-of-numbers-range/NOTES.md b/0201-bitwise-and-of-numbers-range/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0201-bitwise-and-of-numbers-range/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0201-bitwise-and-of-numbers-range/README.md b/0201-bitwise-and-of-numbers-range/README.md new file mode 100644 index 00000000..5a34dffc --- /dev/null +++ b/0201-bitwise-and-of-numbers-range/README.md @@ -0,0 +1,28 @@ +

201. Bitwise AND of Numbers Range

Medium


Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

+ +

 

+

Example 1:

+ +
Input: left = 5, right = 7
+Output: 4
+
+ +

Example 2:

+ +
Input: left = 0, right = 0
+Output: 0
+
+ +

Example 3:

+ +
Input: left = 1, right = 2147483647
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= left <= right <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0205-isomorphic-strings/README.md b/0205-isomorphic-strings/README.md new file mode 100644 index 00000000..c7d544ca --- /dev/null +++ b/0205-isomorphic-strings/README.md @@ -0,0 +1,26 @@ +

205. Isomorphic Strings

Easy


Given two strings s and t, determine if they are isomorphic.

+ +

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

+ +

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

+ +

 

+

Example 1:

+
Input: s = "egg", t = "add"
+Output: true
+

Example 2:

+
Input: s = "foo", t = "bar"
+Output: false
+

Example 3:

+
Input: s = "paper", t = "title"
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • t.length == s.length
  • +
  • s and t consist of any valid ascii character.
  • +
+
\ No newline at end of file diff --git a/0206-reverse-linked-list/0206-reverse-linked-list.cpp b/0206-reverse-linked-list/0206-reverse-linked-list.cpp new file mode 100644 index 00000000..2418f3bc --- /dev/null +++ b/0206-reverse-linked-list/0206-reverse-linked-list.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + + ListNode* prev = nullptr; + + while(head) + { + ListNode* nex = head->next; + head->next = prev; + prev = head; + head = nex; + } + + return prev; + } +}; \ No newline at end of file diff --git a/0206-reverse-linked-list/NOTES.md b/0206-reverse-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0206-reverse-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0231-power-of-two/0231-power-of-two.cpp b/0231-power-of-two/0231-power-of-two.cpp new file mode 100644 index 00000000..183a7700 --- /dev/null +++ b/0231-power-of-two/0231-power-of-two.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + bool isPowerOfTwo(int n) { + if(n == 1) + return true; + if(n & 1 or n == 0) + return false; + return isPowerOfTwo(n/2); + } +}; \ No newline at end of file diff --git a/0231-power-of-two/NOTES.md b/0231-power-of-two/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0231-power-of-two/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0231-power-of-two/README.md b/0231-power-of-two/README.md new file mode 100644 index 00000000..b6b0cad9 --- /dev/null +++ b/0231-power-of-two/README.md @@ -0,0 +1,34 @@ +

231. Power of Two

Easy


Given an integer n, return true if it is a power of two. Otherwise, return false.

+ +

An integer n is a power of two, if there exists an integer x such that n == 2x.

+ +

 

+

Example 1:

+ +
Input: n = 1
+Output: true
+Explanation: 20 = 1
+
+ +

Example 2:

+ +
Input: n = 16
+Output: true
+Explanation: 24 = 16
+
+ +

Example 3:

+ +
Input: n = 3
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion?
\ No newline at end of file diff --git a/0234-palindrome-linked-list/0234-palindrome-linked-list.cpp b/0234-palindrome-linked-list/0234-palindrome-linked-list.cpp new file mode 100644 index 00000000..a1d444cd --- /dev/null +++ b/0234-palindrome-linked-list/0234-palindrome-linked-list.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + bool isPalindrome(ListNode* head) { + + ListNode* fast = head, *slow = head; + + while(fast and fast->next) + { + fast = fast->next->next; + slow = slow->next; + } + + ListNode* nexHalf = slow; + + ListNode* prev = nullptr; + + while(nexHalf) + { + ListNode* nex = nexHalf->next; + nexHalf->next = prev; + prev = nexHalf; + nexHalf = nex; + } + + while(head != slow) + { + if(head->val != prev->val) + return false; + head = head->next; + prev = prev->next; + } + + return true; + + } +}; \ No newline at end of file diff --git a/0234-palindrome-linked-list/README.md b/0234-palindrome-linked-list/README.md new file mode 100644 index 00000000..d71facb1 --- /dev/null +++ b/0234-palindrome-linked-list/README.md @@ -0,0 +1,25 @@ +

234. Palindrome Linked List

Easy


Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,2,1]
+Output: true
+
+ +

Example 2:

+ +
Input: head = [1,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 105].
  • +
  • 0 <= Node.val <= 9
  • +
+ +

 

+Follow up: Could you do it in O(n) time and O(1) space?
\ No newline at end of file diff --git a/0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp b/0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp new file mode 100644 index 00000000..9a3c0cf0 --- /dev/null +++ b/0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp @@ -0,0 +1,16 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + void deleteNode(ListNode* node) { + + node->val = node->next->val; + node->next = node->next->next; + } +}; \ No newline at end of file diff --git a/0237-delete-node-in-a-linked-list/NOTES.md b/0237-delete-node-in-a-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0237-delete-node-in-a-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0237-delete-node-in-a-linked-list/README.md b/0237-delete-node-in-a-linked-list/README.md new file mode 100644 index 00000000..3fc46f11 --- /dev/null +++ b/0237-delete-node-in-a-linked-list/README.md @@ -0,0 +1,48 @@ +

237. Delete Node in a Linked List

Medium


There is a singly-linked list head and we want to delete a node node in it.

+ +

You are given the node to be deleted node. You will not be given access to the first node of head.

+ +

All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.

+ +

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

+ +
    +
  • The value of the given node should not exist in the linked list.
  • +
  • The number of nodes in the linked list should decrease by one.
  • +
  • All the values before node should be in the same order.
  • +
  • All the values after node should be in the same order.
  • +
+ +

Custom testing:

+ +
    +
  • For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
  • +
  • We will build the linked list and pass the node to your function.
  • +
  • The output will be the entire list after calling your function.
  • +
+ +

 

+

Example 1:

+ +
Input: head = [4,5,1,9], node = 5
+Output: [4,1,9]
+Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
+
+ +

Example 2:

+ +
Input: head = [4,5,1,9], node = 1
+Output: [4,5,9]
+Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the given list is in the range [2, 1000].
  • +
  • -1000 <= Node.val <= 1000
  • +
  • The value of each node in the list is unique.
  • +
  • The node to be deleted is in the list and is not a tail node.
  • +
+
\ No newline at end of file diff --git a/0238-product-of-array-except-self/0238-product-of-array-except-self.cpp b/0238-product-of-array-except-self/0238-product-of-array-except-self.cpp new file mode 100644 index 00000000..5dae18ab --- /dev/null +++ b/0238-product-of-array-except-self/0238-product-of-array-except-self.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + + int n = nums.size(); + vector ans(n, 1); + + int pref = 1, suff = 1; + + for(int i = 0; i < n; ++i) + { + ans[i] *= pref; + pref *= nums[i]; + } + + for(int i = n-1; i >= 0; --i) + { + ans[i] *= suff; + suff *= nums[i]; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0238-product-of-array-except-self/README.md b/0238-product-of-array-except-self/README.md new file mode 100644 index 00000000..494c527b --- /dev/null +++ b/0238-product-of-array-except-self/README.md @@ -0,0 +1,26 @@ +

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].

+ +

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

+ +

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

+ +

 

+

Example 1:

+
Input: nums = [1,2,3,4]
+Output: [24,12,8,6]
+

Example 2:

+
Input: nums = [-1,1,0,-3,3]
+Output: [0,0,9,0,0]
+
+

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • -30 <= nums[i] <= 30
  • +
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  • +
+ +

 

+

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.)

+
\ No newline at end of file diff --git a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp index 93aab41f..f0c6f117 100644 --- a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp +++ b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp @@ -2,14 +2,14 @@ class Solution { public: vector maxSlidingWindow(vector& nums, int k) { - int i = 0, j = 0; - int n = nums.size(); - vector ans; + int i = 0, j = 0; list l; + vector ans; + while(j < n) { while(!l.empty() and nums[l.back()] <= nums[j]) @@ -22,9 +22,7 @@ class Solution { ans.push_back(nums[l.front()]); if(l.front() == i) - { l.pop_front(); - } ++i; } @@ -33,6 +31,5 @@ class Solution { } return ans; - } }; \ No newline at end of file diff --git a/0242-valid-anagram/0242-valid-anagram.cpp b/0242-valid-anagram/0242-valid-anagram.cpp new file mode 100644 index 00000000..e0ced785 --- /dev/null +++ b/0242-valid-anagram/0242-valid-anagram.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isAnagram(string s, string t) { + + vector freq(26, 0); + + for(auto& ch : s) + ++freq[ch - 'a']; + + for(auto& ch : t) + --freq[ch - 'a']; + + for(int i = 0; i< 26; ++i) + { + if(freq[i] != 0) return false; + } + + return true; + } +}; \ No newline at end of file diff --git a/0242-valid-anagram/NOTES.md b/0242-valid-anagram/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0242-valid-anagram/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0242-valid-anagram/README.md b/0242-valid-anagram/README.md new file mode 100644 index 00000000..c06723c0 --- /dev/null +++ b/0242-valid-anagram/README.md @@ -0,0 +1,23 @@ +

242. Valid Anagram

Easy


Given two strings s and t, return true if t is an anagram of s, and false otherwise.

+ +

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

+ +

 

+

Example 1:

+
Input: s = "anagram", t = "nagaram"
+Output: true
+

Example 2:

+
Input: s = "rat", t = "car"
+Output: false
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 5 * 104
  • +
  • s and t consist of lowercase English letters.
  • +
+ +

 

+

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

+
\ No newline at end of file diff --git a/0260-single-number-iii/0260-single-number-iii.cpp b/0260-single-number-iii/0260-single-number-iii.cpp new file mode 100644 index 00000000..8adcc2c8 --- /dev/null +++ b/0260-single-number-iii/0260-single-number-iii.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector singleNumber(vector& nums) { + + int b1 = 0, b2 = 0; + + long long xorr = 0; + + for(auto& ele : nums) + xorr ^= ele; + + long long rightMost = (xorr&(xorr-1)) ^ xorr; + + for(auto& ele : nums) + { + if(ele & rightMost) + b1 ^= ele; + else + b2 ^= ele; + } + + return {b1, b2}; + } +}; \ No newline at end of file diff --git a/0260-single-number-iii/README.md b/0260-single-number-iii/README.md new file mode 100644 index 00000000..203cd5d7 --- /dev/null +++ b/0260-single-number-iii/README.md @@ -0,0 +1,33 @@ +

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.

+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,1,3,2,5]
+Output: [3,5]
+Explanation:  [5, 3] is also a valid answer.
+
+ +

Example 2:

+ +
Input: nums = [-1,0]
+Output: [-1,0]
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 3 * 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • Each integer in nums will appear twice, only two integers will appear once.
  • +
+
\ No newline at end of file diff --git a/0264-ugly-number-ii/0264-ugly-number-ii.cpp b/0264-ugly-number-ii/0264-ugly-number-ii.cpp new file mode 100644 index 00000000..27655d96 --- /dev/null +++ b/0264-ugly-number-ii/0264-ugly-number-ii.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int nthUglyNumber(int n) { + + vector v(n+1, 1); + + int i2 = 1, i3 = 1, i5 = 1; + + for(int i = 2; i <= n; ++i) + { + int ithUgly = min({v[i2] * 2, v[i3] * 3, v[i5] * 5}); + + v[i] = ithUgly; + + if(v[i] == v[i2] * 2) ++i2; + if(v[i] == v[i3] * 3) ++i3; + if(v[i] == v[i5] * 5) ++i5; + } + + return v[n]; + + } +}; \ No newline at end of file diff --git a/0264-ugly-number-ii/README.md b/0264-ugly-number-ii/README.md new file mode 100644 index 00000000..296532e1 --- /dev/null +++ b/0264-ugly-number-ii/README.md @@ -0,0 +1,26 @@ +

264. Ugly Number II

Medium


An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

+ +

Given an integer n, return the nth ugly number.

+ +

 

+

Example 1:

+ +
Input: n = 10
+Output: 12
+Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
+
+ +

Example 2:

+ +
Input: n = 1
+Output: 1
+Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1690
  • +
+
\ No newline at end of file diff --git a/0268-missing-number/0268-missing-number.cpp b/0268-missing-number/0268-missing-number.cpp new file mode 100644 index 00000000..dacbf752 --- /dev/null +++ b/0268-missing-number/0268-missing-number.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int missingNumber(vector& nums) { + + int n = nums.size(); + + int sum = 0; + + for(auto& ele : nums) + sum += ele; + + int totSum = (n * (n+1)) / 2; + + return totSum - sum; + + } +}; \ No newline at end of file diff --git a/0268-missing-number/NOTES.md b/0268-missing-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0268-missing-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0268-missing-number/README.md b/0268-missing-number/README.md new file mode 100644 index 00000000..f88e56a1 --- /dev/null +++ b/0268-missing-number/README.md @@ -0,0 +1,37 @@ +

268. Missing Number

Easy


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.

+ +

 

+

Example 1:

+ +
Input: nums = [3,0,1]
+Output: 2
+Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
+
+ +

Example 2:

+ +
Input: nums = [0,1]
+Output: 2
+Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
+
+ +

Example 3:

+ +
Input: nums = [9,6,4,2,3,5,7,0,1]
+Output: 8
+Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 104
  • +
  • 0 <= nums[i] <= n
  • +
  • All the numbers of nums are unique.
  • +
+ +

 

+

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

+
\ No newline at end of file diff --git a/0273-integer-to-english-words/0273-integer-to-english-words.cpp b/0273-integer-to-english-words/0273-integer-to-english-words.cpp new file mode 100644 index 00000000..f6db280d --- /dev/null +++ b/0273-integer-to-english-words/0273-integer-to-english-words.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector> nums ={{1000000000, "Billion"}, {1000000, "Million"}, + {1000, "Thousand"}, {100, "Hundred"}, {90, "Ninety"}, {80, "Eighty"}, {70, "Seventy"}, + {60, "Sixty"}, {50, "Fifty"}, {40, "Forty"}, {30, "Thirty"}, {20, "Twenty"}, {19, "Nineteen"}, + {18, "Eighteen"}, {17, "Seventeen"}, {16, "Sixteen"}, {15, "Fifteen"}, {14, "Fourteen"}, + {13, "Thirteen"}, {12, "Twelve"}, {11, "Eleven"}, {10, "Ten"}, {9, "Nine"}, {8, "Eight"}, + {7, "Seven"}, {6, "Six"}, {5, "Five"}, {4, "Four"}, {3, "Three"}, {2, "Two"}, {1, "One"}}; + + string numberToWords(int num) { + if(num == 0) return "Zero"; + + for(auto it: nums) + if(num >= it.first) + return (num >= 100 ? numberToWords(num/it.first)+" " : "") + it.second + (num%it.first == 0 ? "" : " "+numberToWords(num%it.first)); + + return ""; + } +}; \ No newline at end of file diff --git a/0273-integer-to-english-words/README.md b/0273-integer-to-english-words/README.md new file mode 100644 index 00000000..708515e3 --- /dev/null +++ b/0273-integer-to-english-words/README.md @@ -0,0 +1,28 @@ +

273. Integer to English Words

Hard


Convert a non-negative integer num to its English words representation.

+ +

 

+

Example 1:

+ +
Input: num = 123
+Output: "One Hundred Twenty Three"
+
+ +

Example 2:

+ +
Input: num = 12345
+Output: "Twelve Thousand Three Hundred Forty Five"
+
+ +

Example 3:

+ +
Input: num = 1234567
+Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= num <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0279-perfect-squares/README.md b/0279-perfect-squares/README.md new file mode 100644 index 00000000..cfd0d433 --- /dev/null +++ b/0279-perfect-squares/README.md @@ -0,0 +1,26 @@ +

279. Perfect Squares

Medium


Given an integer n, return the least number of perfect square numbers that sum to n.

+ +

A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.

+ +

 

+

Example 1:

+ +
Input: n = 12
+Output: 3
+Explanation: 12 = 4 + 4 + 4.
+
+ +

Example 2:

+ +
Input: n = 13
+Output: 2
+Explanation: 13 = 4 + 9.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 104
  • +
+
\ No newline at end of file diff --git a/0289-game-of-life/0289-game-of-life.cpp b/0289-game-of-life/0289-game-of-life.cpp new file mode 100644 index 00000000..eec57c7c --- /dev/null +++ b/0289-game-of-life/0289-game-of-life.cpp @@ -0,0 +1,55 @@ +class Solution { +public: + void gameOfLife(vector>& board) { + + vector dx = {-1, -1, -1, 0, 0, +1, +1, +1}; + vector dy = {-1, 0, +1, -1, +1, -1, 0, +1}; + + int n = board.size(); + int m = board[0].size(); + + set> st; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(board[i][j]) + st.insert({i, j}); + } + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + int alive = 0; + int dead = 0; + + for(int k = 0; k < 8; ++k) + { + int newx = i + dx[k]; + int newy = j + dy[k]; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m) + { + if(st.count({newx, newy})) + ++alive; + else + ++dead; + } + } + if(st.count({i, j})) + { + if(alive < 2 or alive > 3) + board[i][j] = 0; + } + else + { + if(alive == 3) + board[i][j] = 1; + } + } + } + } +}; \ No newline at end of file diff --git a/0289-game-of-life/NOTES.md b/0289-game-of-life/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0289-game-of-life/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0289-game-of-life/README.md b/0289-game-of-life/README.md new file mode 100644 index 00000000..4d231fac --- /dev/null +++ b/0289-game-of-life/README.md @@ -0,0 +1,44 @@ +

289. Game of Life

Medium


According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

+ +

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

+ +
    +
  1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
  2. +
  3. Any live cell with two or three live neighbors lives on to the next generation.
  4. +
  5. Any live cell with more than three live neighbors dies, as if by over-population.
  6. +
  7. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
  8. +
+ +

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

+ +

 

+

Example 1:

+ +
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
+Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
+
+ +

Example 2:

+ +
Input: board = [[1,1],[1,0]]
+Output: [[1,1],[1,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == board.length
  • +
  • n == board[i].length
  • +
  • 1 <= m, n <= 25
  • +
  • board[i][j] is 0 or 1.
  • +
+ +

 

+

Follow up:

+ +
    +
  • Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
  • +
  • In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
  • +
+
\ No newline at end of file diff --git a/0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp b/0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp new file mode 100644 index 00000000..b21b83b7 --- /dev/null +++ b/0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp @@ -0,0 +1,52 @@ +class MedianFinder { +public: + + priority_queue maxHeap; + priority_queue, greater> minHeap; + + MedianFinder() { + + } + + void addNum(int num) { + if(maxHeap.empty() and minHeap.empty()) + maxHeap.push(num); + else if(num > maxHeap.top()) + minHeap.push(num); + else + maxHeap.push(num); + + int n = maxHeap.size(); + int m = minHeap.size(); + + if(abs(n-m) == 2) + { + if(n > m) + { + minHeap.push(maxHeap.top()); + maxHeap.pop(); + } + else + { + maxHeap.push(minHeap.top()); + minHeap.pop(); + } + } + } + + double findMedian() { + if(maxHeap.size() == minHeap.size()) + return (maxHeap.top() + minHeap.top()) / 2.0; + else if(maxHeap.size() > minHeap.size()) + return maxHeap.top(); + else + return minHeap.top(); + } +}; + +/** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder* obj = new MedianFinder(); + * obj->addNum(num); + * double param_2 = obj->findMedian(); + */ \ No newline at end of file diff --git a/0295-find-median-from-data-stream/README.md b/0295-find-median-from-data-stream/README.md new file mode 100644 index 00000000..40d10740 --- /dev/null +++ b/0295-find-median-from-data-stream/README.md @@ -0,0 +1,50 @@ +

295. Find Median from Data Stream

Hard


The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.

+ +
    +
  • For example, for arr = [2,3,4], the median is 3.
  • +
  • For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
  • +
+ +

Implement the MedianFinder class:

+ +
    +
  • MedianFinder() initializes the MedianFinder object.
  • +
  • void addNum(int num) adds the integer num from the data stream to the data structure.
  • +
  • double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
+[[], [1], [2], [], [3], []]
+Output
+[null, null, null, 1.5, null, 2.0]
+
+Explanation
+MedianFinder medianFinder = new MedianFinder();
+medianFinder.addNum(1);    // arr = [1]
+medianFinder.addNum(2);    // arr = [1, 2]
+medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
+medianFinder.addNum(3);    // arr[1, 2, 3]
+medianFinder.findMedian(); // return 2.0
+
+ +

 

+

Constraints:

+ +
    +
  • -105 <= num <= 105
  • +
  • There will be at least one element in the data structure before calling findMedian.
  • +
  • At most 5 * 104 calls will be made to addNum and findMedian.
  • +
+ +

 

+

Follow up:

+ +
    +
  • If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • +
  • If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • +
+
\ No newline at end of file diff --git a/0310-minimum-height-trees/0310-minimum-height-trees.cpp b/0310-minimum-height-trees/0310-minimum-height-trees.cpp new file mode 100644 index 00000000..075848c2 --- /dev/null +++ b/0310-minimum-height-trees/0310-minimum-height-trees.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) { + vector> graph(n); + vector indegree(n, 0), ans; + + for(auto &e : edges){ + graph[e[0]].push_back(e[1]); + graph[e[1]].push_back(e[0]); + indegree[e[0]]++; + indegree[e[1]]++; + } + + queue q; + for(int i=0; i310. Minimum Height Trees

Medium


A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.

+ +

Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h))  are called minimum height trees (MHTs).

+ +

Return a list of all MHTs' root labels. You can return the answer in any order.

+ +

The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

+ +

 

+

Example 1:

+ +
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
+Output: [1]
+Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
+
+ +

Example 2:

+ +
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
+Output: [3,4]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 104
  • +
  • edges.length == n - 1
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • All the pairs (ai, bi) are distinct.
  • +
  • The given input is guaranteed to be a tree and there will be no repeated edges.
  • +
+
\ No newline at end of file diff --git a/0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp b/0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp new file mode 100644 index 00000000..b49df90a --- /dev/null +++ b/0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + void wiggleSort(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector ans(n); + + int k = n-1; + + for(int i = 1; i < n; i += 2) + { + ans[i] = nums[k--]; + } + + for(int i = 0; i < n; i += 2) + { + ans[i] = nums[k--]; + } + + nums = ans; + } +}; \ No newline at end of file diff --git a/0324-wiggle-sort-ii/NOTES.md b/0324-wiggle-sort-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0324-wiggle-sort-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0324-wiggle-sort-ii/README.md b/0324-wiggle-sort-ii/README.md new file mode 100644 index 00000000..ae6f43d0 --- /dev/null +++ b/0324-wiggle-sort-ii/README.md @@ -0,0 +1,29 @@ +

324. Wiggle Sort II

Medium


Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....

+ +

You may assume the input array always has a valid answer.

+ +

 

+

Example 1:

+ +
Input: nums = [1,5,1,1,6,4]
+Output: [1,6,1,5,1,4]
+Explanation: [1,4,1,5,1,6] is also accepted.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 0 <= nums[i] <= 5000
  • +
  • It is guaranteed that there will be an answer for the given input nums.
  • +
+ +

 

+Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space?
\ No newline at end of file diff --git a/0330-patching-array/0330-patching-array.cpp b/0330-patching-array/0330-patching-array.cpp new file mode 100644 index 00000000..ae09c27a --- /dev/null +++ b/0330-patching-array/0330-patching-array.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minPatches(vector& nums, int n) { + + long long maxReach = 0; + int patch = 0, i = 0; + + while(maxReach < n) + { + if(i < nums.size() and nums[i] <= maxReach + 1) + { + maxReach += nums[i]; + ++i; + } + else + { + ++patch; + maxReach += (maxReach + 1); + } + } + + return patch; + + } +}; \ No newline at end of file diff --git a/0330-patching-array/README.md b/0330-patching-array/README.md new file mode 100644 index 00000000..fcebede2 --- /dev/null +++ b/0330-patching-array/README.md @@ -0,0 +1,39 @@ +

330. Patching Array

Hard


Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array.

+ +

Return the minimum number of patches required.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3], n = 6
+Output: 1
+Explanation:
+Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
+Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
+Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
+So we only need 1 patch.
+
+ +

Example 2:

+ +
Input: nums = [1,5,10], n = 20
+Output: 2
+Explanation: The two patches can be [2, 4].
+
+ +

Example 3:

+ +
Input: nums = [1,2,2], n = 5
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= 104
  • +
  • nums is sorted in ascending order.
  • +
  • 1 <= n <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp new file mode 100644 index 00000000..f450d064 --- /dev/null +++ b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp @@ -0,0 +1,57 @@ +/** + * // This is the interface that allows for creating nested lists. + * // You should not implement it, or speculate about its implementation + * class NestedInteger { + * public: + * // Return true if this NestedInteger holds a single integer, rather than a nested list. + * bool isInteger() const; + * + * // Return the single integer that this NestedInteger holds, if it holds a single integer + * // The result is undefined if this NestedInteger holds a nested list + * int getInteger() const; + * + * // Return the nested list that this NestedInteger holds, if it holds a nested list + * // The result is undefined if this NestedInteger holds a single integer + * const vector &getList() const; + * }; + */ + +class NestedIterator { +public: + + vector v; + int k = 0; + + void helper(vector& nestedList) + { + for(auto& itr : nestedList) + { + if(itr.isInteger()) + v.push_back(itr.getInteger()); + else + helper(itr.getList()); + } + } + + NestedIterator(vector &nestedList) { + + helper(nestedList); + + } + + int next() { + int ans = v[k]; + ++k; + return ans; + } + + bool hasNext() { + return k < v.size(); + } +}; + +/** + * Your NestedIterator object will be instantiated and called as such: + * NestedIterator i(nestedList); + * while (i.hasNext()) cout << i.next(); + */ \ No newline at end of file diff --git a/0341-flatten-nested-list-iterator/NOTES.md b/0341-flatten-nested-list-iterator/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0341-flatten-nested-list-iterator/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0341-flatten-nested-list-iterator/README.md b/0341-flatten-nested-list-iterator/README.md new file mode 100644 index 00000000..be9109a3 --- /dev/null +++ b/0341-flatten-nested-list-iterator/README.md @@ -0,0 +1,44 @@ +

341. Flatten Nested List Iterator

Medium


You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.

+ +

Implement the NestedIterator class:

+ +
    +
  • NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
  • +
  • int next() Returns the next integer in the nested list.
  • +
  • boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
  • +
+ +

Your code will be tested with the following pseudocode:

+ +
initialize iterator with nestedList
+res = []
+while iterator.hasNext()
+    append iterator.next() to the end of res
+return res
+
+ +

If res matches the expected flattened list, then your code will be judged as correct.

+ +

 

+

Example 1:

+ +
Input: nestedList = [[1,1],2,[1,1]]
+Output: [1,1,2,1,1]
+Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
+
+ +

Example 2:

+ +
Input: nestedList = [1,[4,[6]]]
+Output: [1,4,6]
+Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nestedList.length <= 500
  • +
  • The values of the integers in the nested list is in the range [-106, 106].
  • +
+
\ No newline at end of file diff --git a/0342-power-of-four/0342-power-of-four.cpp b/0342-power-of-four/0342-power-of-four.cpp new file mode 100644 index 00000000..eb1b35f2 --- /dev/null +++ b/0342-power-of-four/0342-power-of-four.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + + bool helper(int n) + { + if(n <= 0) + return false; + + if(n == 1) + return true; + + if(n % 4 == 0) + return helper(n/4); + + return false; + } + + bool isPowerOfFour(int n) { + return helper(n); + } +}; \ No newline at end of file diff --git a/0342-power-of-four/NOTES.md b/0342-power-of-four/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0342-power-of-four/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0342-power-of-four/README.md b/0342-power-of-four/README.md new file mode 100644 index 00000000..e5dba89e --- /dev/null +++ b/0342-power-of-four/README.md @@ -0,0 +1,24 @@ +

342. Power of Four

Easy


Given an integer n, return true if it is a power of four. Otherwise, return false.

+ +

An integer n is a power of four, if there exists an integer x such that n == 4x.

+ +

 

+

Example 1:

+
Input: n = 16
+Output: true
+

Example 2:

+
Input: n = 5
+Output: false
+

Example 3:

+
Input: n = 1
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion?
\ No newline at end of file diff --git a/0343-integer-break/0343-integer-break.cpp b/0343-integer-break/0343-integer-break.cpp new file mode 100644 index 00000000..23c42f94 --- /dev/null +++ b/0343-integer-break/0343-integer-break.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + + int helper(int idx, int sum, int n, vector>& dp) + { + if(sum == 0) + return 1; + + if(sum < 0 or idx >= n) + return 0; + + if(dp[idx][sum] != -1) + return dp[idx][sum]; + + int res = helper(idx+1, sum, n, dp); + + dp[idx][sum] = res = max(res, idx * helper(idx, sum-idx, n, dp)); + + return res; + } + + int integerBreak(int n) { + + vector> dp(n+1, vector(n+1, -1)); + + return helper(1, n, n, dp); + + } +}; \ No newline at end of file diff --git a/0343-integer-break/NOTES.md b/0343-integer-break/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0343-integer-break/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0343-integer-break/README.md b/0343-integer-break/README.md new file mode 100644 index 00000000..eeb0e4fa --- /dev/null +++ b/0343-integer-break/README.md @@ -0,0 +1,26 @@ +

343. Integer Break

Medium


Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

+ +

Return the maximum product you can get.

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 1
+Explanation: 2 = 1 + 1, 1 × 1 = 1.
+
+ +

Example 2:

+ +
Input: n = 10
+Output: 36
+Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 58
  • +
+
\ No newline at end of file diff --git a/0344-reverse-string/0344-reverse-string.cpp b/0344-reverse-string/0344-reverse-string.cpp new file mode 100644 index 00000000..c1441fef --- /dev/null +++ b/0344-reverse-string/0344-reverse-string.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + void reverseString(vector& s) { + + int n = s.size(); + + for(int i = 0; i < n/2; ++i) + swap(s[i], s[n-i-1]); + + } +}; \ No newline at end of file diff --git a/0344-reverse-string/NOTES.md b/0344-reverse-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0344-reverse-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0344-reverse-string/README.md b/0344-reverse-string/README.md new file mode 100644 index 00000000..5c9b221e --- /dev/null +++ b/0344-reverse-string/README.md @@ -0,0 +1,20 @@ +

344. Reverse String

Easy


Write a function that reverses a string. The input string is given as an array of characters s.

+ +

You must do this by modifying the input array in-place with O(1) extra memory.

+ +

 

+

Example 1:

+
Input: s = ["h","e","l","l","o"]
+Output: ["o","l","l","e","h"]
+

Example 2:

+
Input: s = ["H","a","n","n","a","h"]
+Output: ["h","a","n","n","a","H"]
+
+

 

+

Constraints:

+ + +
\ No newline at end of file diff --git a/0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp b/0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp new file mode 100644 index 00000000..c4128d15 --- /dev/null +++ b/0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector intersection(vector& nums1, vector& nums2) { + + vector res; + + set have(nums1.begin(),nums1.end()); + + for(auto& ele : nums2) + { + if(have.count(ele)) + { + res.push_back(ele); + have.erase(ele); + } + } + + return res; + } +}; \ No newline at end of file diff --git a/0349-intersection-of-two-arrays/NOTES.md b/0349-intersection-of-two-arrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0349-intersection-of-two-arrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0349-intersection-of-two-arrays/README.md b/0349-intersection-of-two-arrays/README.md new file mode 100644 index 00000000..24bc7284 --- /dev/null +++ b/0349-intersection-of-two-arrays/README.md @@ -0,0 +1,24 @@ +

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.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,2,1], nums2 = [2,2]
+Output: [2]
+
+ +

Example 2:

+ +
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
+Output: [9,4]
+Explanation: [4,9] is also accepted.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 1000
  • +
  • 0 <= nums1[i], nums2[i] <= 1000
  • +
+
\ No newline at end of file diff --git a/0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp b/0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp new file mode 100644 index 00000000..f0f633ae --- /dev/null +++ b/0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector intersect(vector& nums1, vector& nums2) { + + map mp, mp2; + + vector ans; + + for(auto& ele : nums1) + { + ++mp[ele]; + } + + for(auto& ele : nums2) + { + ++mp2[ele]; + } + + for(auto&[f,e] : mp) + { + if(mp2.find(f) != mp2.end()) + { + for(int i = 0; i < min(e, mp2[f]); ++i) + ans.push_back(f); + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/0350-intersection-of-two-arrays-ii/README.md b/0350-intersection-of-two-arrays-ii/README.md new file mode 100644 index 00000000..20f1306d --- /dev/null +++ b/0350-intersection-of-two-arrays-ii/README.md @@ -0,0 +1,33 @@ +

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.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,2,1], nums2 = [2,2]
+Output: [2,2]
+
+ +

Example 2:

+ +
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
+Output: [4,9]
+Explanation: [9,4] is also accepted.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 1000
  • +
  • 0 <= nums1[i], nums2[i] <= 1000
  • +
+ +

 

+

Follow up:

+ +
    +
  • What if the given array is already sorted? How would you optimize your algorithm?
  • +
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • +
  • 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?
  • +
+
\ No newline at end of file diff --git a/0354-russian-doll-envelopes/NOTES.md b/0354-russian-doll-envelopes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0354-russian-doll-envelopes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0354-russian-doll-envelopes/README.md b/0354-russian-doll-envelopes/README.md new file mode 100644 index 00000000..561fbe15 --- /dev/null +++ b/0354-russian-doll-envelopes/README.md @@ -0,0 +1,31 @@ +

354. Russian Doll Envelopes

Hard


You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.

+ +

One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.

+ +

Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).

+ +

Note: You cannot rotate an envelope.

+ +

 

+

Example 1:

+ +
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
+Output: 3
+Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
+
+ +

Example 2:

+ +
Input: envelopes = [[1,1],[1,1],[1,1]]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= envelopes.length <= 105
  • +
  • envelopes[i].length == 2
  • +
  • 1 <= wi, hi <= 105
  • +
+
\ No newline at end of file diff --git a/0368-largest-divisible-subset/0368-largest-divisible-subset.cpp b/0368-largest-divisible-subset/0368-largest-divisible-subset.cpp new file mode 100644 index 00000000..d5cc332d --- /dev/null +++ b/0368-largest-divisible-subset/0368-largest-divisible-subset.cpp @@ -0,0 +1,43 @@ +class Solution { +public: + vector largestDivisibleSubset(vector& nums) { + + int n = nums.size(); + + vector dp(n+1,1); + vector hash(n); + int lastIndex = -1; + int res = 1; + sort(nums.begin(),nums.end()); + + for(int i = 0; i dp[i]) + { + dp[i] = dp[j] + 1; + hash[i] = j; + } + } + if(dp[i] > res) + { + lastIndex = i; + res = dp[i]; + } + } + if(res == 1) + return {nums[0]}; + vector ans; + ans.push_back(nums[lastIndex]); + + while(hash[lastIndex] != lastIndex) + { + lastIndex = hash[lastIndex]; + ans.push_back(nums[lastIndex]); + } + + return ans; + } +}; \ No newline at end of file diff --git a/0368-largest-divisible-subset/README.md b/0368-largest-divisible-subset/README.md new file mode 100644 index 00000000..ae432a88 --- /dev/null +++ b/0368-largest-divisible-subset/README.md @@ -0,0 +1,32 @@ +

368. Largest Divisible Subset

Medium


Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:

+ +
    +
  • answer[i] % answer[j] == 0, or
  • +
  • answer[j] % answer[i] == 0
  • +
+ +

If there are multiple solutions, return any of them.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3]
+Output: [1,2]
+Explanation: [1,3] is also accepted.
+
+ +

Example 2:

+ +
Input: nums = [1,2,4,8]
+Output: [1,2,4,8]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= 2 * 109
  • +
  • All the integers in nums are unique.
  • +
+
\ No newline at end of file diff --git a/0380-insert-delete-getrandom-o1/NOTES.md b/0380-insert-delete-getrandom-o1/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0380-insert-delete-getrandom-o1/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp b/0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp new file mode 100644 index 00000000..654d516d --- /dev/null +++ b/0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int firstUniqChar(string s) { + + int n = s.size(); + + map mp; + + for(auto& ch : s) + ++mp[ch]; + + for(int i = 0; i < n; ++i) + { + if(mp[s[i]] == 1) + return i; + } + + return -1; + } +}; \ No newline at end of file diff --git a/0387-first-unique-character-in-a-string/NOTES.md b/0387-first-unique-character-in-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0387-first-unique-character-in-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0387-first-unique-character-in-a-string/README.md b/0387-first-unique-character-in-a-string/README.md new file mode 100644 index 00000000..e06661f4 --- /dev/null +++ b/0387-first-unique-character-in-a-string/README.md @@ -0,0 +1,21 @@ +

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.

+ +

 

+

Example 1:

+
Input: s = "leetcode"
+Output: 0
+

Example 2:

+
Input: s = "loveleetcode"
+Output: 2
+

Example 3:

+
Input: s = "aabb"
+Output: -1
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0389-find-the-difference/0389-find-the-difference.java b/0389-find-the-difference/0389-find-the-difference.java new file mode 100644 index 00000000..e0564259 --- /dev/null +++ b/0389-find-the-difference/0389-find-the-difference.java @@ -0,0 +1,14 @@ +class Solution { + public char findTheDifference(String s, String t) { + + char ch = 0; + + for(char c : s.toCharArray()) + ch ^= c; + + for(char c : t.toCharArray()) + ch ^= c; + + return ch; + } +} \ No newline at end of file diff --git a/0389-find-the-difference/NOTES.md b/0389-find-the-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0389-find-the-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0389-find-the-difference/README.md b/0389-find-the-difference/README.md new file mode 100644 index 00000000..0663dd96 --- /dev/null +++ b/0389-find-the-difference/README.md @@ -0,0 +1,29 @@ +

389. Find the Difference

Easy


You are given two strings s and t.

+ +

String t is generated by random shuffling string s and then add one more letter at a random position.

+ +

Return the letter that was added to t.

+ +

 

+

Example 1:

+ +
Input: s = "abcd", t = "abcde"
+Output: "e"
+Explanation: 'e' is the letter that was added.
+
+ +

Example 2:

+ +
Input: s = "", t = "y"
+Output: "y"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 1000
  • +
  • t.length == s.length + 1
  • +
  • s and t consist of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0402-remove-k-digits/0402-remove-k-digits.cpp b/0402-remove-k-digits/0402-remove-k-digits.cpp new file mode 100644 index 00000000..3a06c361 --- /dev/null +++ b/0402-remove-k-digits/0402-remove-k-digits.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + string removeKdigits(string num, int k) { + + stack st; + + st.push(num[0] - '0'); + + for(int i = 1; i < num.size(); ++i) + { + while(!st.empty() and st.top() > (num[i] - '0') and k) + { + st.pop(); + k--; + } + + st.push(num[i] - '0'); + if(st.size() == 1 and st.top() == 0) + st.pop(); + } + + while(!st.empty() and k--) + { + st.pop(); + } + + string ans; + + while(!st.empty()) + { + ans += (st.top() + '0'); + st.pop(); + } + + reverse(ans.begin(), ans.end()); + + return ans.empty() ? "0" : ans; + + } +}; \ No newline at end of file diff --git a/0402-remove-k-digits/NOTES.md b/0402-remove-k-digits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0402-remove-k-digits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0402-remove-k-digits/README.md b/0402-remove-k-digits/README.md new file mode 100644 index 00000000..4319e3f3 --- /dev/null +++ b/0402-remove-k-digits/README.md @@ -0,0 +1,33 @@ +

402. Remove K Digits

Medium


Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.

+ +

 

+

Example 1:

+ +
Input: num = "1432219", k = 3
+Output: "1219"
+Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
+
+ +

Example 2:

+ +
Input: num = "10200", k = 1
+Output: "200"
+Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
+
+ +

Example 3:

+ +
Input: num = "10", k = 2
+Output: "0"
+Explanation: Remove all the digits from the number and it is left with nothing which is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= num.length <= 105
  • +
  • num consists of only digits.
  • +
  • num does not have any leading zeros except for the zero itself.
  • +
+
\ No newline at end of file diff --git a/0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp b/0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp new file mode 100644 index 00000000..09675766 --- /dev/null +++ b/0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, int& sum) + { + if(!root) + return; + + if(root->left and !root->left->left and !root->left->right) + { + sum += root->left->val; + helper(root->right, sum); + } + else + { + helper(root->left, sum); + helper(root->right, sum); + } + + } + + int sumOfLeftLeaves(TreeNode* root) { + + int sum = 0; + + helper(root, sum); + + return sum; + + } +}; \ No newline at end of file diff --git a/0404-sum-of-left-leaves/README.md b/0404-sum-of-left-leaves/README.md new file mode 100644 index 00000000..34e2f167 --- /dev/null +++ b/0404-sum-of-left-leaves/README.md @@ -0,0 +1,26 @@ +

404. Sum of Left Leaves

Easy


Given the root of a binary tree, return the sum of all left leaves.

+ +

A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: 24
+Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
+
+ +

Example 2:

+ +
Input: root = [1]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • -1000 <= Node.val <= 1000
  • +
+
\ No newline at end of file diff --git a/0409-longest-palindrome/0409-longest-palindrome.cpp b/0409-longest-palindrome/0409-longest-palindrome.cpp new file mode 100644 index 00000000..d2c5f246 --- /dev/null +++ b/0409-longest-palindrome/0409-longest-palindrome.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int longestPalindrome(string s) { + map m ; + bool odd = 0; + for(auto i : s) + ++m[i]; + + int ind = 0; + + for(auto i : m) + { + if(i.second % 2 == 0) + ind += i.second; + else + { + ind += i.second - 1; + odd = true; + } + } + if(odd) + ind += 1; + return ind; + } +}; \ No newline at end of file diff --git a/0409-longest-palindrome/NOTES.md b/0409-longest-palindrome/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0409-longest-palindrome/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0409-longest-palindrome/README.md b/0409-longest-palindrome/README.md new file mode 100644 index 00000000..2d84a7b5 --- /dev/null +++ b/0409-longest-palindrome/README.md @@ -0,0 +1,27 @@ +

409. Longest Palindrome

Easy


Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.

+ +

Letters are case sensitive, for example, "Aa" is not considered a palindrome.

+ +

 

+

Example 1:

+ +
Input: s = "abccccdd"
+Output: 7
+Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
+
+ +

Example 2:

+ +
Input: s = "a"
+Output: 1
+Explanation: The longest palindrome that can be built is "a", whose length is 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 2000
  • +
  • s consists of lowercase and/or uppercase English letters only.
  • +
+
\ No newline at end of file diff --git a/0442-find-all-duplicates-in-an-array/NOTES.md b/0442-find-all-duplicates-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0442-find-all-duplicates-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0455-assign-cookies/0455-assign-cookies.cpp b/0455-assign-cookies/0455-assign-cookies.cpp new file mode 100644 index 00000000..de9ce97d --- /dev/null +++ b/0455-assign-cookies/0455-assign-cookies.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + + int j = 0; + int cnt = 0; + int i = 0; + + while(j < s.size() and i < g.size()) + { + if(g[i] <= s[j]) + { + ++i, ++j, ++cnt; + } + else + ++j; + } + + return cnt; + } +}; \ No newline at end of file diff --git a/0455-assign-cookies/README.md b/0455-assign-cookies/README.md new file mode 100644 index 00000000..9387f6b5 --- /dev/null +++ b/0455-assign-cookies/README.md @@ -0,0 +1,32 @@ +

455. Assign Cookies

Easy


Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

+ +

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

+ +

 

+

Example 1:

+ +
Input: g = [1,2,3], s = [1,1]
+Output: 1
+Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
+And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
+You need to output 1.
+
+ +

Example 2:

+ +
Input: g = [1,2], s = [1,2,3]
+Output: 2
+Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
+You have 3 cookies and their sizes are big enough to gratify all of the children, 
+You need to output 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= g.length <= 3 * 104
  • +
  • 0 <= s.length <= 3 * 104
  • +
  • 1 <= g[i], s[j] <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0456-132-pattern/0456-132-pattern.cpp b/0456-132-pattern/0456-132-pattern.cpp new file mode 100644 index 00000000..a1973605 --- /dev/null +++ b/0456-132-pattern/0456-132-pattern.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + bool find132pattern(vector& nums) { + + int n = nums.size(); + + int s3 = INT_MIN; + + stack st; + + for(int i = n-1; i >= 0; --i) + { + if(nums[i] < s3) + return true; + + while(!st.empty() and st.top() < nums[i]) + { + s3 = st.top(); + st.pop(); + } + + st.push(nums[i]); + } + + return false; + + } +}; \ No newline at end of file diff --git a/0456-132-pattern/NOTES.md b/0456-132-pattern/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0456-132-pattern/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0456-132-pattern/README.md b/0456-132-pattern/README.md new file mode 100644 index 00000000..049ce47c --- /dev/null +++ b/0456-132-pattern/README.md @@ -0,0 +1,35 @@ +

456. 132 Pattern

Medium


Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

+ +

Return true if there is a 132 pattern in nums, otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: false
+Explanation: There is no 132 pattern in the sequence.
+
+ +

Example 2:

+ +
Input: nums = [3,1,4,2]
+Output: true
+Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
+
+ +

Example 3:

+ +
Input: nums = [-1,3,2,0]
+Output: true
+Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 2 * 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp b/0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp new file mode 100644 index 00000000..4e5db170 --- /dev/null +++ b/0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int minMoves2(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + int med; + if(n&1) + med = nums[n/2]; + else + med = (nums[n/2] + nums[(n/2) - 1])/2; + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + ans += abs(med - nums[i]); + } + + return ans; + } +}; \ No newline at end of file diff --git a/0462-minimum-moves-to-equal-array-elements-ii/NOTES.md b/0462-minimum-moves-to-equal-array-elements-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0462-minimum-moves-to-equal-array-elements-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0462-minimum-moves-to-equal-array-elements-ii/README.md b/0462-minimum-moves-to-equal-array-elements-ii/README.md new file mode 100644 index 00000000..9c851770 --- /dev/null +++ b/0462-minimum-moves-to-equal-array-elements-ii/README.md @@ -0,0 +1,31 @@ +

462. Minimum Moves to Equal Array Elements II

Medium


Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.

+ +

In one move, you can increment or decrement an element of the array by 1.

+ +

Test cases are designed so that the answer will fit in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3]
+Output: 2
+Explanation:
+Only two moves are needed (remember each move increments or decrements one element):
+[1,2,3]  =>  [2,2,3]  =>  [2,2,2]
+
+ +

Example 2:

+ +
Input: nums = [1,10,2,9]
+Output: 16
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/0476-number-complement/0476-number-complement.cpp b/0476-number-complement/0476-number-complement.cpp new file mode 100644 index 00000000..1670a955 --- /dev/null +++ b/0476-number-complement/0476-number-complement.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int findComplement(int num) { + + int ans = 0; + bool ok = false; + + for(int i = 31; i >= 0; --i) + { + if(!(num & (1 << i)) and ok) + { + ans += (1 << i); + } + + if(num & (1 << i)) + { + ok = true; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/0476-number-complement/README.md b/0476-number-complement/README.md new file mode 100644 index 00000000..b46eec80 --- /dev/null +++ b/0476-number-complement/README.md @@ -0,0 +1,33 @@ +

476. Number Complement

Easy


The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.

+ +
    +
  • For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
  • +
+ +

Given an integer num, return its complement.

+ +

 

+

Example 1:

+ +
Input: num = 5
+Output: 2
+Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
+
+ +

Example 2:

+ +
Input: num = 1
+Output: 0
+Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num < 231
  • +
+ +

 

+

Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/

+
\ No newline at end of file diff --git a/0493-reverse-pairs/NOTES.md b/0493-reverse-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0493-reverse-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp b/0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp new file mode 100644 index 00000000..e9971d98 --- /dev/null +++ b/0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp @@ -0,0 +1,44 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, unordered_map& mp, int& maxi) + { + if(root) + { + helper(root->left, mp, maxi); + ++mp[root->val]; + maxi = max(maxi, mp[root->val]); + helper(root->right, mp, maxi); + } + } + + vector findMode(TreeNode* root) { + + unordered_map mp; + + int maxi = 0; + + helper(root, mp, maxi); + + vector ans; + + for(auto& itr : mp) + { + if(itr.second == maxi) + ans.push_back(itr.first); + } + + return ans; + } +}; \ No newline at end of file diff --git a/0501-find-mode-in-binary-search-tree/README.md b/0501-find-mode-in-binary-search-tree/README.md new file mode 100644 index 00000000..eaf7c94d --- /dev/null +++ b/0501-find-mode-in-binary-search-tree/README.md @@ -0,0 +1,35 @@ +

501. Find Mode in Binary Search Tree

Easy


Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

+ +

If the tree has more than one mode, return them in any order.

+ +

Assume a BST is defined as follows:

+ +
    +
  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • +
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • +
  • Both the left and right subtrees must also be binary search trees.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [1,null,2,2]
+Output: [2]
+
+ +

Example 2:

+ +
Input: root = [0]
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -105 <= Node.val <= 105
  • +
+ +

 

+Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
\ No newline at end of file diff --git a/0502-ipo/NOTES.md b/0502-ipo/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0502-ipo/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp b/0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp new file mode 100644 index 00000000..2859c4be --- /dev/null +++ b/0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + map mp; + + void findLeftValue(TreeNode* root, int currLevel , int& maxLevel) + { + if(root) + { + findLeftValue(root->left, currLevel + 1, maxLevel); + findLeftValue(root->right, currLevel + 1, maxLevel); + + maxLevel = max(maxLevel , currLevel); + + if(mp.find(maxLevel) == mp.end()) + mp[maxLevel] = root->val; + } + } + + int findBottomLeftValue(TreeNode* root) { + + int maxLevel = 0; + + findLeftValue(root, 0, maxLevel); + + return mp[maxLevel]; + } +}; \ No newline at end of file diff --git a/0513-find-bottom-left-tree-value/NOTES.md b/0513-find-bottom-left-tree-value/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0513-find-bottom-left-tree-value/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0513-find-bottom-left-tree-value/README.md b/0513-find-bottom-left-tree-value/README.md new file mode 100644 index 00000000..a733cb60 --- /dev/null +++ b/0513-find-bottom-left-tree-value/README.md @@ -0,0 +1,23 @@ +

513. Find Bottom Left Tree Value

Medium


Given the root of a binary tree, return the leftmost value in the last row of the tree.

+ +

 

+

Example 1:

+ +
Input: root = [2,1,3]
+Output: 1
+
+ +

Example 2:

+ +
Input: root = [1,2,3,4,null,5,6,null,null,7]
+Output: 7
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -231 <= Node.val <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0514-freedom-trail/0514-freedom-trail.cpp b/0514-freedom-trail/0514-freedom-trail.cpp new file mode 100644 index 00000000..4eaf3a81 --- /dev/null +++ b/0514-freedom-trail/0514-freedom-trail.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + + int helper(int idx, int ptr, int n, int m, string& ring, string& key, vector>& dp) + { + if(idx >= m) + return 0; + + if(dp[idx][ptr] != -1) + return dp[idx][ptr]; + + int steps = 1e9; + + for(int i = 0; i < n; ++i) + { + if(ring[i] == key[idx]) + steps = min(steps, 1 + min(abs(i - ptr), n - abs(i-ptr)) + helper(idx+1, i, n, m, ring, key, dp)); + } + + return dp[idx][ptr] = steps; + } + + int findRotateSteps(string ring, string key) { + + int n = ring.size(); + int m = key.size(); + + vector> dp(m+1, vector(n+1, -1)); + + return helper(0, 0, n, m, ring, key, dp); + + } +}; \ No newline at end of file diff --git a/0514-freedom-trail/NOTES.md b/0514-freedom-trail/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0514-freedom-trail/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0514-freedom-trail/README.md b/0514-freedom-trail/README.md new file mode 100644 index 00000000..7f40ba56 --- /dev/null +++ b/0514-freedom-trail/README.md @@ -0,0 +1,40 @@ +

514. Freedom Trail

Hard


In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring" and use the dial to spell a specific keyword to open the door.

+ +

Given a string ring that represents the code engraved on the outer ring and another string key that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.

+ +

Initially, the first character of the ring is aligned at the "12:00" direction. You should spell all the characters in key one by one by rotating ring clockwise or anticlockwise to make each character of the string key aligned at the "12:00" direction and then by pressing the center button.

+ +

At the stage of rotating the ring to spell the key character key[i]:

+ +
    +
  1. You can rotate the ring clockwise or anticlockwise by one place, which counts as one step. The final purpose of the rotation is to align one of ring's characters at the "12:00" direction, where this character must equal key[i].
  2. +
  3. If the character key[i] has been aligned at the "12:00" direction, press the center button to spell, which also counts as one step. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.
  4. +
+ +

 

+

Example 1:

+ +
Input: ring = "godding", key = "gd"
+Output: 4
+Explanation:
+For the first key character 'g', since it is already in place, we just need 1 step to spell this character. 
+For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
+Also, we need 1 more step for spelling.
+So the final output is 4.
+
+ +

Example 2:

+ +
Input: ring = "godding", key = "godding"
+Output: 13
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= ring.length, key.length <= 100
  • +
  • ring and key consist of only lower case English letters.
  • +
  • It is guaranteed that key could always be spelled by rotating ring.
  • +
+
\ No newline at end of file diff --git a/0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp b/0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp new file mode 100644 index 00000000..f2f14855 --- /dev/null +++ b/0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp @@ -0,0 +1,49 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector largestValues(TreeNode* root) { + + if(!root) + return {}; + + queue q; + + vector ans; + + q.push(root); + + while(!q.empty()) + { + int size = q.size(); + + int maxi = INT_MIN; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + maxi = max(maxi, curr->val); + + if(curr->left) + q.push(curr->left); + + if(curr->right) + q.push(curr->right); + } + + ans.push_back(maxi); + } + return ans; + } +}; \ No newline at end of file diff --git a/0515-find-largest-value-in-each-tree-row/NOTES.md b/0515-find-largest-value-in-each-tree-row/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0515-find-largest-value-in-each-tree-row/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0515-find-largest-value-in-each-tree-row/README.md b/0515-find-largest-value-in-each-tree-row/README.md new file mode 100644 index 00000000..95bb431d --- /dev/null +++ b/0515-find-largest-value-in-each-tree-row/README.md @@ -0,0 +1,23 @@ +

515. Find Largest Value in Each Tree Row

Medium


Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

+ +

 

+

Example 1:

+ +
Input: root = [1,3,2,5,3,null,9]
+Output: [1,3,9]
+
+ +

Example 2:

+ +
Input: root = [1,2,3]
+Output: [1,3]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree will be in the range [0, 104].
  • +
  • -231 <= Node.val <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp new file mode 100644 index 00000000..47037b9c --- /dev/null +++ b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + + int n = nums.size(); + + map mp; + + int sum = 0; + + mp[0] = -1; + + for(int i = 0; i < n; ++i) + { + sum += nums[i]; + + int rem = sum % k; + + if(mp.find(rem) != mp.end()) + { + if(i - mp[rem] > 1) + return true; + } + else + mp[rem] = i; + } + + return false; + } +}; \ No newline at end of file diff --git a/0523-continuous-subarray-sum/NOTES.md b/0523-continuous-subarray-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0523-continuous-subarray-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0523-continuous-subarray-sum/README.md b/0523-continuous-subarray-sum/README.md new file mode 100644 index 00000000..0bf5d8e1 --- /dev/null +++ b/0523-continuous-subarray-sum/README.md @@ -0,0 +1,48 @@ +

523. Continuous Subarray Sum

Medium


Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.

+ +

A good subarray is a subarray where:

+ +
    +
  • its length is at least two, and
  • +
  • the sum of the elements of the subarray is a multiple of k.
  • +
+ +

Note that:

+ +
    +
  • A subarray is a contiguous part of the array.
  • +
  • An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [23,2,4,6,7], k = 6
+Output: true
+Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
+
+ +

Example 2:

+ +
Input: nums = [23,2,6,4,7], k = 6
+Output: true
+Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
+42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
+
+ +

Example 3:

+ +
Input: nums = [23,2,6,4,7], k = 13
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= sum(nums[i]) <= 231 - 1
  • +
  • 1 <= k <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0525-contiguous-array/0525-contiguous-array.cpp b/0525-contiguous-array/0525-contiguous-array.cpp new file mode 100644 index 00000000..1d4d98ec --- /dev/null +++ b/0525-contiguous-array/0525-contiguous-array.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int findMaxLength(vector& nums) { + + map mp; + + mp.insert({0, -1}); + + int sum = 0, ans = 0, n = nums.size(); + + for(int i = 0; i < n; ++i) + { + sum += (nums[i] == 1 ? 1 : -1); + if(mp.find(sum) != mp.end()) + ans = max(ans, i - mp[sum]); + else + mp[sum] = i; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0525-contiguous-array/NOTES.md b/0525-contiguous-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0525-contiguous-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp new file mode 100644 index 00000000..e64aec71 --- /dev/null +++ b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp @@ -0,0 +1,34 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int helper(TreeNode* root, int& ans) + { + if(!root) + return 0; + + int left = helper(root->left, ans); + int right = helper(root->right, ans); + + ans = max(ans, left + right); + + return 1 + max(left, right); + } + + int diameterOfBinaryTree(TreeNode* root) { + + int ans = 0; + helper(root, ans); + return ans; + } +}; \ No newline at end of file diff --git a/0543-diameter-of-binary-tree/NOTES.md b/0543-diameter-of-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0543-diameter-of-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0543-diameter-of-binary-tree/README.md b/0543-diameter-of-binary-tree/README.md new file mode 100644 index 00000000..26e6fc34 --- /dev/null +++ b/0543-diameter-of-binary-tree/README.md @@ -0,0 +1,28 @@ +

543. Diameter of Binary Tree

Easy


Given the root of a binary tree, return the length of the diameter of the tree.

+ +

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

+ +

The length of a path between two nodes is represented by the number of edges between them.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4,5]
+Output: 3
+Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
+
+ +

Example 2:

+ +
Input: root = [1,2]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -100 <= Node.val <= 100
  • +
+
\ No newline at end of file diff --git a/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp new file mode 100644 index 00000000..f87965aa --- /dev/null +++ b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int n, int absent, int late, vector>>& dp) + { + if(n == 0) + return 1; + + if(dp[n][absent][late] != -1) + return dp[n][absent][late]; + + int total = 0; + + total += helper(n-1, absent, 2, dp); + total %= mod; + + if(absent > 0) + { + total += helper(n-1, absent-1, 2, dp); + total %= mod; + } + + if(late > 0) + { + total += helper(n-1, absent, late-1, dp); + total %= mod; + } + + return dp[n][absent][late] = total; + } + + int checkRecord(int n) { + + vector>> dp(n+1, vector>(2, vector(3, -1))); + + return helper(n, 1, 2, dp); + + } +}; \ No newline at end of file diff --git a/0552-student-attendance-record-ii/NOTES.md b/0552-student-attendance-record-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0552-student-attendance-record-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0556-next-greater-element-iii/0556-next-greater-element-iii.cpp b/0556-next-greater-element-iii/0556-next-greater-element-iii.cpp new file mode 100644 index 00000000..6d89562c --- /dev/null +++ b/0556-next-greater-element-iii/0556-next-greater-element-iii.cpp @@ -0,0 +1,49 @@ +class Solution { +public: + + long long next_Permutation(int n) + { + string str = to_string(n); + + int breakPoint = -1; + + int m = str.size(); + + for(int i = m-2; i >= 0; --i) + { + if(str[i] < str[i+1]) + { + breakPoint = i; + break; + } + } + + if(breakPoint == -1) + { + reverse(str.begin(), str.end()); + } + else + { + for(int i = m-1; i > breakPoint; --i) + { + if(str[i] > str[breakPoint]) + { + swap(str[i], str[breakPoint]); + break; + } + } + + reverse(str.begin() + breakPoint + 1 , str.end()); + } + + return stoll(str); + } + + int nextGreaterElement(int n) { + + long long nextGreater = next_Permutation(n); + + return (nextGreater > INT_MAX or nextGreater <= n ? -1 : nextGreater); + + } +}; \ No newline at end of file diff --git a/0556-next-greater-element-iii/README.md b/0556-next-greater-element-iii/README.md new file mode 100644 index 00000000..3827d0f3 --- /dev/null +++ b/0556-next-greater-element-iii/README.md @@ -0,0 +1,19 @@ +

556. Next Greater Element III

Medium


Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1.

+ +

Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.

+ +

 

+

Example 1:

+
Input: n = 12
+Output: 21
+

Example 2:

+
Input: n = 21
+Output: -1
+
+

 

+

Constraints:

+ +
    +
  • 1 <= n <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp b/0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp new file mode 100644 index 00000000..610932ec --- /dev/null +++ b/0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + string reverseWords(string s) { + + stringstream ss(s); + + string word; + + string ans; + + while(ss >> word) + { + reverse(word.begin(), word.end()); + ans += word + ' '; + } + + ans.pop_back(); + + return ans; + + } +}; \ No newline at end of file diff --git a/0557-reverse-words-in-a-string-iii/NOTES.md b/0557-reverse-words-in-a-string-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0557-reverse-words-in-a-string-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0557-reverse-words-in-a-string-iii/README.md b/0557-reverse-words-in-a-string-iii/README.md new file mode 100644 index 00000000..cdc6104c --- /dev/null +++ b/0557-reverse-words-in-a-string-iii/README.md @@ -0,0 +1,21 @@ +

557. Reverse Words in a String III

Easy


Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

+ +

 

+

Example 1:

+
Input: s = "Let's take LeetCode contest"
+Output: "s'teL ekat edoCteeL tsetnoc"
+

Example 2:

+
Input: s = "God Ding"
+Output: "doG gniD"
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • s contains printable ASCII characters.
  • +
  • s does not contain any leading or trailing spaces.
  • +
  • There is at least one word in s.
  • +
  • All the words in s are separated by a single space.
  • +
+
\ No newline at end of file diff --git a/0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp b/0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp new file mode 100644 index 00000000..0d79df33 --- /dev/null +++ b/0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp @@ -0,0 +1,61 @@ +using ll = long long; + +class Solution { +public: + + ll palindrome(ll firstHalf, bool isEven) + { + ll resultNum = firstHalf; + + if(!isEven) + firstHalf /= 10; + + while(firstHalf > 0) + { + resultNum = (resultNum * 10) + (firstHalf%10); + firstHalf /= 10; + } + + return resultNum; + } + + string nearestPalindromic(string n) { + + int sz = n.size(); + int mid = sz/2; + + ll firstHalfLen = (sz % 2 == 0 ? mid : mid+1); + + ll firstHalf = stoll(n.substr(0, firstHalfLen)); + + vector pos; + + pos.push_back(palindrome(firstHalf, sz % 2 == 0)); + pos.push_back(palindrome(firstHalf+1, sz % 2 == 0)); + pos.push_back(palindrome(firstHalf-1, sz % 2 == 0)); + pos.push_back(pow(10, sz-1) - 1); + pos.push_back(pow(10, sz) + 1); + + ll diff = LONG_MAX; + ll res = LONG_MAX; + ll orgNum = stoll(n); + + for(auto& num : pos) + { + if(num == orgNum) continue; + + if(abs(num - orgNum) < diff) + { + diff = abs(num - orgNum); + res = num; + } + else if(abs(num - orgNum) == diff) + { + res = min(res, num); + } + } + + return to_string(res); + + } +}; \ No newline at end of file diff --git a/0564-find-the-closest-palindrome/NOTES.md b/0564-find-the-closest-palindrome/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0564-find-the-closest-palindrome/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0564-find-the-closest-palindrome/README.md b/0564-find-the-closest-palindrome/README.md new file mode 100644 index 00000000..a5b4ec14 --- /dev/null +++ b/0564-find-the-closest-palindrome/README.md @@ -0,0 +1,28 @@ +

564. Find the Closest Palindrome

Hard


Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.

+ +

The closest is defined as the absolute difference minimized between two integers.

+ +

 

+

Example 1:

+ +
Input: n = "123"
+Output: "121"
+
+ +

Example 2:

+ +
Input: n = "1"
+Output: "0"
+Explanation: 0 and 2 are the closest palindromes but we return the smallest which is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n.length <= 18
  • +
  • n consists of only digits.
  • +
  • n does not have leading zeros.
  • +
  • n is representing an integer in the range [1, 1018 - 1].
  • +
+
\ No newline at end of file diff --git a/0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp b/0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp new file mode 100644 index 00000000..379ead6e --- /dev/null +++ b/0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + + int dp[55][55][55]; + + const int mod = 1e9+7; + + int boundaryPaths(int i, int j, int n, int m, int maxMove) + { + if(maxMove >= 0 and i < 0 or j < 0 or i >= n or j >= m) + return 1; + + if(dp[i][j][maxMove] != -1) + return dp[i][j][maxMove]; + + int up = 0, right = 0, down = 0, left = 0; + + if(maxMove) + { + up = boundaryPaths(i-1, j, n, m, maxMove-1); + right = boundaryPaths(i, j+1, n, m, maxMove-1); + down = boundaryPaths(i+1, j, n, m, maxMove-1); + left = boundaryPaths(i, j-1, n, m, maxMove-1); + } + + return dp[i][j][maxMove] = ((((up%mod + right%mod)%mod + down%mod)%mod + left%mod) % mod); + } + + int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { + + memset(dp, -1, sizeof(dp)); + + return boundaryPaths(startRow, startColumn, m, n, maxMove); + + } +}; \ No newline at end of file diff --git a/0576-out-of-boundary-paths/NOTES.md b/0576-out-of-boundary-paths/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0576-out-of-boundary-paths/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0576-out-of-boundary-paths/README.md b/0576-out-of-boundary-paths/README.md new file mode 100644 index 00000000..25a688e2 --- /dev/null +++ b/0576-out-of-boundary-paths/README.md @@ -0,0 +1,27 @@ +

576. Out of Boundary Paths

Medium


There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.

+ +

Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
+Output: 6
+
+ +

Example 2:

+ +
Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
+Output: 12
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= m, n <= 50
  • +
  • 0 <= maxMove <= 50
  • +
  • 0 <= startRow < m
  • +
  • 0 <= startColumn < n
  • +
+
\ No newline at end of file diff --git a/0584-find-customer-referee/0584-find-customer-referee.sql b/0584-find-customer-referee/0584-find-customer-referee.sql new file mode 100644 index 00000000..7f77caac --- /dev/null +++ b/0584-find-customer-referee/0584-find-customer-referee.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select name from Customer where referee_id is null or referee_id != 2; \ No newline at end of file diff --git a/0584-find-customer-referee/NOTES.md b/0584-find-customer-referee/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0584-find-customer-referee/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0584-find-customer-referee/README.md b/0584-find-customer-referee/README.md new file mode 100644 index 00000000..372be3cf --- /dev/null +++ b/0584-find-customer-referee/README.md @@ -0,0 +1,47 @@ +

584. Find Customer Referee

Easy


Table: Customer

+ +
+-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| id          | int     |
+| name        | varchar |
+| referee_id  | int     |
++-------------+---------+
+In SQL, id is the primary key column for this table.
+Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
+
+ +

 

+ +

Find the names of the customer that are not referred by the customer with id = 2.

+ +

Return the result table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Customer table:
++----+------+------------+
+| id | name | referee_id |
++----+------+------------+
+| 1  | Will | null       |
+| 2  | Jane | null       |
+| 3  | Alex | 2          |
+| 4  | Bill | null       |
+| 5  | Zack | 1          |
+| 6  | Mark | 2          |
++----+------+------------+
+Output: 
++------+
+| name |
++------+
+| Will |
+| Jane |
+| Bill |
+| Zack |
++------+
+
+
\ No newline at end of file diff --git a/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp new file mode 100644 index 00000000..fb8c1b94 --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp @@ -0,0 +1,45 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; +*/ + +class Solution { +public: + + void postorder(Node* root, vector& ans) + { + if(root) + { + for(auto& chlNode : root->children) + { + postorder(chlNode, ans); + } + ans.push_back(root->val); + } + } + + vector postorder(Node* root) { + + vector ans; + + postorder(root, ans); + + return ans; + + } +}; \ No newline at end of file diff --git a/0590-n-ary-tree-postorder-traversal/NOTES.md b/0590-n-ary-tree-postorder-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0590-n-ary-tree-postorder-traversal/README.md b/0590-n-ary-tree-postorder-traversal/README.md new file mode 100644 index 00000000..cee6f44d --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/README.md @@ -0,0 +1,29 @@ +

590. N-ary Tree Postorder Traversal

Easy


Given the root of an n-ary tree, return the postorder traversal of its nodes' values.

+ +

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

+ +

 

+

Example 1:

+ +
Input: root = [1,null,3,2,4,null,5,6]
+Output: [5,6,3,2,4,1]
+
+ +

Example 2:

+ +
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
+Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 104].
  • +
  • 0 <= Node.val <= 104
  • +
  • The height of the n-ary tree is less than or equal to 1000.
  • +
+ +

 

+

Follow up: Recursive solution is trivial, could you do it iteratively?

+
\ No newline at end of file diff --git a/0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp b/0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp new file mode 100644 index 00000000..532239f0 --- /dev/null +++ b/0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + string fractionAddition(string expression) { + + int num = 0; + int den = 1; + + int i = 0, n = expression.size(); + + while(i < n) + { + int curNum = 0; + int curDen = 0; + + bool isNeg = (expression[i] == '-'); + + if(expression[i] == '+' or expression[i] == '-') + ++i; + + while(i < n && isdigit(expression[i])) + { + int val = expression[i] - '0'; + curNum = (curNum * 10) + val; + ++i; + } + + ++i; + if(isNeg) curNum *= -1; + + while(i < n && isdigit(expression[i])) + { + int val = expression[i] - '0'; + curDen = (curDen * 10) + val; + ++i; + } + + num = (num * curDen) + (curNum * den); + den = den * curDen; + } + + int gcd = abs(__gcd(num, den)); + num /= gcd; + den /= gcd; + + return to_string(num) + "/" + to_string(den); + } +}; \ No newline at end of file diff --git a/0592-fraction-addition-and-subtraction/README.md b/0592-fraction-addition-and-subtraction/README.md new file mode 100644 index 00000000..5d1558c5 --- /dev/null +++ b/0592-fraction-addition-and-subtraction/README.md @@ -0,0 +1,34 @@ +

592. Fraction Addition and Subtraction

Medium


Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.

+ +

The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.

+ +

 

+

Example 1:

+ +
Input: expression = "-1/2+1/2"
+Output: "0/1"
+
+ +

Example 2:

+ +
Input: expression = "-1/2+1/2+1/3"
+Output: "1/3"
+
+ +

Example 3:

+ +
Input: expression = "1/3-1/2"
+Output: "-1/6"
+
+ +

 

+

Constraints:

+ +
    +
  • The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
  • +
  • Each fraction (input and output) has the format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
  • +
  • The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1, 10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  • +
  • The number of given fractions will be in the range [1, 10].
  • +
  • The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
  • +
+
\ No newline at end of file diff --git a/0595-big-countries/0595-big-countries.sql b/0595-big-countries/0595-big-countries.sql new file mode 100644 index 00000000..4ade2dde --- /dev/null +++ b/0595-big-countries/0595-big-countries.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select name, population, area from World where area >= 3000000 or population >= 25000000 order by name asc; \ No newline at end of file diff --git a/0595-big-countries/NOTES.md b/0595-big-countries/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0595-big-countries/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp b/0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp new file mode 100644 index 00000000..0323b754 --- /dev/null +++ b/0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp @@ -0,0 +1,45 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, string& ans) + { + if(root) + { + ans += to_string(root->val); + + if(root->left) + { + ans += '('; + helper(root->left, ans); + ans += ')'; + } + + if(root->right) + { + if(!root->left) + ans += "()"; + ans += '('; + helper(root->right, ans); + ans += ')'; + } + } + } + + string tree2str(TreeNode* root) { + + string ans; + helper(root, ans); + return ans; + } +}; \ No newline at end of file diff --git a/0606-construct-string-from-binary-tree/NOTES.md b/0606-construct-string-from-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0606-construct-string-from-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0606-construct-string-from-binary-tree/README.md b/0606-construct-string-from-binary-tree/README.md new file mode 100644 index 00000000..d304033f --- /dev/null +++ b/0606-construct-string-from-binary-tree/README.md @@ -0,0 +1,27 @@ +

606. Construct String from Binary Tree

Easy


Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.

+ +

Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4]
+Output: "1(2(4))(3)"
+Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)"
+
+ +

Example 2:

+ +
Input: root = [1,2,3,null,4]
+Output: "1(2()(4))(3)"
+Explanation: Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -1000 <= Node.val <= 1000
  • +
+
\ No newline at end of file diff --git a/0621-task-scheduler/0621-task-scheduler.cpp b/0621-task-scheduler/0621-task-scheduler.cpp new file mode 100644 index 00000000..1b34b091 --- /dev/null +++ b/0621-task-scheduler/0621-task-scheduler.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int leastInterval(vector& tasks, int n) { + + mapmp; + int count = 0; + + for(auto ele : tasks) + { + mp[ele]++; + count = max(count, mp[ele]); + } + + int ans = (count-1)*(n+1); + + for(auto e : mp) + { + if(e.second == count) + ans++; + } + + return max((int)tasks.size(), ans); + } +}; \ No newline at end of file diff --git a/0621-task-scheduler/README.md b/0621-task-scheduler/README.md new file mode 100644 index 00000000..a449ed32 --- /dev/null +++ b/0621-task-scheduler/README.md @@ -0,0 +1,95 @@ +

621. Task Scheduler

Medium


You are given an array of CPU tasks, each represented by letters A to Z, and a cooling time, n. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: identical tasks must be separated by at least n intervals due to cooling time.

+ +

​Return the minimum number of intervals required to complete all tasks.

+ +

 

+

Example 1:

+ +
+

Input: tasks = ["A","A","A","B","B","B"], n = 2

+ +

Output: 8

+ +

Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.

+ +

After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th cycle, you can do A again as 2 intervals have passed.

+
+ +

Example 2:

+ +
+

Input: tasks = ["A","C","A","B","D","B"], n = 1

+ +

Output: 6

+ +

Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.

+ +

With a cooling interval of 1, you can repeat a task after just one other task.

+
+ +

Example 3:

+ +
+

Input: tasks = ["A","A","A", "B","B","B"], n = 3

+ +

Output: 10

+ +

Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.

+ +

There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= tasks.length <= 104
  • +
  • tasks[i] is an uppercase English letter.
  • +
  • 0 <= n <= 100
  • +
+
\ No newline at end of file diff --git a/0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp b/0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp new file mode 100644 index 00000000..6b5b032c --- /dev/null +++ b/0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp @@ -0,0 +1,63 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* addOneRow(TreeNode* root, int val, int depth) { + + if(depth == 1) + { + TreeNode* node = new TreeNode(val); + + node->left = root; + + return node; + } + + queue q; + + --depth; + q.push(root); + + while(!q.empty()) + { + int sz = q.size(); + + for(int i = 0; i < sz; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + if(depth == 1) + { + TreeNode* nexLeft = curr->left ? curr->left : nullptr; + TreeNode* newNode1 = new TreeNode(val); + curr->left = newNode1; + newNode1->left = nexLeft; + + TreeNode* nexRight = curr->right ? curr->right : nullptr; + TreeNode* newNode2 = new TreeNode(val); + curr->right = newNode2; + newNode2->right = nexRight; + } + + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + } + + --depth; + } + + return root; + } +}; \ No newline at end of file diff --git a/0623-add-one-row-to-tree/README.md b/0623-add-one-row-to-tree/README.md new file mode 100644 index 00000000..c1f4b23e --- /dev/null +++ b/0623-add-one-row-to-tree/README.md @@ -0,0 +1,37 @@ +

623. Add One Row to Tree

Medium


Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.

+ +

Note that the root node is at depth 1.

+ +

The adding rule is:

+ +
    +
  • Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
  • +
  • cur's original left subtree should be the left subtree of the new left subtree root.
  • +
  • cur's original right subtree should be the right subtree of the new right subtree root.
  • +
  • If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
+Output: [4,1,1,2,null,null,6,3,1,5]
+
+ +

Example 2:

+ +
Input: root = [4,2,null,3,1], val = 1, depth = 3
+Output: [4,2,null,1,1,3,null,null,1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • The depth of the tree is in the range [1, 104].
  • +
  • -100 <= Node.val <= 100
  • +
  • -105 <= val <= 105
  • +
  • 1 <= depth <= the depth of tree + 1
  • +
+
\ No newline at end of file diff --git a/0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp b/0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp new file mode 100644 index 00000000..5ceb9190 --- /dev/null +++ b/0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxDistance(vector>& arrays) { + + int left = arrays[0][0], right = arrays[0].back(), ans = 0; + + for(int i = 1; i < arrays.size(); i++) + { + ans = max(ans, max(abs(arrays[i][0] - right), abs(arrays[i].back() - left))); + left = min(left, arrays[i][0]); + right = max(right, arrays[i].back()); + } + + + return ans; + } +}; \ No newline at end of file diff --git a/0624-maximum-distance-in-arrays/README.md b/0624-maximum-distance-in-arrays/README.md new file mode 100644 index 00000000..77b7a756 --- /dev/null +++ b/0624-maximum-distance-in-arrays/README.md @@ -0,0 +1,32 @@ +

624. Maximum Distance in Arrays

Medium


You are given m arrays, where each array is sorted in ascending order.

+ +

You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a - b|.

+ +

Return the maximum distance.

+ +

 

+

Example 1:

+ +
Input: arrays = [[1,2,3],[4,5],[1,2,3]]
+Output: 4
+Explanation: One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
+
+ +

Example 2:

+ +
Input: arrays = [[1],[1]]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • m == arrays.length
  • +
  • 2 <= m <= 105
  • +
  • 1 <= arrays[i].length <= 500
  • +
  • -104 <= arrays[i][j] <= 104
  • +
  • arrays[i] is sorted in ascending order.
  • +
  • There will be at most 105 integers in all the arrays.
  • +
+
\ No newline at end of file diff --git a/0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp b/0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp new file mode 100644 index 00000000..75508a34 --- /dev/null +++ b/0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int maximumProduct(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + return max({nums[0] * nums[1] * nums[2], nums[0] * nums[1] * nums[n-1], nums[n-3] * nums[n-2] * nums[n-1]}); + + } +}; \ No newline at end of file diff --git a/0628-maximum-product-of-three-numbers/NOTES.md b/0628-maximum-product-of-three-numbers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0628-maximum-product-of-three-numbers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0628-maximum-product-of-three-numbers/README.md b/0628-maximum-product-of-three-numbers/README.md new file mode 100644 index 00000000..ecd0b816 --- /dev/null +++ b/0628-maximum-product-of-three-numbers/README.md @@ -0,0 +1,21 @@ +

628. Maximum Product of Three Numbers

Easy


Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

+ +

 

+

Example 1:

+
Input: nums = [1,2,3]
+Output: 6
+

Example 2:

+
Input: nums = [1,2,3,4]
+Output: 24
+

Example 3:

+
Input: nums = [-1,-2,-3]
+Output: -6
+
+

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 104
  • +
  • -1000 <= nums[i] <= 1000
  • +
+
\ No newline at end of file diff --git a/0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp b/0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp new file mode 100644 index 00000000..44bda9a0 --- /dev/null +++ b/0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int mod = 1e9+7; + int kInversePairs(int n, int K) { + //dp[i][j] denote till i numbers how many jth inversion pair-containing permutations are there + vector> dp(n+1,vector(K+1,0)); + dp[0][0]=0; + for(int i=1;i<=K;i++){ + dp[1][i]=0; + } + for(int i=1;i<=n;i++){ + dp[i][0]=1; + } +for(int i=2;i<=n;i++){ + for(int j=1;j<=K;j++){ + dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod; + if(j-i>=0){ + dp[i][j]=(dp[i][j]-dp[i-1][j-i]+mod)%mod; + } + } + } + return dp[n][K]; + } +}; diff --git a/0629-k-inverse-pairs-array/README.md b/0629-k-inverse-pairs-array/README.md new file mode 100644 index 00000000..b9c1e1ff --- /dev/null +++ b/0629-k-inverse-pairs-array/README.md @@ -0,0 +1,27 @@ +

629. K Inverse Pairs Array

Hard


For an integer array nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j].

+ +

Given two integers n and k, return the number of different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 3, k = 0
+Output: 1
+Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
+
+ +

Example 2:

+ +
Input: n = 3, k = 1
+Output: 2
+Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
  • 0 <= k <= 1000
  • +
+
\ No newline at end of file diff --git a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp new file mode 100644 index 00000000..185bb33f --- /dev/null +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + bool judgeSquareSum(int c) { + + for(int i = 2; i * i <= c; ++i) + { + int cnt = 0; + + while(c % i == 0) + { + ++cnt; + c /= i; + } + + if(i % 4 == 3 and cnt % 2 != 0) + return false; + } + + return c%4 != 3; + + } +}; \ No newline at end of file diff --git a/0633-sum-of-square-numbers/NOTES.md b/0633-sum-of-square-numbers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0633-sum-of-square-numbers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0633-sum-of-square-numbers/README.md b/0633-sum-of-square-numbers/README.md new file mode 100644 index 00000000..efeadaa1 --- /dev/null +++ b/0633-sum-of-square-numbers/README.md @@ -0,0 +1,23 @@ +

633. Sum of Square Numbers

Medium


Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.

+ +

 

+

Example 1:

+ +
Input: c = 5
+Output: true
+Explanation: 1 * 1 + 2 * 2 = 5
+
+ +

Example 2:

+ +
Input: c = 3
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= c <= 231 - 1
  • +
+
\ No newline at end of file diff --git a/0645-set-mismatch/0645-set-mismatch.cpp b/0645-set-mismatch/0645-set-mismatch.cpp new file mode 100644 index 00000000..37bdfca6 --- /dev/null +++ b/0645-set-mismatch/0645-set-mismatch.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector findErrorNums(vector& nums) { + + int n = nums.size(); + int missing = -1, repeat = -1; + + for(int i = 0; i < n; ++i) + { + if(nums[abs(nums[i])-1] < 0) + { + repeat = abs(nums[i]); + } + else + { + nums[abs(nums[i])-1] *= -1; + } + } + + for(int i = 0; i < n; ++i) + { + if(nums[i] > 0) + missing = i + 1; + } + + return {repeat, missing}; + } +}; \ No newline at end of file diff --git a/0645-set-mismatch/NOTES.md b/0645-set-mismatch/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0645-set-mismatch/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0645-set-mismatch/README.md b/0645-set-mismatch/README.md new file mode 100644 index 00000000..78699612 --- /dev/null +++ b/0645-set-mismatch/README.md @@ -0,0 +1,22 @@ +

645. Set Mismatch

Easy


You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

+ +

You are given an integer array nums representing the data status of this set after the error.

+ +

Find the number that occurs twice and the number that is missing and return them in the form of an array.

+ +

 

+

Example 1:

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

Example 2:

+
Input: nums = [1,1]
+Output: [1,2]
+
+

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 104
  • +
  • 1 <= nums[i] <= 104
  • +
+
\ No newline at end of file diff --git a/0647-palindromic-substrings/0647-palindromic-substrings.cpp b/0647-palindromic-substrings/0647-palindromic-substrings.cpp new file mode 100644 index 00000000..750babdc --- /dev/null +++ b/0647-palindromic-substrings/0647-palindromic-substrings.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int countSubstrings(string s) { + + int n = s.size(); + + vector> dp(n+1, vector(n+1, 0)); + + int cnt = 0; + + for(int diff = 0; diff < n; ++diff) + { + for(int i = 0, j = diff; i < n and j < n; ++i, ++j) + { + if(i == j) + { + ++cnt; + dp[i][j] = 1; + } + else if(diff == 1 and s[i] == s[j]) + { + ++cnt; + dp[i][j] = 2; + } + else if(diff > 1 and s[i] == s[j] and dp[i+1][j-1]) + { + ++cnt; + dp[i][j] = dp[i+1][j-1] + 2; + } + } + } + + return cnt; + } +}; \ No newline at end of file diff --git a/0647-palindromic-substrings/README.md b/0647-palindromic-substrings/README.md new file mode 100644 index 00000000..f833b17e --- /dev/null +++ b/0647-palindromic-substrings/README.md @@ -0,0 +1,29 @@ +

647. Palindromic Substrings

Medium


Given a string s, return the number of palindromic substrings in it.

+ +

A string is a palindrome when it reads the same backward as forward.

+ +

A substring is a contiguous sequence of characters within the string.

+ +

 

+

Example 1:

+ +
Input: s = "abc"
+Output: 3
+Explanation: Three palindromic strings: "a", "b", "c".
+
+ +

Example 2:

+ +
Input: s = "aaa"
+Output: 6
+Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0648-replace-words/0648-replace-words.cpp b/0648-replace-words/0648-replace-words.cpp new file mode 100644 index 00000000..c01b7524 --- /dev/null +++ b/0648-replace-words/0648-replace-words.cpp @@ -0,0 +1,110 @@ +class Node +{ + private: + Node* child[26] = {nullptr}; + bool isWord = false; + + public: + bool containsKey(char ch) + { + return child[ch-'a'] != nullptr; + } + + Node* get(char ch) + { + return child[ch-'a']; + } + + void put(char ch, Node* newNode) + { + child[ch-'a'] = newNode; + } + + void setEnd() + { + isWord = true; + } + + bool isEnd() + { + return isWord; + } +}; + +class Trie{ + private: + Node* root; + + public: + Trie() + { + root = new Node(); + } + + void insert(string& word) + { + int n = word.size(); + Node* temp = root; + + for(int i = 0; i < n; ++i) + { + if(!temp->containsKey(word[i])) + temp->put(word[i], new Node()); + temp = temp->get(word[i]); + } + temp->setEnd(); + } + + string search(string& word) + { + int n = word.size(); + Node* temp = root; + string ans; + + for(int i = 0; i < n; ++i) + { + if(temp->containsKey(word[i])) + { + ans += word[i]; + } + else + return "-1"; + + temp = temp->get(word[i]); + + if(temp->isEnd()) + return ans; + } + + return "-1"; + } +}; + +class Solution { +public: + string replaceWords(vector& dictionary, string sentence) { + + Trie *trie = new Trie(); + + stringstream ss(sentence); + string word, ans; + + for(auto& str : dictionary) + trie->insert(str); + + while(ss >> word) + { + string curr = trie->search(word); + + if(curr != "-1") + ans += curr; + else + ans += word; + ans += " "; + } + + ans.pop_back(); + + return ans; + } +}; \ No newline at end of file diff --git a/0648-replace-words/NOTES.md b/0648-replace-words/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0648-replace-words/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0648-replace-words/README.md b/0648-replace-words/README.md new file mode 100644 index 00000000..aed11e37 --- /dev/null +++ b/0648-replace-words/README.md @@ -0,0 +1,34 @@ +

648. Replace Words

Medium


In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".

+ +

Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.

+ +

Return the sentence after the replacement.

+ +

 

+

Example 1:

+ +
Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
+Output: "the cat was rat by the bat"
+
+ +

Example 2:

+ +
Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
+Output: "a a b c"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= dictionary.length <= 1000
  • +
  • 1 <= dictionary[i].length <= 100
  • +
  • dictionary[i] consists of only lower-case letters.
  • +
  • 1 <= sentence.length <= 106
  • +
  • sentence consists of only lower-case letters and spaces.
  • +
  • The number of words in sentence is in the range [1, 1000]
  • +
  • The length of each word in sentence is in the range [1, 1000]
  • +
  • Every two consecutive words in sentence will be separated by exactly one space.
  • +
  • sentence does not have leading or trailing spaces.
  • +
+
\ No newline at end of file diff --git a/0650-2-keys-keyboard/0650-2-keys-keyboard.cpp b/0650-2-keys-keyboard/0650-2-keys-keyboard.cpp new file mode 100644 index 00000000..bf53ed7a --- /dev/null +++ b/0650-2-keys-keyboard/0650-2-keys-keyboard.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + int helper(int n, int last, int tot, vector>& dp) + { + if(n < 0) + return 1e9; + + if(n == 0) + return 0; + + if(dp[n][last] != -1) + return dp[n][last]; + + int previousCopy = 1 + helper(n - last, last, tot, dp); + + int alreadyTaken = tot - n; + + int newCopy = 2 + helper(n - alreadyTaken, alreadyTaken, tot, dp); + + return dp[n][last] = min(newCopy, previousCopy); + + } + + int minSteps(int n) { + + if(n == 1) + return 0; + + vector> dp(n+1,vector(n+1, -1)); + + return helper(n-1, 1, n, dp) + 1; + + } +}; \ No newline at end of file diff --git a/0650-2-keys-keyboard/NOTES.md b/0650-2-keys-keyboard/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0650-2-keys-keyboard/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0650-2-keys-keyboard/README.md b/0650-2-keys-keyboard/README.md new file mode 100644 index 00000000..4d778549 --- /dev/null +++ b/0650-2-keys-keyboard/README.md @@ -0,0 +1,33 @@ +

650. 2 Keys Keyboard

Medium


There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step:

+ +
    +
  • Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
  • +
  • Paste: You can paste the characters which are copied last time.
  • +
+ +

Given an integer n, return the minimum number of operations to get the character 'A' exactly n times on the screen.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 3
+Explanation: Initially, we have one character 'A'.
+In step 1, we use Copy All operation.
+In step 2, we use Paste operation to get 'AA'.
+In step 3, we use Paste operation to get 'AAA'.
+
+ +

Example 2:

+ +
Input: n = 1
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file diff --git a/0661-image-smoother/0661-image-smoother.cpp b/0661-image-smoother/0661-image-smoother.cpp new file mode 100644 index 00000000..81933ccf --- /dev/null +++ b/0661-image-smoother/0661-image-smoother.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector> imageSmoother(vector>& img) { + int rows = img.size(); + int cols = img[0].size(); + vector> result(rows, vector(cols, 0)); + + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + int total_sum = 0; + int count = 0; + + for (int l = max(0, i-1); l < min(rows, i+2); ++l) + { + for (int k = max(0, j-1); k < min(cols, j+2); ++k) + { + total_sum += img[l][k]; + count += 1; + } + } + + result[i][j] = total_sum / count; + } + } + + return result; + } +}; \ No newline at end of file diff --git a/0661-image-smoother/README.md b/0661-image-smoother/README.md new file mode 100644 index 00000000..6bd8ee84 --- /dev/null +++ b/0661-image-smoother/README.md @@ -0,0 +1,35 @@ +

661. Image Smoother

Easy


An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).

+ +

Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.

+ +

 

+

Example 1:

+ +
Input: img = [[1,1,1],[1,0,1],[1,1,1]]
+Output: [[0,0,0],[0,0,0],[0,0,0]]
+Explanation:
+For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
+For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
+For the point (1,1): floor(8/9) = floor(0.88888889) = 0
+
+ +

Example 2:

+ +
Input: img = [[100,200,100],[200,50,200],[100,200,100]]
+Output: [[137,141,137],[141,138,141],[137,141,137]]
+Explanation:
+For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
+For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
+For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
+
+ +

 

+

Constraints:

+ +
    +
  • m == img.length
  • +
  • n == img[i].length
  • +
  • 1 <= m, n <= 200
  • +
  • 0 <= img[i][j] <= 255
  • +
+
\ No newline at end of file diff --git a/0664-strange-printer/NOTES.md b/0664-strange-printer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0664-strange-printer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0678-valid-parenthesis-string/NOTES.md b/0678-valid-parenthesis-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0678-valid-parenthesis-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0678-valid-parenthesis-string/README.md b/0678-valid-parenthesis-string/README.md new file mode 100644 index 00000000..26a61d26 --- /dev/null +++ b/0678-valid-parenthesis-string/README.md @@ -0,0 +1,30 @@ +

678. Valid Parenthesis String

Medium


Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.

+ +

The following rules define a valid string:

+ +
    +
  • Any left parenthesis '(' must have a corresponding right parenthesis ')'.
  • +
  • Any right parenthesis ')' must have a corresponding left parenthesis '('.
  • +
  • Left parenthesis '(' must go before the corresponding right parenthesis ')'.
  • +
  • '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".
  • +
+ +

 

+

Example 1:

+
Input: s = "()"
+Output: true
+

Example 2:

+
Input: s = "(*)"
+Output: true
+

Example 3:

+
Input: s = "(*))"
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s[i] is '(', ')' or '*'.
  • +
+
\ No newline at end of file diff --git a/0698-partition-to-k-equal-sum-subsets/NOTES.md b/0698-partition-to-k-equal-sum-subsets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0698-partition-to-k-equal-sum-subsets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0705-design-hashset/0705-design-hashset.cpp b/0705-design-hashset/0705-design-hashset.cpp index c76dbb4f..f8bc114b 100644 --- a/0705-design-hashset/0705-design-hashset.cpp +++ b/0705-design-hashset/0705-design-hashset.cpp @@ -1,22 +1,55 @@ class MyHashSet { public: - unordered_map mp; + vector> hash; + int size = 10001; MyHashSet() { - + hash.resize(size); } void add(int key) { - ++mp[key]; + + int idx = key % size; + + for(auto& itr : hash[idx]) + { + if(itr == key) + { + return; + } + } + + hash[idx].push_back(key); } void remove(int key) { - mp.erase(key); + + int idx = key % size; + + for(auto itr = hash[idx].begin(); itr != hash[idx].end(); ++itr) + { + if(*itr == key) + { + hash[idx].erase(itr); + return; + } + } + } bool contains(int key) { - return mp[key] > 0; + + int idx = key % size; + + for(auto& itr : hash[idx]) + { + if(itr == key) + { + return true; + } + } + return false; } }; diff --git a/0706-design-hashmap/0706-design-hashmap.cpp b/0706-design-hashmap/0706-design-hashmap.cpp new file mode 100644 index 00000000..d991530d --- /dev/null +++ b/0706-design-hashmap/0706-design-hashmap.cpp @@ -0,0 +1,63 @@ +class MyHashMap { +public: + + vector> > hash; + int size = 10001; + + MyHashMap() { + hash.resize(size); + } + + void put(int key, int value) { + int idx = key % size; + + for(auto& [k, val] : hash[idx]) + { + if(k == key) + { + val = value; + return; + } + } + + hash[idx].push_back({key, value}); + } + + int get(int key) { + + int idx = key % size; + + if(hash[idx].empty()) + return -1; + + for(auto& [k, val] : hash[idx]) + { + if(k == key) + return val; + } + + return -1; + } + + void remove(int key) { + + int idx = key % size; + + for(auto itr = hash[idx].begin(); itr != hash[idx].end(); ++itr) + { + if(itr->first == key) + { + hash[idx].erase(itr); + return; + } + } + } +}; + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap* obj = new MyHashMap(); + * obj->put(key,value); + * int param_2 = obj->get(key); + * obj->remove(key); + */ \ No newline at end of file diff --git a/0706-design-hashmap/README.md b/0706-design-hashmap/README.md new file mode 100644 index 00000000..830381e2 --- /dev/null +++ b/0706-design-hashmap/README.md @@ -0,0 +1,40 @@ +

706. Design HashMap

Easy


Design a HashMap without using any built-in hash table libraries.

+ +

Implement the MyHashMap class:

+ +
    +
  • MyHashMap() initializes the object with an empty map.
  • +
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • +
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • +
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
+[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
+Output
+[null, null, null, 1, -1, null, 1, null, -1]
+
+Explanation
+MyHashMap myHashMap = new MyHashMap();
+myHashMap.put(1, 1); // The map is now [[1,1]]
+myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
+myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
+myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
+myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
+myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
+myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
+myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= key, value <= 106
  • +
  • At most 104 calls will be made to put, get, and remove.
  • +
+
\ No newline at end of file diff --git a/0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp b/0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp new file mode 100644 index 00000000..af0b24dc --- /dev/null +++ b/0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int smallestDistancePair(vector& nums, int k) { + + int n = nums.size(), N = 1000000; + vector cnt(N, 0); + + for (int i = 0; i < n; i++) { + for (int j = i+1; j < n; j++) + cnt[abs(nums[i]-nums[j])]++; + } + + for (int i = 0; i < N; i++) { + if (cnt[i] >= k) return i; + k -= cnt[i]; + } + + return 0; + } +}; \ No newline at end of file diff --git a/0719-find-k-th-smallest-pair-distance/NOTES.md b/0719-find-k-th-smallest-pair-distance/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0719-find-k-th-smallest-pair-distance/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0719-find-k-th-smallest-pair-distance/README.md b/0719-find-k-th-smallest-pair-distance/README.md new file mode 100644 index 00000000..cdd3d60c --- /dev/null +++ b/0719-find-k-th-smallest-pair-distance/README.md @@ -0,0 +1,38 @@ +

719. Find K-th Smallest Pair Distance

Hard


The distance of a pair of integers a and b is defined as the absolute difference between a and b.

+ +

Given an integer array nums and an integer k, return the kth smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,1], k = 1
+Output: 0
+Explanation: Here are all the pairs:
+(1,3) -> 2
+(1,1) -> 0
+(3,1) -> 2
+Then the 1st smallest distance pair is (1,1), and its distance is 0.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1], k = 2
+Output: 0
+
+ +

Example 3:

+ +
Input: nums = [1,6,1], k = 3
+Output: 5
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 2 <= n <= 104
  • +
  • 0 <= nums[i] <= 106
  • +
  • 1 <= k <= n * (n - 1) / 2
  • +
+
\ No newline at end of file diff --git a/0721-accounts-merge/NOTES.md b/0721-accounts-merge/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0721-accounts-merge/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp b/0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp new file mode 100644 index 00000000..f2dbaf0d --- /dev/null +++ b/0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + int helper(int idx, int n, vector& cost, vector& dp) + { + if(idx >= n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int oneStep = cost[idx] + helper(idx+1, n, cost, dp); + int twoStep = cost[idx] + helper(idx+2, n, cost, dp); + + return dp[idx] = min(oneStep, twoStep); + } + + int minCostClimbingStairs(vector& cost) { + + int n = cost.size(); + + vector dp(n+1, -1); + + return min(helper(0, n, cost, dp), helper(1, n, cost, dp)); + + } +}; \ No newline at end of file diff --git a/0746-min-cost-climbing-stairs/NOTES.md b/0746-min-cost-climbing-stairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0746-min-cost-climbing-stairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0746-min-cost-climbing-stairs/README.md b/0746-min-cost-climbing-stairs/README.md new file mode 100644 index 00000000..c810b720 --- /dev/null +++ b/0746-min-cost-climbing-stairs/README.md @@ -0,0 +1,38 @@ +

746. Min Cost Climbing Stairs

Easy


You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

+ +

You can either start from the step with index 0, or the step with index 1.

+ +

Return the minimum cost to reach the top of the floor.

+ +

 

+

Example 1:

+ +
Input: cost = [10,15,20]
+Output: 15
+Explanation: You will start at index 1.
+- Pay 15 and climb two steps to reach the top.
+The total cost is 15.
+
+ +

Example 2:

+ +
Input: cost = [1,100,1,1,1,100,1,1,100,1]
+Output: 6
+Explanation: You will start at index 0.
+- Pay 1 and climb two steps to reach index 2.
+- Pay 1 and climb two steps to reach index 4.
+- Pay 1 and climb two steps to reach index 6.
+- Pay 1 and climb one step to reach index 7.
+- Pay 1 and climb two steps to reach index 9.
+- Pay 1 and climb one step to reach the top.
+The total cost is 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= cost.length <= 1000
  • +
  • 0 <= cost[i] <= 999
  • +
+
\ No newline at end of file diff --git a/0752-open-the-lock/0752-open-the-lock.cpp b/0752-open-the-lock/0752-open-the-lock.cpp new file mode 100644 index 00000000..b7539355 --- /dev/null +++ b/0752-open-the-lock/0752-open-the-lock.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + + string forward(string curr, int idx) + { + if(curr[idx] == '9') + curr[idx] = '0'; + else + ++curr[idx]; + return curr; + } + + string backward(string curr, int idx) + { + if(curr[idx] == '0') + curr[idx] = '9'; + else + --curr[idx]; + return curr; + } + + int openLock(vector& deadends, string target) { + + set st(deadends.begin(), deadends.end()); + + set visited; + + queue q; + + if(!st.count("0000")) + { + q.push("0000"); + visited.insert("0000"); + } + + int steps = 0; + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0; i < size; ++i) + { + string curr = q.front(); + q.pop(); + + if(curr == target) + return steps; + + for(int i = 0; i < 4; ++i) + { + string f = forward(curr, i); + string s = backward(curr, i); + + if(!st.count(f) and !visited.count(f)) + { + q.push(f); + visited.insert(f); + } + if(!st.count(s) and !visited.count(s)) + { + q.push(s); + visited.insert(s); + } + } + } + ++steps; + } + + return -1; + } +}; \ No newline at end of file diff --git a/0752-open-the-lock/README.md b/0752-open-the-lock/README.md new file mode 100644 index 00000000..36cb276f --- /dev/null +++ b/0752-open-the-lock/README.md @@ -0,0 +1,44 @@ +

752. Open the Lock

Medium


You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.

+ +

The lock initially starts at '0000', a string representing the state of the 4 wheels.

+ +

You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.

+ +

Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.

+ +

 

+

Example 1:

+ +
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
+Output: 6
+Explanation: 
+A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
+Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
+because the wheels of the lock become stuck after the display becomes the dead end "0102".
+
+ +

Example 2:

+ +
Input: deadends = ["8888"], target = "0009"
+Output: 1
+Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
+
+ +

Example 3:

+ +
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
+Output: -1
+Explanation: We cannot reach the target without getting stuck.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= deadends.length <= 500
  • +
  • deadends[i].length == 4
  • +
  • target.length == 4
  • +
  • target will not be in the list deadends.
  • +
  • target and deadends[i] consist of digits only.
  • +
+
\ No newline at end of file diff --git a/0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp b/0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp new file mode 100644 index 00000000..7c02db31 --- /dev/null +++ b/0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector kthSmallestPrimeFraction(vector& arr, int k) { + + vector>> res; + + int n = arr.size(); + + for(int i = 0; i < n; ++i) + { + for(int j = n-1; j >= 0; --j) + { + if(i != j) + { + res.push_back({((double)arr[i]/(double)arr[j]),{arr[i], arr[j]}}); + } + } + } + + sort(res.begin(), res.end()); + + return {res[k-1].second.first, res[k-1].second.second}; + } +}; \ No newline at end of file diff --git a/0786-k-th-smallest-prime-fraction/NOTES.md b/0786-k-th-smallest-prime-fraction/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0786-k-th-smallest-prime-fraction/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0787-cheapest-flights-within-k-stops/NOTES.md b/0787-cheapest-flights-within-k-stops/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0787-cheapest-flights-within-k-stops/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0791-custom-sort-string/0791-custom-sort-string.cpp b/0791-custom-sort-string/0791-custom-sort-string.cpp new file mode 100644 index 00000000..7124d4f8 --- /dev/null +++ b/0791-custom-sort-string/0791-custom-sort-string.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + string customSortString(string order, string s) { + + map mp; + + for(int i = 0; i < order.size(); ++i) + { + if(mp.find(order[i]) == mp.end()) + mp[order[i]] = i; + } + + sort(s.begin(), s.end(),[&](const auto& a, const auto& b){ + return mp[a] < mp[b]; + }); + + return s; + + } +}; \ No newline at end of file diff --git a/0791-custom-sort-string/NOTES.md b/0791-custom-sort-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0791-custom-sort-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0791-custom-sort-string/README.md b/0791-custom-sort-string/README.md new file mode 100644 index 00000000..df4199fe --- /dev/null +++ b/0791-custom-sort-string/README.md @@ -0,0 +1,41 @@ +

791. Custom Sort String

Medium


You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously.

+ +

Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.

+ +

Return any permutation of s that satisfies this property.

+ +

 

+

Example 1:

+ +
+

Input: order = "cba", s = "abcd"

+ +

Output: "cbad"

+ +

Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".

+ +

Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.

+
+ +

Example 2:

+ +
+

Input: order = "bcafg", s = "abcd"

+ +

Output: "bcad"

+ +

Explanation: The characters "b", "c", and "a" from order dictate the order for the characters in s. The character "d" in s does not appear in order, so its position is flexible.

+ +

Following the order of appearance in order, "b", "c", and "a" from s should be arranged as "b", "c", "a". "d" can be placed at any position since it's not in order. The output "bcad" correctly follows this rule. Other arrangements like "bacd" or "bcda" would also be valid, as long as "b", "c", "a" maintain their order.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= order.length <= 26
  • +
  • 1 <= s.length <= 200
  • +
  • order and s consist of lowercase English letters.
  • +
  • All the characters of order are unique.
  • +
+
\ No newline at end of file diff --git a/0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp b/0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp new file mode 100644 index 00000000..d2d96ba0 --- /dev/null +++ b/0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int numFactoredBinaryTrees(vector& arr) { + + int n = arr.size(); + + unordered_map mp; + + for(auto& itr : arr) + ++mp[itr]; + + sort(arr.begin(), arr.end()); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + for(int j = 0; j < i; ++j) + { + if(arr[i] % arr[j] == 0) + { + long long count = (mp[arr[j]] * 1LL * mp[arr[i]/arr[j]]) % mod; + mp[arr[i]] = (mp[arr[i]] % mod + count)% mod; + } + } + } + + for(auto& itr : mp) + { + ans = (ans + itr.second) % mod; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0823-binary-trees-with-factors/NOTES.md b/0823-binary-trees-with-factors/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0823-binary-trees-with-factors/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0823-binary-trees-with-factors/README.md b/0823-binary-trees-with-factors/README.md new file mode 100644 index 00000000..af1c0af6 --- /dev/null +++ b/0823-binary-trees-with-factors/README.md @@ -0,0 +1,28 @@ +

823. Binary Trees With Factors

Medium


Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.

+ +

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

+ +

Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: arr = [2,4]
+Output: 3
+Explanation: We can make these trees: [2], [4], [4, 2, 2]
+ +

Example 2:

+ +
Input: arr = [2,4,5,10]
+Output: 7
+Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 2 <= arr[i] <= 109
  • +
  • All the values of arr are unique.
  • +
+
\ No newline at end of file diff --git a/0827-making-a-large-island/0827-making-a-large-island.cpp b/0827-making-a-large-island/0827-making-a-large-island.cpp new file mode 100644 index 00000000..79b5e3d4 --- /dev/null +++ b/0827-making-a-large-island/0827-making-a-large-island.cpp @@ -0,0 +1,145 @@ +class Solution { +public: + + class DSU{ + public: + vector parent, size, rank; + public: + DSU(int n) + { + parent.resize(n+1); + size.resize(n+1, 1); + rank.resize(n+1, 0); + + for(int i = 0; i <= n; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(rank[parU] < rank[parV]) + parent[parU] = parV; + else if(rank[parV] > rank[parU]) + parent[parV] = parU; + else + { + parent[parV] = parU; + ++rank[parU]; + } + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } + }; + + int largestIsland(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + DSU dsu((n*m) + 1); + + vector dx = {-1, 0, +1, 0}; + vector dy = {0, -1, 0, +1}; + + auto isValid = [&](int x, int y) + { + return (x >= 0 and y >= 0 and x < n and y < m); + }; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(grid[i][j] == 0) + continue; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy) and grid[newx][newy]) + { + int node = (i * m) + j; + int adjNode = (newx * m) + newy; + + if(!dsu.isSame(node, adjNode)) + dsu.unionBySize(node, adjNode); + } + } + } + } + + int maxi = -1; + + unordered_set visited; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(grid[i][j] == 0) + { + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy) and grid[newx][newy]) + { + visited.insert(dsu.findParent((newx*m) + newy)); + } + } + + int curr = 0; + + for(auto& ele : visited) + curr += dsu.size[ele]; + + visited.clear(); + + maxi = max(maxi, curr + 1); + } + } + } + + + return (maxi == -1 ? (n * m) : maxi); + } +}; \ No newline at end of file diff --git a/0827-making-a-large-island/NOTES.md b/0827-making-a-large-island/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0827-making-a-large-island/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0827-making-a-large-island/README.md b/0827-making-a-large-island/README.md new file mode 100644 index 00000000..930714d9 --- /dev/null +++ b/0827-making-a-large-island/README.md @@ -0,0 +1,36 @@ +

827. Making A Large Island

Hard


You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.

+ +

Return the size of the largest island in grid after applying this operation.

+ +

An island is a 4-directionally connected group of 1s.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,0],[0,1]]
+Output: 3
+Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
+
+ +

Example 2:

+ +
Input: grid = [[1,1],[1,0]]
+Output: 4
+Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
+ +

Example 3:

+ +
Input: grid = [[1,1],[1,1]]
+Output: 4
+Explanation: Can't change any 0 to 1, only one island with area = 4.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= n <= 500
  • +
  • grid[i][j] is either 0 or 1.
  • +
\ No newline at end of file diff --git a/0834-sum-of-distances-in-tree/NOTES.md b/0834-sum-of-distances-in-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0834-sum-of-distances-in-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp b/0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp new file mode 100644 index 00000000..3cfd5c6a --- /dev/null +++ b/0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp @@ -0,0 +1,81 @@ +class Solution { +public: + + int checkMatrix(vector>& mat) + { + + int diagSumA = 0, diagSumB = 0, end = 2; + map mp; + vector freq(10, 0); + + for(int i = 0; i < mat.size(); ++i) + { + int sum = 0; + for(int j = 0; j < mat[0].size(); ++j) + { + if(mat[i][j] >= 0 and mat[i][j] <= 9) + ++freq[mat[i][j]]; + sum += mat[i][j]; + if(i == j) + diagSumA += mat[i][j]; + if(j == end) + { + diagSumB += mat[i][j]; + --end; + } + } + ++mp[sum]; + } + + for(int j = 0; j < mat[0].size(); ++j) + { + int sum = 0; + for(int i = 0; i < mat.size(); ++i) + sum += mat[i][j]; + ++mp[sum]; + } + + for(int i = 1; i <= 9; ++i) + { + if(freq[i] == 0) + return 0; + } + + ++mp[diagSumA]; + ++mp[diagSumB]; + + return mp.size() == 1; + } + + int numMagicSquaresInside(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + vector> mat; + + if(i + 3 > n or j + 3 > m) + break; + for(int k = i; k < i + 3; ++k) + { + vector row; + for(int l = j; l < j + 3; ++l) + row.push_back(grid[k][l]); + mat.push_back(row); + } + + if(checkMatrix(mat)) + ++ans; + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0840-magic-squares-in-grid/NOTES.md b/0840-magic-squares-in-grid/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0840-magic-squares-in-grid/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0840-magic-squares-in-grid/README.md b/0840-magic-squares-in-grid/README.md new file mode 100644 index 00000000..06ddc8d2 --- /dev/null +++ b/0840-magic-squares-in-grid/README.md @@ -0,0 +1,35 @@ +

840. Magic Squares In Grid

Medium


A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.

+ +

Given a row x col grid of integers, how many 3 x 3 contiguous magic square subgrids are there?

+ +

Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15.

+ +

 

+

Example 1:

+ +
Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
+Output: 1
+Explanation: 
+The following subgrid is a 3 x 3 magic square:
+
+while this one is not:
+
+In total, there is only one magic square inside the given grid.
+
+ +

Example 2:

+ +
Input: grid = [[8]]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • row == grid.length
  • +
  • col == grid[i].length
  • +
  • 1 <= row, col <= 10
  • +
  • 0 <= grid[i][j] <= 15
  • +
+
\ No newline at end of file diff --git a/0844-backspace-string-compare/0844-backspace-string-compare.cpp b/0844-backspace-string-compare/0844-backspace-string-compare.cpp new file mode 100644 index 00000000..0e7ac67b --- /dev/null +++ b/0844-backspace-string-compare/0844-backspace-string-compare.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + bool backspaceCompare(string s, string t) { + + string one, two; + + for(auto& itr : s) + { + if(itr == '#') + { + if(!one.empty()) + one.pop_back(); + } + else + one.push_back(itr); + } + + for(auto& itr : t) + { + if(itr == '#') + { + if(!two.empty()) + two.pop_back(); + } + else + two.push_back(itr); + } + + return one == two; + + } +}; \ No newline at end of file diff --git a/0844-backspace-string-compare/NOTES.md b/0844-backspace-string-compare/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0844-backspace-string-compare/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0846-hand-of-straights/0846-hand-of-straights.cpp b/0846-hand-of-straights/0846-hand-of-straights.cpp new file mode 100644 index 00000000..a0c9450b --- /dev/null +++ b/0846-hand-of-straights/0846-hand-of-straights.cpp @@ -0,0 +1,51 @@ +class Solution { +public: + bool isNStraightHand(vector& hand, int groupSize) { + + int n = hand.size(); + + if(n % groupSize != 0) + return false; + + int step = n / groupSize; + + map mp; + + for(auto& ele : hand) + ++mp[ele]; + + while(step--) + { + int k = 0, prev = -1; + + if(mp.size() < groupSize) + return false; + + for(auto& ele : mp) + { + ++k; + + if(prev == -1) + prev = ele.first; + else + { + if(prev + 1 != ele.first) + return false; + } + + prev = ele.first; + + --ele.second; + + if(ele.second == 0) + mp.erase(ele.first); // remove + + if(k == groupSize) + break; + } + } + + return true; + + } +}; \ No newline at end of file diff --git a/0846-hand-of-straights/NOTES.md b/0846-hand-of-straights/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0846-hand-of-straights/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0846-hand-of-straights/README.md b/0846-hand-of-straights/README.md new file mode 100644 index 00000000..fe6ec095 --- /dev/null +++ b/0846-hand-of-straights/README.md @@ -0,0 +1,32 @@ +

846. Hand of Straights

Medium


Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.

+ +

Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
+Output: true
+Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
+
+ +

Example 2:

+ +
Input: hand = [1,2,3,4,5], groupSize = 4
+Output: false
+Explanation: Alice's hand can not be rearranged into groups of 4.
+
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= hand.length <= 104
  • +
  • 0 <= hand[i] <= 109
  • +
  • 1 <= groupSize <= hand.length
  • +
+ +

 

+

Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/

+
\ No newline at end of file diff --git a/0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp b/0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp new file mode 100644 index 00000000..38566929 --- /dev/null +++ b/0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + double mincostToHireWorkers(vector& quality, vector& wage, int k) { + vector> v; + int n=quality.size(); + for(int i=0;i pq; + + double ans=DBL_MAX; + int q=0; + for(int i=0;iv[i].second){ + q -= pq.top(); + pq.pop(); + + pq.push(v[i].second); + q += v[i].second; + } + } + if(pq.size()==k){ + double cost = q*v[i].first; + if(ans > cost) ans=cost; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/0857-minimum-cost-to-hire-k-workers/NOTES.md b/0857-minimum-cost-to-hire-k-workers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0857-minimum-cost-to-hire-k-workers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0857-minimum-cost-to-hire-k-workers/README.md b/0857-minimum-cost-to-hire-k-workers/README.md new file mode 100644 index 00000000..d251e14b --- /dev/null +++ b/0857-minimum-cost-to-hire-k-workers/README.md @@ -0,0 +1,35 @@ +

857. Minimum Cost to Hire K Workers

Hard


There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the ith worker.

+ +

We want to hire exactly k workers to form a paid group. To hire a group of k workers, we must pay them according to the following rules:

+ +
    +
  1. Every worker in the paid group must be paid at least their minimum wage expectation.
  2. +
  3. In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
  4. +
+ +

Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5 of the actual answer will be accepted.

+ +

 

+

Example 1:

+ +
Input: quality = [10,20,5], wage = [70,50,30], k = 2
+Output: 105.00000
+Explanation: We pay 70 to 0th worker and 35 to 2nd worker.
+
+ +

Example 2:

+ +
Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
+Output: 30.66667
+Explanation: We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.
+
+ +

 

+

Constraints:

+ +
    +
  • n == quality.length == wage.length
  • +
  • 1 <= k <= n <= 104
  • +
  • 1 <= quality[i], wage[i] <= 104
  • +
+
\ No newline at end of file diff --git a/0860-lemonade-change/0860-lemonade-change.cpp b/0860-lemonade-change/0860-lemonade-change.cpp new file mode 100644 index 00000000..630ace19 --- /dev/null +++ b/0860-lemonade-change/0860-lemonade-change.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + bool lemonadeChange(vector& bills) { + + map mp; + + for(auto& ele : bills) + { + int rem = ele - 5; + + if(rem) + { + while(mp[10] and rem >= 10) + { + --mp[10]; + rem -= 10; + } + + while(mp[5] and rem) + { + --mp[5]; + rem -= 5; + } + } + + if(rem > 0) + return false; + + ++mp[ele]; + } + + return true; + + } +}; \ No newline at end of file diff --git a/0860-lemonade-change/NOTES.md b/0860-lemonade-change/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0860-lemonade-change/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0860-lemonade-change/README.md b/0860-lemonade-change/README.md new file mode 100644 index 00000000..d3a40735 --- /dev/null +++ b/0860-lemonade-change/README.md @@ -0,0 +1,37 @@ +

860. Lemonade Change

Easy


At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

+ +

Note that you do not have any change in hand at first.

+ +

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: bills = [5,5,5,10,20]
+Output: true
+Explanation: 
+From the first 3 customers, we collect three $5 bills in order.
+From the fourth customer, we collect a $10 bill and give back a $5.
+From the fifth customer, we give a $10 bill and a $5 bill.
+Since all customers got correct change, we output true.
+
+ +

Example 2:

+ +
Input: bills = [5,5,10,10,20]
+Output: false
+Explanation: 
+From the first two customers in order, we collect two $5 bills.
+For the next two customers in order, we collect a $10 bill and give back a $5 bill.
+For the last customer, we can not give the change of $15 back because we only have two $10 bills.
+Since not every customer received the correct change, the answer is false.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= bills.length <= 105
  • +
  • bills[i] is either 5, 10, or 20.
  • +
+
\ No newline at end of file diff --git a/0867-transpose-matrix/0867-transpose-matrix.cpp b/0867-transpose-matrix/0867-transpose-matrix.cpp new file mode 100644 index 00000000..b24a5239 --- /dev/null +++ b/0867-transpose-matrix/0867-transpose-matrix.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector> transpose(vector>& matrix) { + + vector> ans; + + int n = matrix[0].size(), m = matrix.size(); + + for(int i = 0; i < n; ++i) + { + vector curr; + for(int j = 0; j < m; ++j) + { + curr.push_back(matrix[j][i]); + } + ans.push_back(curr); + } + return ans; + } +}; \ No newline at end of file diff --git a/0867-transpose-matrix/NOTES.md b/0867-transpose-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0867-transpose-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0867-transpose-matrix/README.md b/0867-transpose-matrix/README.md new file mode 100644 index 00000000..8eaf9eef --- /dev/null +++ b/0867-transpose-matrix/README.md @@ -0,0 +1,30 @@ +

867. Transpose Matrix

Easy


Given a 2D integer array matrix, return the transpose of matrix.

+ +

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

+ +

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [[1,4,7],[2,5,8],[3,6,9]]
+
+ +

Example 2:

+ +
Input: matrix = [[1,2,3],[4,5,6]]
+Output: [[1,4],[2,5],[3,6]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 1000
  • +
  • 1 <= m * n <= 105
  • +
  • -109 <= matrix[i][j] <= 109
  • +
+
\ No newline at end of file diff --git a/0872-leaf-similar-trees/README.md b/0872-leaf-similar-trees/README.md new file mode 100644 index 00000000..71da5fe7 --- /dev/null +++ b/0872-leaf-similar-trees/README.md @@ -0,0 +1,31 @@ +

872. Leaf-Similar Trees

Easy


Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.

+ +

+ +

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

+ +

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

+ +

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

+ +

 

+

Example 1:

+ +
Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
+Output: true
+
+ +

Example 2:

+ +
Input: root1 = [1,2,3], root2 = [1,3,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in each tree will be in the range [1, 200].
  • +
  • Both of the given trees will have values in the range [0, 200].
  • +
+
\ No newline at end of file diff --git a/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp b/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp new file mode 100644 index 00000000..71c8ab29 --- /dev/null +++ b/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + + ListNode* fast = head, *slow = head; + + while(fast and fast->next) + { + slow = slow->next; + fast = fast->next->next; + } + + return slow; + } +}; \ No newline at end of file diff --git a/0876-middle-of-the-linked-list/README.md b/0876-middle-of-the-linked-list/README.md new file mode 100644 index 00000000..bb08722c --- /dev/null +++ b/0876-middle-of-the-linked-list/README.md @@ -0,0 +1,27 @@ +

876. Middle of the Linked List

Easy


Given the head of a singly linked list, return the middle node of the linked list.

+ +

If there are two middle nodes, return the second middle node.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4,5]
+Output: [3,4,5]
+Explanation: The middle node of the list is node 3.
+
+ +

Example 2:

+ +
Input: head = [1,2,3,4,5,6]
+Output: [4,5,6]
+Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 100].
  • +
  • 1 <= Node.val <= 100
  • +
+
\ No newline at end of file diff --git a/0880-decoded-string-at-index/0880-decoded-string-at-index.cpp b/0880-decoded-string-at-index/0880-decoded-string-at-index.cpp new file mode 100644 index 00000000..09875315 --- /dev/null +++ b/0880-decoded-string-at-index/0880-decoded-string-at-index.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string decodeAtIndex(string s, int k) { + + int n = s.size(); + + long long totalLength = 0; + + for(int i = 0; i < n; ++i) + { + if(isalpha(s[i])) + ++totalLength; + else + totalLength = totalLength * (s[i] - '0'); + } + + for(int i = n-1; i >= 0; --i) + { + k = k % totalLength; + if(k == 0 and isalpha(s[i])) + return string(1, s[i]); + if(isalpha(s[i])) + totalLength -= 1; + else + totalLength /= (s[i] - '0'); + } + + return ""; + } +}; \ No newline at end of file diff --git a/0880-decoded-string-at-index/README.md b/0880-decoded-string-at-index/README.md new file mode 100644 index 00000000..2831eaa7 --- /dev/null +++ b/0880-decoded-string-at-index/README.md @@ -0,0 +1,46 @@ +

880. Decoded String at Index

Medium


You are given an encoded string s. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:

+ +
    +
  • If the character read is a letter, that letter is written onto the tape.
  • +
  • If the character read is a digit d, the entire current tape is repeatedly written d - 1 more times in total.
  • +
+ +

Given an integer k, return the kth letter (1-indexed) in the decoded string.

+ +

 

+

Example 1:

+ +
Input: s = "leet2code3", k = 10
+Output: "o"
+Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode".
+The 10th letter in the string is "o".
+
+ +

Example 2:

+ +
Input: s = "ha22", k = 5
+Output: "h"
+Explanation: The decoded string is "hahahaha".
+The 5th letter is "h".
+
+ +

Example 3:

+ +
Input: s = "a2345678999999999999999", k = 1
+Output: "a"
+Explanation: The decoded string is "a" repeated 8301530446056247680 times.
+The 1st letter is "a".
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 100
  • +
  • s consists of lowercase English letters and digits 2 through 9.
  • +
  • s starts with a letter.
  • +
  • 1 <= k <= 109
  • +
  • It is guaranteed that k is less than or equal to the length of the decoded string.
  • +
  • The decoded string is guaranteed to have less than 263 letters.
  • +
+
\ No newline at end of file diff --git a/0881-boats-to-save-people/0881-boats-to-save-people.cpp b/0881-boats-to-save-people/0881-boats-to-save-people.cpp new file mode 100644 index 00000000..9bdda773 --- /dev/null +++ b/0881-boats-to-save-people/0881-boats-to-save-people.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int numRescueBoats(vector& people, int limit) { + + int cnt = 0; + + sort(people.begin(),people.end()); + + int n = people.size(); + int start = 0, end = n-1; + + while(start <= end) + { + if(people[start] + people[end] <= limit) + { + ++start, --end; + } + else + --end; + ++cnt; + } + + return cnt; + } +}; \ No newline at end of file diff --git a/0881-boats-to-save-people/NOTES.md b/0881-boats-to-save-people/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0881-boats-to-save-people/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp b/0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp new file mode 100644 index 00000000..78e13cb1 --- /dev/null +++ b/0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + vector> spiralMatrixIII(int rows, int cols, int rStart, int cStart) { + + vector> res; + int step = 1, ctr = 0; + + auto isValid = [&]() + { + return (rStart >= 0 and rStart < rows and cStart >= 0 and cStart < cols); + }; + + while(ctr < rows * cols) + { + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + ++cStart; + } + + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + ++rStart; + } + + ++step; + + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + --cStart; + } + + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + --rStart; + } + + ++step; + } + + return res; + } +}; \ No newline at end of file diff --git a/0885-spiral-matrix-iii/README.md b/0885-spiral-matrix-iii/README.md new file mode 100644 index 00000000..721d4ed3 --- /dev/null +++ b/0885-spiral-matrix-iii/README.md @@ -0,0 +1,28 @@ +

885. Spiral Matrix III

Medium


You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

+ +

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.

+ +

Return an array of coordinates representing the positions of the grid in the order you visited them.

+ +

 

+

Example 1:

+ +
Input: rows = 1, cols = 4, rStart = 0, cStart = 0
+Output: [[0,0],[0,1],[0,2],[0,3]]
+
+ +

Example 2:

+ +
Input: rows = 5, cols = 6, rStart = 1, cStart = 4
+Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= rows, cols <= 100
  • +
  • 0 <= rStart < rows
  • +
  • 0 <= cStart < cols
  • +
+
\ No newline at end of file diff --git a/0912-sort-an-array/README.md b/0912-sort-an-array/README.md new file mode 100644 index 00000000..9b50c617 --- /dev/null +++ b/0912-sort-an-array/README.md @@ -0,0 +1,27 @@ +

912. Sort an Array

Medium


Given an array of integers nums, sort the array in ascending order and return it.

+ +

You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.

+ +

 

+

Example 1:

+ +
Input: nums = [5,2,3,1]
+Output: [1,2,3,5]
+Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
+
+ +

Example 2:

+ +
Input: nums = [5,1,1,2,0,0]
+Output: [0,0,1,1,2,5]
+Explanation: Note that the values of nums are not necessairly unique.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • -5 * 104 <= nums[i] <= 5 * 104
  • +
+
\ No newline at end of file diff --git a/0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp b/0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp new file mode 100644 index 00000000..d9a7b100 --- /dev/null +++ b/0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int numSubarraysWithSum(vector& nums, int goal) { + + int ans = 0, n = nums.size(); + + map mp; + mp.insert({0, 1}); + + int sum = 0; + + for(int j = 0; j < n; ++j) + { + sum += nums[j]; + if(mp.find(sum - goal) != mp.end()) + { + ans += mp[sum - goal]; + } + + ++mp[sum]; + } + + return ans; + } +}; diff --git a/0930-binary-subarrays-with-sum/NOTES.md b/0930-binary-subarrays-with-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0930-binary-subarrays-with-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0930-binary-subarrays-with-sum/README.md b/0930-binary-subarrays-with-sum/README.md new file mode 100644 index 00000000..63710621 --- /dev/null +++ b/0930-binary-subarrays-with-sum/README.md @@ -0,0 +1,30 @@ +

930. Binary Subarrays With Sum

Medium


Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.

+ +

A subarray is a contiguous part of the array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,0,1,0,1], goal = 2
+Output: 4
+Explanation: The 4 subarrays are bolded and underlined below:
+[1,0,1,0,1]
+[1,0,1,0,1]
+[1,0,1,0,1]
+[1,0,1,0,1]
+
+ +

Example 2:

+ +
Input: nums = [0,0,0,0,0], goal = 0
+Output: 15
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • nums[i] is either 0 or 1.
  • +
  • 0 <= goal <= nums.length
  • +
\ No newline at end of file diff --git a/0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp b/0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp new file mode 100644 index 00000000..f480dd49 --- /dev/null +++ b/0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int minFallingPathSum(vector>& A) { + int m = A.size(); + vector> t(m, vector(m)); + + for(int col = 0; col931. Minimum Falling Path Sum

Medium


Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix.

+ +

A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).

+ +

 

+

Example 1:

+ +
Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
+Output: 13
+Explanation: There are two falling paths with a minimum sum as shown.
+
+ +

Example 2:

+ +
Input: matrix = [[-19,57],[-40,-5]]
+Output: -59
+Explanation: The falling path with a minimum sum is shown.
+
+ +

 

+

Constraints:

+ +
    +
  • n == matrix.length == matrix[i].length
  • +
  • 1 <= n <= 100
  • +
  • -100 <= matrix[i][j] <= 100
  • +
+
\ No newline at end of file diff --git a/0938-range-sum-of-bst/0938-range-sum-of-bst.cpp b/0938-range-sum-of-bst/0938-range-sum-of-bst.cpp new file mode 100644 index 00000000..235bfc29 --- /dev/null +++ b/0938-range-sum-of-bst/0938-range-sum-of-bst.cpp @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int rangeSumBST(TreeNode* root, int low, int high) { + + if(!root) + return 0; + + if(root->val < low) + return rangeSumBST(root->right, low, high); + + if(root->val > high) + return rangeSumBST(root->left, low, high); + + return root->val + rangeSumBST(root->left, low, high) + rangeSumBST(root->right, low, high); + } +}; \ No newline at end of file diff --git a/0938-range-sum-of-bst/NOTES.md b/0938-range-sum-of-bst/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0938-range-sum-of-bst/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0938-range-sum-of-bst/README.md b/0938-range-sum-of-bst/README.md new file mode 100644 index 00000000..25f2c7e6 --- /dev/null +++ b/0938-range-sum-of-bst/README.md @@ -0,0 +1,27 @@ +

938. Range Sum of BST

Easy


Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

+ +

 

+

Example 1:

+ +
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
+Output: 32
+Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
+
+ +

Example 2:

+ +
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
+Output: 23
+Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 2 * 104].
  • +
  • 1 <= Node.val <= 105
  • +
  • 1 <= low <= high <= 105
  • +
  • All Node.val are unique.
  • +
+
\ No newline at end of file diff --git a/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp new file mode 100644 index 00000000..c46746a6 --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int minIncrementForUnique(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + if(nums[i-1] >= nums[i]) + { + ans += (nums[i-1] - nums[i] + 1); + nums[i] = nums[i-1] + 1; + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0945-minimum-increment-to-make-array-unique/NOTES.md b/0945-minimum-increment-to-make-array-unique/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0945-minimum-increment-to-make-array-unique/README.md b/0945-minimum-increment-to-make-array-unique/README.md new file mode 100644 index 00000000..b81b99e4 --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/README.md @@ -0,0 +1,30 @@ +

945. Minimum Increment to Make Array Unique

Medium


You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.

+ +

Return the minimum number of moves to make every value in nums unique.

+ +

The test cases are generated so that the answer fits in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,2]
+Output: 1
+Explanation: After 1 move, the array could be [1, 2, 3].
+
+ +

Example 2:

+ +
Input: nums = [3,2,1,2,1,7]
+Output: 6
+Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
+It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 105
  • +
+
\ No newline at end of file diff --git a/0948-bag-of-tokens/0948-bag-of-tokens.cpp b/0948-bag-of-tokens/0948-bag-of-tokens.cpp new file mode 100644 index 00000000..b1fbb888 --- /dev/null +++ b/0948-bag-of-tokens/0948-bag-of-tokens.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int bagOfTokensScore(vector& tokens, int power) { + + int start = 0, end = tokens.size()-1; + int score = 0, maxScore = 0; + + sort(tokens.begin(), tokens.end()); + + while(start <= end) + { + if(power >= tokens[start]) + { + ++score; + maxScore = max(maxScore, score); + power -= tokens[start++]; + } + else if(score > 0) + { + power += tokens[end--]; + --score; + } + else + break; + } + + return maxScore; + } +}; \ No newline at end of file diff --git a/0948-bag-of-tokens/NOTES.md b/0948-bag-of-tokens/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0948-bag-of-tokens/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0948-bag-of-tokens/README.md b/0948-bag-of-tokens/README.md new file mode 100644 index 00000000..968d02c5 --- /dev/null +++ b/0948-bag-of-tokens/README.md @@ -0,0 +1,106 @@ +

948. Bag of Tokens

Medium


You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] donates the value of tokeni.

+ +

Your goal is to maximize the total score by strategically playing these tokens. In one move, you can play an unplayed token in one of the two ways (but not both for the same token):

+ +
    +
  • Face-up: If your current power is at least tokens[i], you may play tokeni, losing tokens[i] power and gaining 1 score.
  • +
  • Face-down: If your current score is at least 1, you may play tokeni, gaining tokens[i] power and losing 1 score.
  • +
+ +

Return the maximum possible score you can achieve after playing any number of tokens.

+ +

 

+

Example 1:

+ +
+

Input: tokens = [100], power = 50

+ +

Output: 0

+ +

Explanation: Since your score is 0 initially, you cannot play the token face-down. You also cannot play it face-up since your power (50) is less than tokens[0] (100).

+
+ +

Example 2:

+ +
+

Input: tokens = [200,100], power = 150

+ +

Output: 1

+ +

Explanation: Play token1 (100) face-up, reducing your power to 50 and increasing your score to 1.

+ +

There is no need to play token0, since you cannot play it face-up to add to your score. The maximum score achievable is 1.

+
+ +

Example 3:

+ +
+

Input: tokens = [100,200,300,400], power = 200

+ +

Output: 2

+ +

Explanation: Play the tokens in this order to get a score of 2:

+ +
    +
  1. Play token0 (100) face-up, reducing power to 100 and increasing score to 1.
  2. +
  3. Play token3 (400) face-down, increasing power to 500 and reducing score to 0.
  4. +
  5. Play token1 (200) face-up, reducing power to 300 and increasing score to 1.
  6. +
  7. Play token2 (300) face-up, reducing power to 0 and increasing score to 2.
  8. +
+ +

The maximum score achievable is 2.

+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= tokens.length <= 1000
  • +
  • 0 <= tokens[i], power < 104
  • +
+
\ No newline at end of file diff --git a/0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp b/0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp new file mode 100644 index 00000000..5c3092eb --- /dev/null +++ b/0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector deckRevealedIncreasing(vector& deck) { + sort(deck.rbegin(), deck.rend()); + deque d; + d.push_back(deck[0]); + for (int i = 1; i < deck.size(); i++) { + d.push_front(d.back()); + d.pop_back(); + d.push_front(deck[i]); + } + vector res(d.begin(), d.end()); + return res; + } +}; \ No newline at end of file diff --git a/0950-reveal-cards-in-increasing-order/NOTES.md b/0950-reveal-cards-in-increasing-order/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0950-reveal-cards-in-increasing-order/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0950-reveal-cards-in-increasing-order/README.md b/0950-reveal-cards-in-increasing-order/README.md new file mode 100644 index 00000000..8fd4c765 --- /dev/null +++ b/0950-reveal-cards-in-increasing-order/README.md @@ -0,0 +1,49 @@ +

950. Reveal Cards In Increasing Order

Medium


You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].

+ +

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

+ +

You will do the following steps repeatedly until all cards are revealed:

+ +
    +
  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. +
  3. If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
  4. +
  5. If there are still unrevealed cards, go back to step 1. Otherwise, stop.
  6. +
+ +

Return an ordering of the deck that would reveal the cards in increasing order.

+ +

Note that the first entry in the answer is considered to be the top of the deck.

+ +

 

+

Example 1:

+ +
Input: deck = [17,13,11,2,3,5,7]
+Output: [2,13,3,11,5,17,7]
+Explanation: 
+We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
+After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
+We reveal 2, and move 13 to the bottom.  The deck is now [3,11,5,17,7,13].
+We reveal 3, and move 11 to the bottom.  The deck is now [5,17,7,13,11].
+We reveal 5, and move 17 to the bottom.  The deck is now [7,13,11,17].
+We reveal 7, and move 13 to the bottom.  The deck is now [11,17,13].
+We reveal 11, and move 17 to the bottom.  The deck is now [13,17].
+We reveal 13, and move 17 to the bottom.  The deck is now [17].
+We reveal 17.
+Since all the cards revealed are in increasing order, the answer is correct.
+
+ +

Example 2:

+ +
Input: deck = [1,1000]
+Output: [1,1000]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= deck.length <= 1000
  • +
  • 1 <= deck[i] <= 106
  • +
  • All the values of deck are unique.
  • +
+
\ No newline at end of file diff --git a/0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp b/0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp new file mode 100644 index 00000000..614e2c88 --- /dev/null +++ b/0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int regionsBySlashes(vector& grid) { + + int n = grid.size(); + int m = grid[0].size(); + int cnt = 0; + + vector> mat(n*3, vector(m*3, 0)); + vector> visited = mat; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == '/') + { + mat[i*3][j*3+2] = 1; + mat[i*3+1][j*3+1] = 1; + mat[i*3+2][j*3] = 1; + } + else if(grid[i][j] == '\\') + { + mat[i*3][j*3] = 1; + mat[i*3+1][j*3+1] = 1; + mat[i*3+2][j*3+2] = 1; + } + } + } + + function dfs = [&](int i, int j) + { + if(i < 0 or j < 0 or i >= n*3 or j >= m*3 or visited[i][j] or mat[i][j] == 1) + return; + visited[i][j] = 1; + dfs(i-1, j); + dfs(i, j+1); + dfs(i+1, j); + dfs(i, j-1); + }; + + for(int i = 0; i < n*3; ++i) + { + for(int j = 0; j < m*3; ++j) + { + if(!visited[i][j] and mat[i][j] == 0) + { + ++cnt; + dfs(i, j); + } + } + } + + return cnt; + } +}; \ No newline at end of file diff --git a/0959-regions-cut-by-slashes/NOTES.md b/0959-regions-cut-by-slashes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0959-regions-cut-by-slashes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0959-regions-cut-by-slashes/README.md b/0959-regions-cut-by-slashes/README.md new file mode 100644 index 00000000..fc2c5d3b --- /dev/null +++ b/0959-regions-cut-by-slashes/README.md @@ -0,0 +1,35 @@ +

959. Regions Cut By Slashes

Medium


An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions.

+ +

Given the grid grid represented as a string array, return the number of regions.

+ +

Note that backslash characters are escaped, so a '\' is represented as '\\'.

+ +

 

+

Example 1:

+ +
Input: grid = [" /","/ "]
+Output: 2
+
+ +

Example 2:

+ +
Input: grid = [" /","  "]
+Output: 1
+
+ +

Example 3:

+ +
Input: grid = ["/\\","\\/"]
+Output: 5
+Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 1 <= n <= 30
  • +
  • grid[i][j] is either '/', '\', or ' '.
  • +
+
\ No newline at end of file diff --git a/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp new file mode 100644 index 00000000..b31ae1e2 --- /dev/null +++ b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector sortedSquares(vector& nums) { + + int n = nums.size(); + vector squares(n); + + int i = 0, j = n-1, k = n-1; + + while(k >= 0) + { + if(abs(nums[i]) > abs(nums[j])) + { + squares[k] = nums[i] * nums[i]; + ++i; + } + else + { + squares[k] = nums[j] * nums[j]; + --j; + } + --k; + } + + return squares; + } +}; \ No newline at end of file diff --git a/0977-squares-of-a-sorted-array/NOTES.md b/0977-squares-of-a-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0977-squares-of-a-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0977-squares-of-a-sorted-array/README.md b/0977-squares-of-a-sorted-array/README.md new file mode 100644 index 00000000..9e0b7c42 --- /dev/null +++ b/0977-squares-of-a-sorted-array/README.md @@ -0,0 +1,28 @@ +

977. Squares of a Sorted Array

Easy


Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

+ +

 

+

Example 1:

+ +
Input: nums = [-4,-1,0,3,10]
+Output: [0,1,9,16,100]
+Explanation: After squaring, the array becomes [16,1,0,9,100].
+After sorting, it becomes [0,1,9,16,100].
+
+ +

Example 2:

+ +
Input: nums = [-7,-3,2,3,11]
+Output: [4,9,9,49,121]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums is sorted in non-decreasing order.
  • +
+ +

 

+Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
\ No newline at end of file diff --git a/0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp b/0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp new file mode 100644 index 00000000..a8764161 --- /dev/null +++ b/0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp @@ -0,0 +1,37 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int helper(TreeNode* root, int& moves) + { + if(!root) + return 0; + + int left = helper(root->left, moves); + int right = helper(root->right, moves); + + moves += (abs(left) + abs(right)); + + return (left + right + root->val - 1); + } + + int distributeCoins(TreeNode* root) { + + int moves = 0; + + helper(root, moves); + + return moves; + + } +}; \ No newline at end of file diff --git a/0979-distribute-coins-in-binary-tree/README.md b/0979-distribute-coins-in-binary-tree/README.md new file mode 100644 index 00000000..e7b0415a --- /dev/null +++ b/0979-distribute-coins-in-binary-tree/README.md @@ -0,0 +1,31 @@ +

979. Distribute Coins in Binary Tree

Medium


You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.

+ +

In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.

+ +

Return the minimum number of moves required to make every node have exactly one coin.

+ +

 

+

Example 1:

+ +
Input: root = [3,0,0]
+Output: 2
+Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
+
+ +

Example 2:

+ +
Input: root = [0,3,0]
+Output: 3
+Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is n.
  • +
  • 1 <= n <= 100
  • +
  • 0 <= Node.val <= n
  • +
  • The sum of all Node.val is n.
  • +
+
\ No newline at end of file diff --git a/0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp b/0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp new file mode 100644 index 00000000..2fa6c38d --- /dev/null +++ b/0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp @@ -0,0 +1,45 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + map>> mp; + + void helper(TreeNode* root, int id, int level) + { + if(root) + { + mp[id][level].insert(root->val); + helper(root->left, id-1, level + 1); + helper(root->right, id+1, level + 1); + } + } + + vector> verticalTraversal(TreeNode* root) { + + int id = 0; + helper(root, id, 0); + + vector> ans; + + for(auto&[f, s] : mp) + { + vector col; + for(auto& ms : s) + col.insert(col.end(),ms.second.begin(),ms.second.end()); + ans.push_back(col); + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0987-vertical-order-traversal-of-a-binary-tree/README.md b/0987-vertical-order-traversal-of-a-binary-tree/README.md new file mode 100644 index 00000000..6ca03394 --- /dev/null +++ b/0987-vertical-order-traversal-of-a-binary-tree/README.md @@ -0,0 +1,50 @@ +

987. Vertical Order Traversal of a Binary Tree

Hard


Given the root of a binary tree, calculate the vertical order traversal of the binary tree.

+ +

For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).

+ +

The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.

+ +

Return the vertical order traversal of the binary tree.

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: [[9],[3,15],[20],[7]]
+Explanation:
+Column -1: Only node 9 is in this column.
+Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
+Column 1: Only node 20 is in this column.
+Column 2: Only node 7 is in this column.
+ +

Example 2:

+ +
Input: root = [1,2,3,4,5,6,7]
+Output: [[4],[2],[1,5,6],[3],[7]]
+Explanation:
+Column -2: Only node 4 is in this column.
+Column -1: Only node 2 is in this column.
+Column 0: Nodes 1, 5, and 6 are in this column.
+          1 is at the top, so it comes first.
+          5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
+Column 1: Only node 3 is in this column.
+Column 2: Only node 7 is in this column.
+
+ +

Example 3:

+ +
Input: root = [1,2,3,4,6,5,7]
+Output: [[4],[2],[1,5,6],[3],[7]]
+Explanation:
+This case is the exact same as example 2, but with nodes 5 and 6 swapped.
+Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
+
\ No newline at end of file diff --git a/0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp b/0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp new file mode 100644 index 00000000..c71963a8 --- /dev/null +++ b/0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp @@ -0,0 +1,35 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + string ans = "~"; + + void helper(TreeNode* root, string curr) + { + if(!root) + return; + + if(!root->left and !root->right) + ans = min(ans, char(root->val + 'a') + curr); + + helper(root->left, char(root->val + 'a') + curr); + helper(root->right, char(root->val + 'a') + curr); + } + + string smallestFromLeaf(TreeNode* root) { + + helper(root, ""); + return ans; + + } +}; \ No newline at end of file diff --git a/0988-smallest-string-starting-from-leaf/README.md b/0988-smallest-string-starting-from-leaf/README.md new file mode 100644 index 00000000..2ba3cfed --- /dev/null +++ b/0988-smallest-string-starting-from-leaf/README.md @@ -0,0 +1,39 @@ +

988. Smallest String Starting From Leaf

Medium


You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.

+ +

Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.

+ +

As a reminder, any shorter prefix of a string is lexicographically smaller.

+ +
    +
  • For example, "ab" is lexicographically smaller than "aba".
  • +
+ +

A leaf of a node is a node that has no children.

+ +

 

+

Example 1:

+ +
Input: root = [0,1,2,3,4,3,4]
+Output: "dba"
+
+ +

Example 2:

+ +
Input: root = [25,1,3,1,3,0,2]
+Output: "adz"
+
+ +

Example 3:

+ +
Input: root = [2,2,1,null,1,0,null,0]
+Output: "abc"
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 8500].
  • +
  • 0 <= Node.val <= 25
  • +
+
\ No newline at end of file diff --git a/0992-subarrays-with-k-different-integers/README.md b/0992-subarrays-with-k-different-integers/README.md new file mode 100644 index 00000000..cde878b8 --- /dev/null +++ b/0992-subarrays-with-k-different-integers/README.md @@ -0,0 +1,33 @@ +

992. Subarrays with K Different Integers

Hard


Given an integer array nums and an integer k, return the number of good subarrays of nums.

+ +

A good array is an array where the number of different integers in that array is exactly k.

+ +
    +
  • For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.
  • +
+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,1,2,3], k = 2
+Output: 7
+Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
+
+ +

Example 2:

+ +
Input: nums = [1,2,1,3,4], k = 3
+Output: 3
+Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 2 * 104
  • +
  • 1 <= nums[i], k <= nums.length
  • +
+
\ No newline at end of file diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp new file mode 100644 index 00000000..c2bc0ee2 --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + int minKBitFlips(vector& nums, int k) { + + int n = nums.size(), ans = 0; + + vector> pref(n, {0, 0}); + + bool flag = true; + + for(int i = 0; i < n; ++i) + { + pref[i].second = (i-1 >= 0 ? pref[i-1].second : 0); + + if(i - k >= 0) + { + if(pref[i-k].first) + --pref[i].second; + } + + int moves = pref[i].second; + + int op = (nums[i] % 2 == 0 ? (moves % 2 == 0 ? 1 : 0) : (moves % 2 == 0 ? 0 : 1)); + + if(op){ + if(i <= n-k) + { + pref[i].first = 1; + ++ans; + } + else + flag = false; + } + + pref[i].second += op; + } + + return (flag ? ans : -1); + } +}; \ No newline at end of file diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md b/0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/README.md b/0995-minimum-number-of-k-consecutive-bit-flips/README.md new file mode 100644 index 00000000..4a559ed8 --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/README.md @@ -0,0 +1,41 @@ +

995. Minimum Number of K Consecutive Bit Flips

Hard


You are given a binary array nums and an integer k.

+ +

A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.

+ +

Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1.

+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [0,1,0], k = 1
+Output: 2
+Explanation: Flip nums[0], then flip nums[2].
+
+ +

Example 2:

+ +
Input: nums = [1,1,0], k = 2
+Output: -1
+Explanation: No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
+
+ +

Example 3:

+ +
Input: nums = [0,0,0,1,0,1,1,0], k = 3
+Output: 3
+Explanation: 
+Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
+Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
+Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= k <= nums.length
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp new file mode 100644 index 00000000..d401154d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp @@ -0,0 +1,55 @@ +#define ll long long int + +class Solution { +public: + int maxFrequency(vector& nums, int k) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector pref(n+1, 0); + + for(int i = 1; i <= n; ++i) + { + pref[i] = (pref[i-1] + nums[i-1]); + } + + function calculate = [&](int targetIdx) + { + int ans = 0, val = nums[targetIdx]; + + int low = 0, high = targetIdx; + + while(low <= high) + { + int mid = (low + high) >> 1; + + int cnt = targetIdx - mid + 1; + + ll windowSum = val * 1LL * cnt; + + ll currSum = pref[targetIdx+1] - pref[mid]; + + if((windowSum - currSum) <= k) + { + ans = max(ans, cnt); + high = mid-1; + } + else + low = mid+1; + } + + return ans; + }; + + int res = 0; + + for(int i = 0; i < n; ++i) + { + res = max(res, calculate(i)); + } + + return res; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp new file mode 100644 index 00000000..2acce397 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int kthGrammar(int n, int k) { + + if(n == 1 and k == 1) + return 0; + + int mid = pow(2, n-1) / 2; + + if(k <= mid) + return kthGrammar(n-1, k); + else + return !kthGrammar(n-1, k-mid); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp new file mode 100644 index 00000000..81cf155a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp @@ -0,0 +1,29 @@ +class MyHashMap { +public: + + int arr[1000001]; + + MyHashMap() { + fill(arr, arr+1000001, -1); + } + + void put(int key, int value) { + arr[key] = value; + } + + int get(int key) { + return arr[key]; + } + + void remove(int key) { + arr[key] = -1; + } +}; + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap* obj = new MyHashMap(); + * obj->put(key,value); + * int param_2 = obj->get(key); + * obj->remove(key); + */ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md new file mode 100644 index 00000000..afd9c7ec --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md @@ -0,0 +1,40 @@ +

Amazon
10
Microsoft
6
LinkedIn
5
Oracle
4
Salesforce
3
Apple
3
Goldman Sachs
3
Walmart Global Tech
3
706. Design HashMap

Easy


Design a HashMap without using any built-in hash table libraries.

+ +

Implement the MyHashMap class:

+ +
    +
  • MyHashMap() initializes the object with an empty map.
  • +
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • +
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • +
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
+[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
+Output
+[null, null, null, 1, -1, null, 1, null, -1]
+
+Explanation
+MyHashMap myHashMap = new MyHashMap();
+myHashMap.put(1, 1); // The map is now [[1,1]]
+myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
+myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
+myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
+myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
+myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
+myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
+myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= key, value <= 106
  • +
  • At most 104 calls will be made to put, get, and remove.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp new file mode 100644 index 00000000..776c6783 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + string findDifferentBinaryString(vector& nums) { + + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + if(nums[0][i] = nums[i][i] == '0' ? '1' : '0'); + } + + return nums[0]; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp new file mode 100644 index 00000000..d2d96ba0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int numFactoredBinaryTrees(vector& arr) { + + int n = arr.size(); + + unordered_map mp; + + for(auto& itr : arr) + ++mp[itr]; + + sort(arr.begin(), arr.end()); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + for(int j = 0; j < i; ++j) + { + if(arr[i] % arr[j] == 0) + { + long long count = (mp[arr[j]] * 1LL * mp[arr[i]/arr[j]]) % mod; + mp[arr[i]] = (mp[arr[i]] % mod + count)% mod; + } + } + } + + for(auto& itr : mp) + { + ans = (ans + itr.second) % mod; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md new file mode 100644 index 00000000..7292e9fd --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md @@ -0,0 +1,28 @@ +

Amazon
2
823. Binary Trees With Factors

Medium


Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.

+ +

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

+ +

Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: arr = [2,4]
+Output: 3
+Explanation: We can make these trees: [2], [4], [4, 2, 2]
+ +

Example 2:

+ +
Input: arr = [2,4,5,10]
+Output: 7
+Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 2 <= arr[i] <= 109
  • +
  • All the values of arr are unique.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md new file mode 100644 index 00000000..158d4a8f --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md @@ -0,0 +1,35 @@ +

Amazon
32
Google
8
Microsoft
6
Salesforce
6
Uber
5
Facebook
5
Adobe
4
Citadel
4
ByteDance
4
Apple
3
Twilio
3
tiktok
3
Bloomberg
2
VMware
2
Booking.com
2
DoorDash
2
DE Shaw
2
Quora
2
239. Sliding Window Maximum

Hard


You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

+ +

Return the max sliding window.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
+Output: [3,3,5,5,6,7]
+Explanation: 
+Window position                Max
+---------------               -----
+[1  3  -1] -3  5  3  6  7       3
+ 1 [3  -1  -3] 5  3  6  7       3
+ 1  3 [-1  -3  5] 3  6  7       5
+ 1  3  -1 [-3  5  3] 6  7       5
+ 1  3  -1  -3 [5  3  6] 7       6
+ 1  3  -1  -3  5 [3  6  7]      7
+
+ +

Example 2:

+ +
Input: nums = [1], k = 1
+Output: [1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
  • 1 <= k <= nums.length
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp new file mode 100644 index 00000000..b9aeff39 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + string longestPalindrome(string s) { + + int n = s.size(); + + vector> dp(n+1, vector(n+1, 0)); + + int maxLength = 0; + + string ans; + + for(int diff = 0; diff < n; ++ diff) + { + for(int i = 0, j = diff; i < n and j < n; ++i, ++j) + { + if(i == j) + { + dp[i][j] = 1; + } + else if(diff == 1 and s[i] == s[j]) + { + dp[i][j] = 2; + } + else if(diff > 1 and s[i] == s[j] and dp[i+1][j-1]) + { + dp[i][j] = dp[i+1][j-1] + 2; + } + + if(dp[i][j] > maxLength) + { + maxLength = dp[i][j]; + ans = s.substr(i, j-i+1); + } + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md new file mode 100644 index 00000000..2a2b8010 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md @@ -0,0 +1,24 @@ +

Amazon
35
Microsoft
18
Google
12
Adobe
12
Facebook
7
Apple
7
Oracle
7
Bloomberg
6
Goldman Sachs
5
5. Longest Palindromic Substring

Medium


Given a string s, return the longest palindromic substring in s.

+ +

 

+

Example 1:

+ +
Input: s = "babad"
+Output: "bab"
+Explanation: "aba" is also a valid answer.
+
+ +

Example 2:

+ +
Input: s = "cbbd"
+Output: "bb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consist of only digits and English letters.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp new file mode 100644 index 00000000..ead08dec --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + bool find132pattern(vector& nums) { + + int n = nums.size(); + + vector prefMin(n); + + prefMin[0] = nums[0]; + + for(int i = 1; i < n; ++i) + prefMin[i] = min(nums[i], prefMin[i-1]); + + stack st; + + for(int i = n-1; i >= 0; --i) + { + if(nums[i] > prefMin[i]) + { + while(!st.empty() and st.top() <= prefMin[i]) + st.pop(); + if(!st.empty() and nums[i] > st.top()) + return true; + st.push(nums[i]); + } + } + + return false; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md new file mode 100644 index 00000000..c0815542 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md @@ -0,0 +1,35 @@ +

Amazon
4
456. 132 Pattern

Medium


Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

+ +

Return true if there is a 132 pattern in nums, otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: false
+Explanation: There is no 132 pattern in the sequence.
+
+ +

Example 2:

+ +
Input: nums = [3,1,4,2]
+Output: true
+Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
+
+ +

Example 3:

+ +
Input: nums = [-1,3,2,0]
+Output: true
+Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 2 * 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp new file mode 100644 index 00000000..cb2c75f3 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp @@ -0,0 +1,57 @@ +class Solution { +public: + vector majorityElement(vector& nums) { + + int n = nums.size(); + + int count1 = 0, count2 = 0; + + int num1 = INT_MAX, num2 = INT_MAX; + + for(auto& itr : nums) + { + if(itr == num1) + { + ++count1; + } + else if(itr == num2) + { + ++count2; + } + else if(count1 == 0) + { + count1 = 1; + num1 = itr; + } + else if(count2 == 0) + { + count2 = 1; + num2 = itr; + } + else + { + --count1, --count2; + } + } + + count1 = count2 = 0; + + for(auto& itr : nums) + { + if(itr == num1) + ++count1; + else if(itr == num2) + ++count2; + } + + vector ans; + + if(count1 > n/3) + ans.push_back(num1); + if(count2 > n/3) + ans.push_back(num2); + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp new file mode 100644 index 00000000..383cab45 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + + int n = cost.size(); + + vector dp(n+1, 0); + + for(int i = 2; i <= n; ++i) + { + int oneStep = cost[i-1] + dp[i-1]; + int twoStep = cost[i-2] + dp[i-2]; + dp[i] = min(oneStep, twoStep); + } + + return dp[n]; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md new file mode 100644 index 00000000..5ca7f39b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md @@ -0,0 +1,38 @@ +

Amazon
5
Adobe
4
Bloomberg
2
746. Min Cost Climbing Stairs

Easy


You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

+ +

You can either start from the step with index 0, or the step with index 1.

+ +

Return the minimum cost to reach the top of the floor.

+ +

 

+

Example 1:

+ +
Input: cost = [10,15,20]
+Output: 15
+Explanation: You will start at index 1.
+- Pay 15 and climb two steps to reach the top.
+The total cost is 15.
+
+ +

Example 2:

+ +
Input: cost = [1,100,1,1,1,100,1,1,100,1]
+Output: 6
+Explanation: You will start at index 0.
+- Pay 1 and climb two steps to reach index 2.
+- Pay 1 and climb two steps to reach index 4.
+- Pay 1 and climb two steps to reach index 6.
+- Pay 1 and climb one step to reach index 7.
+- Pay 1 and climb two steps to reach index 9.
+- Pay 1 and climb one step to reach the top.
+The total cost is 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= cost.length <= 1000
  • +
  • 0 <= cost[i] <= 999
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp new file mode 100644 index 00000000..08c3a3b1 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + bool arrayStringsAreEqual(vector& word1, vector& word2) { + + int i = 0, j = 0; + int n = 0, m = 0; + + while(i < word1.size() and j < word2.size()) + { + if(word1[i][n] != word2[j][m]) + return false; + + ++n, ++m; + + if(n >= word1[i].size()) n = 0, ++i; + if(m >= word2[j].size()) m = 0, ++j; + } + + return (i == word1.size() and j == word2.size()); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md new file mode 100644 index 00000000..3e4a16f2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md @@ -0,0 +1,36 @@ +

Apple
2
Facebook
1
1662. Check If Two String Arrays are Equivalent

Easy


Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

+ +

A string is represented by an array if the array elements concatenated in order forms the string.

+ +

 

+

Example 1:

+ +
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
+Output: true
+Explanation:
+word1 represents string "ab" + "c" -> "abc"
+word2 represents string "a" + "bc" -> "abc"
+The strings are the same, so return true.
+ +

Example 2:

+ +
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
+Output: false
+
+ +

Example 3:

+ +
Input: word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
+Output: true
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word1.length, word2.length <= 103
  • +
  • 1 <= word1[i].length, word2[i].length <= 103
  • +
  • 1 <= sum(word1[i].length), sum(word2[i].length) <= 103
  • +
  • word1[i] and word2[i] consist of lowercase letters.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp new file mode 100644 index 00000000..a4e92859 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int minTimeToVisitAllPoints(vector>& points) { + + int n = points.size(); + + int t = 0; + + for(int i = 1; i < n; ++i) + { + t += max(abs(points[i][1] - points[i-1][1]), abs(points[i][0] - points[i-1][0])); + } + + return t; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md new file mode 100644 index 00000000..2fd5cedb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md @@ -0,0 +1,43 @@ +

Apple
3
1266. Minimum Time Visiting All Points

Easy


On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points.

+ +

You can move according to these rules:

+ +
    +
  • In 1 second, you can either: + +
      +
    • move vertically by one unit,
    • +
    • move horizontally by one unit, or
    • +
    • move diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).
    • +
    +
  • +
  • You have to visit the points in the same order as they appear in the array.
  • +
  • You are allowed to pass through points that appear later in the order, but these do not count as visits.
  • +
+ +

 

+

Example 1:

+ +
Input: points = [[1,1],[3,4],[-1,0]]
+Output: 7
+Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]   
+Time from [1,1] to [3,4] = 3 seconds 
+Time from [3,4] to [-1,0] = 4 seconds
+Total time = 7 seconds
+ +

Example 2:

+ +
Input: points = [[3,2],[-2,2]]
+Output: 5
+
+ +

 

+

Constraints:

+ +
    +
  • points.length == n
  • +
  • 1 <= n <= 100
  • +
  • points[i].length == 2
  • +
  • -1000 <= points[i][0], points[i][1] <= 1000
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp new file mode 100644 index 00000000..9f335b1a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + string removeDuplicateLetters(string s) { + + int n = s.size(); + + vector freq(26, 0); + + vector visited(26, 0); + + string ans; + + for(int i = 0; i < n; ++i) + { + ++freq[s[i] - 'a']; + } + + for(int i = 0; i < n; ++i) + { + while(!ans.empty() and !visited[s[i] - 'a'] and s[i] < ans.back() and freq[ans.back()-'a']) + { + visited[ans.back() - 'a'] = false; + ans.pop_back(); + } + + if(!visited[s[i] - 'a']) + { + ans += s[i]; + visited[s[i] - 'a'] = 1; + } + + --freq[s[i] - 'a']; + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md new file mode 100644 index 00000000..86a62d23 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md @@ -0,0 +1,26 @@ +

Apple
4
Facebook
2
Amazon
2
Salesforce
2
316. Remove Duplicate Letters

Medium


Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

+ +

 

+

Example 1:

+ +
Input: s = "bcabc"
+Output: "abc"
+
+ +

Example 2:

+ +
Input: s = "cbacdcbc"
+Output: "acdb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of lowercase English letters.
  • +
+ +

 

+

Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/

+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp new file mode 100644 index 00000000..9fdae1a4 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + + const int mod = 1e9+7; + + vector dx = {-2, -2, -1 , -1, +2, +2, +1, +1}; + vector dy = {-1, +1, -2, +2, -1, +1, -2, +2}; + + int helper(int i, int j, int n, vector>>& dp) + { + if(n == 1) + return 1; + + if(dp[i][j][n] != -1) + return dp[i][j][n]; + + int count = 0; + + for(int k = 0; k < 8; ++k) + { + int x = dx[k] + i; + int y = dy[k] + j; + + if(x >= 0 and x < 4 and y >= 0 and y < 3) + { + if(x == 3 and (y == 0 or y == 2)) + continue; + count = (count + helper(x, y, n-1, dp)) % mod; + } + } + + return dp[i][j][n] = count; + } + + int knightDialer(int n) { + + int count = 0; + + vector>> dp(4, vector>(3, vector(n+1,-1))); + + for(int i = 0; i < 4; ++i) + { + for(int j = 0; j < 3; ++j) + { + if(i == 3 and (j == 0 or j == 2)) + continue; + count = (count + helper(i,j,n,dp)) % mod; + } + } + + return count; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp new file mode 100644 index 00000000..dd0d10a6 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int hammingWeight(uint32_t n) { + + int cnt = 0; + + while(n > 0) + { + if(n & 1) + ++cnt; + n >>= 1; + } + + return cnt; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp new file mode 100644 index 00000000..b0bb50ab --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + vector restoreArray(vector>& adjacentPairs) { + + unordered_map> adj; + unordered_map mp; + + int sv = -1; + + for(auto& itr : adjacentPairs) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + + ++mp[itr[0]]; + ++mp[itr[1]]; + } + + for(auto& [f, s] : mp) + { + if(s == 1) + sv = f; + } + + vector ans; + + unordered_set visited; + + function dfs = [&](int sv) + { + ans.push_back(sv); + visited.insert(sv); + + for(auto& itr : adj[sv]) + { + if(!visited.count(itr)) + dfs(itr); + } + }; + + cout<
Capital One
2
1743. Restore the Array From Adjacent Pairs

Medium


There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.

+ +

You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.

+ +

It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.

+ +

Return the original array nums. If there are multiple solutions, return any of them.

+ +

 

+

Example 1:

+ +
Input: adjacentPairs = [[2,1],[3,4],[3,2]]
+Output: [1,2,3,4]
+Explanation: This array has all its adjacent pairs in adjacentPairs.
+Notice that adjacentPairs[i] may not be in left-to-right order.
+
+ +

Example 2:

+ +
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
+Output: [-2,4,1,-3]
+Explanation: There can be negative numbers.
+Another solution is [-3,1,4,-2], which would also be accepted.
+
+ +

Example 3:

+ +
Input: adjacentPairs = [[100000,-100000]]
+Output: [100000,-100000]
+
+ +

 

+

Constraints:

+ +
    +
  • nums.length == n
  • +
  • adjacentPairs.length == n - 1
  • +
  • adjacentPairs[i].length == 2
  • +
  • 2 <= n <= 105
  • +
  • -105 <= nums[i], ui, vi <= 105
  • +
  • There exists some nums that has adjacentPairs as its pairs.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp new file mode 100644 index 00000000..3385d741 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int getWinner(vector& arr, int k) { + + int n = arr.size(); + + int maxi = *max_element(arr.begin(), arr.end()); + + if(k >= arr.size()) return maxi; + + int cnt = 0; + + int winner = arr[0]; + + for(int i = 1; i < n; ++i) + { + if(winner > arr[i]) + { + ++cnt; + } + else + { + winner = arr[i]; + cnt = 1; + } + + if(cnt == k or winner == maxi) + return winner; + } + + return winner; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md new file mode 100644 index 00000000..669d5500 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md @@ -0,0 +1,39 @@ +

Directi
1
1535. Find the Winner of an Array Game

Medium


Given an integer array arr of distinct integers and an integer k.

+ +

A game will be played between the first two elements of the array (i.e. arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of the array. The game ends when an integer wins k consecutive rounds.

+ +

Return the integer which will win the game.

+ +

It is guaranteed that there will be a winner of the game.

+ +

 

+

Example 1:

+ +
Input: arr = [2,1,3,5,4,6,7], k = 2
+Output: 5
+Explanation: Let's see the rounds of the game:
+Round |       arr       | winner | win_count
+  1   | [2,1,3,5,4,6,7] | 2      | 1
+  2   | [2,3,5,4,6,7,1] | 3      | 1
+  3   | [3,5,4,6,7,1,2] | 5      | 1
+  4   | [5,4,6,7,1,2,3] | 5      | 2
+So we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.
+
+ +

Example 2:

+ +
Input: arr = [3,2,1], k = 10
+Output: 3
+Explanation: 3 will win the first 10 rounds consecutively.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= arr.length <= 105
  • +
  • 1 <= arr[i] <= 106
  • +
  • arr contains distinct integers.
  • +
  • 1 <= k <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp new file mode 100644 index 00000000..323ea613 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp @@ -0,0 +1,41 @@ +class SeatManager { +public: + + set st; + int counter = 1; + int N; + + SeatManager(int n) { + N = n; + } + + int reserve() { + + if(!st.empty()) + { + int least = *st.begin(); + st.erase(*st.begin()); + return least; + } + + if(counter <= N) + { + int smallestUnreserved = counter; + ++counter; + return smallestUnreserved; + } + + return -1; + } + + void unreserve(int seatNumber) { + st.insert(seatNumber); + } +}; + +/** + * Your SeatManager object will be instantiated and called as such: + * SeatManager* obj = new SeatManager(n); + * int param_1 = obj->reserve(); + * obj->unreserve(seatNumber); + */ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md new file mode 100644 index 00000000..3c53089a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md @@ -0,0 +1,42 @@ +

Dropbox
1
1845. Seat Reservation Manager

Medium


Design a system that manages the reservation state of n seats that are numbered from 1 to n.

+ +

Implement the SeatManager class:

+ +
    +
  • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
  • +
  • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
  • +
  • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
  • +
+ +

 

+

Example 1:

+ +
Input
+["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
+[[5], [], [], [2], [], [], [], [], [5]]
+Output
+[null, 1, 2, null, 2, 3, 4, 5, null]
+
+Explanation
+SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
+seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
+seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
+seatManager.reserve();    // The only available seat is seat 5, so return 5.
+seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= seatNumber <= n
  • +
  • For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
  • +
  • For each call to unreserve, it is guaranteed that seatNumber will be reserved.
  • +
  • At most 105 calls in total will be made to reserve and unreserve.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp new file mode 100644 index 00000000..ffa18222 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isMonotonic(vector& nums) { + + int n = nums.size(); + + bool incr = true, decr = true; + + for(int i = 1; i < n; ++i) + { + incr &= (nums[i-1] <= nums[i]); + decr &= (nums[i-1] >= nums[i]); + } + + return incr or decr; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md new file mode 100644 index 00000000..cb6f2c41 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md @@ -0,0 +1,33 @@ +

Facebook
12
Amazon
2
896. Monotonic Array

Easy


An array is monotonic if it is either monotone increasing or monotone decreasing.

+ +

An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

+ +

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,2,3]
+Output: true
+
+ +

Example 2:

+ +
Input: nums = [6,5,4,4]
+Output: true
+
+ +

Example 3:

+ +
Input: nums = [1,3,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -105 <= nums[i] <= 105
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp new file mode 100644 index 00000000..f2f14855 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp @@ -0,0 +1,49 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector largestValues(TreeNode* root) { + + if(!root) + return {}; + + queue q; + + vector ans; + + q.push(root); + + while(!q.empty()) + { + int size = q.size(); + + int maxi = INT_MIN; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + maxi = max(maxi, curr->val); + + if(curr->left) + q.push(curr->left); + + if(curr->right) + q.push(curr->right); + } + + ans.push_back(maxi); + } + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp new file mode 100644 index 00000000..5549c5cf --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp @@ -0,0 +1,115 @@ +class DSU{ + private: + vector parent, size, rank; + public: + DSU(int n) + { + rank.resize(n+1, 0); + size.resize(n+1, 1); + parent.resize(n+1); + + for(int i = 0; i <= n; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(parent[u] == u) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(rank[parU] < rank[parV]) + { + parent[parU] = parV; + } + else if(rank[parV] < rank[parU]) + { + parent[parV] = parU; + } + else + { + parent[parV] = parU; + ++rank[parU]; + } + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } + +}; + + +class Solution { +public: + bool validateBinaryTreeNodes(int n, vector& leftChild, vector& rightChild) { + + vector indegree(n+1, 0); + + DSU dsu(n); + + for(int i = 0; i < n; ++i) + { + if(leftChild[i] != -1 and ++indegree[leftChild[i]] > 1) + return false; + if(rightChild[i] != -1 and ++indegree[rightChild[i]] > 1) + return false; + + if(leftChild[i] != -1) + { + if(dsu.findParent(i) == dsu.findParent(leftChild[i])) + return false; + else + dsu.unionByRank(i, leftChild[i]); + } + + if(rightChild[i] != -1) + { + if(dsu.findParent(i) == dsu.findParent(rightChild[i])) + return false; + else + dsu.unionByRank(i, rightChild[i]); + } + } + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + if(dsu.findParent(i) == i) + ++cnt; + } + + return cnt == 1; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md new file mode 100644 index 00000000..6f7ec432 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md @@ -0,0 +1,34 @@ +

Facebook
2
1361. Validate Binary Tree Nodes

Medium


You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.

+ +

If node i has no left child then leftChild[i] will equal -1, similarly for the right child.

+ +

Note that the nodes have no values and that we only use the node numbers in this problem.

+ +

 

+

Example 1:

+ +
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
+Output: true
+
+ +

Example 2:

+ +
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
+Output: false
+
+ +

Example 3:

+ +
Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • n == leftChild.length == rightChild.length
  • +
  • 1 <= n <= 104
  • +
  • -1 <= leftChild[i], rightChild[i] <= n - 1
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp new file mode 100644 index 00000000..03f2b022 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + + int helper(int n, vector& dp) + { + if(n == 1) + return 1; + + if(dp[n] != -1) + return dp[n]; + + int res = 1; + + for(int i = 1; i <= n-1; ++i) + { + int prod = i * max(n - i, helper(n-i, dp)); + res = max(res, prod); + } + + return dp[n] = res; + } + + int integerBreak(int n) { + + vector dp(n+1, -1); + + return helper(n, dp); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md new file mode 100644 index 00000000..929907f0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md @@ -0,0 +1,26 @@ +

Facebook
3
343. Integer Break

Medium


Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

+ +

Return the maximum product you can get.

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 1
+Explanation: 2 = 1 + 1, 1 × 1 = 1.
+
+ +

Example 2:

+ +
Input: n = 10
+Output: 36
+Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 58
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp new file mode 100644 index 00000000..de864305 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + int largestSubmatrix(vector>& matrix) { + + int n = matrix.size(); + int m = matrix[0].size(); + + int ans = 0; + + vector> prevHeights; + + for(int row = 0; row < n; ++row) + { + vector> currHeights; + vector visited(m, false); + + for(auto& [height, col] : prevHeights) + { + if(matrix[row][col] == 1) + { + visited[col] = true; + currHeights.push_back({height+1, col}); + } + } + + for(int col = 0; col < m; ++col) + { + if(!visited[col] and matrix[row][col] == 1) + currHeights.push_back({1, col}); + } + + + for(int i = 0; i < currHeights.size(); ++i) + { + int height = currHeights[i].first; + int base = i+1; + ans = max(ans, height * base); + } + + prevHeights = currHeights; + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md new file mode 100644 index 00000000..258022d0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md @@ -0,0 +1,38 @@ +

Google
1
1727. Largest Submatrix With Rearrangements

Medium


You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.

+ +

Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.

+ +

 

+

Example 1:

+ +
Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
+Output: 4
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 4.
+
+ +

Example 2:

+ +
Input: matrix = [[1,0,1,0,1]]
+Output: 3
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 3.
+
+ +

Example 3:

+ +
Input: matrix = [[1,1,0],[1,0,1]]
+Output: 2
+Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m * n <= 105
  • +
  • matrix[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md new file mode 100644 index 00000000..cb2c131c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md @@ -0,0 +1,32 @@ +

Google
15
Roblox
2
792. Number of Matching Subsequences

Medium


Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.

+ +

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

+ +
    +
  • For example, "ace" is a subsequence of "abcde".
  • +
+ +

 

+

Example 1:

+ +
Input: s = "abcde", words = ["a","bb","acd","ace"]
+Output: 3
+Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
+
+ +

Example 2:

+ +
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
+Output: 2
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • 1 <= words.length <= 5000
  • +
  • 1 <= words[i].length <= 50
  • +
  • s and words[i] consist of only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp new file mode 100644 index 00000000..38db9baf --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector buildArray(vector& target, int n) { + + vector ans; + + int k = 0; + + for(int i = 1; i <= n and k < target.size(); ++i) + { + if(target[k] == i) + { + ans.push_back("Push"); + ++k; + } + else + { + ans.push_back("Push"); + ans.push_back("Pop"); + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md new file mode 100644 index 00000000..051d4f2d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md @@ -0,0 +1,64 @@ +

Google
2
1441. Build an Array With Stack Operations

Medium


You are given an integer array target and an integer n.

+ +

You have an empty stack with the two following operations:

+ +
    +
  • "Push": pushes an integer to the top of the stack.
  • +
  • "Pop": removes the integer on the top of the stack.
  • +
+ +

You also have a stream of the integers in the range [1, n].

+ +

Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:

+ +
    +
  • If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
  • +
  • If the stack is not empty, pop the integer at the top of the stack.
  • +
  • If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
  • +
+ +

Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.

+ +

 

+

Example 1:

+ +
Input: target = [1,3], n = 3
+Output: ["Push","Push","Pop","Push"]
+Explanation: Initially the stack s is empty. The last element is the top of the stack.
+Read 1 from the stream and push it to the stack. s = [1].
+Read 2 from the stream and push it to the stack. s = [1,2].
+Pop the integer on the top of the stack. s = [1].
+Read 3 from the stream and push it to the stack. s = [1,3].
+
+ +

Example 2:

+ +
Input: target = [1,2,3], n = 3
+Output: ["Push","Push","Push"]
+Explanation: Initially the stack s is empty. The last element is the top of the stack.
+Read 1 from the stream and push it to the stack. s = [1].
+Read 2 from the stream and push it to the stack. s = [1,2].
+Read 3 from the stream and push it to the stack. s = [1,2,3].
+
+ +

Example 3:

+ +
Input: target = [1,2], n = 4
+Output: ["Push","Push"]
+Explanation: Initially the stack s is empty. The last element is the top of the stack.
+Read 1 from the stream and push it to the stack. s = [1].
+Read 2 from the stream and push it to the stack. s = [1,2].
+Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
+The answers that read integer 3 from the stream are not accepted.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= target.length <= 100
  • +
  • 1 <= n <= 100
  • +
  • 1 <= target[i] <= n
  • +
  • target is strictly increasing.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp new file mode 100644 index 00000000..12c01f52 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int minPairSum(vector& nums) { + + int n = nums.size(); + + int ans = 0; + + sort(nums.begin(), nums.end()); + + for(int i = 0; i <= n/2; ++i) + { + ans = max(ans, nums[i] + nums[n-i-1]); + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp new file mode 100644 index 00000000..02b72711 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + char findTheDifference(string s, string t) { + + sort(t.begin(), t.end()); + sort(s.begin(), s.end()); + + int k = 0; + + for(int i = 0; i < s.size(); ++i) + { + if(s[i] == t[k]) + ++k; + else + return t[k]; + } + + return t.back(); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md new file mode 100644 index 00000000..34fec164 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md @@ -0,0 +1,29 @@ +

Google
6
Amazon
3
Apple
2
389. Find the Difference

Easy


You are given two strings s and t.

+ +

String t is generated by random shuffling string s and then add one more letter at a random position.

+ +

Return the letter that was added to t.

+ +

 

+

Example 1:

+ +
Input: s = "abcd", t = "abcde"
+Output: "e"
+Explanation: 'e' is the letter that was added.
+
+ +

Example 2:

+ +
Input: s = "", t = "y"
+Output: "y"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 1000
  • +
  • t.length == s.length + 1
  • +
  • s and t consist of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp new file mode 100644 index 00000000..e4860bae --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + string solve(string& str, int n) + { + int k = 0; + + for(int i = 0; i < n; ++i) + { + if(str[i] == '#') + { + k = max(0, --k); + } + else + { + str[k++] = str[i]; + } + } + + return str.substr(0, k); + } + + bool backspaceCompare(string s, string t) { + + int n = s.size(), m = t.size(); + + return (solve(s, n) == solve(t, m)); + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md new file mode 100644 index 00000000..f563b42e --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md @@ -0,0 +1,37 @@ +

IBM
6
Google
3
Booking.com
3
Apple
3
Visa
3
Bloomberg
2
tiktok
2
844. Backspace String Compare

Easy


Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

+ +

Note that after backspacing an empty text, the text will continue empty.

+ +

 

+

Example 1:

+ +
Input: s = "ab#c", t = "ad#c"
+Output: true
+Explanation: Both s and t become "ac".
+
+ +

Example 2:

+ +
Input: s = "ab##", t = "c#d#"
+Output: true
+Explanation: Both s and t become "".
+
+ +

Example 3:

+ +
Input: s = "a#c", t = "b"
+Output: false
+Explanation: s becomes "c" while t becomes "b".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 200
  • +
  • s and t only contain lowercase letters and '#' characters.
  • +
+ +

 

+

Follow up: Can you solve it in O(n) time and O(1) space?

+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp new file mode 100644 index 00000000..efe6dfbd --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector sortByBits(vector& arr) { + + sort(arr.begin(), arr.end(), [&](int& a, int & b) + { + int cntA = __builtin_popcount(a); + int cntB = __builtin_popcount(b); + + return (cntA == cntB ? a < b : cntA < cntB); + }); + + return arr; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md new file mode 100644 index 00000000..b5d4b137 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md @@ -0,0 +1,31 @@ +

Infosys
2
1356. Sort Integers by The Number of 1 Bits

Easy


You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

+ +

Return the array after sorting it.

+ +

 

+

Example 1:

+ +
Input: arr = [0,1,2,3,4,5,6,7,8]
+Output: [0,1,2,4,8,3,5,6,7]
+Explantion: [0] is the only integer with 0 bits.
+[1,2,4,8] all have 1 bit.
+[3,5,6] have 2 bits.
+[7] has 3 bits.
+The sorted array by bits is [0,1,2,4,8,3,5,6,7]
+
+ +

Example 2:

+ +
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
+Output: [1,2,4,8,16,32,64,128,256,512,1024]
+Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 500
  • +
  • 0 <= arr[i] <= 104
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp new file mode 100644 index 00000000..1b331ed1 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int countCharacters(vector& words, string chars) { + + int counter = 0; + + unordered_map mp; + + for(auto& ch : chars) + ++mp[ch]; + + for(auto& word : words) + { + bool canFormed = true; + + unordered_map freq; + + for(auto& ch : word) + { + ++freq[ch]; + } + + for(auto& ch : word) + { + if(freq[ch] > mp[ch]) + { + canFormed = false; + break; + } + } + + if(canFormed) + counter += word.size(); + } + + return counter; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md new file mode 100644 index 00000000..de231256 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md @@ -0,0 +1,30 @@ +

Karat
12
Indeed
6
Amazon
2
Google
2
1160. Find Words That Can Be Formed by Characters

Easy


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

+ +

A string is good if it can be formed by characters from chars (each character can only be used once).

+ +

Return the sum of lengths of all good strings in words.

+ +

 

+

Example 1:

+ +
Input: words = ["cat","bt","hat","tree"], chars = "atach"
+Output: 6
+Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
+
+ +

Example 2:

+ +
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
+Output: 10
+Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length, chars.length <= 100
  • +
  • words[i] and chars consist of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp new file mode 100644 index 00000000..942b1785 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector getRow(int rowIndex) { + + vector res(rowIndex+1, 1); + + for(int i = 0; i < rowIndex; ++i) + { + for(int j = i; j > 0; --j) + res[j] += res[j-1]; + } + return res; + } +}; + diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md new file mode 100644 index 00000000..e5747c89 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md @@ -0,0 +1,25 @@ +

Microsoft
2
Goldman Sachs
2
119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

+ +

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

+ +

 

+

Example 1:

+
Input: rowIndex = 3
+Output: [1,3,3,1]
+

Example 2:

+
Input: rowIndex = 0
+Output: [1]
+

Example 3:

+
Input: rowIndex = 1
+Output: [1,1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= rowIndex <= 33
  • +
+ +

 

+

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp new file mode 100644 index 00000000..1b672930 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int numIdenticalPairs(vector& nums) { + + int n = nums.size(); + + unordered_map mp; + + for(auto& itr : nums) + ++mp[itr]; + + int count = 0; + + for(auto& [key, value] : mp) + { + count += (value * (value-1))/2; + } + + return count; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md new file mode 100644 index 00000000..c5e5ff2c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md @@ -0,0 +1,33 @@ +

Microsoft
3
Amazon
3
Adobe
2
1512. Number of Good Pairs

Easy


Given an array of integers nums, return the number of good pairs.

+ +

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1,1,3]
+Output: 4
+Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1]
+Output: 6
+Explanation: Each pair in the array are good.
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp new file mode 100644 index 00000000..610932ec --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + string reverseWords(string s) { + + stringstream ss(s); + + string word; + + string ans; + + while(ss >> word) + { + reverse(word.begin(), word.end()); + ans += word + ' '; + } + + ans.pop_back(); + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md new file mode 100644 index 00000000..b13bbc68 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md @@ -0,0 +1,21 @@ +

Microsoft
5
Amazon
3
Bolt
3
Yandex
2
Facebook
2
Apple
2
PayTM
2
557. Reverse Words in a String III

Easy


Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

+ +

 

+

Example 1:

+
Input: s = "Let's take LeetCode contest"
+Output: "s'teL ekat edoCteeL tsetnoc"
+

Example 2:

+
Input: s = "God Ding"
+Output: "doG gniD"
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • s contains printable ASCII characters.
  • +
  • s does not contain any leading or trailing spaces.
  • +
  • There is at least one word in s.
  • +
  • All the words in s are separated by a single space.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp new file mode 100644 index 00000000..92d4e476 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minimumTime(int n, vector>& E, vector& T) { + vector> G(n); + vector indegree(n), dist(n); + for (auto &e : E) { + G[e[0] - 1].push_back(e[1] - 1); + indegree[e[1] - 1]++; + } + queue q; + for (int i = 0; i < n; ++i) { + if (indegree[i] == 0) { + q.push(i); + dist[i] = T[i]; + } + } + while (q.size()) { + int u = q.front(); + q.pop(); + for (int v : G[u]) { + dist[v] = max(dist[u] + T[v], dist[v]); + if (--indegree[v] == 0) q.push(v); + } + } + return *max_element(begin(dist), end(dist)); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md new file mode 100644 index 00000000..b26d9a0c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md @@ -0,0 +1,53 @@ +

Pinterest
4
2050. Parallel Courses III

Hard


You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that course prevCoursej has to be completed before course nextCoursej (prerequisite relationship). Furthermore, you are given a 0-indexed integer array time where time[i] denotes how many months it takes to complete the (i+1)th course.

+ +

You must find the minimum number of months needed to complete all the courses following these rules:

+ +
    +
  • You may start taking a course at any time if the prerequisites are met.
  • +
  • Any number of courses can be taken at the same time.
  • +
+ +

Return the minimum number of months needed to complete all the courses.

+ +

Note: The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).

+ +

 

+

Example 1:

+ + +
Input: n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
+Output: 8
+Explanation: The figure above represents the given graph and the time required to complete each course. 
+We start course 1 and course 2 simultaneously at month 0.
+Course 1 takes 3 months and course 2 takes 2 months to complete respectively.
+Thus, the earliest time we can start course 3 is at month 3, and the total time required is 3 + 5 = 8 months.
+
+ +

Example 2:

+ + +
Input: n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
+Output: 12
+Explanation: The figure above represents the given graph and the time required to complete each course.
+You can start courses 1, 2, and 3 at month 0.
+You can complete them after 1, 2, and 3 months respectively.
+Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed after 3 + 4 = 7 months.
+Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed, i.e., after max(1,2,3,7) = 7 months.
+Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 5 * 104
  • +
  • 0 <= relations.length <= min(n * (n - 1) / 2, 5 * 104)
  • +
  • relations[j].length == 2
  • +
  • 1 <= prevCoursej, nextCoursej <= n
  • +
  • prevCoursej != nextCoursej
  • +
  • All the pairs [prevCoursej, nextCoursej] are unique.
  • +
  • time.length == n
  • +
  • 1 <= time[i] <= 104
  • +
  • The given graph is a directed acyclic graph.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp new file mode 100644 index 00000000..eb1b35f2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + + bool helper(int n) + { + if(n <= 0) + return false; + + if(n == 1) + return true; + + if(n % 4 == 0) + return helper(n/4); + + return false; + } + + bool isPowerOfFour(int n) { + return helper(n); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md new file mode 100644 index 00000000..f84ffe4d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md @@ -0,0 +1,24 @@ +

Two Sigma
1
342. Power of Four

Easy


Given an integer n, return true if it is a power of four. Otherwise, return false.

+ +

An integer n is a power of four, if there exists an integer x such that n == 4x.

+ +

 

+

Example 1:

+
Input: n = 16
+Output: true
+

Example 2:

+
Input: n = 5
+Output: false
+

Example 3:

+
Input: n = 1
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion?
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp new file mode 100644 index 00000000..f2a40390 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp @@ -0,0 +1,23 @@ +class Solution { +public: +double champagneTower(int poured, int query_row, int query_glass) { + + double result[101][101] = {0.0}; + + result[0][0] = poured; + + for (int i = 0; i < 100; i++) + { + for (int j = 0; j <= i; j++) + { + if (result[i][j] >= 1) + { + result[i + 1][j] += (result[i][j] - 1) / 2.0; + result[i + 1][j + 1] += (result[i][j] - 1) / 2.0; + result[i][j] = 1; + } + } + } + return result[query_row][query_glass]; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md new file mode 100644 index 00000000..11954b9d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md @@ -0,0 +1,38 @@ +

Uber
2
Amazon
2
Adobe
2
799. Champagne Tower

Medium


We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row.  Each glass holds one cup of champagne.

+ +

Then, some champagne is poured into the first glass at the top.  When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it.  When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.  (A glass at the bottom row has its excess champagne fall on the floor.)

+ +

For example, after one cup of champagne is poured, the top most glass is full.  After two cups of champagne are poured, the two glasses on the second row are half full.  After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.  After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.

+ +

+ +

Now after pouring some non-negative integer cups of champagne, return how full the jth glass in the ith row is (both i and j are 0-indexed.)

+ +

 

+

Example 1:

+ +
Input: poured = 1, query_row = 1, query_glass = 1
+Output: 0.00000
+Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
+
+ +

Example 2:

+ +
Input: poured = 2, query_row = 1, query_glass = 1
+Output: 0.50000
+Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
+
+ +

Example 3:

+ +
Input: poured = 100000009, query_row = 33, query_glass = 17
+Output: 1.00000
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= poured <= 109
  • +
  • 0 <= query_glass <= query_row < 100
  • +
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp new file mode 100644 index 00000000..02b8bc5c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int countHomogenous(string s) { + + int n = s.size(), cnt = 0, prev = 0, res = 0; + + const int mod = 1e9 + 7; + + for(auto& itr : s) + { + cnt = (itr == prev ? cnt+1 : 1); + + prev = itr; + + res = (res + cnt) % mod; + } + + return res; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md new file mode 100644 index 00000000..f0e7b65d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md @@ -0,0 +1,40 @@ +

Virtu Financial
1
1759. Count Number of Homogenous Substrings

Medium


Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.

+ +

A string is homogenous if all the characters of the string are the same.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "abbcccaa"
+Output: 13
+Explanation: The homogenous substrings are listed as below:
+"a"   appears 3 times.
+"aa"  appears 1 time.
+"b"   appears 2 times.
+"bb"  appears 1 time.
+"c"   appears 3 times.
+"cc"  appears 2 times.
+"ccc" appears 1 time.
+3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
+ +

Example 2:

+ +
Input: s = "xy"
+Output: 2
+Explanation: The homogenous substrings are "x" and "y".
+ +

Example 3:

+ +
Input: s = "zzzzz"
+Output: 15
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase letters.
  • +
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp new file mode 100644 index 00000000..adbdb99b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool winnerOfGame(string colors) { + + int n = colors.size(); + + int aliceMoves = 0, bobMoves = 0; + + for(int i = 1; i < n-1; ++i) + { + char prev = colors[i-1]; + char curr = colors[i]; + char next = colors[i+1]; + + if(prev == curr and curr == next) + { + if(curr == 'A') + ++aliceMoves; + else + ++bobMoves; + } + } + + return (aliceMoves >= bobMoves + 1 ? 1 : 0); + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md new file mode 100644 index 00000000..6499f289 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md @@ -0,0 +1,63 @@ +

Walmart Global Tech
9
2038. Remove Colored Pieces if Both Neighbors are the Same Color

Medium


There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.

+ +

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

+ +
    +
  • Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
  • +
  • Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
  • +
  • Alice and Bob cannot remove pieces from the edge of the line.
  • +
  • If a player cannot make a move on their turn, that player loses and the other player wins.
  • +
+ +

Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.

+ +

 

+

Example 1:

+ +
Input: colors = "AAABABB"
+Output: true
+Explanation:
+AAABABB -> AABABB
+Alice moves first.
+She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
+
+Now it's Bob's turn.
+Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
+Thus, Alice wins, so return true.
+
+ +

Example 2:

+ +
Input: colors = "AA"
+Output: false
+Explanation:
+Alice has her turn first.
+There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
+Thus, Bob wins, so return false.
+
+ +

Example 3:

+ +
Input: colors = "ABBBBBBBAAA"
+Output: false
+Explanation:
+ABBBBBBBAAA -> ABBBBBBBAA
+Alice moves first.
+Her only option is to remove the second to last 'A' from the right.
+
+ABBBBBBBAA -> ABBBBBBAA
+Next is Bob's turn.
+He has many options for which 'B' piece to remove. He can pick any.
+
+On Alice's second turn, she has no more pieces that she can remove.
+Thus, Bob wins, so return false.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= colors.length <= 105
  • +
  • colors consists of only the letters 'A' and 'B'
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp new file mode 100644 index 00000000..b9602471 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + string smallestSubsequence(string s) { + + int n = s.size(); + + vector lastOccurence(26, -1); + + vector visited(26, false); + + for(int i = 0; i < n; ++i) + lastOccurence[s[i] - 'a'] = i; + + string ans; + + for(int i = 0; i < n; ++i) + { + while(!ans.empty() and !visited[s[i] - 'a'] and s[i] < ans.back() and lastOccurence[ans.back() - 'a'] > i) + { + visited[ans.back() - 'a'] = false; + + ans.pop_back(); + } + + if(!visited[s[i] - 'a']) + { + ans += s[i]; + visited[s[i] - 'a'] = true; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md new file mode 100644 index 00000000..1e0f5f90 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md @@ -0,0 +1,25 @@ +

No companies found for this problem
1081. Smallest Subsequence of Distinct Characters

Medium


Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

+ +

 

+

Example 1:

+ +
Input: s = "bcabc"
+Output: "abc"
+
+ +

Example 2:

+ +
Input: s = "cbacdcbc"
+Output: "acdb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of lowercase English letters.
  • +
+ +

 

+Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp new file mode 100644 index 00000000..f0261a5c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp @@ -0,0 +1,51 @@ +/** + * // This is the MountainArray's API interface. + * // You should not implement it, or speculate about its implementation + * class MountainArray { + * public: + * int get(int index); + * int length(); + * }; + */ + +class Solution { +public: + int findInMountainArray(int target, MountainArray A) { + int n = A.length(), l, r, m, peak = 0; + + l = 0; + r = n - 1; + while (l < r) { + m = (l + r) / 2; + if (A.get(m) < A.get(m + 1)) + l = peak = m + 1; + else + r = m; + } + + l = 0; + r = peak; + while (l <= r) { + m = (l + r) / 2; + if (A.get(m) < target) + l = m + 1; + else if (A.get(m) > target) + r = m - 1; + else + return m; + } + + l = peak; + r = n - 1; + while (l <= r) { + m = (l + r) / 2; + if (A.get(m) > target) + l = m + 1; + else if (A.get(m) < target) + r = m - 1; + else + return m; + } + return -1; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md new file mode 100644 index 00000000..2e3225cb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md @@ -0,0 +1,48 @@ +

No companies found for this problem
1095. Find in Mountain Array

Hard


(This problem is an interactive problem.)

+ +

You may recall that an array arr is a mountain array if and only if:

+ +
    +
  • arr.length >= 3
  • +
  • There exists some i with 0 < i < arr.length - 1 such that: +
      +
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • +
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
    • +
    +
  • +
+ +

Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.

+ +

You cannot access the mountain array directly. You may only access the array using a MountainArray interface:

+ +
    +
  • MountainArray.get(k) returns the element of the array at index k (0-indexed).
  • +
  • MountainArray.length() returns the length of the array.
  • +
+ +

Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.

+ +

 

+

Example 1:

+ +
Input: array = [1,2,3,4,5,3,1], target = 3
+Output: 2
+Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
+ +

Example 2:

+ +
Input: array = [0,1,2,4,2,1], target = 3
+Output: -1
+Explanation: 3 does not exist in the array, so we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= mountain_arr.length() <= 104
  • +
  • 0 <= target <= 109
  • +
  • 0 <= mountain_arr.get(index) <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp new file mode 100644 index 00000000..56bada35 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp @@ -0,0 +1,55 @@ +class Solution { + unordered_map> mp; + int mod = 1e9+7; + +public: + + int helper(int n ,int i ,char prev, vector>& t) + { + if(i > n) + return 0; + + if(i == n) + { + switch(prev) + { + case 'a': + return 1; + case 'e': + return 2; + case 'i': + return 4; + case 'o': + return 2; + case 'u': + return 1; + default: + return 5; + } + } + + int idx = prev - 'a'; + if(t[i][idx] != -1) + return t[i][idx]; + + long long ans = 0; + for(auto next : mp[prev]) + ans += (helper(n,i+1,next,t) % mod); + + return t[i][idx] = ans % mod; + } + + int countVowelPermutation(int n) { + + mp['c'] = {'a','e','i','o','u'}; + mp['a'] = {'e'}; + mp['e'] = {'a','i'}; + mp['i'] = {'a','e','o','u'}; + mp['o'] = {'i','u'}; + mp['u'] = {'a'}; + + vector> t(n+2,vector(27,-1)); + return helper(n,1,'c',t); + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp new file mode 100644 index 00000000..5c0ea1e2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int idx, int steps, int arrLen, vector>& dp) + { + if(steps == 0 and idx == 0) + { + return 1; + } + + if(idx < 0 or idx >= arrLen or (idx != 0 and steps == 0)) + return 0; + + if(dp[idx][steps] != -1) + return dp[idx][steps]; + + int stay = helper(idx, steps-1, arrLen, dp); + + int left = helper(idx-1, steps-1, arrLen, dp); + + int right = helper(idx+1, steps-1, arrLen, dp); + + return dp[idx][steps] = ((((stay)%mod + left)%mod + right)%mod)%mod; + } + + int numWays(int steps, int arrLen) { + + vector> dp(steps+1, vector(steps+1, -1)); + + return helper(0, steps, arrLen, dp); + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md new file mode 100644 index 00000000..ad55ddcb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md @@ -0,0 +1,39 @@ +

No companies found for this problem
1269. Number of Ways to Stay in the Same Place After Some Steps

Hard


You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).

+ +

Given two integers steps and arrLen, return the number of ways such that your pointer is still at index 0 after exactly steps steps. Since the answer may be too large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: steps = 3, arrLen = 2
+Output: 4
+Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
+Right, Left, Stay
+Stay, Right, Left
+Right, Stay, Left
+Stay, Stay, Stay
+
+ +

Example 2:

+ +
Input: steps = 2, arrLen = 4
+Output: 2
+Explanation: There are 2 differents ways to stay at index 0 after 2 steps
+Right, Left
+Stay, Stay
+
+ +

Example 3:

+ +
Input: steps = 4, arrLen = 2
+Output: 8
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= steps <= 500
  • +
  • 1 <= arrLen <= 106
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp new file mode 100644 index 00000000..70edfa32 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int numOfArrays(int n, int m, int h) { + int64_t dp[n][h+1][m+1]; + memset(dp, 0, (n)*(h+1)*(m+1)*sizeof(int64_t)); + for(int i = 0; i < n; ++i) { + for(int j = 1; j <= h; ++j) { + for(int k = j; k <= m; ++k) { + dp[i][j][k] = dp[i][j][k-1]; + if(i == 0) { + dp[i][j][k] += j == 1 ? 1 : 0; + continue; + } + dp[i][j][k] = (dp[i][j][k] + dp[i-1][j-1][k-1] + (dp[i-1][j][k] + MOD - dp[i-1][j][k-1]) * k) % MOD; + } + } + } + return dp[n-1][h][m]; + } + static const int MOD = 1e9+7; +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md new file mode 100644 index 00000000..b71eddf5 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md @@ -0,0 +1,43 @@ +

No companies found for this problem
1420. Build Array Where You Can Find The Maximum Exactly K Comparisons

Hard


You are given three integers n, m and k. Consider the following algorithm to find the maximum element of an array of positive integers:

+ +

You should build the array arr which has the following properties:

+ +
    +
  • arr has exactly n integers.
  • +
  • 1 <= arr[i] <= m where (0 <= i < n).
  • +
  • After applying the mentioned algorithm to arr, the value search_cost is equal to k.
  • +
+ +

Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 2, m = 3, k = 1
+Output: 6
+Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
+
+ +

Example 2:

+ +
Input: n = 5, m = 2, k = 3
+Output: 0
+Explanation: There are no possible arrays that satisify the mentioned conditions.
+
+ +

Example 3:

+ +
Input: n = 9, m = 1, k = 1
+Output: 1
+Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 50
  • +
  • 1 <= m <= 100
  • +
  • 0 <= k <= n
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp new file mode 100644 index 00000000..2a6a2dc4 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + + int n = nums.size(); + + vector dp(nums); + + deque dq; + + int ans = dp[0]; + + for(int i = 0; i < n; ++i) + { + while(!dq.empty() and i - dq.front() > k) + dq.pop_front(); + + if(!dq.empty()) + dp[i] = max(dp[i], nums[i] + dp[dq.front()]); + + while(!dq.empty() and dp[i] >= dp[dq.back()]) + dq.pop_back(); + + dq.push_back(i); + + ans = max(ans, dp[i]); + } + + return ans; + } +}; diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md new file mode 100644 index 00000000..83b7de49 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md @@ -0,0 +1,34 @@ +

No companies found for this problem
1425. Constrained Subsequence Sum

Hard


Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.

+ +

A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

+ +

 

+

Example 1:

+ +
Input: nums = [10,2,-10,5,20], k = 2
+Output: 37
+Explanation: The subsequence is [10, 2, 5, 20].
+
+ +

Example 2:

+ +
Input: nums = [-1,-2,-3], k = 1
+Output: -1
+Explanation: The subsequence must be non-empty, so we choose the largest number.
+
+ +

Example 3:

+ +
Input: nums = [10,-2,-10,-5,20], k = 2
+Output: 23
+Explanation: The subsequence is [10, -2, -5, 20].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp new file mode 100644 index 00000000..145c11a2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + int helper(int i, int j, int n, int m, vector& nums1, vector& nums2, vector>& dp) + { + if(i == n or j == m) + return -1e9; + + if(dp[i][j] != -1) + return dp[i][j]; + + int res = INT_MIN; + + int take = max(nums1[i] * nums2[j], nums1[i] * nums2[j] + helper(i+1, j+1, n, m, nums1, nums2, dp)); + + int notTake = max(helper(i+1, j, n, m, nums1, nums2, dp), helper(i, j+1, n, m, nums1, nums2, dp)); + + return dp[i][j] = max(take, notTake); + + } + + int maxDotProduct(vector& nums1, vector& nums2) { + + int n = nums1.size(); + int m = nums2.size(); + + vector> dp(n+1, vector(m+1, -1)); + + return helper(0, 0, n, m, nums1, nums2, dp); + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md new file mode 100644 index 00000000..c15422c7 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md @@ -0,0 +1,36 @@ +

No companies found for this problem
1458. Max Dot Product of Two Subsequences

Hard


Given two arrays nums1 and nums2.

+ +

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

+ +

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

+ +

 

+

Example 1:

+ +
Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
+Output: 18
+Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
+Their dot product is (2*3 + (-2)*(-6)) = 18.
+ +

Example 2:

+ +
Input: nums1 = [3,-2], nums2 = [2,-6,7]
+Output: 21
+Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
+Their dot product is (3*7) = 21.
+ +

Example 3:

+ +
Input: nums1 = [-1,-1], nums2 = [1,1]
+Output: -1
+Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
+Their dot product is -1.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 500
  • +
  • -1000 <= nums1[i], nums2[i] <= 1000
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp new file mode 100644 index 00000000..eaad07b4 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int minimumOneBitOperations(int n) { + if (n <= 1) + return n; + int bit = 0; + while ((1 << bit) <= n) + bit++; + return ((1 << bit) - 1) - minimumOneBitOperations(n - (1 << (bit-1))); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md new file mode 100644 index 00000000..683b1133 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md @@ -0,0 +1,37 @@ +

No companies found for this problem
1611. Minimum One Bit Operations to Make Integers Zero

Hard


Given an integer n, you must transform it into 0 using the following operations any number of times:

+ +
    +
  • Change the rightmost (0th) bit in the binary representation of n.
  • +
  • Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.
  • +
+ +

Return the minimum number of operations to transform n into 0.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 2
+Explanation: The binary representation of 3 is "11".
+"11" -> "01" with the 2nd operation since the 0th bit is 1.
+"01" -> "00" with the 1st operation.
+
+ +

Example 2:

+ +
Input: n = 6
+Output: 4
+Explanation: The binary representation of 6 is "110".
+"110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
+"010" -> "011" with the 1st operation.
+"011" -> "001" with the 2nd operation since the 0th bit is 1.
+"001" -> "000" with the 1st operation.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp new file mode 100644 index 00000000..e25a2d5b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector getSumAbsoluteDifferences(vector& nums) { + + int n = nums.size(); + + vector pref(n+1,0); + + for(int i = 1; i <= n; ++i) + pref[i] = pref[i-1] + nums[i-1]; + + vector ans; + + for(int i = 1; i <= n; ++i) + { + long long leftSum = nums[i-1] * 1LL * i; + long long left = leftSum - pref[i]; + + long long rightSum = nums[i-1] * 1LL * (n-i); + long long right = pref[n] - pref[i] - rightSum; + + ans.push_back(left + right); + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp new file mode 100644 index 00000000..1e22038a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int maximumScore(vector& nums, int k) { + + int n = nums.size(); + + int i = k, j = k; + + int ans = nums[k], currMin = nums[k]; + + while(i > 0 or j < n-1) + { + int left = (i > 0 ? nums[i-1] : 0); + int right = (j < n-1 ? nums[j+1] : 0); + + if(left > right) + { + --i; + currMin = min(currMin, nums[i]); + } + else + { + ++j; + currMin = min(currMin,nums[j]); + } + + ans = max(ans, currMin * (j-i+1)); + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md new file mode 100644 index 00000000..8e27f09c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md @@ -0,0 +1,30 @@ +

No companies found for this problem
1793. Maximum Score of a Good Subarray

Hard


You are given an array of integers nums (0-indexed) and an integer k.

+ +

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.

+ +

Return the maximum possible score of a good subarray.

+ +

 

+

Example 1:

+ +
Input: nums = [1,4,3,7,4,5], k = 3
+Output: 15
+Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
+
+ +

Example 2:

+ +
Input: nums = [5,5,4,5,4,1,1,1], k = 0
+Output: 20
+Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 2 * 104
  • +
  • 0 <= k < nums.length
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp new file mode 100644 index 00000000..39056bf9 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int maxFrequency(vector& nums, int k) { + + long long currSum = 0; + + int i = 0, j = 0, n = nums.size(); + + int ans = 0; + + sort(nums.begin(), nums.end()); + + while(j < n) + { + currSum += nums[j]; + + int cnt = j - i + 1; + + long long windowSum = cnt * 1LL * nums[j]; + + if(windowSum - currSum > k) + { + windowSum -= nums[j]; + currSum -= nums[i++]; + } + + ans = max(ans, (j-i+1)); + + ++j; + } + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md new file mode 100644 index 00000000..bdb094f6 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md @@ -0,0 +1,39 @@ +

No companies found for this problem
1838. Frequency of the Most Frequent Element

Medium


The frequency of an element is the number of times it occurs in an array.

+ +

You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.

+ +

Return the maximum possible frequency of an element after performing at most k operations.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,4], k = 5
+Output: 3
+Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
+4 has a frequency of 3.
+ +

Example 2:

+ +
Input: nums = [1,4,8,13], k = 5
+Output: 2
+Explanation: There are multiple optimal solutions:
+- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
+- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
+- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
+
+ +

Example 3:

+ +
Input: nums = [3,9,6], k = 2
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp new file mode 100644 index 00000000..62f90fac --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int maximumElementAfterDecrementingAndRearranging(vector& arr) { + + int n = arr.size(); + + sort(arr.begin(), arr.end()); + + int operation = 0; + + if(arr[0] != 1) + { + arr[0] = 1; + } + + int ans = arr[0]; + + for(int i = 1; i < n; ++i) + { + if(abs(arr[i] - arr[i-1]) > 1) + { + arr[i] = arr[i-1] + 1; + ++operation; + } + ans = max(ans, arr[i]); + } + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp new file mode 100644 index 00000000..894f7ffb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int reductionOperations(vector& nums) { + + int n = nums.size(), ans = 0; + + sort(nums.begin(), nums.end()); + + for(int i = n-1; i > 0; --i) + { + if(nums[i] != nums[i-1]) + { + ans += (n - i); + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md new file mode 100644 index 00000000..2cd89aae --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md @@ -0,0 +1,47 @@ +

No companies found for this problem
1887. Reduction Operations to Make the Array Elements Equal

Medium


Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:

+ +
    +
  1. Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
  2. +
  3. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
  4. +
  5. Reduce nums[i] to nextLargest.
  6. +
+ +

Return the number of operations to make all elements in nums equal.

+ +

 

+

Example 1:

+ +
Input: nums = [5,1,3]
+Output: 3
+Explanation: It takes 3 operations to make all elements in nums equal:
+1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
+2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
+3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
+
+ +

Example 2:

+ +
Input: nums = [1,1,1]
+Output: 0
+Explanation: All elements in nums are already equal.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2,2,3]
+Output: 4
+Explanation: It takes 4 operations to make all elements in nums equal:
+1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
+2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
+3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
+4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 1 <= nums[i] <= 5 * 104
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp new file mode 100644 index 00000000..e7a702a0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int eliminateMaximum(vector& dist, vector& speed) { + + int n = dist.size(); + + vector vec; + + for(int i = 0; i < n; ++i) + { + vec.push_back((float)dist[i]/speed[i]); + } + + sort(vec.begin(), vec.end()); + + int counter = 1, ans = 1; + + for(int i = 1; i < n; ++i) + { + if(counter < vec[i]) + ++ans; + else + break; + ++counter; + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md new file mode 100644 index 00000000..684cc6cc --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md @@ -0,0 +1,50 @@ +

No companies found for this problem
1921. Eliminate Maximum Number of Monsters

Medium


You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.

+ +

The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.

+ +

You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.

+ +

You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.

+ +

Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.

+ +

 

+

Example 1:

+ +
Input: dist = [1,3,4], speed = [1,1,1]
+Output: 3
+Explanation:
+In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
+After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.
+All 3 monsters can be eliminated.
+ +

Example 2:

+ +
Input: dist = [1,1,2,3], speed = [1,1,1,1]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,1,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

Example 3:

+ +
Input: dist = [3,2,4], speed = [5,3,2]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

 

+

Constraints:

+ +
    +
  • n == dist.length == speed.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= dist[i], speed[i] <= 105
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp new file mode 100644 index 00000000..0214797b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int minOperations(vector& arr) { + int n = arr.size(); + if(n == 1) return 0; + sort(arr.begin(), arr.end()); + + vector a; + for(int i = 0; i < n-1; i++) { + while(arr[i] == arr[i+1]) i++; + a.push_back(arr[i]); + } + if(arr.back() != a.back()) a.push_back(arr.back()); + + int mx = 0; + for(int i = 0, j = 0; i < a.size(); i++) { + while(j <= i && (a[i]-a[j]+1) > n) j++; + mx = max(mx, i-j+1); + } + return n - mx; +} +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md new file mode 100644 index 00000000..0ec8717b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md @@ -0,0 +1,48 @@ +

No companies found for this problem
2009. Minimum Number of Operations to Make Array Continuous

Hard


You are given an integer array nums. In one operation, you can replace any element in nums with any integer.

+ +

nums is considered continuous if both of the following conditions are fulfilled:

+ +
    +
  • All elements in nums are unique.
  • +
  • The difference between the maximum element and the minimum element in nums equals nums.length - 1.
  • +
+ +

For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.

+ +

Return the minimum number of operations to make nums continuous.

+ +

 

+

Example 1:

+ +
Input: nums = [4,2,5,3]
+Output: 0
+Explanation: nums is already continuous.
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,5,6]
+Output: 1
+Explanation: One possible solution is to change the last element to 4.
+The resulting array is [1,2,3,5,4], which is continuous.
+
+ +

Example 3:

+ +
Input: nums = [1,10,100,1000]
+Output: 3
+Explanation: One possible solution is to:
+- Change the second element to 2.
+- Change the third element to 3.
+- Change the fourth element to 4.
+The resulting array is [1,2,3,4], which is continuous.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp new file mode 100644 index 00000000..89923009 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int numberOfWays(string corridor) { + + int n = corridor.size(); + + int cnt = count(corridor.begin(), corridor.end(), 'S'); + + if(!cnt or cnt & 1) return 0; + + int inc = 0; + + vector v; + + int currCnt = 0; + + for(int i = 0; i < n; ++i) + { + if(!cnt) break; + + if(corridor[i] == 'S') + ++currCnt, --cnt; + if(currCnt == 3) + { + v.push_back(inc); + inc = 0; + currCnt = 1; + } + if(currCnt == 2) + (inc = !inc ? 1 : inc+1); + } + + if(currCnt) + { + v.push_back(inc); + } + + long long ans = v[0]; + + for(int i = 1; i < v.size(); ++i) + ans = (ans *1LL* v[i]) % mod; + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md new file mode 100644 index 00000000..da38196d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md @@ -0,0 +1,42 @@ +

No companies found for this problem
2147. Number of Ways to Divide a Long Corridor

Hard


Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.

+ +

One room divider has already been installed to the left of index 0, and another to the right of index n - 1. Additional room dividers can be installed. For each position between indices i - 1 and i (1 <= i <= n - 1), at most one divider can be installed.

+ +

Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.

+ +

Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo 109 + 7. If there is no way, return 0.

+ +

 

+

Example 1:

+ +
Input: corridor = "SSPPSPS"
+Output: 3
+Explanation: There are 3 different ways to divide the corridor.
+The black bars in the above image indicate the two room dividers already installed.
+Note that in each of the ways, each section has exactly two seats.
+
+ +

Example 2:

+ +
Input: corridor = "PPSPSP"
+Output: 1
+Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers.
+Installing any would create some section that does not have exactly two seats.
+
+ +

Example 3:

+ +
Input: corridor = "S"
+Output: 0
+Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
+
+ +

 

+

Constraints:

+ +
    +
  • n == corridor.length
  • +
  • 1 <= n <= 105
  • +
  • corridor[i] is either 'S' or 'P'.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp new file mode 100644 index 00000000..b4d8e152 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& persons) { + vector start, end; + for (auto& t : flowers) + start.push_back(t[0]), end.push_back(t[1]); + sort(start.begin(), start.end()); + sort(end.begin(), end.end()); + vector res; + for (int t : persons) { + int started = upper_bound(start.begin(), start.end(), t) - start.begin(); + int ended = lower_bound(end.begin(), end.end(), t) - end.begin(); + res.push_back(started - ended); + } + return res; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md new file mode 100644 index 00000000..fc333323 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md @@ -0,0 +1,32 @@ +

No companies found for this problem
2251. Number of Flowers in Full Bloom

Hard


You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.

+ +

Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.

+ +

 

+

Example 1:

+ +
Input: flowers = [[1,6],[3,7],[9,12],[4,13]], poeple = [2,3,7,11]
+Output: [1,2,2,2]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

Example 2:

+ +
Input: flowers = [[1,10],[3,3]], poeple = [3,3,2]
+Output: [2,2,1]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= flowers.length <= 5 * 104
  • +
  • flowers[i].length == 2
  • +
  • 1 <= starti <= endi <= 109
  • +
  • 1 <= people.length <= 5 * 104
  • +
  • 1 <= people[i] <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp new file mode 100644 index 00000000..2dc69092 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + string largestGoodInteger(string num) { + + int n = num.size(); + + string ans; + + int i = 0, j = 0, k = 3; + + string curr; + + while(j < n) + { + curr += num[j]; + + if(curr.size() == 3) + { + if(j>=2 and num[j] == num[j-1] and num[j-1] == num[j-2]) + { + ans = max(ans, curr); + } + curr.erase(curr.begin()); + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md new file mode 100644 index 00000000..aeb415ac --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md @@ -0,0 +1,47 @@ +

No companies found for this problem
2264. Largest 3-Same-Digit Number in String

Easy


You are given a string num representing a large integer. An integer is good if it meets the following conditions:

+ +
    +
  • It is a substring of num with length 3.
  • +
  • It consists of only one unique digit.
  • +
+ +

Return the maximum good integer as a string or an empty string "" if no such integer exists.

+ +

Note:

+ +
    +
  • A substring is a contiguous sequence of characters within a string.
  • +
  • There may be leading zeroes in num or a good integer.
  • +
+ +

 

+

Example 1:

+ +
Input: num = "6777133339"
+Output: "777"
+Explanation: There are two distinct good integers: "777" and "333".
+"777" is the largest, so we return "777".
+
+ +

Example 2:

+ +
Input: num = "2300019"
+Output: "000"
+Explanation: "000" is the only good integer.
+
+ +

Example 3:

+ +
Input: num = "42352338"
+Output: ""
+Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= num.length <= 1000
  • +
  • num only consists of digits.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp new file mode 100644 index 00000000..19d7070b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp @@ -0,0 +1,47 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int ans = 0; + + pair helper(TreeNode* root) + { + if(!root) + { + return {0, 0}; + } + + auto left = helper(root->left); + auto right = helper(root->right); + + int sum = left.first + right.first + root->val; + int cnt = left.second + right.second + 1; + + // cout<val<<" " <val) + { + ++ans; + } + + return {sum, cnt}; + } + + int averageOfSubtree(TreeNode* root) { + + helper(root); + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md new file mode 100644 index 00000000..42ee90a0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md @@ -0,0 +1,37 @@ +

No companies found for this problem
2265. Count Nodes Equal to Average of Subtree

Medium


Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.

+ +

Note:

+ +
    +
  • The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
  • +
  • A subtree of root is a tree consisting of root and all of its descendants.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,8,5,0,1,null,6]
+Output: 5
+Explanation: 
+For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
+For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
+For the node with value 0: The average of its subtree is 0 / 1 = 0.
+For the node with value 1: The average of its subtree is 1 / 1 = 1.
+For the node with value 6: The average of its subtree is 6 / 1 = 6.
+
+ +

Example 2:

+ +
Input: root = [1]
+Output: 1
+Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp new file mode 100644 index 00000000..80607765 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int garbageCollection(vector& garbage, vector& travel) { + + int n = garbage.size(); + + int totalTime = 0; + + unordered_map mp; + + for(int i = 0; i < n; ++i) + { + for(auto& gar : garbage[i]) + ++mp[gar]; + } + + for(int i = 0; i < n; ++i) + { + int p = 0, g = 0, m = 0; + + for(auto& gar : garbage[i]) + { + if(gar == 'P') + ++p; + else if(gar == 'G') + ++g; + else + ++m; + } + + totalTime += (p+g+m); + + if(i-1 >= 0) + { + if(mp['P']) totalTime += travel[i-1]; + if(mp['G']) totalTime += travel[i-1]; + if(mp['M']) totalTime += travel[i-1]; + } + + mp['P'] -= p; + mp['M'] -= m; + mp['G'] -= g; + + if(mp['P'] == 0) + mp.erase('P'); + if(mp['G'] == 0) + mp.erase('G'); + if(mp['M'] == 0) + mp.erase('M'); + + } + + return totalTime; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md new file mode 100644 index 00000000..b4706728 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md @@ -0,0 +1,56 @@ +

No companies found for this problem
2391. Minimum Amount of Time to Collect Garbage

Medium


You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the ith house. garbage[i] consists only of the characters 'M', 'P' and 'G' representing one unit of metal, paper and glass garbage respectively. Picking up one unit of any type of garbage takes 1 minute.

+ +

You are also given a 0-indexed integer array travel where travel[i] is the number of minutes needed to go from house i to house i + 1.

+ +

There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order; however, they do not need to visit every house.

+ +

Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.

+ +

Return the minimum number of minutes needed to pick up all the garbage.

+ +

 

+

Example 1:

+ +
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
+Output: 21
+Explanation:
+The paper garbage truck:
+1. Travels from house 0 to house 1
+2. Collects the paper garbage at house 1
+3. Travels from house 1 to house 2
+4. Collects the paper garbage at house 2
+Altogether, it takes 8 minutes to pick up all the paper garbage.
+The glass garbage truck:
+1. Collects the glass garbage at house 0
+2. Travels from house 0 to house 1
+3. Travels from house 1 to house 2
+4. Collects the glass garbage at house 2
+5. Travels from house 2 to house 3
+6. Collects the glass garbage at house 3
+Altogether, it takes 13 minutes to pick up all the glass garbage.
+Since there is no metal garbage, we do not need to consider the metal garbage truck.
+Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
+
+ +

Example 2:

+ +
Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
+Output: 37
+Explanation:
+The metal garbage truck takes 7 minutes to pick up all the metal garbage.
+The paper garbage truck takes 15 minutes to pick up all the paper garbage.
+The glass garbage truck takes 15 minutes to pick up all the glass garbage.
+It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= garbage.length <= 105
  • +
  • garbage[i] consists of only the letters 'M', 'P', and 'G'.
  • +
  • 1 <= garbage[i].length <= 10
  • +
  • travel.length == garbage.length - 1
  • +
  • 1 <= travel[i] <= 100
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp new file mode 100644 index 00000000..1151b9bd --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector findArray(vector& pref) { + + int n = pref.size(); + + vector ans(n); + + ans[0] = pref[0]; + + for(int i = 1; i < n; ++i) + ans[i] = pref[i] ^ pref[i-1]; + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md new file mode 100644 index 00000000..a6752d89 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md @@ -0,0 +1,38 @@ +

No companies found for this problem
2433. Find The Original Array of Prefix Xor

Medium


You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

+ +
    +
  • pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
  • +
+ +

Note that ^ denotes the bitwise-xor operation.

+ +

It can be proven that the answer is unique.

+ +

 

+

Example 1:

+ +
Input: pref = [5,2,0,3,1]
+Output: [5,7,2,3,2]
+Explanation: From the array [5,7,2,3,2] we have the following:
+- pref[0] = 5.
+- pref[1] = 5 ^ 7 = 2.
+- pref[2] = 5 ^ 7 ^ 2 = 0.
+- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
+- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
+
+ +

Example 2:

+ +
Input: pref = [13]
+Output: [13]
+Explanation: We have pref[0] = arr[0] = 13.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= pref.length <= 105
  • +
  • 0 <= pref[i] <= 106
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp new file mode 100644 index 00000000..b46998f3 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp @@ -0,0 +1,76 @@ +class Graph { +public: + + vector > adj[105]; + + int dijistras(int src, int dest) + { + vector dist(105, INT_MAX); + + priority_queue, vector>, greater> > pq; + + dist[src] = 0; + + pq.push({dist[src], src}); + + while(!pq.empty()) + { + pair curr = pq.top(); + pq.pop(); + + int distance = curr.first; + int node = curr.second; + + if(node == dest) + { + return distance; + } + + for(auto& child : adj[node]) + { + if(distance + child.second < dist[child.first]) + { + dist[child.first] = distance + child.second; + + pq.push({dist[child.first], child.first}); + } + } + } + return -1; + } + + Graph(int n, vector>& edges) { + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + } + } + + void addEdge(vector edge) { + + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + + } + + int shortestPath(int node1, int node2) { + + return dijistras(node1, node2); + + } +}; + +/** + * Your Graph object will be instantiated and called as such: + * Graph* obj = new Graph(n, edges); + * obj->addEdge(edge); + * int param_2 = obj->shortestPath(node1,node2); + */ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md new file mode 100644 index 00000000..09d240cb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md @@ -0,0 +1,41 @@ +

No companies found for this problem
2642. Design Graph With Shortest Path Calculator

Hard


There is a directed weighted graph that consists of n nodes numbered from 0 to n - 1. The edges of the graph are initially represented by the given array edges where edges[i] = [fromi, toi, edgeCosti] meaning that there is an edge from fromi to toi with the cost edgeCosti.

+ +

Implement the Graph class:

+ +
    +
  • Graph(int n, int[][] edges) initializes the object with n nodes and the given edges.
  • +
  • addEdge(int[] edge) adds an edge to the list of edges where edge = [from, to, edgeCost]. It is guaranteed that there is no edge between the two nodes before adding this one.
  • +
  • int shortestPath(int node1, int node2) returns the minimum cost of a path from node1 to node2. If no path exists, return -1. The cost of a path is the sum of the costs of the edges in the path.
  • +
+ +

 

+

Example 1:

+ +
Input
+["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
+[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
+Output
+[null, 6, -1, null, 6]
+
+Explanation
+Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
+g.shortestPath(3, 2); // return 6. The shortest path from 3 to 2 in the first diagram above is 3 -> 0 -> 1 -> 2 with a total cost of 3 + 2 + 1 = 6.
+g.shortestPath(0, 3); // return -1. There is no path from 0 to 3.
+g.addEdge([1, 3, 4]); // We add an edge from node 1 to node 3, and we get the second diagram above.
+g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -> 1 -> 3 with a total cost of 2 + 4 = 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= edges.length <= n * (n - 1)
  • +
  • edges[i].length == edge.length == 3
  • +
  • 0 <= fromi, toi, from, to, node1, node2 <= n - 1
  • +
  • 1 <= edgeCosti, edgeCost <= 106
  • +
  • There are no repeated edges and no self-loops in the graph at any point.
  • +
  • At most 100 calls will be made for addEdge.
  • +
  • At most 100 calls will be made for shortestPath.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md new file mode 100644 index 00000000..b6396262 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md @@ -0,0 +1,34 @@ +

No companies found for this problem
2742. Painting the Walls

Hard


You are given two 0-indexed integer arrays, cost and time, of size n representing the costs and the time taken to paint n different walls respectively. There are two painters available:

+ +
    +
  • A paid painter that paints the ith wall in time[i] units of time and takes cost[i] units of money.
  • +
  • A free painter that paints any wall in 1 unit of time at a cost of 0. But the free painter can only be used if the paid painter is already occupied.
  • +
+ +

Return the minimum amount of money required to paint the n walls.

+ +

 

+

Example 1:

+ +
Input: cost = [1,2,3,2], time = [1,2,3,2]
+Output: 3
+Explanation: The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.
+
+ +

Example 2:

+ +
Input: cost = [2,3,4,2], time = [1,1,1,1]
+Output: 4
+Explanation: The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= cost.length <= 500
  • +
  • cost.length == time.length
  • +
  • 1 <= cost[i] <= 106
  • +
  • 1 <= time[i] <= 500
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp new file mode 100644 index 00000000..f6265c5a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string sortVowels(string s) { + + string vowels; + + auto isVowel = [&](char ch) + { + return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U'); + }; + + for(auto& ch : s) + { + if(isVowel(ch)) + vowels += ch; + } + + sort(vowels.begin(), vowels.end()); + + int i = 0; + + for(auto& ch : s) + { + if(isVowel(ch)) + ch = vowels[i++]; + } + + return s; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md new file mode 100644 index 00000000..c4634ae8 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md @@ -0,0 +1,34 @@ +

No companies found for this problem
2785. Sort Vowels in a String

Medium


Given a 0-indexed string s, permute s to get a new string t such that:

+ +
    +
  • All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
  • +
  • The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
  • +
+ +

Return the resulting string.

+ +

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

+ +

 

+

Example 1:

+ +
Input: s = "lEetcOde"
+Output: "lEOtcede"
+Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
+
+ +

Example 2:

+ +
Input: s = "lYmpH"
+Output: "lYmpH"
+Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists only of letters of the English alphabet in uppercase and lowercase.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp new file mode 100644 index 00000000..e87e582f --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { + + int xDiff = abs(sx - fx); + int yDiff = abs(sy - fy); + + int diagonalDist = min(xDiff, yDiff); + + if(sx == fx and sy == fy and t == 1) + return false; + + int minDist = diagonalDist + abs(xDiff - yDiff); + + return minDist <= t; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md new file mode 100644 index 00000000..13a5881b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md @@ -0,0 +1,31 @@ +

No companies found for this problem
2849. Determine if a Cell Is Reachable at a Given Time

Medium


You are given four integers sx, sy, fx, fy, and a non-negative integer t.

+ +

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

+ +

Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.

+ +

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

+ +

 

+

Example 1:

+ +
Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
+Output: true
+Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. 
+
+ +

Example 2:

+ +
Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
+Output: false
+Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= sx, sy, fx, fy <= 109
  • +
  • 0 <= t <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp new file mode 100644 index 00000000..878be0de --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp @@ -0,0 +1,84 @@ +class Solution { +public: + vector countVisitedNodes(vector& edges) { + + int n = edges.size(); + + vector indegree(n, 0); + + for(int i = 0; i < n; ++i) + { + ++indegree[edges[i]]; + } + + vector visited(n, false); + + queue q; + stack st; + + for(int i = 0; i < n; ++i) + { + if(indegree[i] == 0) + { + q.push(i); + } + } + + while(!q.empty()) + { + int curr = q.front(); + visited[curr] = true; + st.push(curr); + q.pop(); + + if(--indegree[edges[curr]] == 0) + { + q.push(edges[curr]); + } + } + + vector res(n, 0); + + function dfs = [&](int sv) + { + int length = 0; + + for(int i = sv; !visited[i]; i = edges[i]) + { + visited[i] = true; + ++length; + } + + res[sv] = length; + + for(int i = edges[sv]; i != sv; i = edges[i]) + { + res[i] = length; + } + }; + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + { + dfs(i); + } + } + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + dfs(i); + } + + while(!st.empty()) + { + int curr = st.top(); + res[curr] = res[edges[curr]] + 1; + st.pop(); + } + + return res; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md new file mode 100644 index 00000000..a4dcde69 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md @@ -0,0 +1,41 @@ +

No companies found for this problem
2876. Count Visited Nodes in a Directed Graph

Hard


There is a directed graph consisting of n nodes numbered from 0 to n - 1 and n directed edges.

+ +

You are given a 0-indexed array edges where edges[i] indicates that there is an edge from node i to node edges[i].

+ +

Consider the following process on the graph:

+ +
    +
  • You start from a node x and keep visiting other nodes through edges until you reach a node that you have already visited before on this same process.
  • +
+ +

Return an array answer where answer[i] is the number of different nodes that you will visit if you perform the process starting from node i.

+ +

 

+

Example 1:

+ +
Input: edges = [1,2,0,0]
+Output: [3,3,3,4]
+Explanation: We perform the process starting from each node in the following way:
+- Starting from node 0, we visit the nodes 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 3.
+- Starting from node 1, we visit the nodes 1 -> 2 -> 0 -> 1. The number of different nodes we visit is 3.
+- Starting from node 2, we visit the nodes 2 -> 0 -> 1 -> 2. The number of different nodes we visit is 3.
+- Starting from node 3, we visit the nodes 3 -> 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 4.
+
+ +

Example 2:

+ +
Input: edges = [1,2,3,4,0]
+Output: [5,5,5,5,5]
+Explanation: Starting from any node we can visit every node in the graph in the process.
+
+ +

 

+

Constraints:

+ +
    +
  • n == edges.length
  • +
  • 2 <= n <= 105
  • +
  • 0 <= edges[i] <= n - 1
  • +
  • edges[i] != i
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp new file mode 100644 index 00000000..3bde932d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + long long helper(int idx, int n, vector& nums, int k, vector& dp) + { + if(idx >= n) + return 0; + + long long ans = LONG_MAX; + + if(dp[idx] != -1) + return dp[idx]; + + for(int i = 0; i < 3 and idx + i < n; ++i) + { + long long take = (nums[idx] < k ? k - nums[idx] : 0) + helper(idx+i+1, n, nums, k , dp); + + ans = min(ans, take); + } + + return dp[idx] = ans; + } + + long long minIncrementOperations(vector& nums, int k) { + + int n = nums.size(); + + vector dp(n+1, -1); + + long long op1 = helper(0, n, nums, k, dp); + long long op2 = helper(1, n, nums, k, dp); + long long op3 = helper(2, n, nums, k, dp); + + return min({op1, op2, op3}); + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md new file mode 100644 index 00000000..c5ad89b8 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md @@ -0,0 +1,60 @@ +

No companies found for this problem
2919. Minimum Increment Operations to Make Array Beautiful

Medium


You are given a 0-indexed integer array nums having length n, and an integer k.

+ +

You can perform the following increment operation any number of times (including zero):

+ +
    +
  • Choose an index i in the range [0, n - 1], and increase nums[i] by 1.
  • +
+ +

An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.

+ +

Return an integer denoting the minimum number of increment operations needed to make nums beautiful.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,0,0,2], k = 4
+Output: 3
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
+The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
+In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
+Hence, the answer is 3.
+
+ +

Example 2:

+ +
Input: nums = [0,1,3,3], k = 5
+Output: 2
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
+The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
+In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
+Hence, the answer is 2.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2], k = 1
+Output: 0
+Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
+The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
+Hence, the answer is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n == nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= k <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp new file mode 100644 index 00000000..53c005cb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp @@ -0,0 +1,42 @@ +#define ll long long + +class Solution { +public: + long long beautifulSubstrings(string s, int k) { + + int n = s.size(); + + function isVowel = [&](char ch) + { + return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'); + }; + + unordered_map > mp; + + ll ans = 0; + + ++mp[0][0]; + + ll pref = 0, vow = 0; + + for(int i = 0; i < n; ++i) + { + if(isVowel(s[i])) + ++pref, ++vow; + else + --pref; + + for(auto& [f, cnt] : mp[pref]) + { + ll curr = (vow%k) - f; + if((curr * curr) % k == 0) + ans += cnt; + } + + ++mp[pref][vow%k]; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md new file mode 100644 index 00000000..f8de40bc --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md @@ -0,0 +1,59 @@ +

No companies found for this problem
2949. Count Beautiful Substrings II

Hard


You are given a string s and a positive integer k.

+ +

Let vowels and consonants be the number of vowels and consonants in a string.

+ +

A string is beautiful if:

+ +
    +
  • vowels == consonants.
  • +
  • (vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
  • +
+ +

Return the number of non-empty beautiful substrings in the given string s.

+ +

A substring is a contiguous sequence of characters in a string.

+ +

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

+ +

Consonant letters in English are every letter except vowels.

+ +

 

+

Example 1:

+ +
Input: s = "baeyh", k = 2
+Output: 2
+Explanation: There are 2 beautiful substrings in the given string.
+- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
+You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
+- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
+You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
+It can be shown that there are only 2 beautiful substrings in the given string.
+
+ +

Example 2:

+ +
Input: s = "abba", k = 1
+Output: 3
+Explanation: There are 3 beautiful substrings in the given string.
+- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
+- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
+- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
+It can be shown that there are only 3 beautiful substrings in the given string.
+
+ +

Example 3:

+ +
Input: s = "bcdf", k = 1
+Output: 0
+Explanation: There are no beautiful substrings in the given string.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • 1 <= k <= 1000
  • +
  • s consists of only English lowercase letters.
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp new file mode 100644 index 00000000..16bba9d0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + + int binarySearchFirst(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int first = - 1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + first = mid; + end = mid-1; + } + else if(nums[mid] < target) + { + start = mid+1; + } + else + { + end = mid - 1; + } + } + + return first; + } + + int binarySearchEnd(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int last = -1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + last = mid; + start = mid+1; + } + else if(nums[mid] > target) + { + end = mid-1; + } + else + { + start = mid+1; + } + } + + return last; + } + + vector searchRange(vector& nums, int target) { + + int n = nums.size(); + + int first = -1, last = -1; + + first = binarySearchFirst(nums, target); + last = binarySearchEnd(nums, target); + + return {first, last}; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md new file mode 100644 index 00000000..30176ded --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md @@ -0,0 +1,27 @@ +

No companies found for this problem
34. Find First and Last Position of Element in Sorted Array

Medium


Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

+ +

If target is not found in the array, return [-1, -1].

+ +

You must write an algorithm with O(log n) runtime complexity.

+ +

 

+

Example 1:

+
Input: nums = [5,7,7,8,8,10], target = 8
+Output: [3,4]
+

Example 2:

+
Input: nums = [5,7,7,8,8,10], target = 6
+Output: [-1,-1]
+

Example 3:

+
Input: nums = [], target = 0
+Output: [-1,-1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
  • nums is a non-decreasing array.
  • +
  • -109 <= target <= 109
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp new file mode 100644 index 00000000..c9a68d06 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + int poorPigs(int buckets, int minutesToDie, int minutesToTest) { + + // trial^pigs = buckets + + return ceil(log2(buckets) / log2(minutesToTest / minutesToDie + 1)); + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md new file mode 100644 index 00000000..5b73b537 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md @@ -0,0 +1,48 @@ +

No companies found for this problem
458. Poor Pigs

Hard


There are buckets buckets of liquid, where exactly one of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have minutesToTest minutes to determine which bucket is poisonous.

+ +

You can feed the pigs according to these steps:

+ +
    +
  1. Choose some live pigs to feed.
  2. +
  3. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.
  4. +
  5. Wait for minutesToDie minutes. You may not feed any other pigs during this time.
  6. +
  7. After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
  8. +
  9. Repeat this process until you run out of time.
  10. +
+ +

Given buckets, minutesToDie, and minutesToTest, return the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.

+ +

 

+

Example 1:

+ +
Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
+Output: 2
+Explanation: We can determine the poisonous bucket as follows:
+At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3.
+At time 15, there are 4 possible outcomes:
+- If only the first pig dies, then bucket 1 must be poisonous.
+- If only the second pig dies, then bucket 3 must be poisonous.
+- If both pigs die, then bucket 2 must be poisonous.
+- If neither pig dies, then bucket 4 must be poisonous.
+
+ +

Example 2:

+ +
Input: buckets = 4, minutesToDie = 15, minutesToTest = 30
+Output: 2
+Explanation: We can determine the poisonous bucket as follows:
+At time 0, feed the first pig bucket 1, and feed the second pig bucket 2.
+At time 15, there are 2 possible outcomes:
+- If either pig dies, then the poisonous bucket is the one it was fed.
+- If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4.
+At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= buckets <= 1000
  • +
  • 1 <= minutesToDie <= minutesToTest <= 100
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp new file mode 100644 index 00000000..e9971d98 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp @@ -0,0 +1,44 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, unordered_map& mp, int& maxi) + { + if(root) + { + helper(root->left, mp, maxi); + ++mp[root->val]; + maxi = max(maxi, mp[root->val]); + helper(root->right, mp, maxi); + } + } + + vector findMode(TreeNode* root) { + + unordered_map mp; + + int maxi = 0; + + helper(root, mp, maxi); + + vector ans; + + for(auto& itr : mp) + { + if(itr.second == maxi) + ans.push_back(itr.first); + } + + return ans; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md new file mode 100644 index 00000000..184716c6 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md @@ -0,0 +1,35 @@ +

No companies found for this problem
501. Find Mode in Binary Search Tree

Easy


Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

+ +

If the tree has more than one mode, return them in any order.

+ +

Assume a BST is defined as follows:

+ +
    +
  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • +
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • +
  • Both the left and right subtrees must also be binary search trees.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [1,null,2,2]
+Output: [2]
+
+ +

Example 2:

+ +
Input: root = [0]
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -105 <= Node.val <= 105
  • +
+ +

 

+Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp new file mode 100644 index 00000000..fd1cd0af --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + int numBusesToDestination(vector>& routes, int source, int target) { + + if(source == target) + return 0; + + unordered_map> adj; + + for(int route = 0; route < routes.size(); ++route) + { + for(auto& stop : routes[route]) + { + adj[stop].push_back(route); + } + } + + queue q; + + unordered_set visited; + + for(auto& route : adj[source]) + { + q.push(route); + visited.insert(route); + } + + int bus = 1; + + while(!q.empty()) + { + int size = q.size(); + + while(size--) + { + int route = q.front(); + q.pop(); + + for(auto& stop : routes[route]) + { + if(stop == target) + return bus; + + for(auto& nextRoute : adj[stop]) + { + if(visited.count(nextRoute)) + continue; + q.push(nextRoute); + visited.insert(nextRoute); + } + } + } + ++bus; + } + + return -1; + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md new file mode 100644 index 00000000..6d9ea192 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md @@ -0,0 +1,36 @@ +

No companies found for this problem
815. Bus Routes

Hard


You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.

+ +
    +
  • For example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.
  • +
+ +

You will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.

+ +

Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.

+ +

 

+

Example 1:

+ +
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
+Output: 2
+Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
+
+ +

Example 2:

+ +
Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
+Output: -1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= routes.length <= 500.
  • +
  • 1 <= routes[i].length <= 105
  • +
  • All the values of routes[i] are unique.
  • +
  • sum(routes[i].length) <= 105
  • +
  • 0 <= routes[i][j] < 106
  • +
  • 0 <= source, target < 106
  • +
+
\ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp new file mode 100644 index 00000000..6455ee55 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector sortArrayByParity(vector& nums) { + + int n = nums.size(); + + int start = 0, end = n-1; + + while(start < end) + { + if(nums[start] & 1) + { + swap(nums[start], nums[end--]); + } + else + ++start; + } + + return nums; + + } +}; \ No newline at end of file diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1002-find-common-characters/1002-find-common-characters.cpp b/1002-find-common-characters/1002-find-common-characters.cpp new file mode 100644 index 00000000..f2254e0a --- /dev/null +++ b/1002-find-common-characters/1002-find-common-characters.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + vector commonChars(vector& words) { + + int n = words.size(); + + vector freq(26, 0); + + for(int i = 0; i < n; ++i) + { + vector curr(26, 0); + + for(int j = 0; j < words[i].size(); ++j) + ++curr[words[i][j] - 'a']; + + if(i != 0) + { + for(int j = 0; j < 26; ++j) + curr[j] = min(curr[j], freq[j]); + } + + freq = curr; + } + + vector ans; + + for(int i = 0; i < 26; ++i) + { + for(int j = 0; j < freq[i]; ++j) + { + ans.push_back(string(1, 'a' + i)); + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/1002-find-common-characters/NOTES.md b/1002-find-common-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1002-find-common-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1002-find-common-characters/README.md b/1002-find-common-characters/README.md new file mode 100644 index 00000000..54c8be40 --- /dev/null +++ b/1002-find-common-characters/README.md @@ -0,0 +1,19 @@ +

1002. Find Common Characters

Easy


Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.

+ +

 

+

Example 1:

+
Input: words = ["bella","label","roller"]
+Output: ["e","l","l"]
+

Example 2:

+
Input: words = ["cool","lock","cook"]
+Output: ["c","o"]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp b/1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp new file mode 100644 index 00000000..b2d6bfa9 --- /dev/null +++ b/1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int longestOnes(vector& nums, int k) { + + int n = nums.size(); + + int i = 0, j = 0, zeroCnt = 0, ans = 0; + + while(j < n) + { + zeroCnt += (nums[j] == 0 ? 1 : 0); + + while(zeroCnt > k) + { + zeroCnt -= (nums[i] == 0 ? 1 : 0); + ++i; + } + + ans = max(ans, j - i + 1); + + ++j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/1004-max-consecutive-ones-iii/NOTES.md b/1004-max-consecutive-ones-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1004-max-consecutive-ones-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp b/1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp new file mode 100644 index 00000000..170912a6 --- /dev/null +++ b/1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int helper(TreeNode* root, int currMax, int currMin) + { + if(!root) + return currMax - currMin; + currMax = max(currMax,root->val); + currMin = min(currMin,root->val); + + int left = helper(root->left,currMax,currMin); + int right = helper(root->right,currMax,currMin); + + return max(left,right); + } + + + int maxAncestorDiff(TreeNode* root) { + + if(!root) + return 0; + + return helper(root,root->val,root->val); + + + } +}; \ No newline at end of file diff --git a/1026-maximum-difference-between-node-and-ancestor/README.md b/1026-maximum-difference-between-node-and-ancestor/README.md new file mode 100644 index 00000000..67c353c5 --- /dev/null +++ b/1026-maximum-difference-between-node-and-ancestor/README.md @@ -0,0 +1,30 @@ +

1026. Maximum Difference Between Node and Ancestor

Medium


Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.

+ +

A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.

+ +

 

+

Example 1:

+ +
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
+Output: 7
+Explanation: We have various ancestor-node differences, some of which are given below :
+|8 - 3| = 5
+|3 - 7| = 4
+|8 - 1| = 7
+|10 - 13| = 3
+Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
+ +

Example 2:

+ +
Input: root = [1,null,2,null,0,3]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [2, 5000].
  • +
  • 0 <= Node.val <= 105
  • +
+
\ No newline at end of file diff --git a/1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp b/1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp new file mode 100644 index 00000000..fd6e8ffc --- /dev/null +++ b/1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, int& sum) + { + if(!root) + return; + + helper(root->right, sum); + + root->val += sum; + + sum = root->val; + + helper(root-> left, sum); + } + + TreeNode* bstToGst(TreeNode* root) { + + int sum = 0; + + helper(root, sum); + + return root; + + } +}; \ No newline at end of file diff --git a/1038-binary-search-tree-to-greater-sum-tree/README.md b/1038-binary-search-tree-to-greater-sum-tree/README.md new file mode 100644 index 00000000..c503c670 --- /dev/null +++ b/1038-binary-search-tree-to-greater-sum-tree/README.md @@ -0,0 +1,35 @@ +

1038. Binary Search Tree to Greater Sum Tree

Medium


Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

+ +

As a reminder, a binary search tree is a tree that satisfies these constraints:

+ +
    +
  • The left subtree of a node contains only nodes with keys less than the node's key.
  • +
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • +
  • Both the left and right subtrees must also be binary search trees.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
+Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
+
+ +

Example 2:

+ +
Input: root = [0,null,1]
+Output: [1,null,1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 100].
  • +
  • 0 <= Node.val <= 100
  • +
  • All the values in the tree are unique.
  • +
+ +

 

+

Note: This question is the same as 538: https://leetcode.com/problems/convert-bst-to-greater-tree/

+
\ No newline at end of file diff --git a/1051-height-checker/1051-height-checker.cpp b/1051-height-checker/1051-height-checker.cpp new file mode 100644 index 00000000..d54f8363 --- /dev/null +++ b/1051-height-checker/1051-height-checker.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int heightChecker(vector& heights) { + + int n = heights.size(); + + vector copy = heights; + + sort(copy.begin(), copy.end()); + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + cnt += (heights[i] != copy[i]); + } + + return cnt; + + } +}; \ No newline at end of file diff --git a/1051-height-checker/README.md b/1051-height-checker/README.md new file mode 100644 index 00000000..59363b7e --- /dev/null +++ b/1051-height-checker/README.md @@ -0,0 +1,45 @@ +

1051. Height Checker

Easy


A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.

+ +

You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).

+ +

Return the number of indices where heights[i] != expected[i].

+ +

 

+

Example 1:

+ +
Input: heights = [1,1,4,2,1,3]
+Output: 3
+Explanation: 
+heights:  [1,1,4,2,1,3]
+expected: [1,1,1,2,3,4]
+Indices 2, 4, and 5 do not match.
+
+ +

Example 2:

+ +
Input: heights = [5,1,2,3,4]
+Output: 5
+Explanation:
+heights:  [5,1,2,3,4]
+expected: [1,2,3,4,5]
+All indices do not match.
+
+ +

Example 3:

+ +
Input: heights = [1,2,3,4,5]
+Output: 0
+Explanation:
+heights:  [1,2,3,4,5]
+expected: [1,2,3,4,5]
+All indices match.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= heights.length <= 100
  • +
  • 1 <= heights[i] <= 100
  • +
+
\ No newline at end of file diff --git a/1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp b/1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp new file mode 100644 index 00000000..d4f877be --- /dev/null +++ b/1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + + int helper(int idx, bool flag, int n, int minutes, vector& grumpy, vector& customers, vector& pref, vector>& dp) + { + if(idx >= n) + return 0; + + if(dp[idx][flag] != -1) + return dp[idx][flag]; + + int ans = (grumpy[idx] == 0 ? customers[idx] : 0) + helper(idx+1, flag, n, minutes, grumpy, customers, pref, dp); + + int secretMove = 0; + + if(!flag) + { + secretMove = (idx + minutes - 1 < n ? (pref[idx+minutes-1] - (idx-1 >= 0 ? pref[idx-1] : 0)) : pref[n-1] - (idx-1 >= 0 ? pref[idx-1] : 0)) + helper(idx + minutes, 1, n, minutes, grumpy, customers, pref, dp); + + ans = max(ans, secretMove); + } + + return dp[idx][flag] = ans; + } + + int maxSatisfied(vector& customers, vector& grumpy, int minutes) { + + int n = customers.size(); + + vector> dp(n, vector(2, -1)); + + vector pref(n, 0); + + pref[0] = customers[0]; + + for(int i = 1; i < n; ++i) + { + pref[i] = pref[i-1] + customers[i]; + } + + return helper(0, 0, n, minutes, grumpy, customers, pref, dp); + + } +}; \ No newline at end of file diff --git a/1052-grumpy-bookstore-owner/NOTES.md b/1052-grumpy-bookstore-owner/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1052-grumpy-bookstore-owner/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1052-grumpy-bookstore-owner/README.md b/1052-grumpy-bookstore-owner/README.md new file mode 100644 index 00000000..dce57e16 --- /dev/null +++ b/1052-grumpy-bookstore-owner/README.md @@ -0,0 +1,35 @@ +

1052. Grumpy Bookstore Owner

Medium


There is a bookstore owner that has a store open for n minutes. Every minute, some number of customers enter the store. You are given an integer array customers of length n where customers[i] is the number of the customer that enters the store at the start of the ith minute and all those customers leave after the end of that minute.

+ +

On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise.

+ +

When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.

+ +

The bookstore owner knows a secret technique to keep themselves not grumpy for minutes consecutive minutes, but can only use it once.

+ +

Return the maximum number of customers that can be satisfied throughout the day.

+ +

 

+

Example 1:

+ +
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
+Output: 16
+Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. 
+The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
+
+ +

Example 2:

+ +
Input: customers = [1], grumpy = [0], minutes = 1
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • n == customers.length == grumpy.length
  • +
  • 1 <= minutes <= n <= 2 * 104
  • +
  • 0 <= customers[i] <= 1000
  • +
  • grumpy[i] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql new file mode 100644 index 00000000..e4ba918b --- /dev/null +++ b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below + + +select Product.product_name, Sales.year, Sales.price from Sales +inner join Product on +Sales.product_id = Product.product_id; \ No newline at end of file diff --git a/1068-product-sales-analysis-i/NOTES.md b/1068-product-sales-analysis-i/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1068-product-sales-analysis-i/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1068-product-sales-analysis-i/README.md b/1068-product-sales-analysis-i/README.md new file mode 100644 index 00000000..cfcc2de3 --- /dev/null +++ b/1068-product-sales-analysis-i/README.md @@ -0,0 +1,73 @@ +

1068. Product Sales Analysis I

Easy


Table: Sales

+ +
+-------------+-------+
+| Column Name | Type  |
++-------------+-------+
+| sale_id     | int   |
+| product_id  | int   |
+| year        | int   |
+| quantity    | int   |
+| price       | int   |
++-------------+-------+
+(sale_id, year) is the primary key (combination of columns with unique values) of this table.
+product_id is a foreign key (reference column) to Product table.
+Each row of this table shows a sale on the product product_id in a certain year.
+Note that the price is per unit.
+
+ +

 

+ +

Table: Product

+ +
+--------------+---------+
+| Column Name  | Type    |
++--------------+---------+
+| product_id   | int     |
+| product_name | varchar |
++--------------+---------+
+product_id is the primary key (column with unique values) of this table.
+Each row of this table indicates the product name of each product.
+
+ +

 

+ +

Write a solution to report the product_name, year, and price for each sale_id in the Sales table.

+ +

Return the resulting table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Sales table:
++---------+------------+------+----------+-------+
+| sale_id | product_id | year | quantity | price |
++---------+------------+------+----------+-------+ 
+| 1       | 100        | 2008 | 10       | 5000  |
+| 2       | 100        | 2009 | 12       | 5000  |
+| 7       | 200        | 2011 | 15       | 9000  |
++---------+------------+------+----------+-------+
+Product table:
++------------+--------------+
+| product_id | product_name |
++------------+--------------+
+| 100        | Nokia        |
+| 200        | Apple        |
+| 300        | Samsung      |
++------------+--------------+
+Output: 
++--------------+-------+-------+
+| product_name | year  | price |
++--------------+-------+-------+
+| Nokia        | 2008  | 5000  |
+| Nokia        | 2009  | 5000  |
+| Apple        | 2011  | 9000  |
++--------------+-------+-------+
+Explanation: 
+From sale_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008.
+From sale_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009.
+From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
+
+
\ No newline at end of file diff --git a/1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp b/1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp new file mode 100644 index 00000000..1fe999aa --- /dev/null +++ b/1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + int numSubmatrixSumTarget(vector>& matrix, int target) { + + int m = matrix.size(); + int n = matrix[0].size(); + + // prefix sum + + for(int row = 0; row < m; ++row) + { + for(int col = 1; col < n; ++col) + { + matrix[row][col] += matrix[row][col-1]; + } + } + + int count = 0; + + // fix 2 colums + + for(int c1 = 0; c1 < n; ++c1) + { + for(int c2 = c1; c2 < n; ++c2) + { + unordered_map mp; + int sum = 0; + mp.insert({0,1}); + + for(int row = 0; row < m; ++row) + { + sum += matrix[row][c2] - (c1 > 0 ? matrix[row][c1-1] : 0); + count += mp[sum-target]; + + if(mp.find(sum) != mp.end()) + ++mp[sum]; + else + mp[sum] = 1; + } + } + } + + return count; + } +}; \ No newline at end of file diff --git a/1074-number-of-submatrices-that-sum-to-target/NOTES.md b/1074-number-of-submatrices-that-sum-to-target/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1074-number-of-submatrices-that-sum-to-target/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp b/1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp new file mode 100644 index 00000000..b9602471 --- /dev/null +++ b/1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + string smallestSubsequence(string s) { + + int n = s.size(); + + vector lastOccurence(26, -1); + + vector visited(26, false); + + for(int i = 0; i < n; ++i) + lastOccurence[s[i] - 'a'] = i; + + string ans; + + for(int i = 0; i < n; ++i) + { + while(!ans.empty() and !visited[s[i] - 'a'] and s[i] < ans.back() and lastOccurence[ans.back() - 'a'] > i) + { + visited[ans.back() - 'a'] = false; + + ans.pop_back(); + } + + if(!visited[s[i] - 'a']) + { + ans += s[i]; + visited[s[i] - 'a'] = true; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/1081-smallest-subsequence-of-distinct-characters/NOTES.md b/1081-smallest-subsequence-of-distinct-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1081-smallest-subsequence-of-distinct-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1081-smallest-subsequence-of-distinct-characters/README.md b/1081-smallest-subsequence-of-distinct-characters/README.md new file mode 100644 index 00000000..27cd917c --- /dev/null +++ b/1081-smallest-subsequence-of-distinct-characters/README.md @@ -0,0 +1,25 @@ +

1081. Smallest Subsequence of Distinct Characters

Medium


Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

+ +

 

+

Example 1:

+ +
Input: s = "bcabc"
+Output: "abc"
+
+ +

Example 2:

+ +
Input: s = "cbacdcbc"
+Output: "acdb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of lowercase English letters.
  • +
+ +

 

+Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/
\ No newline at end of file diff --git a/1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp b/1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp new file mode 100644 index 00000000..47e9291d --- /dev/null +++ b/1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + string shortestCommonSupersequence(string str1, string str2) { + + int n = str1.size(), m = str2.size(); + + vector> dp(n+1, vector(m+1, 0)); + + for(int i = 1; i <= n; ++i) + { + for(int j = 1; j <= m; ++j) + { + if(str1[i-1] == str2[j-1]) + dp[i][j] = 1 + dp[i-1][j-1]; + else + dp[i][j] = max(dp[i-1][j], dp[i][j-1]); + } + } + + int i = n, j = m; + string ans; + + while(i > 0 and j > 0) + { + if(str1[i-1] == str2[j-1]) + { + ans += str1[i-1]; + --i, --j; + } + else if(dp[i-1][j] > dp[i][j-1]) + { + ans += str1[i-1]; + --i; + } + else + { + ans += str2[j-1]; + --j; + } + } + + while(i > 0) + { + ans += str1[i-1]; + --i; + } + + while(j > 0) + { + ans += str2[j-1]; + --j; + } + + reverse(ans.begin(), ans.end()); + + return ans; + } +}; \ No newline at end of file diff --git a/1092-shortest-common-supersequence/NOTES.md b/1092-shortest-common-supersequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1092-shortest-common-supersequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1092-shortest-common-supersequence/README.md b/1092-shortest-common-supersequence/README.md new file mode 100644 index 00000000..f93f6296 --- /dev/null +++ b/1092-shortest-common-supersequence/README.md @@ -0,0 +1,29 @@ +

1092. Shortest Common Supersequence

Hard


Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.

+ +

A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.

+ +

 

+

Example 1:

+ +
Input: str1 = "abac", str2 = "cab"
+Output: "cabac"
+Explanation: 
+str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
+str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
+The answer provided is the shortest such string that satisfies these properties.
+
+ +

Example 2:

+ +
Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
+Output: "aaaaaaaa"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= str1.length, str2.length <= 1000
  • +
  • str1 and str2 consist of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp b/1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp new file mode 100644 index 00000000..1c7ef1c7 --- /dev/null +++ b/1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp @@ -0,0 +1,33 @@ +class Solution { +private: + int solve(vector> &books, int i, int remShelfWidth, int maxShelfHeight, + int shelfWidth, vector> &dp) { + + if(i == books.size()) { + return maxShelfHeight; + } + + if(dp[i][remShelfWidth] != -1) + return dp[i][remShelfWidth]; + + int currShelf = INT_MAX, nextShelf = INT_MAX; + + int bookWidth = books[i][0]; + int bookHeight = books[i][1]; + + if(bookWidth <= remShelfWidth) { + currShelf = solve(books, i + 1, remShelfWidth - bookWidth, + max(maxShelfHeight, bookHeight), shelfWidth, dp); + } + + nextShelf = maxShelfHeight + solve(books, i + 1, shelfWidth - bookWidth, + bookHeight, shelfWidth, dp); + + return dp[i][remShelfWidth] = min(currShelf, nextShelf); + } +public: + int minHeightShelves(vector>& books, int shelfWidth) { + vector> dp(1001, vector (shelfWidth + 1, -1)); + return solve(books, 0, shelfWidth, 0, shelfWidth, dp); + } +}; \ No newline at end of file diff --git a/1105-filling-bookcase-shelves/NOTES.md b/1105-filling-bookcase-shelves/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1105-filling-bookcase-shelves/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp b/1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp new file mode 100644 index 00000000..b0d40593 --- /dev/null +++ b/1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool set[1001] = {}; + void dfs(TreeNode* &root, bool flag, vector& res){ + if (root == nullptr) return; + dfs(root->left, set[root->val], res); + dfs(root->right, set[root->val], res); + if (!set[root->val] && flag) res.push_back(root); + if (set[root->val]) root = nullptr; + } +public: + vector delNodes(TreeNode* root, vector& to_delete) { + vector res; + for (int num : to_delete) + set[num] = true; + dfs(root, true, res); + return res; + } + +}; \ No newline at end of file diff --git a/1110-delete-nodes-and-return-forest/NOTES.md b/1110-delete-nodes-and-return-forest/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1110-delete-nodes-and-return-forest/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1110-delete-nodes-and-return-forest/README.md b/1110-delete-nodes-and-return-forest/README.md new file mode 100644 index 00000000..d613487a --- /dev/null +++ b/1110-delete-nodes-and-return-forest/README.md @@ -0,0 +1,29 @@ +

1110. Delete Nodes And Return Forest

Medium


Given the root of a binary tree, each node in the tree has a distinct value.

+ +

After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).

+ +

Return the roots of the trees in the remaining forest. You may return the result in any order.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
+Output: [[1,2,null,4],[6],[7]]
+
+ +

Example 2:

+ +
Input: root = [1,2,4,null,3], to_delete = [3]
+Output: [[1,2,4]]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the given tree is at most 1000.
  • +
  • Each node has a distinct value between 1 and 1000.
  • +
  • to_delete.length <= 1000
  • +
  • to_delete contains distinct values between 1 and 1000.
  • +
+
\ No newline at end of file diff --git a/1122-relative-sort-array/1122-relative-sort-array.cpp b/1122-relative-sort-array/1122-relative-sort-array.cpp new file mode 100644 index 00000000..e9aae6f3 --- /dev/null +++ b/1122-relative-sort-array/1122-relative-sort-array.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector relativeSortArray(vector& arr1, vector& arr2) { + + map mp; + + for(auto& ele : arr1) + ++mp[ele]; + + int j = 0; + + for(int i = 0; i < arr2.size(); ++i) + { + for(int k = 0; k < mp[arr2[i]]; ++k) + { + arr1[j++] = arr2[i]; + } + mp.erase(arr2[i]); + } + + for(auto&[f, e] : mp) + { + for(int k = 0; k < e; ++k) + arr1[j++] = f; + } + + return arr1; + } +}; \ No newline at end of file diff --git a/1122-relative-sort-array/NOTES.md b/1122-relative-sort-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1122-relative-sort-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1122-relative-sort-array/README.md b/1122-relative-sort-array/README.md new file mode 100644 index 00000000..fabd6b8c --- /dev/null +++ b/1122-relative-sort-array/README.md @@ -0,0 +1,27 @@ +

1122. Relative Sort Array

Easy


Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

+ +

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.

+ +

 

+

Example 1:

+ +
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
+Output: [2,2,2,1,4,3,3,9,6,7,19]
+
+ +

Example 2:

+ +
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
+Output: [22,28,8,6,17,44]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr1.length, arr2.length <= 1000
  • +
  • 0 <= arr1[i], arr2[i] <= 1000
  • +
  • All the elements of arr2 are distinct.
  • +
  • Each arr2[i] is in arr1.
  • +
+
\ No newline at end of file diff --git a/1140-stone-game-ii/NOTES.md b/1140-stone-game-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1140-stone-game-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1143-longest-common-subsequence/NOTES.md b/1143-longest-common-subsequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1143-longest-common-subsequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1148-article-views-i/1148-article-views-i.sql b/1148-article-views-i/1148-article-views-i.sql new file mode 100644 index 00000000..f6d15e95 --- /dev/null +++ b/1148-article-views-i/1148-article-views-i.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select distinct(author_id) as id from Views where author_id = viewer_id order by author_id asc; \ No newline at end of file diff --git a/1148-article-views-i/NOTES.md b/1148-article-views-i/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1148-article-views-i/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1148-article-views-i/README.md b/1148-article-views-i/README.md new file mode 100644 index 00000000..fa2d7609 --- /dev/null +++ b/1148-article-views-i/README.md @@ -0,0 +1,48 @@ +

1148. Article Views I

Easy


Table: Views

+ +
+---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| article_id    | int     |
+| author_id     | int     |
+| viewer_id     | int     |
+| view_date     | date    |
++---------------+---------+
+There is no primary key (column with unique values) for this table, the table may have duplicate rows.
+Each row of this table indicates that some viewer viewed an article (written by some author) on some date. 
+Note that equal author_id and viewer_id indicate the same person.
+
+ +

 

+ +

Write a solution to find all the authors that viewed at least one of their own articles.

+ +

Return the result table sorted by id in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Views table:
++------------+-----------+-----------+------------+
+| article_id | author_id | viewer_id | view_date  |
++------------+-----------+-----------+------------+
+| 1          | 3         | 5         | 2019-08-01 |
+| 1          | 3         | 6         | 2019-08-02 |
+| 2          | 7         | 7         | 2019-08-01 |
+| 2          | 7         | 6         | 2019-08-02 |
+| 4          | 7         | 1         | 2019-07-22 |
+| 3          | 4         | 4         | 2019-07-21 |
+| 3          | 4         | 4         | 2019-07-21 |
++------------+-----------+-----------+------------+
+Output: 
++------+
+| id   |
++------+
+| 4    |
+| 7    |
++------+
+
+
\ No newline at end of file diff --git a/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp new file mode 100644 index 00000000..af9c24b2 --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int numRollsToTarget(int n, int k, int target) { + + const int mod = 1e9+7; + + vector> dp(n+1, vector(target+1, 0)); + + dp[0][0] = 1; + + for(int i = 1; i <= n; ++i) + { + for(int j = 1; j <= target; ++j) + { + for(int f = 1; f <= k; ++f) + { + if(j - f >= 0) + dp[i][j] = (dp[i][j] + dp[i-1][j-f]) % mod; + } + } + } + + return dp[n][target]; + + } +}; \ No newline at end of file diff --git a/1155-number-of-dice-rolls-with-target-sum/NOTES.md b/1155-number-of-dice-rolls-with-target-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1155-number-of-dice-rolls-with-target-sum/README.md b/1155-number-of-dice-rolls-with-target-sum/README.md new file mode 100644 index 00000000..19dd039a --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/README.md @@ -0,0 +1,36 @@ +

1155. Number of Dice Rolls With Target Sum

Medium


You have n dice, and each die has k faces numbered from 1 to k.

+ +

Given three integers n, k, and target, return the number of possible ways (out of the kn total ways) to roll the dice, so the sum of the face-up numbers equals target. Since the answer may be too large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 1, k = 6, target = 3
+Output: 1
+Explanation: You throw one die with 6 faces.
+There is only one way to get a sum of 3.
+
+ +

Example 2:

+ +
Input: n = 2, k = 6, target = 7
+Output: 6
+Explanation: You throw two dice, each with 6 faces.
+There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
+
+ +

Example 3:

+ +
Input: n = 30, k = 30, target = 500
+Output: 222616187
+Explanation: The answer must be returned modulo 109 + 7.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n, k <= 30
  • +
  • 1 <= target <= 1000
  • +
+
\ No newline at end of file diff --git a/1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp b/1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp new file mode 100644 index 00000000..1b331ed1 --- /dev/null +++ b/1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int countCharacters(vector& words, string chars) { + + int counter = 0; + + unordered_map mp; + + for(auto& ch : chars) + ++mp[ch]; + + for(auto& word : words) + { + bool canFormed = true; + + unordered_map freq; + + for(auto& ch : word) + { + ++freq[ch]; + } + + for(auto& ch : word) + { + if(freq[ch] > mp[ch]) + { + canFormed = false; + break; + } + } + + if(canFormed) + counter += word.size(); + } + + return counter; + + } +}; \ No newline at end of file diff --git a/1160-find-words-that-can-be-formed-by-characters/NOTES.md b/1160-find-words-that-can-be-formed-by-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1160-find-words-that-can-be-formed-by-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1160-find-words-that-can-be-formed-by-characters/README.md b/1160-find-words-that-can-be-formed-by-characters/README.md new file mode 100644 index 00000000..62067b24 --- /dev/null +++ b/1160-find-words-that-can-be-formed-by-characters/README.md @@ -0,0 +1,30 @@ +

1160. Find Words That Can Be Formed by Characters

Easy


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

+ +

A string is good if it can be formed by characters from chars (each character can only be used once).

+ +

Return the sum of lengths of all good strings in words.

+ +

 

+

Example 1:

+ +
Input: words = ["cat","bt","hat","tree"], chars = "atach"
+Output: 6
+Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
+
+ +

Example 2:

+ +
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
+Output: 10
+Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length, chars.length <= 100
  • +
  • words[i] and chars consist of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp new file mode 100644 index 00000000..40d9fa70 --- /dev/null +++ b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp @@ -0,0 +1,41 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeZeroSumSublists(ListNode* head) { + ListNode *dummynode=new ListNode(0),*curr=dummynode; + dummynode->next=head; + map mp; + int sum=0; + while(curr) + { + sum+=curr->val; + if(mp.count(sum)) + { + curr=mp[sum]->next; + int p=sum+curr->val; + while(p!=sum) + { + mp.erase(p); + curr=curr->next; + p+=curr->val; + } + mp[sum]->next=curr->next; + } + else + { + mp[sum]=curr; + } + curr=curr->next; + } + return dummynode->next; + } +}; \ No newline at end of file diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md new file mode 100644 index 00000000..a851823f --- /dev/null +++ b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md @@ -0,0 +1,34 @@ +

1171. Remove Zero Sum Consecutive Nodes from Linked List

Medium


Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.

+ +

After doing so, return the head of the final linked list.  You may return any such answer.

+ +

 

+

(Note that in the examples below, all sequences are serializations of ListNode objects.)

+ +

Example 1:

+ +
Input: head = [1,2,-3,3,1]
+Output: [3,1]
+Note: The answer [1,2,1] would also be accepted.
+
+ +

Example 2:

+ +
Input: head = [1,2,3,-3,4]
+Output: [1,2,4]
+
+ +

Example 3:

+ +
Input: head = [1,2,3,-3,-2]
+Output: [1]
+
+ +

 

+

Constraints:

+ +
    +
  • The given linked list will contain between 1 and 1000 nodes.
  • +
  • Each node in the linked list has -1000 <= node.val <= 1000.
  • +
+
\ No newline at end of file diff --git a/1207-unique-number-of-occurrences/NOTES.md b/1207-unique-number-of-occurrences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1207-unique-number-of-occurrences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp new file mode 100644 index 00000000..430101aa --- /dev/null +++ b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int equalSubstring(string s, string t, int maxCost) { + + int n = s.size(); + + int i = 0, j = 0, ans = 0; + + while(j < n) + { + maxCost -= abs(s[j] - t[j]); + + if(maxCost < 0) + { + maxCost += abs(s[i] - t[i]); + ++i; + } + + ans = max(ans, j - i + 1); + + ++j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/1208-get-equal-substrings-within-budget/NOTES.md b/1208-get-equal-substrings-within-budget/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1208-get-equal-substrings-within-budget/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1208-get-equal-substrings-within-budget/README.md b/1208-get-equal-substrings-within-budget/README.md new file mode 100644 index 00000000..45da0232 --- /dev/null +++ b/1208-get-equal-substrings-within-budget/README.md @@ -0,0 +1,39 @@ +

1208. Get Equal Substrings Within Budget

Medium


You are given two strings s and t of the same length and an integer maxCost.

+ +

You want to change s to t. Changing the ith character of s to ith character of t costs |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters).

+ +

Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost. If there is no substring from s that can be changed to its corresponding substring from t, return 0.

+ +

 

+

Example 1:

+ +
Input: s = "abcd", t = "bcdf", maxCost = 3
+Output: 3
+Explanation: "abc" of s can change to "bcd".
+That costs 3, so the maximum length is 3.
+
+ +

Example 2:

+ +
Input: s = "abcd", t = "cdef", maxCost = 3
+Output: 1
+Explanation: Each character in s costs 2 to change to character in t,  so the maximum length is 1.
+
+ +

Example 3:

+ +
Input: s = "abcd", t = "acde", maxCost = 0
+Output: 1
+Explanation: You cannot make any change, so the maximum length is 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • t.length == s.length
  • +
  • 0 <= maxCost <= 106
  • +
  • s and t consist of only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp b/1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp new file mode 100644 index 00000000..d38be720 --- /dev/null +++ b/1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp @@ -0,0 +1,57 @@ +class Solution { +public: + + vector dx = {-1, +1, 0, 0}; + vector dy = {0, 0, +1, -1}; + + bool isValid(int x, int y, int n, int m) + { + return (x >= 0 and y >= 0 and x < n and y < m); + } + + int dfs(int i, int j, int n, int m, vector>& grid, vector>& visited){ + + int maxAns = 0; + + visited[i][j] = true; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy, n, m) and grid[newx][newy] != 0 and !visited[newx][newy]) + { + int ans = grid[newx][newy] + dfs(newx, newy, n, m, grid, visited); + maxAns = max(maxAns, ans); + } + } + + visited[i][j] = false; + + return maxAns; + } + + int getMaximumGold(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + int ans = 0; + + vector> visited(n, vector(m, false)); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] != 0) + { + int currVal = grid[i][j] + dfs(i, j, n, m, grid, visited); + ans = max(ans, currVal); + } + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/1219-path-with-maximum-gold/README.md b/1219-path-with-maximum-gold/README.md new file mode 100644 index 00000000..399cf0bd --- /dev/null +++ b/1219-path-with-maximum-gold/README.md @@ -0,0 +1,48 @@ +

1219. Path with Maximum Gold

Medium


In a gold mine grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.

+ +

Return the maximum amount of gold you can collect under the conditions:

+ +
    +
  • Every time you are located in a cell you will collect all the gold in that cell.
  • +
  • From your position, you can walk one step to the left, right, up, or down.
  • +
  • You can't visit the same cell more than once.
  • +
  • Never visit a cell with 0 gold.
  • +
  • You can start and stop collecting gold from any position in the grid that has some gold.
  • +
+ +

 

+

Example 1:

+ +
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
+Output: 24
+Explanation:
+[[0,6,0],
+ [5,8,7],
+ [0,9,0]]
+Path to get the maximum gold, 9 -> 8 -> 7.
+
+ +

Example 2:

+ +
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
+Output: 28
+Explanation:
+[[1,0,7],
+ [2,0,6],
+ [3,4,5],
+ [0,3,0],
+ [9,0,20]]
+Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 15
  • +
  • 0 <= grid[i][j] <= 100
  • +
  • There are at most 25 cells containing gold.
  • +
+
\ No newline at end of file diff --git a/1220-count-vowels-permutation/1220-count-vowels-permutation.cpp b/1220-count-vowels-permutation/1220-count-vowels-permutation.cpp new file mode 100644 index 00000000..56bada35 --- /dev/null +++ b/1220-count-vowels-permutation/1220-count-vowels-permutation.cpp @@ -0,0 +1,55 @@ +class Solution { + unordered_map> mp; + int mod = 1e9+7; + +public: + + int helper(int n ,int i ,char prev, vector>& t) + { + if(i > n) + return 0; + + if(i == n) + { + switch(prev) + { + case 'a': + return 1; + case 'e': + return 2; + case 'i': + return 4; + case 'o': + return 2; + case 'u': + return 1; + default: + return 5; + } + } + + int idx = prev - 'a'; + if(t[i][idx] != -1) + return t[i][idx]; + + long long ans = 0; + for(auto next : mp[prev]) + ans += (helper(n,i+1,next,t) % mod); + + return t[i][idx] = ans % mod; + } + + int countVowelPermutation(int n) { + + mp['c'] = {'a','e','i','o','u'}; + mp['a'] = {'e'}; + mp['e'] = {'a','i'}; + mp['i'] = {'a','e','o','u'}; + mp['o'] = {'i','u'}; + mp['u'] = {'a'}; + + vector> t(n+2,vector(27,-1)); + return helper(n,1,'c',t); + + } +}; \ No newline at end of file diff --git a/1220-count-vowels-permutation/NOTES.md b/1220-count-vowels-permutation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1220-count-vowels-permutation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1220-count-vowels-permutation/README.md b/1220-count-vowels-permutation/README.md new file mode 100644 index 00000000..0eaafd10 --- /dev/null +++ b/1220-count-vowels-permutation/README.md @@ -0,0 +1,40 @@ +

1220. Count Vowels Permutation

Hard


Given an integer n, your task is to count how many strings of length n can be formed under the following rules:

+ +
    +
  • Each character is a lower case vowel ('a', 'e', 'i', 'o', 'u')
  • +
  • Each vowel 'a' may only be followed by an 'e'.
  • +
  • Each vowel 'e' may only be followed by an 'a' or an 'i'.
  • +
  • Each vowel 'i' may not be followed by another 'i'.
  • +
  • Each vowel 'o' may only be followed by an 'i' or a 'u'.
  • +
  • Each vowel 'u' may only be followed by an 'a'.
  • +
+ +

Since the answer may be too large, return it modulo 10^9 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 1
+Output: 5
+Explanation: All possible strings are: "a", "e", "i" , "o" and "u".
+
+ +

Example 2:

+ +
Input: n = 2
+Output: 10
+Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".
+
+ +

Example 3: 

+ +
Input: n = 5
+Output: 68
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 10^4
  • +
+
\ No newline at end of file diff --git a/1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp b/1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp new file mode 100644 index 00000000..a2d455e9 --- /dev/null +++ b/1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + + int getNextIdx(int low, int last, vector>& jobs) + { + int n = jobs.size(); + int high = n-1, result = n; + + while(low <= high) + { + int mid = low + (high - low)/2; + + if(jobs[mid][0] >= last) + { + result = mid; + high = mid-1; + } + else + low = mid+1; + } + + return result; + } + + int helper(int idx, int n, vector>& jobs, vector& dp) + { + if(idx == n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int next = getNextIdx(idx+1, jobs[idx][1], jobs); + + int notTake = helper(idx+1, n, jobs, dp); + + int take = jobs[idx][2] + helper(next, n, jobs, dp); + + return dp[idx] = max(take, notTake); + } + + int jobScheduling(vector& startTime, vector& endTime, vector& profit) { + + int n = startTime.size(); + + vector> jobs; + + for(int i = 0; i < n; ++i) + { + jobs.push_back({startTime[i], endTime[i], profit[i]}); + } + + sort(jobs.begin(), jobs.end()); + + vector dp(n+1, -1); + + return helper(0, n, jobs, dp); + + } +}; \ No newline at end of file diff --git a/1235-maximum-profit-in-job-scheduling/README.md b/1235-maximum-profit-in-job-scheduling/README.md new file mode 100644 index 00000000..8660019e --- /dev/null +++ b/1235-maximum-profit-in-job-scheduling/README.md @@ -0,0 +1,44 @@ +

1235. Maximum Profit in Job Scheduling

Hard


We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].

+ +

You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.

+ +

If you choose a job that ends at time X you will be able to start another job that starts at time X.

+ +

 

+

Example 1:

+ +

+ +
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
+Output: 120
+Explanation: The subset chosen is the first and fourth job. 
+Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
+
+ +

Example 2:

+ +

+ +
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
+Output: 150
+Explanation: The subset chosen is the first, fourth and fifth job. 
+Profit obtained 150 = 20 + 70 + 60.
+
+ +

Example 3:

+ +

+ +
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
+Output: 6
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= startTime.length == endTime.length == profit.length <= 5 * 104
  • +
  • 1 <= startTime[i] < endTime[i] <= 109
  • +
  • 1 <= profit[i] <= 104
  • +
+
\ No newline at end of file diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp new file mode 100644 index 00000000..dfde3d51 --- /dev/null +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + + void helper(int idx, int n, string str, set st, int& maxLength, vector& arr) + { + if(idx == n) + { + maxLength = max(maxLength, (int)str.size()); + return; + } + + helper(idx+1, n, str, st, maxLength, arr); + + bool canTake = true; + + set curr; + + for(auto& ch : arr[idx]) + { + if(curr.count(ch)) + return; + else + curr.insert(ch); + } + + if(canTake) + { + for(auto& ch : curr) + { + if(st.count(ch)) + return; + } + } + + + for(auto& ch : curr) + st.insert(ch); + + helper(idx+1, n, str + arr[idx], st, maxLength, arr); + + for(auto& ch : curr) + st.erase(ch); + + } + + int maxLength(vector& arr) { + + int n = arr.size(); + + set st; + string str; + int maxLength = 0; + + helper(0, n, str, st, maxLength, arr); + + return maxLength; + } +}; \ No newline at end of file diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md new file mode 100644 index 00000000..32b1a080 --- /dev/null +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md @@ -0,0 +1,44 @@ +

1239. Maximum Length of a Concatenated String with Unique Characters

Medium


You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.

+ +

Return the maximum possible length of s.

+ +

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

+ +

 

+

Example 1:

+ +
Input: arr = ["un","iq","ue"]
+Output: 4
+Explanation: All the valid concatenations are:
+- ""
+- "un"
+- "iq"
+- "ue"
+- "uniq" ("un" + "iq")
+- "ique" ("iq" + "ue")
+Maximum length is 4.
+
+ +

Example 2:

+ +
Input: arr = ["cha","r","act","ers"]
+Output: 6
+Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
+
+ +

Example 3:

+ +
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
+Output: 26
+Explanation: The only string in arr has all 26 characters.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 16
  • +
  • 1 <= arr[i].length <= 26
  • +
  • arr[i] contains only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp b/1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp new file mode 100644 index 00000000..aac4bacf --- /dev/null +++ b/1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int numberOfSubarrays(vector& nums, int k) { + + int n = nums.size(); + + int i = 0, j = 0, ans = 0; + + list indexes; + + while(j < n) + { + if(nums[j] & 1) + indexes.push_back(j); + + if(indexes.size() > k) + { + i = indexes.front() + 1; + indexes.pop_front(); + } + + if(indexes.size() == k) ans += (indexes.front() - i + 1); + + ++j; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/1248-count-number-of-nice-subarrays/NOTES.md b/1248-count-number-of-nice-subarrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1248-count-number-of-nice-subarrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp b/1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp new file mode 100644 index 00000000..c1a6bc24 --- /dev/null +++ b/1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + string minRemoveToMakeValid(string s) { + + int n = s.size(); + + stack st; + + for(int i = 0; i < n; ++i) + { + if(!st.empty() and s[st.top()] == '(' and s[i] == ')') + st.pop(); + else if(s[i] == '(' or s[i] == ')') + st.push(i); + } + + while(!st.empty()) + { + s[st.top()] = '#'; + st.pop(); + } + + string ans; + for(int i = 0; i < n; ++i) + { + if(s[i] != '#') + ans += s[i]; + } + + return ans; + } +}; \ No newline at end of file diff --git a/1249-minimum-remove-to-make-valid-parentheses/README.md b/1249-minimum-remove-to-make-valid-parentheses/README.md new file mode 100644 index 00000000..d5ec3542 --- /dev/null +++ b/1249-minimum-remove-to-make-valid-parentheses/README.md @@ -0,0 +1,41 @@ +

1249. Minimum Remove to Make Valid Parentheses

Medium


Given a string s of '(' , ')' and lowercase English characters.

+ +

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

+ +

Formally, a parentheses string is valid if and only if:

+ +
    +
  • It is the empty string, contains only lowercase characters, or
  • +
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • +
  • It can be written as (A), where A is a valid string.
  • +
+ +

 

+

Example 1:

+ +
Input: s = "lee(t(c)o)de)"
+Output: "lee(t(c)o)de"
+Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
+
+ +

Example 2:

+ +
Input: s = "a)b(c)d"
+Output: "ab(c)d"
+
+ +

Example 3:

+ +
Input: s = "))(("
+Output: ""
+Explanation: An empty string is also valid.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s[i] is either'(' , ')', or lowercase English letter.
  • +
+
\ No newline at end of file diff --git a/1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp b/1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp new file mode 100644 index 00000000..f5a2fa74 --- /dev/null +++ b/1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int findSpecialInteger(vector& arr) { + + int n = arr.size(), oneFourth = n/4; + + for(int i = 0; i < n - oneFourth; ++i) + { + if(arr[i] == arr[i+oneFourth]) + return arr[i]; + } + + return -1; + + } +}; \ No newline at end of file diff --git a/1287-element-appearing-more-than-25-in-sorted-array/README.md b/1287-element-appearing-more-than-25-in-sorted-array/README.md new file mode 100644 index 00000000..d7dfbbff --- /dev/null +++ b/1287-element-appearing-more-than-25-in-sorted-array/README.md @@ -0,0 +1,23 @@ +

1287. Element Appearing More Than 25% In Sorted Array

Easy


Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

+ +

 

+

Example 1:

+ +
Input: arr = [1,2,2,6,6,6,6,7,10]
+Output: 6
+
+ +

Example 2:

+ +
Input: arr = [1,1]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 104
  • +
  • 0 <= arr[i] <= 105
  • +
+
\ No newline at end of file diff --git a/1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp b/1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp new file mode 100644 index 00000000..b667b066 --- /dev/null +++ b/1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + int helper(int idx, int prev, int n, vector>& grid, vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][prev+1] != -1) + return dp[idx][prev+1]; + + int ans = INT_MAX; + for(int j = 0; j < n; ++j) + { + if(j != prev) + { + int curr = grid[idx][j] + helper(idx+1, j, n, grid, dp); + ans = min(ans, curr); + } + } + + return dp[idx][prev+1] = ans; + } + + int minFallingPathSum(vector>& grid) { + + int n = grid.size(); + vector> dp(n+1, vector(n+1, -1)); + + return helper(0, -1, n, grid, dp); + } +}; \ No newline at end of file diff --git a/1289-minimum-falling-path-sum-ii/README.md b/1289-minimum-falling-path-sum-ii/README.md new file mode 100644 index 00000000..90e5bbb6 --- /dev/null +++ b/1289-minimum-falling-path-sum-ii/README.md @@ -0,0 +1,32 @@ +

1289. Minimum Falling Path Sum II

Hard


Given an n x n integer matrix grid, return the minimum sum of a falling path with non-zero shifts.

+ +

A falling path with non-zero shifts is a choice of exactly one element from each row of grid such that no two elements chosen in adjacent rows are in the same column.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
+Output: 13
+Explanation: 
+The possible falling paths are:
+[1,5,9], [1,5,7], [1,6,7], [1,6,8],
+[2,4,8], [2,4,9], [2,6,7], [2,6,8],
+[3,4,8], [3,4,9], [3,5,7], [3,5,9]
+The falling path with the smallest sum is [1,5,7], so the answer is 13.
+
+ +

Example 2:

+ +
Input: grid = [[7]]
+Output: 7
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 1 <= n <= 200
  • +
  • -99 <= grid[i][j] <= 99
  • +
+
\ No newline at end of file diff --git a/1291-sequential-digits/1291-sequential-digits.cpp b/1291-sequential-digits/1291-sequential-digits.cpp new file mode 100644 index 00000000..180d3292 --- /dev/null +++ b/1291-sequential-digits/1291-sequential-digits.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector sequentialDigits(int low, int high) { + + vector ans; + + for(int i = 1; i <=8; ++i) + { + int currNum = i; + for(int j = i+1; j <= 9; ++j) + { + currNum = (currNum * 10) + j; + + if(currNum >= low and currNum <= high) + ans.push_back(currNum); + } + } + + sort(ans.begin(), ans.end()); + + return ans; + } +}; \ No newline at end of file diff --git a/1291-sequential-digits/NOTES.md b/1291-sequential-digits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1291-sequential-digits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp b/1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp new file mode 100644 index 00000000..16914d74 --- /dev/null +++ b/1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + TreeNode* helper(TreeNode* root, int target) + { + if(!root) + return nullptr; + + TreeNode* left = helper(root->left, target); + TreeNode* right = helper(root->right, target); + + if(!left) + root->left = nullptr; + + if(!right) + root->right = nullptr; + + if(!left and !right and root->val == target) + { + return nullptr; + } + return root; + } + + + TreeNode* removeLeafNodes(TreeNode* root, int target) { + + return helper(root, target); + + } +}; \ No newline at end of file diff --git a/1325-delete-leaves-with-a-given-value/README.md b/1325-delete-leaves-with-a-given-value/README.md new file mode 100644 index 00000000..23f321db --- /dev/null +++ b/1325-delete-leaves-with-a-given-value/README.md @@ -0,0 +1,40 @@ +

1325. Delete Leaves With a Given Value

Medium


Given a binary tree root and an integer target, delete all the leaf nodes with value target.

+ +

Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).

+ +

 

+

Example 1:

+ +

+ +
Input: root = [1,2,3,2,null,2,4], target = 2
+Output: [1,null,3,null,4]
+Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left). 
+After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
+
+ +

Example 2:

+ +

+ +
Input: root = [1,3,3,3,2], target = 3
+Output: [1,3,null,null,2]
+
+ +

Example 3:

+ +

+ +
Input: root = [1,2,null,2,null,2], target = 2
+Output: [1]
+Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 3000].
  • +
  • 1 <= Node.val, target <= 1000
  • +
+
\ No newline at end of file diff --git a/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp b/1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp new file mode 100644 index 00000000..8f18eb59 --- /dev/null +++ b/1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int t[301][11]; + int solve(vector& mat, int n, int idx, int d) { + if(d == 1) + return *max_element(begin(mat)+idx, end(mat)); + + if(t[idx][d] != -1) + return t[idx][d]; + + + int Max = INT_MIN; + int result = INT_MAX; + + for(int i = idx; i<=n-d; i++) { + Max = max(Max, mat[i]); + result = min(result, Max + solve(mat, n, i+1, d-1)); + } + return t[idx][d] = result; + } + + int minDifficulty(vector& jobDifficulty, int d) { + int n = jobDifficulty.size(); + if(n < d) + return -1; + memset(t, -1, sizeof(t)); + return solve(jobDifficulty, n, 0, d); + } +}; \ No newline at end of file diff --git a/1335-minimum-difficulty-of-a-job-schedule/README.md b/1335-minimum-difficulty-of-a-job-schedule/README.md new file mode 100644 index 00000000..2c1f6130 --- /dev/null +++ b/1335-minimum-difficulty-of-a-job-schedule/README.md @@ -0,0 +1,41 @@ +

1335. Minimum Difficulty of a Job Schedule

Hard


You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).

+ +

You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.

+ +

You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].

+ +

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

+ +

 

+

Example 1:

+ +
Input: jobDifficulty = [6,5,4,3,2,1], d = 2
+Output: 7
+Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
+Second day you can finish the last job, total difficulty = 1.
+The difficulty of the schedule = 6 + 1 = 7 
+
+ +

Example 2:

+ +
Input: jobDifficulty = [9,9,9], d = 4
+Output: -1
+Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
+
+ +

Example 3:

+ +
Input: jobDifficulty = [1,1,1], d = 3
+Output: 3
+Explanation: The schedule is one job per day. total difficulty will be 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= jobDifficulty.length <= 300
  • +
  • 0 <= jobDifficulty[i] <= 1000
  • +
  • 1 <= d <= 10
  • +
+
\ No newline at end of file diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp b/1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp new file mode 100644 index 00000000..2a4429de --- /dev/null +++ b/1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minSteps(string s, string t) { + + map mp; + + int need = 0; + + for(auto& ele : s) + ++mp[ele]; + + for(auto& ele : t) + { + if(mp[ele] > 0) + { + --mp[ele]; + if(mp[ele] == 0) + mp.erase(ele); + } + else + ++need; + } + + return need; + } +}; \ No newline at end of file diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md b/1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md b/1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md new file mode 100644 index 00000000..6838a54f --- /dev/null +++ b/1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md @@ -0,0 +1,37 @@ +

1347. Minimum Number of Steps to Make Two Strings Anagram

Medium


You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.

+ +

Return the minimum number of steps to make t an anagram of s.

+ +

An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.

+ +

 

+

Example 1:

+ +
Input: s = "bab", t = "aba"
+Output: 1
+Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
+
+ +

Example 2:

+ +
Input: s = "leetcode", t = "practice"
+Output: 5
+Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
+
+ +

Example 3:

+ +
Input: s = "anagram", t = "mangaar"
+Output: 0
+Explanation: "anagram" and "mangaar" are anagrams. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • s.length == t.length
  • +
  • s and t consist of lowercase English letters only.
  • +
+
\ No newline at end of file diff --git a/1358-number-of-substrings-containing-all-three-characters/NOTES.md b/1358-number-of-substrings-containing-all-three-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1358-number-of-substrings-containing-all-three-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1358-number-of-substrings-containing-all-three-characters/README.md b/1358-number-of-substrings-containing-all-three-characters/README.md new file mode 100644 index 00000000..fbdc7088 --- /dev/null +++ b/1358-number-of-substrings-containing-all-three-characters/README.md @@ -0,0 +1,33 @@ +

1358. Number of Substrings Containing All Three Characters

Medium


Given a string s consisting only of characters a, b and c.

+ +

Return the number of substrings containing at least one occurrence of all these characters a, b and c.

+ +

 

+

Example 1:

+ +
Input: s = "abcabc"
+Output: 10
+Explanation: The substrings containing at least one occurrence of the characters ab and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). 
+
+ +

Example 2:

+ +
Input: s = "aaacb"
+Output: 3
+Explanation: The substrings containing at least one occurrence of the characters ab and c are "aaacb", "aacb" and "acb". 
+
+ +

Example 3:

+ +
Input: s = "abc"
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 5 x 10^4
  • +
  • s only consists of a, b or characters.
  • +
+
\ No newline at end of file diff --git a/1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql b/1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql new file mode 100644 index 00000000..9fce03ec --- /dev/null +++ b/1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below + + +select e2.unique_id , e1.name from Employees e1 +left join EmployeeUNI e2 +on e1.id = e2.id; \ No newline at end of file diff --git a/1378-replace-employee-id-with-the-unique-identifier/NOTES.md b/1378-replace-employee-id-with-the-unique-identifier/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1378-replace-employee-id-with-the-unique-identifier/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1378-replace-employee-id-with-the-unique-identifier/README.md b/1378-replace-employee-id-with-the-unique-identifier/README.md new file mode 100644 index 00000000..283a0d5d --- /dev/null +++ b/1378-replace-employee-id-with-the-unique-identifier/README.md @@ -0,0 +1,73 @@ +

1378. Replace Employee ID With The Unique Identifier

Easy


Table: Employees

+ +
+---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| id            | int     |
+| name          | varchar |
++---------------+---------+
+id is the primary key (column with unique values) for this table.
+Each row of this table contains the id and the name of an employee in a company.
+
+ +

 

+ +

Table: EmployeeUNI

+ +
+---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| id            | int     |
+| unique_id     | int     |
++---------------+---------+
+(id, unique_id) is the primary key (combination of columns with unique values) for this table.
+Each row of this table contains the id and the corresponding unique id of an employee in the company.
+
+ +

 

+ +

Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.

+ +

Return the result table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Employees table:
++----+----------+
+| id | name     |
++----+----------+
+| 1  | Alice    |
+| 7  | Bob      |
+| 11 | Meir     |
+| 90 | Winston  |
+| 3  | Jonathan |
++----+----------+
+EmployeeUNI table:
++----+-----------+
+| id | unique_id |
++----+-----------+
+| 3  | 1         |
+| 11 | 2         |
+| 90 | 3         |
++----+-----------+
+Output: 
++-----------+----------+
+| unique_id | name     |
++-----------+----------+
+| null      | Alice    |
+| null      | Bob      |
+| 2         | Meir     |
+| 3         | Winston  |
+| 1         | Jonathan |
++-----------+----------+
+Explanation: 
+Alice and Bob do not have a unique ID, We will show null instead.
+The unique ID of Meir is 2.
+The unique ID of Winston is 3.
+The unique ID of Jonathan is 1.
+
+
\ No newline at end of file diff --git a/1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp b/1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp new file mode 100644 index 00000000..3d76be27 --- /dev/null +++ b/1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector luckyNumbers (vector>& matrix) { + vector ans; + int r = matrix.size(); + int c = matrix[0].size(); + + vector rmin(r,INT_MAX); + vector cmax(c,INT_MIN); + + for(int i = 0; i1380. Lucky Numbers in a Matrix

Easy


Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

+ +

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

+ +

 

+

Example 1:

+ +
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
+Output: [15]
+Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.
+
+ +

Example 2:

+ +
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
+Output: [12]
+Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
+
+ +

Example 3:

+ +
Input: matrix = [[7,8],[1,2]]
+Output: [7]
+Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 1 <= n, m <= 50
  • +
  • 1 <= matrix[i][j] <= 105.
  • +
  • All elements in the matrix are distinct.
  • +
+
\ No newline at end of file diff --git a/1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp b/1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp new file mode 100644 index 00000000..6ee5d2f7 --- /dev/null +++ b/1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void inorder(TreeNode* root, vector& arr) + { + if(root) + { + inorder(root->left, arr); + arr.push_back(root->val); + inorder(root->right, arr); + } + } + + TreeNode* balance(vector& arr, int low, int high) + { + if(low > high) + return nullptr; + + int mid = (low + high) / 2; + + TreeNode* root = new TreeNode(arr[mid]); + + root->left = balance(arr, low, mid-1); + + root->right = balance(arr, mid+1, high); + + return root; + + } + + TreeNode* balanceBST(TreeNode* root) { + + vector arr; + + inorder(root, arr); + + return balance(arr, 0, arr.size()-1); + + } +}; \ No newline at end of file diff --git a/1382-balance-a-binary-search-tree/NOTES.md b/1382-balance-a-binary-search-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1382-balance-a-binary-search-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1382-balance-a-binary-search-tree/README.md b/1382-balance-a-binary-search-tree/README.md new file mode 100644 index 00000000..b30659a0 --- /dev/null +++ b/1382-balance-a-binary-search-tree/README.md @@ -0,0 +1,26 @@ +

1382. Balance a Binary Search Tree

Medium


Given the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them.

+ +

A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1.

+ +

 

+

Example 1:

+ +
Input: root = [1,null,2,null,3,null,4,null,null]
+Output: [2,1,3,null,null,null,4]
+Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.
+
+ +

Example 2:

+ +
Input: root = [2,1,3]
+Output: [2,1,3]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • 1 <= Node.val <= 105
  • +
+
\ No newline at end of file diff --git a/1395-count-number-of-teams/README.md b/1395-count-number-of-teams/README.md new file mode 100644 index 00000000..e0ebb7f2 --- /dev/null +++ b/1395-count-number-of-teams/README.md @@ -0,0 +1,42 @@ +

1395. Count Number of Teams

Medium


There are n soldiers standing in a line. Each soldier is assigned a unique rating value.

+ +

You have to form a team of 3 soldiers amongst them under the following rules:

+ +
    +
  • Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
  • +
  • A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
  • +
+ +

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

+ +

 

+

Example 1:

+ +
Input: rating = [2,5,3,4,1]
+Output: 3
+Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 
+
+ +

Example 2:

+ +
Input: rating = [2,1,3]
+Output: 0
+Explanation: We can't form any team given the conditions.
+
+ +

Example 3:

+ +
Input: rating = [1,2,3,4]
+Output: 4
+
+ +

 

+

Constraints:

+ +
    +
  • n == rating.length
  • +
  • 3 <= n <= 1000
  • +
  • 1 <= rating[i] <= 105
  • +
  • All the integers in rating are unique.
  • +
+
\ No newline at end of file diff --git a/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp new file mode 100644 index 00000000..3f9ac903 --- /dev/null +++ b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int numSteps(string s) { + int step = 0; + + while (s != "1") { + + if (s.back() == '0') s.pop_back(); + else { + + while (!s.empty() && s.back() == '1') { + s.pop_back(); + ++step; + } + + if (s.empty()) return step + 1; + else s.back() = '1'; + + } + ++step; + } + + return step; + } +}; \ No newline at end of file diff --git a/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md new file mode 100644 index 00000000..d5797331 --- /dev/null +++ b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md @@ -0,0 +1,50 @@ +

1404. Number of Steps to Reduce a Number in Binary Representation to One

Medium


Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules:

+ +
    +
  • +

    If the current number is even, you have to divide it by 2.

    +
  • +
  • +

    If the current number is odd, you have to add 1 to it.

    +
  • +
+ +

It is guaranteed that you can always reach one for all test cases.

+ +

 

+

Example 1:

+ +
Input: s = "1101"
+Output: 6
+Explanation: "1101" corressponds to number 13 in their decimal representation.
+Step 1) 13 is odd, add 1 and obtain 14. 
+Step 2) 14 is even, divide by 2 and obtain 7.
+Step 3) 7 is odd, add 1 and obtain 8.
+Step 4) 8 is even, divide by 2 and obtain 4.  
+Step 5) 4 is even, divide by 2 and obtain 2. 
+Step 6) 2 is even, divide by 2 and obtain 1.  
+
+ +

Example 2:

+ +
Input: s = "10"
+Output: 1
+Explanation: "10" corressponds to number 2 in their decimal representation.
+Step 1) 2 is even, divide by 2 and obtain 1.  
+
+ +

Example 3:

+ +
Input: s = "1"
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 500
  • +
  • s consists of characters '0' or '1'
  • +
  • s[0] == '1'
  • +
+
\ No newline at end of file diff --git a/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp new file mode 100644 index 00000000..4aa0af90 --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int maxScore(string s) { + + int n = s.size(); + + int ones = count(s.begin(), s.end(), '1'); + + int zero = 0, ans = 0; + + for(int i = 0; i < n-1; ++i) + { + if(s[i] == '0') ++zero; + else --ones; + + ans = max(ans, zero + ones); + } + + return ans; + } +}; \ No newline at end of file diff --git a/1422-maximum-score-after-splitting-a-string/NOTES.md b/1422-maximum-score-after-splitting-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1422-maximum-score-after-splitting-a-string/README.md b/1422-maximum-score-after-splitting-a-string/README.md new file mode 100644 index 00000000..a8524c03 --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/README.md @@ -0,0 +1,39 @@ +

1422. Maximum Score After Splitting a String

Easy


Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).

+ +

The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.

+ +

 

+

Example 1:

+ +
Input: s = "011101"
+Output: 5 
+Explanation: 
+All possible ways of splitting s into two non-empty substrings are:
+left = "0" and right = "11101", score = 1 + 4 = 5 
+left = "01" and right = "1101", score = 1 + 3 = 4 
+left = "011" and right = "101", score = 1 + 2 = 3 
+left = "0111" and right = "01", score = 1 + 1 = 2 
+left = "01110" and right = "1", score = 2 + 1 = 3
+
+ +

Example 2:

+ +
Input: s = "00111"
+Output: 5
+Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
+
+ +

Example 3:

+ +
Input: s = "1111"
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 500
  • +
  • The string s consists of characters '0' and '1' only.
  • +
+
\ No newline at end of file diff --git a/1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp b/1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp new file mode 100644 index 00000000..ef988336 --- /dev/null +++ b/1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + void solve(int k, int& ans, vector& pref, vector& suff) + { + int n = pref.size(); + for(int i = 0; i < k; ++i) + { + int end = n-(k-(i+1)); + int endSum = end < n ? suff[end] : 0; + ans = max(ans, pref[i] + endSum); + } + } + + int maxScore(vector& cardPoints, int k) { + + int n = cardPoints.size(); + vector pref(n, 0), suff(n, 0); + + pref[0] = cardPoints[0]; + suff[n-1] = cardPoints[n-1]; + + for(int i = 1; i < n; ++i) + pref[i] = (pref[i-1] + cardPoints[i]); + + for(int i = n-2; i >= 0; --i) + suff[i] = (suff[i+1] + cardPoints[i]); + + int ans = 0; + + solve(k, ans, pref, suff); + swap(pref, suff); + reverse(pref.begin(), pref.end()); + reverse(suff.begin(), suff.end()); + solve(k, ans, pref, suff); + + return ans; + } +}; \ No newline at end of file diff --git a/1423-maximum-points-you-can-obtain-from-cards/NOTES.md b/1423-maximum-points-you-can-obtain-from-cards/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1423-maximum-points-you-can-obtain-from-cards/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1423-maximum-points-you-can-obtain-from-cards/README.md b/1423-maximum-points-you-can-obtain-from-cards/README.md new file mode 100644 index 00000000..89a65089 --- /dev/null +++ b/1423-maximum-points-you-can-obtain-from-cards/README.md @@ -0,0 +1,39 @@ +

1423. Maximum Points You Can Obtain from Cards

Medium


There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.

+ +

In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.

+ +

Your score is the sum of the points of the cards you have taken.

+ +

Given the integer array cardPoints and the integer k, return the maximum score you can obtain.

+ +

 

+

Example 1:

+ +
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
+Output: 12
+Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
+
+ +

Example 2:

+ +
Input: cardPoints = [2,2,2], k = 2
+Output: 4
+Explanation: Regardless of which two cards you take, your score will always be 4.
+
+ +

Example 3:

+ +
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
+Output: 55
+Explanation: You have to take all the cards. Your score is the sum of points of all cards.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= cardPoints.length <= 105
  • +
  • 1 <= cardPoints[i] <= 104
  • +
  • 1 <= k <= cardPoints.length
  • +
+
\ No newline at end of file diff --git a/1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp b/1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp new file mode 100644 index 00000000..0120df06 --- /dev/null +++ b/1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + + int n = nums.size(); + + vector dp(n+1); + + for(int i = 0; i < n; ++i) + dp[i] = nums[i]; + + priority_queue> pq; + + int ans = *max_element(begin(nums), end(nums)); + + pq.push({nums[0], 0}); + + for(int i = 1; i < n; ++i) + { + while(!pq.empty() and i - pq.top().second > k) + pq.pop(); + + dp[i] = max(dp[i], nums[i] + pq.top().first); + + ans = max(ans, dp[i]); + + pq.push({dp[i], i}); + } + + return ans; + } +}; + + \ No newline at end of file diff --git a/1425-constrained-subsequence-sum/NOTES.md b/1425-constrained-subsequence-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1425-constrained-subsequence-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1425-constrained-subsequence-sum/README.md b/1425-constrained-subsequence-sum/README.md new file mode 100644 index 00000000..6afbb1b6 --- /dev/null +++ b/1425-constrained-subsequence-sum/README.md @@ -0,0 +1,34 @@ +

1425. Constrained Subsequence Sum

Hard


Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.

+ +

A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

+ +

 

+

Example 1:

+ +
Input: nums = [10,2,-10,5,20], k = 2
+Output: 37
+Explanation: The subsequence is [10, 2, 5, 20].
+
+ +

Example 2:

+ +
Input: nums = [-1,-2,-3], k = 1
+Output: -1
+Explanation: The subsequence must be non-empty, so we choose the largest number.
+
+ +

Example 3:

+ +
Input: nums = [10,-2,-10,-5,20], k = 2
+Output: 23
+Explanation: The subsequence is [10, -2, -5, 20].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+
\ No newline at end of file diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp new file mode 100644 index 00000000..e129d313 --- /dev/null +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int longestSubarray(vector& nums, int limit) { + + list maxi, mini; + + int i = 0, j = 0, n = nums.size(); + + int ans = 0; + + while(j < n) + { + while(!maxi.empty() and nums[maxi.back()] < nums[j]) + maxi.pop_back(); + + while(!mini.empty() and nums[mini.back()] > nums[j]) + mini.pop_back(); + + maxi.push_back(j); + mini.push_back(j); + + while(!maxi.empty() and !mini.empty() and nums[maxi.front()] - nums[mini.front()] > limit) + { + if(maxi.front() == i) maxi.pop_front(); + if(mini.front() == i) mini.pop_front(); + ++i; + } + + ans = max(ans, j - i + 1); + + ++j; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md new file mode 100644 index 00000000..8081e912 --- /dev/null +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md @@ -0,0 +1,43 @@ +

1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

Medium


Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.

+ +

 

+

Example 1:

+ +
Input: nums = [8,2,4,7], limit = 4
+Output: 2 
+Explanation: All subarrays are: 
+[8] with maximum absolute diff |8-8| = 0 <= 4.
+[8,2] with maximum absolute diff |8-2| = 6 > 4. 
+[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
+[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
+[2] with maximum absolute diff |2-2| = 0 <= 4.
+[2,4] with maximum absolute diff |2-4| = 2 <= 4.
+[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
+[4] with maximum absolute diff |4-4| = 0 <= 4.
+[4,7] with maximum absolute diff |4-7| = 3 <= 4.
+[7] with maximum absolute diff |7-7| = 0 <= 4. 
+Therefore, the size of the longest subarray is 2.
+
+ +

Example 2:

+ +
Input: nums = [10,1,2,4,7,2], limit = 5
+Output: 4 
+Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
+
+ +

Example 3:

+ +
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 0 <= limit <= 109
  • +
+
\ No newline at end of file diff --git a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp new file mode 100644 index 00000000..5136fb83 --- /dev/null +++ b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int countTriplets(vector& arr) { + + int n = arr.size(); + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = i+1; j < n; ++j) + { + int curXorF = 0, curXorS = 0; + + for(int k = i; k < j; ++k) + curXorF ^= arr[k]; + + for(int k = j; k < n; ++k) + { + curXorS ^= arr[k]; + if(curXorF == curXorS) + ++cnt; + } + } + } + + return cnt; + } +}; diff --git a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md new file mode 100644 index 00000000..5a85fce7 --- /dev/null +++ b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md @@ -0,0 +1,37 @@ +

1442. Count Triplets That Can Form Two Arrays of Equal XOR

Medium


Given an array of integers arr.

+ +

We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).

+ +

Let's define a and b as follows:

+ +
    +
  • a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
  • +
  • b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
  • +
+ +

Note that ^ denotes the bitwise-xor operation.

+ +

Return the number of triplets (i, j and k) Where a == b.

+ +

 

+

Example 1:

+ +
Input: arr = [2,3,1,6,7]
+Output: 4
+Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
+
+ +

Example 2:

+ +
Input: arr = [1,1,1,1,1]
+Output: 10
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 300
  • +
  • 1 <= arr[i] <= 108
  • +
+
\ No newline at end of file diff --git a/1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp b/1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp new file mode 100644 index 00000000..1370abd3 --- /dev/null +++ b/1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, vector freq, int& cnt) + { + if(root) + { + ++freq[root->val]; + + helper(root->left, freq, cnt); + helper(root->right, freq, cnt); + + int odd = 0; + + if(!root->left and !root->right) + { + for(int i = 0; i < 10; ++i) + { + odd += (freq[i] & 1); + } + + cnt += (odd == 1 or odd == 0); + } + + --freq[root->val]; + } + } + + int pseudoPalindromicPaths (TreeNode* root) { + + int cnt = 0; + + vector freq(10, 0); + + helper(root, freq, cnt); + + return cnt; + + } +}; \ No newline at end of file diff --git a/1457-pseudo-palindromic-paths-in-a-binary-tree/README.md b/1457-pseudo-palindromic-paths-in-a-binary-tree/README.md new file mode 100644 index 00000000..746fb259 --- /dev/null +++ b/1457-pseudo-palindromic-paths-in-a-binary-tree/README.md @@ -0,0 +1,37 @@ +

1457. Pseudo-Palindromic Paths in a Binary Tree

Medium


Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.

+ +

Return the number of pseudo-palindromic paths going from the root node to leaf nodes.

+ +

 

+

Example 1:

+ +

+ +
Input: root = [2,3,1,3,1,null,1]
+Output: 2 
+Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
+
+ +

Example 2:

+ +

+ +
Input: root = [2,1,1,1,3,null,null,null,null,null,1]
+Output: 1 
+Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
+
+ +

Example 3:

+ +
Input: root = [9]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 105].
  • +
  • 1 <= Node.val <= 9
  • +
+
\ No newline at end of file diff --git a/1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp b/1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp new file mode 100644 index 00000000..4413d5c2 --- /dev/null +++ b/1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + int helper(int i, int j, int n, int m, vector& nums1, vector& nums2, vector>& dp) + { + if(i == n or j == m) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int res = INT_MIN; + + int take = nums1[i] * nums2[j] + helper(i+1, j+1, n, m, nums1, nums2, dp); + + int moveI = helper(i+1, j, n, m, nums1, nums2, dp); + + int moveJ = helper(i, j+1, n, m, nums1, nums2, dp); + + return dp[i][j] = max({take, moveI, moveJ}); + + } + + int getMaxPair(vector& nums1, vector& nums2) + { + int ans = INT_MIN; + + for(auto& fir : nums1) + { + for(auto& sec : nums2) + { + ans = max(ans, fir*sec); + } + } + + return ans; + } + + int maxDotProduct(vector& nums1, vector& nums2) { + + int n = nums1.size(); + int m = nums2.size(); + + vector> dp(n+1, vector(m+1, -1)); + + int ans = helper(0, 0, n, m, nums1, nums2, dp); + + int maxPairValue = getMaxPair(nums1, nums2); + + if(ans == 0) + return maxPairValue; + + return ans; + + } +}; \ No newline at end of file diff --git a/1458-max-dot-product-of-two-subsequences/NOTES.md b/1458-max-dot-product-of-two-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1458-max-dot-product-of-two-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1458-max-dot-product-of-two-subsequences/README.md b/1458-max-dot-product-of-two-subsequences/README.md new file mode 100644 index 00000000..d2dce7b9 --- /dev/null +++ b/1458-max-dot-product-of-two-subsequences/README.md @@ -0,0 +1,36 @@ +

1458. Max Dot Product of Two Subsequences

Hard


Given two arrays nums1 and nums2.

+ +

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

+ +

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

+ +

 

+

Example 1:

+ +
Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
+Output: 18
+Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
+Their dot product is (2*3 + (-2)*(-6)) = 18.
+ +

Example 2:

+ +
Input: nums1 = [3,-2], nums2 = [2,-6,7]
+Output: 21
+Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
+Their dot product is (3*7) = 21.
+ +

Example 3:

+ +
Input: nums1 = [-1,-1], nums2 = [1,1]
+Output: -1
+Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
+Their dot product is -1.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 500
  • +
  • -1000 <= nums1[i], nums2[i] <= 1000
  • +
+
\ No newline at end of file diff --git a/1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp b/1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp new file mode 100644 index 00000000..7b717361 --- /dev/null +++ b/1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + bool canBeEqual(vector& target, vector& arr) { + + sort(arr.begin(), arr.end()); + sort(target.begin(), target.end()); + + return arr == target; + + } +}; \ No newline at end of file diff --git a/1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md b/1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1463-cherry-pickup-ii/NOTES.md b/1463-cherry-pickup-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1463-cherry-pickup-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp b/1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp new file mode 100644 index 00000000..c3d7c4e1 --- /dev/null +++ b/1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + int maxProduct(vector& nums) { + + sort(nums.begin(), nums.end()); + + return (--nums[nums.size()-1] * --nums[nums.size()-2]); + + } +}; \ No newline at end of file diff --git a/1464-maximum-product-of-two-elements-in-an-array/NOTES.md b/1464-maximum-product-of-two-elements-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1464-maximum-product-of-two-elements-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1464-maximum-product-of-two-elements-in-an-array/README.md b/1464-maximum-product-of-two-elements-in-an-array/README.md new file mode 100644 index 00000000..c3561d41 --- /dev/null +++ b/1464-maximum-product-of-two-elements-in-an-array/README.md @@ -0,0 +1,30 @@ +

1464. Maximum Product of Two Elements in an Array

Easy


Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1). +

 

+

Example 1:

+ +
Input: nums = [3,4,5,2]
+Output: 12 
+Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 
+
+ +

Example 2:

+ +
Input: nums = [1,5,4,5]
+Output: 16
+Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
+
+ +

Example 3:

+ +
Input: nums = [3,7]
+Output: 12
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 500
  • +
  • 1 <= nums[i] <= 10^3
  • +
+
\ No newline at end of file diff --git a/1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp b/1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp new file mode 100644 index 00000000..c378674a --- /dev/null +++ b/1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int findLeastNumOfUniqueInts(vector& arr, int k) { + + map mp; + + for(auto& ele : arr) + ++mp[ele]; + + priority_queue, vector>, greater>> pq; + + for(auto&[f, e] : mp) + pq.push({e, f}); + + while(!pq.empty()) + { + auto curr = pq.top(); + pq.pop(); + + if(curr.first <= k) + { + k -= curr.first; + } + else + { + pq.push(curr); + break; + } + } + + return (int)pq.size(); + } +}; \ No newline at end of file diff --git a/1481-least-number-of-unique-integers-after-k-removals/README.md b/1481-least-number-of-unique-integers-after-k-removals/README.md new file mode 100644 index 00000000..a8ace42d --- /dev/null +++ b/1481-least-number-of-unique-integers-after-k-removals/README.md @@ -0,0 +1,26 @@ +

1481. Least Number of Unique Integers after K Removals

Medium


Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.

+ +
    +
+ +

 

+

Example 1:

+ +
Input: arr = [5,5,4], k = 1
+Output: 1
+Explanation: Remove the single 4, only 5 is left.
+
+Example 2: + +
Input: arr = [4,3,1,1,3,3,2], k = 3
+Output: 2
+Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 10^5
  • +
  • 1 <= arr[i] <= 10^9
  • +
  • 0 <= k <= arr.length
  • +
\ No newline at end of file diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp b/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp new file mode 100644 index 00000000..df9f3f33 --- /dev/null +++ b/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp @@ -0,0 +1,45 @@ +using ll = long long; + +class Solution { +public: + + int minimumNumberOfDays(int m, int k, int mid, vector& bloomDay) + { + ll bouquet = 0; + ll currSum = 0; + + for(auto& day : bloomDay) + { + currSum = (day <= mid ? currSum+1 : 0); + bouquet += (currSum / k); + currSum %= k; + } + + return bouquet >= m; + } + + int minDays(vector& bloomDay, int m, int k) { + + int low = *min_element(bloomDay.begin(), bloomDay.end()); + int high = *max_element(bloomDay.begin(), bloomDay.end()); + + int ans = INT_MAX; + + while(low <= high) + { + int mid = (low + high) / 2; + + if(minimumNumberOfDays(m, k, mid, bloomDay)) + { + ans = mid; + high = mid-1; + } + else + { + low = mid+1; + } + } + + return (ans == INT_MAX ? -1 : ans); + } +}; \ No newline at end of file diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md b/1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/README.md b/1482-minimum-number-of-days-to-make-m-bouquets/README.md new file mode 100644 index 00000000..5d2527df --- /dev/null +++ b/1482-minimum-number-of-days-to-make-m-bouquets/README.md @@ -0,0 +1,50 @@ +

1482. Minimum Number of Days to Make m Bouquets

Medium


You are given an integer array bloomDay, an integer m and an integer k.

+ +

You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

+ +

The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.

+ +

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.

+ +

 

+

Example 1:

+ +
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
+Output: 3
+Explanation: Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden.
+We need 3 bouquets each should contain 1 flower.
+After day 1: [x, _, _, _, _]   // we can only make one bouquet.
+After day 2: [x, _, _, _, x]   // we can only make two bouquets.
+After day 3: [x, _, x, _, x]   // we can make 3 bouquets. The answer is 3.
+
+ +

Example 2:

+ +
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
+Output: -1
+Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
+
+ +

Example 3:

+ +
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
+Output: 12
+Explanation: We need 2 bouquets each should have 3 flowers.
+Here is the garden after the 7 and 12 days:
+After day 7: [x, x, x, x, _, x, x]
+We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
+After day 12: [x, x, x, x, x, x, x]
+It is obvious that we can make two bouquets in different ways.
+
+ +

 

+

Constraints:

+ +
    +
  • bloomDay.length == n
  • +
  • 1 <= n <= 105
  • +
  • 1 <= bloomDay[i] <= 109
  • +
  • 1 <= m <= 106
  • +
  • 1 <= k <= n
  • +
+
\ No newline at end of file diff --git a/1496-path-crossing/1496-path-crossing.cpp b/1496-path-crossing/1496-path-crossing.cpp new file mode 100644 index 00000000..867b4685 --- /dev/null +++ b/1496-path-crossing/1496-path-crossing.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + bool isPathCrossing(string path) { + + int n = path.size(); + + set> used; + + int x = 0, y = 0; + + used.insert({x, y}); + + for(int i = 0; i < n; ++i) + { + if(path[i] == 'N') + ++y; + else if(path[i] == 'E') + ++x; + else if(path[i] == 'S') + --y; + else + --x; + if(used.count({x, y})) + return true; + used.insert({x,y}); + } + + return false; + } +}; \ No newline at end of file diff --git a/1496-path-crossing/NOTES.md b/1496-path-crossing/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1496-path-crossing/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1496-path-crossing/README.md b/1496-path-crossing/README.md new file mode 100644 index 00000000..71c6c3b5 --- /dev/null +++ b/1496-path-crossing/README.md @@ -0,0 +1,26 @@ +

1496. Path Crossing

Easy


Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.

+ +

Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise.

+ +

 

+

Example 1:

+ +
Input: path = "NES"
+Output: false 
+Explanation: Notice that the path doesn't cross any point more than once.
+
+ +

Example 2:

+ +
Input: path = "NESWW"
+Output: true
+Explanation: Notice that the path visits the origin twice.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= path.length <= 104
  • +
  • path[i] is either 'N', 'S', 'E', or 'W'.
  • +
+
\ No newline at end of file diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp b/1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp new file mode 100644 index 00000000..fb29609d --- /dev/null +++ b/1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int getLastMoment(int n, vector& left, vector& right) { + + int maxi = 0; + + for(auto& itr : left) + { + maxi = max(maxi, itr); + } + + for(auto& itr : right) + { + maxi = max(maxi, n - itr); + } + + return maxi; + + } +}; \ No newline at end of file diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md b/1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md b/1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md new file mode 100644 index 00000000..0087fe01 --- /dev/null +++ b/1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md @@ -0,0 +1,48 @@ +

1503. Last Moment Before All Ants Fall Out of a Plank

Medium


We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right.

+ +

When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.

+ +

When an ant reaches one end of the plank at a time t, it falls out of the plank immediately.

+ +

Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right, return the moment when the last ant(s) fall out of the plank.

+ +

 

+

Example 1:

+ +
Input: n = 4, left = [4,3], right = [0,1]
+Output: 4
+Explanation: In the image above:
+-The ant at index 0 is named A and going to the right.
+-The ant at index 1 is named B and going to the right.
+-The ant at index 3 is named C and going to the left.
+-The ant at index 4 is named D and going to the left.
+The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank).
+
+ +

Example 2:

+ +
Input: n = 7, left = [], right = [0,1,2,3,4,5,6,7]
+Output: 7
+Explanation: All ants are going to the right, the ant at index 0 needs 7 seconds to fall.
+
+ +

Example 3:

+ +
Input: n = 7, left = [0,1,2,3,4,5,6,7], right = []
+Output: 7
+Explanation: All ants are going to the left, the ant at index 7 needs 7 seconds to fall.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 104
  • +
  • 0 <= left.length <= n + 1
  • +
  • 0 <= left[i] <= n
  • +
  • 0 <= right.length <= n + 1
  • +
  • 0 <= right[i] <= n
  • +
  • 1 <= left.length + right.length <= n + 1
  • +
  • All values of left and right are unique, and each value can appear only in one of the two arrays.
  • +
+
\ No newline at end of file diff --git a/1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp b/1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp new file mode 100644 index 00000000..a4ebe508 --- /dev/null +++ b/1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int rangeSum(vector& nums, int n, int left, int right) { + + vector sums; + + for(int i = 0; i < n; ++i) + { + int sum = 0; + for(int j = i; j < n; ++j) + { + sum += nums[j]; + sums.push_back(sum); + } + } + + sort(sums.begin(), sums.end()); + + vector pref(sums.size()); + + pref[0] = sums[0]; + + const int mod = 1e9+7; + int ans = 0; + + for(int i = left-1; i < right; ++i) + ans = (ans + sums[i]) % mod; + + return ans; + + } +}; \ No newline at end of file diff --git a/1508-range-sum-of-sorted-subarray-sums/NOTES.md b/1508-range-sum-of-sorted-subarray-sums/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1508-range-sum-of-sorted-subarray-sums/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1508-range-sum-of-sorted-subarray-sums/README.md b/1508-range-sum-of-sorted-subarray-sums/README.md new file mode 100644 index 00000000..ac175361 --- /dev/null +++ b/1508-range-sum-of-sorted-subarray-sums/README.md @@ -0,0 +1,35 @@ +

1508. Range Sum of Sorted Subarray Sums

Medium


You are given the array nums consisting of n positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of n * (n + 1) / 2 numbers.

+ +

Return the sum of the numbers from index left to index right (indexed from 1), inclusive, in the new array. Since the answer can be a huge number return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
+Output: 13 
+Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13. 
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,4], n = 4, left = 3, right = 4
+Output: 6
+Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
+
+ +

Example 3:

+ +
Input: nums = [1,2,3,4], n = 4, left = 1, right = 10
+Output: 50
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= 100
  • +
  • 1 <= left <= right <= n * (n + 1) / 2
  • +
+
\ No newline at end of file diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp new file mode 100644 index 00000000..f0cfa336 --- /dev/null +++ b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minDifference(vector& nums) { + + int n = nums.size(); + + if(n <= 3) + return 0; + + auto calc = [&](int i, int j) + { + return nums[j] - nums[i]; + }; + + sort(nums.begin(), nums.end()); + + return min({calc(3, n-1), calc(0, n-4), calc(1, n-3), calc(2, n-2)}); + + } +}; \ No newline at end of file diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md new file mode 100644 index 00000000..ea8a7485 --- /dev/null +++ b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md @@ -0,0 +1,48 @@ +

1509. Minimum Difference Between Largest and Smallest Value in Three Moves

Medium


You are given an integer array nums.

+ +

In one move, you can choose one element of nums and change it to any value.

+ +

Return the minimum difference between the largest and smallest value of nums after performing at most three moves.

+ +

 

+

Example 1:

+ +
Input: nums = [5,3,2,4]
+Output: 0
+Explanation: We can make at most 3 moves.
+In the first move, change 2 to 3. nums becomes [5,3,3,4].
+In the second move, change 4 to 3. nums becomes [5,3,3,3].
+In the third move, change 5 to 3. nums becomes [3,3,3,3].
+After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
+
+ +

Example 2:

+ +
Input: nums = [1,5,0,10,14]
+Output: 1
+Explanation: We can make at most 3 moves.
+In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
+In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
+In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
+After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
+It can be shown that there is no way to make the difference 0 in 3 moves.
+ +

Example 3:

+ +
Input: nums = [3,100,20]
+Output: 0
+Explanation: We can make at most 3 moves.
+In the first move, change 100 to 7. nums becomes [3,7,20].
+In the second move, change 20 to 7. nums becomes [3,7,7].
+In the third move, change 3 to 7. nums becomes [7,7,7].
+After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/1512-number-of-good-pairs/1512-number-of-good-pairs.cpp b/1512-number-of-good-pairs/1512-number-of-good-pairs.cpp new file mode 100644 index 00000000..1b672930 --- /dev/null +++ b/1512-number-of-good-pairs/1512-number-of-good-pairs.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int numIdenticalPairs(vector& nums) { + + int n = nums.size(); + + unordered_map mp; + + for(auto& itr : nums) + ++mp[itr]; + + int count = 0; + + for(auto& [key, value] : mp) + { + count += (value * (value-1))/2; + } + + return count; + } +}; \ No newline at end of file diff --git a/1512-number-of-good-pairs/NOTES.md b/1512-number-of-good-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1512-number-of-good-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1512-number-of-good-pairs/README.md b/1512-number-of-good-pairs/README.md new file mode 100644 index 00000000..8cdeb85f --- /dev/null +++ b/1512-number-of-good-pairs/README.md @@ -0,0 +1,33 @@ +

1512. Number of Good Pairs

Easy


Given an array of integers nums, return the number of good pairs.

+ +

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1,1,3]
+Output: 4
+Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1]
+Output: 6
+Explanation: Each pair in the array are good.
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
+
\ No newline at end of file diff --git a/1518-water-bottles/1518-water-bottles.cpp b/1518-water-bottles/1518-water-bottles.cpp new file mode 100644 index 00000000..d7fba118 --- /dev/null +++ b/1518-water-bottles/1518-water-bottles.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int numWaterBottles(int numBottles, int numExchange) { + + int ans = numBottles; + + while((numBottles/numExchange) > 0) + { + ans += (numBottles/numExchange); + int rem = (numBottles%numExchange); + numBottles /= numExchange; + numBottles += rem; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/1518-water-bottles/NOTES.md b/1518-water-bottles/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1518-water-bottles/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1518-water-bottles/README.md b/1518-water-bottles/README.md new file mode 100644 index 00000000..cb5e9b9e --- /dev/null +++ b/1518-water-bottles/README.md @@ -0,0 +1,31 @@ +

1518. Water Bottles

Easy


There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

+ +

The operation of drinking a full water bottle turns it into an empty bottle.

+ +

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.

+ +

 

+

Example 1:

+ +
Input: numBottles = 9, numExchange = 3
+Output: 13
+Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
+Number of water bottles you can drink: 9 + 3 + 1 = 13.
+
+ +

Example 2:

+ +
Input: numBottles = 15, numExchange = 4
+Output: 19
+Explanation: You can exchange 4 empty bottles to get 1 full water bottle. 
+Number of water bottles you can drink: 15 + 3 + 1 = 19.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= numBottles <= 100
  • +
  • 2 <= numExchange <= 100
  • +
+
\ No newline at end of file diff --git a/1530-number-of-good-leaf-nodes-pairs/NOTES.md b/1530-number-of-good-leaf-nodes-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1530-number-of-good-leaf-nodes-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1531-string-compression-ii/1531-string-compression-ii.cpp b/1531-string-compression-ii/1531-string-compression-ii.cpp new file mode 100644 index 00000000..a8c534fa --- /dev/null +++ b/1531-string-compression-ii/1531-string-compression-ii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int dp[101][101]; + int dfs(string &s, int left, int K) { + int k = K; + if(s.size() - left <= k) return 0; + if(dp[left][k] >= 0) return dp[left][k]; + int res = k ? dfs(s, left + 1, k - 1) : 10000, c = 1; + for(int i = left + 1; i <= s.size(); ++i) { + res = min(res, dfs(s, i, k) + 1 + (c >= 100 ? 3 : (c >= 10 ? 2 : (c > 1 ? 1 :0)))); + if(i == s.size()) break; + if(s[i] == s[left]) ++c; + else if(--k < 0) break; + } + return dp[left][K] = res; + } + + int getLengthOfOptimalCompression(string s, int k) { + memset(dp, -1, sizeof(dp)); + return dfs(s, 0, k); + } +}; diff --git a/1531-string-compression-ii/README.md b/1531-string-compression-ii/README.md new file mode 100644 index 00000000..74ed32f7 --- /dev/null +++ b/1531-string-compression-ii/README.md @@ -0,0 +1,38 @@ +

1531. String Compression II

Hard


Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".

+ +

Notice that in this problem, we are not adding '1' after single characters.

+ +

Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.

+ +

Find the minimum length of the run-length encoded version of s after deleting at most k characters.

+ +

 

+

Example 1:

+ +
Input: s = "aaabcccd", k = 2
+Output: 4
+Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
+ +

Example 2:

+ +
Input: s = "aabbaa", k = 2
+Output: 2
+Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
+
+ +

Example 3:

+ +
Input: s = "aaaaaaaaaaa", k = 0
+Output: 3
+Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • 0 <= k <= s.length
  • +
  • s contains only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1539-kth-missing-positive-number/NOTES.md b/1539-kth-missing-positive-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1539-kth-missing-positive-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1544-make-the-string-great/1544-make-the-string-great.cpp b/1544-make-the-string-great/1544-make-the-string-great.cpp new file mode 100644 index 00000000..4c9d7994 --- /dev/null +++ b/1544-make-the-string-great/1544-make-the-string-great.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + + string makeGood(string s) { + + string ans; + + ans += s[0]; + + int n = s.size(); + + for(int i = 1; i < n; ++i) + { + if(!ans.empty() and tolower(ans.back()) == tolower(s[i])) + { + if(s[i] != ans.back()) + ans.pop_back(); + else + ans += s[i]; + } + else + ans += s[i]; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/1544-make-the-string-great/NOTES.md b/1544-make-the-string-great/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1544-make-the-string-great/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1544-make-the-string-great/README.md b/1544-make-the-string-great/README.md new file mode 100644 index 00000000..4ce6592e --- /dev/null +++ b/1544-make-the-string-great/README.md @@ -0,0 +1,46 @@ +

1544. Make The String Great

Easy


Given a string s of lower and upper case English letters.

+ +

A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:

+ +
    +
  • 0 <= i <= s.length - 2
  • +
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.
  • +
+ +

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

+ +

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

+ +

Notice that an empty string is also good.

+ +

 

+

Example 1:

+ +
Input: s = "leEeetcode"
+Output: "leetcode"
+Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
+
+ +

Example 2:

+ +
Input: s = "abBAcC"
+Output: ""
+Explanation: We have many possible scenarios, and all lead to the same answer. For example:
+"abBAcC" --> "aAcC" --> "cC" --> ""
+"abBAcC" --> "abBA" --> "aA" --> ""
+
+ +

Example 3:

+ +
Input: s = "s"
+Output: "s"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s contains only lower and upper case English letters.
  • +
+
\ No newline at end of file diff --git a/1550-three-consecutive-odds/1550-three-consecutive-odds.cpp b/1550-three-consecutive-odds/1550-three-consecutive-odds.cpp new file mode 100644 index 00000000..9d510a25 --- /dev/null +++ b/1550-three-consecutive-odds/1550-three-consecutive-odds.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool threeConsecutiveOdds(vector& arr) { + + int n = arr.size(); + + if(n < 3) + return false; + + for(int i = 2; i < n; ++i) + { + if(arr[i] & 1 and arr[i-1] & 1 and arr[i-2] & 1) return true; + } + + return false; + } +}; \ No newline at end of file diff --git a/1550-three-consecutive-odds/NOTES.md b/1550-three-consecutive-odds/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1550-three-consecutive-odds/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1550-three-consecutive-odds/README.md b/1550-three-consecutive-odds/README.md new file mode 100644 index 00000000..6efb9b86 --- /dev/null +++ b/1550-three-consecutive-odds/README.md @@ -0,0 +1,24 @@ +

1550. Three Consecutive Odds

Easy


Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false. +

 

+

Example 1:

+ +
Input: arr = [2,6,4,1]
+Output: false
+Explanation: There are no three consecutive odds.
+
+ +

Example 2:

+ +
Input: arr = [1,2,34,3,4,5,7,23,12]
+Output: true
+Explanation: [5,7,23] are three consecutive odds.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 1 <= arr[i] <= 1000
  • +
+
\ No newline at end of file diff --git a/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp new file mode 100644 index 00000000..b510ea76 --- /dev/null +++ b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + + int isPossible(int mid, int m, vector& position) + { + int prev = 0, n = position.size(); + int balls = 1 ; + + for(int i = 1; i < n; ++i) + { + if(position[i] - position[prev] >= mid) + { + ++balls; + prev = i; + } + } + + return balls >= m; + } + + int maxDistance(vector& position, int m) { + + int n = position.size(); + + sort(position.begin(), position.end()); + + int low = 1, high = position.back() - position[0]; + int ans = 0; + + while(low <= high) + { + int mid = (low + high) / 2; + + if(isPossible(mid, m, position)) + { + ans = mid; + low = mid+1; + } + else + high = mid-1; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/1552-magnetic-force-between-two-balls/NOTES.md b/1552-magnetic-force-between-two-balls/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1552-magnetic-force-between-two-balls/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1552-magnetic-force-between-two-balls/README.md b/1552-magnetic-force-between-two-balls/README.md new file mode 100644 index 00000000..eee52ed4 --- /dev/null +++ b/1552-magnetic-force-between-two-balls/README.md @@ -0,0 +1,32 @@ +

1552. Magnetic Force Between Two Balls

Medium


In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

+ +

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

+ +

Given the integer array position and the integer m. Return the required force.

+ +

 

+

Example 1:

+ +
Input: position = [1,2,3,4,7], m = 3
+Output: 3
+Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
+
+ +

Example 2:

+ +
Input: position = [5,4,3,2,1,1000000000], m = 2
+Output: 999999999
+Explanation: We can use baskets 1 and 1000000000.
+
+ +

 

+

Constraints:

+ +
    +
  • n == position.length
  • +
  • 2 <= n <= 105
  • +
  • 1 <= position[i] <= 109
  • +
  • All integers in position are distinct.
  • +
  • 2 <= m <= position.length
  • +
+
\ No newline at end of file diff --git a/1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp b/1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp new file mode 100644 index 00000000..7c93a1bf --- /dev/null +++ b/1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp @@ -0,0 +1,82 @@ +class Solution { +public: + + vector dx = {-1,0,0,+1}; + vector dy = {0,-1,+1,0}; + + void dfs(int i, int j, vector>& grid, vector>& visited) + { + visited[i][j] = 1; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(newx >= 0 and newy >= 0 and newx < grid.size() and newy < grid[0].size() and grid[newx][newy] == 1 and !visited[newx][newy]) + { + dfs(newx,newy, grid, visited); + } + } + } + + int countNumberofIslands(vector>& grid) + { + int n = grid.size(); + int m = grid[0].size(); + int count = 0; + + vector> visited(n,vector(m,0)); + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1 and !visited[i][j]) + { + ++count; + dfs(i,j,grid,visited); + } + } + } + + return count; + } + + int minDays(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + int island = countNumberofIslands(grid); + + if(island > 1 or island == 0) + return 0; + + int oneCount = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + { + grid[i][j] = 0; + + island = countNumberofIslands(grid); + + if(island > 1) + return 1; + + grid[i][j] = 1; + + ++oneCount; + } + } + } + + if(oneCount == 1) + return 1; + + return 2; + + } +}; \ No newline at end of file diff --git a/1568-minimum-number-of-days-to-disconnect-island/README.md b/1568-minimum-number-of-days-to-disconnect-island/README.md new file mode 100644 index 00000000..652dcb78 --- /dev/null +++ b/1568-minimum-number-of-days-to-disconnect-island/README.md @@ -0,0 +1,35 @@ +

1568. Minimum Number of Days to Disconnect Island

Hard


You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1's.

+ +

The grid is said to be connected if we have exactly one island, otherwise is said disconnected.

+ +

In one day, we are allowed to change any single land cell (1) into a water cell (0).

+ +

Return the minimum number of days to disconnect the grid.

+ +

 

+

Example 1:

+ +
Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
+
+Output: 2
+Explanation: We need at least 2 days to get a disconnected grid.
+Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
+
+ +

Example 2:

+ +
Input: grid = [[1,1]]
+Output: 2
+Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 30
  • +
  • grid[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp b/1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp new file mode 100644 index 00000000..7af1455a --- /dev/null +++ b/1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minCost(string colors, vector& neededTime) { + + int n = colors.size(); + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + int cost = neededTime[i], maxSegmentCost = neededTime[i]; + + while(i+1 < n and colors[i] == colors[i+1]) + { + cost += neededTime[i+1]; + maxSegmentCost = max(maxSegmentCost, neededTime[i+1]); + ++i; + } + + cost -= maxSegmentCost; + + ans += cost; + } + + return ans; + } +}; \ No newline at end of file diff --git a/1578-minimum-time-to-make-rope-colorful/NOTES.md b/1578-minimum-time-to-make-rope-colorful/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1578-minimum-time-to-make-rope-colorful/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1578-minimum-time-to-make-rope-colorful/README.md b/1578-minimum-time-to-make-rope-colorful/README.md new file mode 100644 index 00000000..6e5e854a --- /dev/null +++ b/1578-minimum-time-to-make-rope-colorful/README.md @@ -0,0 +1,40 @@ +

1578. Minimum Time to Make Rope Colorful

Medium


Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon.

+ +

Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the ith balloon from the rope.

+ +

Return the minimum time Bob needs to make the rope colorful.

+ +

 

+

Example 1:

+ +
Input: colors = "abaac", neededTime = [1,2,3,4,5]
+Output: 3
+Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
+Bob can remove the blue balloon at index 2. This takes 3 seconds.
+There are no longer two consecutive balloons of the same color. Total time = 3.
+ +

Example 2:

+ +
Input: colors = "abc", neededTime = [1,2,3]
+Output: 0
+Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
+
+ +

Example 3:

+ +
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
+Output: 2
+Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
+There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
+
+ +

 

+

Constraints:

+ +
    +
  • n == colors.length == neededTime.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= neededTime[i] <= 104
  • +
  • colors contains only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp b/1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp new file mode 100644 index 00000000..b1c99cf8 --- /dev/null +++ b/1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int numSpecial(vector>& mat) { + + int n = mat.size(), m = mat[0].size(); + + vector row(n), col(m); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(mat[i][j] == 1) + ++row[i], ++col[j]; + } + } + + int specialPosition = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(mat[i][j] and row[i] == 1 and col[j] == 1) + ++specialPosition; + } + } + + return specialPosition; + } +}; \ No newline at end of file diff --git a/1582-special-positions-in-a-binary-matrix/NOTES.md b/1582-special-positions-in-a-binary-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1582-special-positions-in-a-binary-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1582-special-positions-in-a-binary-matrix/README.md b/1582-special-positions-in-a-binary-matrix/README.md new file mode 100644 index 00000000..c973d914 --- /dev/null +++ b/1582-special-positions-in-a-binary-matrix/README.md @@ -0,0 +1,29 @@ +

1582. Special Positions in a Binary Matrix

Easy


Given an m x n binary matrix mat, return the number of special positions in mat.

+ +

A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).

+ +

 

+

Example 1:

+ +
Input: mat = [[1,0,0],[0,0,1],[1,0,0]]
+Output: 1
+Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
+
+ +

Example 2:

+ +
Input: mat = [[1,0,0],[0,1,0],[0,0,1]]
+Output: 3
+Explanation: (0, 0), (1, 1) and (2, 2) are special positions.
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 1 <= m, n <= 100
  • +
  • mat[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp b/1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp new file mode 100644 index 00000000..cab3c516 --- /dev/null +++ b/1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector> restoreMatrix(vector& rowSum, vector& colSum) { + + int m = rowSum.size(); + int n = colSum.size(); + vector> ans(m, vector(n, 0)); + + for(int i = 0; i < m; i++) { + for(int j = 0; j < n; j++) { + if (rowSum[i] == 0 || colSum[j] == 0) { + ans[i][j] = 0; + } else { + ans[i][j] = min(rowSum[i], colSum[j]); + rowSum[i] -= ans[i][j]; + colSum[j] -= ans[i][j]; + } + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/1605-find-valid-matrix-given-row-and-column-sums/NOTES.md b/1605-find-valid-matrix-given-row-and-column-sums/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1605-find-valid-matrix-given-row-and-column-sums/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp b/1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp new file mode 100644 index 00000000..2de9660e --- /dev/null +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int specialArray(vector& nums) { + + int n = nums.size(); + + vector pref(1001, 0); + + for(auto& ele : nums) + ++pref[ele]; + + for(int i = 999; i >= 0; --i) + pref[i] += pref[i+1]; + + for(int i = 1; i <= 1000; ++i) + { + if(pref[i] == i) + return i; + } + + return -1; + } +}; \ No newline at end of file diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md b/1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x/README.md b/1608-special-array-with-x-elements-greater-than-or-equal-x/README.md new file mode 100644 index 00000000..6ccc0038 --- /dev/null +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x/README.md @@ -0,0 +1,40 @@ +

1608. Special Array With X Elements Greater Than or Equal X

Easy


You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x.

+ +

Notice that x does not have to be an element in nums.

+ +

Return x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique.

+ +

 

+

Example 1:

+ +
Input: nums = [3,5]
+Output: 2
+Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
+
+ +

Example 2:

+ +
Input: nums = [0,0]
+Output: -1
+Explanation: No numbers fit the criteria for x.
+If x = 0, there should be 0 numbers >= x, but there are 2.
+If x = 1, there should be 1 number >= x, but there are 0.
+If x = 2, there should be 2 numbers >= x, but there are 0.
+x cannot be greater since there are only 2 numbers in nums.
+
+ +

Example 3:

+ +
Input: nums = [0,4,3,0,4]
+Output: 3
+Explanation: There are 3 values that are greater than or equal to 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 1000
  • +
+
\ No newline at end of file diff --git a/1609-even-odd-tree/1609-even-odd-tree.cpp b/1609-even-odd-tree/1609-even-odd-tree.cpp new file mode 100644 index 00000000..ccdc9a04 --- /dev/null +++ b/1609-even-odd-tree/1609-even-odd-tree.cpp @@ -0,0 +1,98 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + bool checkAscending(vector& currLevel) + { + int n = currLevel.size(); + + if(currLevel[0] % 2 == 0) + return false; + + for(int i = 1; i < n; ++i) + { + if(currLevel[i] <= currLevel[i-1]) + return false; + if(currLevel[i] % 2 == 0) + return false; + } + return true; + } + + bool checkDescending(vector& currLevel) + { + int n = currLevel.size(); + + if(currLevel[0] & 1) + return false; + for(int i = 1; i < n; ++i) + { + if(currLevel[i] >= currLevel[i-1]) + return false; + if(currLevel[i] & 1) + return false; + } + return true; + } + + bool isEvenOddTree(TreeNode* root) { + + int level = 0; + + queue q; + + q.push(root); + + bool isEvOddTree = true; + + while(!q.empty()) + { + int size = q.size(); + + vector currLevel; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + currLevel.push_back(curr->val); + + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + + if(level & 1) + { + if(!checkDescending(currLevel)) + { + isEvOddTree = false; + break; + } + } + else + { + if(!checkAscending(currLevel)) + { + isEvOddTree = false; + break; + } + } + } + ++level; + } + + return isEvOddTree; + } +}; \ No newline at end of file diff --git a/1609-even-odd-tree/NOTES.md b/1609-even-odd-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1609-even-odd-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1609-even-odd-tree/README.md b/1609-even-odd-tree/README.md new file mode 100644 index 00000000..706e4f8f --- /dev/null +++ b/1609-even-odd-tree/README.md @@ -0,0 +1,49 @@ +

1609. Even Odd Tree

Medium


A binary tree is named Even-Odd if it meets the following conditions:

+ +
    +
  • The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
  • +
  • For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
  • +
  • For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
  • +
+ +

Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.

+ +

 

+

Example 1:

+ +
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
+Output: true
+Explanation: The node values on each level are:
+Level 0: [1]
+Level 1: [10,4]
+Level 2: [3,7,9]
+Level 3: [12,8,6,2]
+Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
+
+ +

Example 2:

+ +
Input: root = [5,4,2,3,3,7]
+Output: false
+Explanation: The node values on each level are:
+Level 0: [5]
+Level 1: [4,2]
+Level 2: [3,3,7]
+Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
+
+ +

Example 3:

+ +
Input: root = [5,9,1,3,5,7]
+Output: false
+Explanation: Node values in the level 1 should be even integers.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 105].
  • +
  • 1 <= Node.val <= 106
  • +
+
\ No newline at end of file diff --git a/1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp b/1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp new file mode 100644 index 00000000..9cc248b4 --- /dev/null +++ b/1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxDepth(string s) { + + int open = 0, ans = 0; + + for(auto& ch : s) + { + if(ch == '(') + ++open; + else if(ch == ')') + --open; + ans = max(ans, open); + } + + return ans; + } +}; \ No newline at end of file diff --git a/1614-maximum-nesting-depth-of-the-parentheses/NOTES.md b/1614-maximum-nesting-depth-of-the-parentheses/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1614-maximum-nesting-depth-of-the-parentheses/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1614-maximum-nesting-depth-of-the-parentheses/README.md b/1614-maximum-nesting-depth-of-the-parentheses/README.md new file mode 100644 index 00000000..b636dfcb --- /dev/null +++ b/1614-maximum-nesting-depth-of-the-parentheses/README.md @@ -0,0 +1,44 @@ +

1614. Maximum Nesting Depth of the Parentheses

Easy


A string is a valid parentheses string (denoted VPS) if it meets one of the following:

+ +
    +
  • It is an empty string "", or a single character not equal to "(" or ")",
  • +
  • It can be written as AB (A concatenated with B), where A and B are VPS's, or
  • +
  • It can be written as (A), where A is a VPS.
  • +
+ +

We can similarly define the nesting depth depth(S) of any VPS S as follows:

+ +
    +
  • depth("") = 0
  • +
  • depth(C) = 0, where C is a string with a single character not equal to "(" or ")".
  • +
  • depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's.
  • +
  • depth("(" + A + ")") = 1 + depth(A), where A is a VPS.
  • +
+ +

For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.

+ +

Given a VPS represented as string s, return the nesting depth of s.

+ +

 

+

Example 1:

+ +
Input: s = "(1+(2*3)+((8)/4))+1"
+Output: 3
+Explanation: Digit 8 is inside of 3 nested parentheses in the string.
+
+ +

Example 2:

+ +
Input: s = "(1)+((2))+(((3)))"
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.
  • +
  • It is guaranteed that parentheses expression s is a VPS.
  • +
+
\ No newline at end of file diff --git a/1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp b/1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp new file mode 100644 index 00000000..64fb29c3 --- /dev/null +++ b/1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maxLengthBetweenEqualCharacters(string s) { + + int n = s.size(); + + int ans = -1; + + vector occurence(26, -1); + + for(int i = 0; i < n; ++i) + { + int firstOccurence = occurence[s[i] - 'a']; + + if(firstOccurence != -1) + ans = max(ans, i - firstOccurence - 1); + else + occurence[s[i] - 'a'] = i; + } + + return ans ; + } +}; \ No newline at end of file diff --git a/1624-largest-substring-between-two-equal-characters/NOTES.md b/1624-largest-substring-between-two-equal-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1624-largest-substring-between-two-equal-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1624-largest-substring-between-two-equal-characters/README.md b/1624-largest-substring-between-two-equal-characters/README.md new file mode 100644 index 00000000..cfc9757d --- /dev/null +++ b/1624-largest-substring-between-two-equal-characters/README.md @@ -0,0 +1,33 @@ +

1624. Largest Substring Between Two Equal Characters

Easy


Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "aa"
+Output: 0
+Explanation: The optimal substring here is an empty substring between the two 'a's.
+ +

Example 2:

+ +
Input: s = "abca"
+Output: 2
+Explanation: The optimal substring here is "bc".
+
+ +

Example 3:

+ +
Input: s = "cbzxy"
+Output: -1
+Explanation: There are no characters that appear twice in s.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 300
  • +
  • s contains only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1636-sort-array-by-increasing-frequency/README.md b/1636-sort-array-by-increasing-frequency/README.md new file mode 100644 index 00000000..cfe5e97a --- /dev/null +++ b/1636-sort-array-by-increasing-frequency/README.md @@ -0,0 +1,32 @@ +

1636. Sort Array by Increasing Frequency

Easy


Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

+ +

Return the sorted array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,1,2,2,2,3]
+Output: [3,1,1,2,2,2]
+Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
+
+ +

Example 2:

+ +
Input: nums = [2,3,1,3,2]
+Output: [1,3,3,2,2]
+Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
+
+ +

Example 3:

+ +
Input: nums = [-1,1,-6,4,5,-6,1,4,1]
+Output: [5,-1,4,4,-6,-6,1,1,1]
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • -100 <= nums[i] <= 100
  • +
+
\ No newline at end of file diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp b/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp new file mode 100644 index 00000000..8da3ae7b --- /dev/null +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxWidthOfVerticalArea(vector>& points) { + + int n = points.size(); + + sort(points.begin(), points.end()); + + int ans = 0; + + for(int i = 1; i < n; ++i) + ans = max(ans, points[i][0] - points[i-1][0]); + + return ans; + + } +}; \ No newline at end of file diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md b/1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/README.md b/1637-widest-vertical-area-between-two-points-containing-no-points/README.md new file mode 100644 index 00000000..1dd90505 --- /dev/null +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/README.md @@ -0,0 +1,30 @@ +

1637. Widest Vertical Area Between Two Points Containing No Points

Medium


Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area.

+ +

A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width.

+ +

Note that points on the edge of a vertical area are not considered included in the area.

+ +

 

+

Example 1:

+​ +
Input: points = [[8,7],[9,9],[7,4],[9,7]]
+Output: 1
+Explanation: Both the red and the blue area are optimal.
+
+ +

Example 2:

+ +
Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • n == points.length
  • +
  • 2 <= n <= 105
  • +
  • points[i].length == 2
  • +
  • 0 <= xi, yi <= 109
  • +
+
\ No newline at end of file diff --git a/1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp b/1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp new file mode 100644 index 00000000..d94b970b --- /dev/null +++ b/1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int furthestBuilding(vector& heights, int bricks, int ladders) { + priority_queue pq; + + int i = 0; + for(i = 0; i1642. Furthest Building You Can Reach

Medium


You are given an integer array heights representing the heights of buildings, some bricks, and some ladders.

+ +

You start your journey from building 0 and move to the next building by possibly using bricks or ladders.

+ +

While moving from building i to building i+1 (0-indexed),

+ +
    +
  • If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
  • +
  • If the current building's height is less than the next building's height, you can either use one ladder or (h[i+1] - h[i]) bricks.
  • +
+ +

Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.

+ +

 

+

Example 1:

+ +
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
+Output: 4
+Explanation: Starting at building 0, you can follow these steps:
+- Go to building 1 without using ladders nor bricks since 4 >= 2.
+- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
+- Go to building 3 without using ladders nor bricks since 7 >= 6.
+- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
+It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
+
+ +

Example 2:

+ +
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
+Output: 7
+
+ +

Example 3:

+ +
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= heights.length <= 105
  • +
  • 1 <= heights[i] <= 106
  • +
  • 0 <= bricks <= 109
  • +
  • 0 <= ladders <= heights.length
  • +
+
\ No newline at end of file diff --git a/1653-minimum-deletions-to-make-string-balanced/README.md b/1653-minimum-deletions-to-make-string-balanced/README.md new file mode 100644 index 00000000..8dc82bd2 --- /dev/null +++ b/1653-minimum-deletions-to-make-string-balanced/README.md @@ -0,0 +1,31 @@ +

1653. Minimum Deletions to Make String Balanced

Medium


You are given a string s consisting only of characters 'a' and 'b'​​​​.

+ +

You can delete any number of characters in s to make s balanced. s is balanced if there is no pair of indices (i,j) such that i < j and s[i] = 'b' and s[j]= 'a'.

+ +

Return the minimum number of deletions needed to make s balanced.

+ +

 

+

Example 1:

+ +
Input: s = "aababbab"
+Output: 2
+Explanation: You can either:
+Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
+Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
+
+ +

Example 2:

+ +
Input: s = "bbaaaaabb"
+Output: 2
+Explanation: The only solution is to delete the first two characters.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s[i] is 'a' or 'b'​​.
  • +
+
\ No newline at end of file diff --git a/1657-determine-if-two-strings-are-close/NOTES.md b/1657-determine-if-two-strings-are-close/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1657-determine-if-two-strings-are-close/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp b/1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp new file mode 100644 index 00000000..2da26e6b --- /dev/null +++ b/1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) { + + ListNode* temp = list1, *temp2 = list2, *prev = temp2; + + while(temp2) + { + prev = temp2; + temp2 = temp2->next; + } + + ListNode* half = nullptr, *nex = nullptr; + + int curr = 0; + + while(temp) + { + ++curr; + if(curr == a) + { + half = temp; + } + if(curr == b+2) + { + nex = temp; + } + temp = temp->next; + } + + half->next = list2; + prev->next = nex; + + return list1; + } +}; \ No newline at end of file diff --git a/1669-merge-in-between-linked-lists/README.md b/1669-merge-in-between-linked-lists/README.md new file mode 100644 index 00000000..80ce1897 --- /dev/null +++ b/1669-merge-in-between-linked-lists/README.md @@ -0,0 +1,32 @@ +

1669. Merge In Between Linked Lists

Medium


You are given two linked lists: list1 and list2 of sizes n and m respectively.

+ +

Remove list1's nodes from the ath node to the bth node, and put list2 in their place.

+ +

The blue edges and nodes in the following figure indicate the result:

+ +

Build the result list and return its head.

+ +

 

+

Example 1:

+ +
Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
+Output: [10,1,13,1000000,1000001,1000002,5]
+Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
+
+ +

Example 2:

+ +
Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
+Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
+Explanation: The blue edges and nodes in the above figure indicate the result.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= list1.length <= 104
  • +
  • 1 <= a <= b < list1.length - 1
  • +
  • 1 <= list2.length <= 104
  • +
+
\ No newline at end of file diff --git a/1683-invalid-tweets/1683-invalid-tweets.sql b/1683-invalid-tweets/1683-invalid-tweets.sql new file mode 100644 index 00000000..122290c0 --- /dev/null +++ b/1683-invalid-tweets/1683-invalid-tweets.sql @@ -0,0 +1,5 @@ +# Write your MySQL query statement below + +select tweet_id from Tweets +where char_length(content) > 15; + diff --git a/1683-invalid-tweets/NOTES.md b/1683-invalid-tweets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1683-invalid-tweets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1683-invalid-tweets/README.md b/1683-invalid-tweets/README.md new file mode 100644 index 00000000..ffab3320 --- /dev/null +++ b/1683-invalid-tweets/README.md @@ -0,0 +1,42 @@ +

1683. Invalid Tweets

Easy


Table: Tweets

+ +
+----------------+---------+
+| Column Name    | Type    |
++----------------+---------+
+| tweet_id       | int     |
+| content        | varchar |
++----------------+---------+
+tweet_id is the primary key (column with unique values) for this table.
+This table contains all the tweets in a social media app.
+
+ +

 

+ +

Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.

+ +

Return the result table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Tweets table:
++----------+----------------------------------+
+| tweet_id | content                          |
++----------+----------------------------------+
+| 1        | Vote for Biden                   |
+| 2        | Let us make America great again! |
++----------+----------------------------------+
+Output: 
++----------+
+| tweet_id |
++----------+
+| 2        |
++----------+
+Explanation: 
+Tweet 1 has length = 14. It is a valid tweet.
+Tweet 2 has length = 32. It is an invalid tweet.
+
+
\ No newline at end of file diff --git a/1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp b/1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp new file mode 100644 index 00000000..e25a2d5b --- /dev/null +++ b/1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector getSumAbsoluteDifferences(vector& nums) { + + int n = nums.size(); + + vector pref(n+1,0); + + for(int i = 1; i <= n; ++i) + pref[i] = pref[i-1] + nums[i-1]; + + vector ans; + + for(int i = 1; i <= n; ++i) + { + long long leftSum = nums[i-1] * 1LL * i; + long long left = leftSum - pref[i]; + + long long rightSum = nums[i-1] * 1LL * (n-i); + long long right = pref[n] - pref[i] - rightSum; + + ans.push_back(left + right); + } + + return ans; + } +}; \ No newline at end of file diff --git a/1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md b/1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp b/1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp new file mode 100644 index 00000000..07c848a7 --- /dev/null +++ b/1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int countStudents(vector& students, vector& sandwiches) { + + int n = students.size(); + + vector freq(2, 0); + + for(auto& ele : students) + ++freq[ele]; + + int cnt = 0; + + for(auto& ele : sandwiches) + { + if(freq[ele] > 0) + { + ++cnt; + --freq[ele]; + } + else + break; + } + + return n - cnt; + + } +}; \ No newline at end of file diff --git a/1700-number-of-students-unable-to-eat-lunch/NOTES.md b/1700-number-of-students-unable-to-eat-lunch/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1700-number-of-students-unable-to-eat-lunch/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1700-number-of-students-unable-to-eat-lunch/README.md b/1700-number-of-students-unable-to-eat-lunch/README.md new file mode 100644 index 00000000..6d562160 --- /dev/null +++ b/1700-number-of-students-unable-to-eat-lunch/README.md @@ -0,0 +1,46 @@ +

1700. Number of Students Unable to Eat Lunch

Easy


The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

+ +

The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:

+ +
    +
  • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
  • +
  • Otherwise, they will leave it and go to the queue's end.
  • +
+ +

This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

+ +

You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

+ +

 

+

Example 1:

+ +
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
+Output: 0 
+Explanation:
+- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
+- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
+- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
+- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
+- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
+- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
+- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
+- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
+Hence all students are able to eat.
+
+ +

Example 2:

+ +
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= students.length, sandwiches.length <= 100
  • +
  • students.length == sandwiches.length
  • +
  • sandwiches[i] is 0 or 1.
  • +
  • students[i] is 0 or 1.
  • +
+
\ No newline at end of file diff --git a/1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp b/1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp new file mode 100644 index 00000000..1bba9dac --- /dev/null +++ b/1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int totalMoney(int n) { + + int totMoney = 0; + int track = 1, day = 0, curr = 1; + + + for(int i = 1; i<=n; ++i) + { + totMoney += curr; + ++curr; + ++day; + + if(day == 7) + { + curr = track+1; + ++track; + day = 0; + } + } + + return totMoney; + } +}; \ No newline at end of file diff --git a/1716-calculate-money-in-leetcode-bank/NOTES.md b/1716-calculate-money-in-leetcode-bank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1716-calculate-money-in-leetcode-bank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1716-calculate-money-in-leetcode-bank/README.md b/1716-calculate-money-in-leetcode-bank/README.md new file mode 100644 index 00000000..3ebe921b --- /dev/null +++ b/1716-calculate-money-in-leetcode-bank/README.md @@ -0,0 +1,35 @@ +

1716. Calculate Money in Leetcode Bank

Easy


Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.

+ +

He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the previous Monday.

+ +

Given n, return the total amount of money he will have in the Leetcode bank at the end of the nth day.

+ +

 

+

Example 1:

+ +
Input: n = 4
+Output: 10
+Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.
+
+ +

Example 2:

+ +
Input: n = 10
+Output: 37
+Explanation: After the 10th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2nd Monday, Hercy only puts in $2.
+
+ +

Example 3:

+ +
Input: n = 20
+Output: 96
+Explanation: After the 20th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file diff --git a/1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp b/1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp new file mode 100644 index 00000000..3eed51f2 --- /dev/null +++ b/1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int largestSubmatrix(vector>& matrix) { + + int i, j, ans = 0, n = matrix.size(), m = matrix[0].size(); + + for(i = 0; i < m; i++) + { + for(j = 1; j < n; j++) + { + if(matrix[j][i] == 1) + matrix[j][i] = matrix[j-1][i] + matrix[j][i]; + else + matrix[j][i] = 0; + } + } + + for(i = 0; i < n; i++) + { + sort(matrix[i].begin(), matrix[i].end(), greater()); + + for(j = 0; j < m; j++) + ans = max(ans, matrix[i][j] * (j + 1)); + } + + return ans; + } +}; \ No newline at end of file diff --git a/1727-largest-submatrix-with-rearrangements/NOTES.md b/1727-largest-submatrix-with-rearrangements/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1727-largest-submatrix-with-rearrangements/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1727-largest-submatrix-with-rearrangements/README.md b/1727-largest-submatrix-with-rearrangements/README.md new file mode 100644 index 00000000..e30720f4 --- /dev/null +++ b/1727-largest-submatrix-with-rearrangements/README.md @@ -0,0 +1,38 @@ +

1727. Largest Submatrix With Rearrangements

Medium


You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.

+ +

Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.

+ +

 

+

Example 1:

+ +
Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
+Output: 4
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 4.
+
+ +

Example 2:

+ +
Input: matrix = [[1,0,1,0,1]]
+Output: 3
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 3.
+
+ +

Example 3:

+ +
Input: matrix = [[1,1,0],[1,0,1]]
+Output: 2
+Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m * n <= 105
  • +
  • matrix[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp b/1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp new file mode 100644 index 00000000..483ec42c --- /dev/null +++ b/1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + vector restoreArray(vector>& adjacentPairs) { + + unordered_map> adj; + unordered_map mp; + + int sv = -1; + + for(auto& itr : adjacentPairs) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + + ++mp[itr[0]]; + ++mp[itr[1]]; + } + + for(auto& [f, s] : mp) + { + if(s == 1) + sv = f; + } + + vector ans; + + unordered_set visited; + + function dfs = [&](int sv) + { + ans.push_back(sv); + visited.insert(sv); + + for(auto& itr : adj[sv]) + { + if(!visited.count(itr)) + dfs(itr); + } + }; + + // cout<1743. Restore the Array From Adjacent Pairs

Medium


There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.

+ +

You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.

+ +

It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.

+ +

Return the original array nums. If there are multiple solutions, return any of them.

+ +

 

+

Example 1:

+ +
Input: adjacentPairs = [[2,1],[3,4],[3,2]]
+Output: [1,2,3,4]
+Explanation: This array has all its adjacent pairs in adjacentPairs.
+Notice that adjacentPairs[i] may not be in left-to-right order.
+
+ +

Example 2:

+ +
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
+Output: [-2,4,1,-3]
+Explanation: There can be negative numbers.
+Another solution is [-3,1,4,-2], which would also be accepted.
+
+ +

Example 3:

+ +
Input: adjacentPairs = [[100000,-100000]]
+Output: [100000,-100000]
+
+ +

 

+

Constraints:

+ +
    +
  • nums.length == n
  • +
  • adjacentPairs.length == n - 1
  • +
  • adjacentPairs[i].length == 2
  • +
  • 2 <= n <= 105
  • +
  • -105 <= nums[i], ui, vi <= 105
  • +
  • There exists some nums that has adjacentPairs as its pairs.
  • +
+
\ No newline at end of file diff --git a/1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp b/1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp new file mode 100644 index 00000000..b5a5ce2c --- /dev/null +++ b/1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minimumLength(string s) { + + int n = s.size(); + int i = 0, j = n-1; + + while(i < j) + { + char pref = s[i]; + char suff = s[j]; + + if(s[i] != s[j]) + break; + + while(j >= 0 and s[j] == pref) + --j; + while(i < j and s[i] == suff) + ++i; + } + + int ans = j - i + 1; + + return max(ans, 0); + } +}; \ No newline at end of file diff --git a/1750-minimum-length-of-string-after-deleting-similar-ends/README.md b/1750-minimum-length-of-string-after-deleting-similar-ends/README.md new file mode 100644 index 00000000..c64aa797 --- /dev/null +++ b/1750-minimum-length-of-string-after-deleting-similar-ends/README.md @@ -0,0 +1,47 @@ +

1750. Minimum Length of String After Deleting Similar Ends

Medium


Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:

+ +
    +
  1. Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
  2. +
  3. Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
  4. +
  5. The prefix and the suffix should not intersect at any index.
  6. +
  7. The characters from the prefix and suffix must be the same.
  8. +
  9. Delete both the prefix and the suffix.
  10. +
+ +

Return the minimum length of s after performing the above operation any number of times (possibly zero times).

+ +

 

+

Example 1:

+ +
Input: s = "ca"
+Output: 2
+Explanation: You can't remove any characters, so the string stays as is.
+
+ +

Example 2:

+ +
Input: s = "cabaabac"
+Output: 0
+Explanation: An optimal sequence of operations is:
+- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
+- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
+- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
+- Take prefix = "a" and suffix = "a" and remove them, s = "".
+ +

Example 3:

+ +
Input: s = "aabccabba"
+Output: 3
+Explanation: An optimal sequence of operations is:
+- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
+- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s only consists of characters 'a', 'b', and 'c'.
  • +
+
\ No newline at end of file diff --git a/1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql b/1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql new file mode 100644 index 00000000..68d567d4 --- /dev/null +++ b/1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select product_id from Products where low_fats = 'Y' and recyclable = 'Y'; diff --git a/1757-recyclable-and-low-fat-products/NOTES.md b/1757-recyclable-and-low-fat-products/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1757-recyclable-and-low-fat-products/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp new file mode 100644 index 00000000..636a953e --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minOperations(string s) { + + string startZero, startOne; + + int n = s.size(), cnt1 = 0, cnt2 = 0; + + for(int i = 0; i < n; ++i) + { + if(i%2 == 0) + startZero += '0'; + else + startZero += '1'; + + if(i%2 == 0) + startOne += '1'; + else + startOne += '0'; + + if(startZero.back() != s[i]) + ++cnt1; + if(startOne.back() != s[i]) + ++cnt2; + } + + return min(cnt1, cnt2); + + } +}; \ No newline at end of file diff --git a/1758-minimum-changes-to-make-alternating-binary-string/NOTES.md b/1758-minimum-changes-to-make-alternating-binary-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1758-minimum-changes-to-make-alternating-binary-string/README.md b/1758-minimum-changes-to-make-alternating-binary-string/README.md new file mode 100644 index 00000000..960678e9 --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/README.md @@ -0,0 +1,36 @@ +

1758. Minimum Changes To Make Alternating Binary String

Easy


You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.

+ +

The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.

+ +

Return the minimum number of operations needed to make s alternating.

+ +

 

+

Example 1:

+ +
Input: s = "0100"
+Output: 1
+Explanation: If you change the last character to '1', s will be "0101", which is alternating.
+
+ +

Example 2:

+ +
Input: s = "10"
+Output: 0
+Explanation: s is already alternating.
+
+ +

Example 3:

+ +
Input: s = "1111"
+Output: 2
+Explanation: You need two operations to reach "0101" or "1010".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s[i] is either '0' or '1'.
  • +
+
\ No newline at end of file diff --git a/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp new file mode 100644 index 00000000..02b8bc5c --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int countHomogenous(string s) { + + int n = s.size(), cnt = 0, prev = 0, res = 0; + + const int mod = 1e9 + 7; + + for(auto& itr : s) + { + cnt = (itr == prev ? cnt+1 : 1); + + prev = itr; + + res = (res + cnt) % mod; + } + + return res; + + } +}; \ No newline at end of file diff --git a/1759-count-number-of-homogenous-substrings/NOTES.md b/1759-count-number-of-homogenous-substrings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1759-count-number-of-homogenous-substrings/README.md b/1759-count-number-of-homogenous-substrings/README.md new file mode 100644 index 00000000..2e82ed3b --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/README.md @@ -0,0 +1,40 @@ +

1759. Count Number of Homogenous Substrings

Medium


Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.

+ +

A string is homogenous if all the characters of the string are the same.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "abbcccaa"
+Output: 13
+Explanation: The homogenous substrings are listed as below:
+"a"   appears 3 times.
+"aa"  appears 1 time.
+"b"   appears 2 times.
+"bb"  appears 1 time.
+"c"   appears 3 times.
+"cc"  appears 2 times.
+"ccc" appears 1 time.
+3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
+ +

Example 2:

+ +
Input: s = "xy"
+Output: 2
+Explanation: The homogenous substrings are "x" and "y".
+ +

Example 3:

+ +
Input: s = "zzzzz"
+Output: 15
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase letters.
  • +
\ No newline at end of file diff --git a/1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp b/1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp new file mode 100644 index 00000000..fd3ab8d2 --- /dev/null +++ b/1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int findCenter(vector>& edges) { + + if(edges[0][0]==edges[1][0] || edges[0][0]==edges[1][1]) + return edges[0][0]; + + else return edges[0][1]; + + } +}; \ No newline at end of file diff --git a/1791-find-center-of-star-graph/README.md b/1791-find-center-of-star-graph/README.md new file mode 100644 index 00000000..fc958c58 --- /dev/null +++ b/1791-find-center-of-star-graph/README.md @@ -0,0 +1,30 @@ +

1791. Find Center of Star Graph

Easy


There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.

+ +

You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph.

+ +

 

+

Example 1:

+ +
Input: edges = [[1,2],[2,3],[4,2]]
+Output: 2
+Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
+
+ +

Example 2:

+ +
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n <= 105
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 1 <= ui, vi <= n
  • +
  • ui != vi
  • +
  • The given edges represent a valid star graph.
  • +
+
\ No newline at end of file diff --git a/1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp b/1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp new file mode 100644 index 00000000..1e22038a --- /dev/null +++ b/1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int maximumScore(vector& nums, int k) { + + int n = nums.size(); + + int i = k, j = k; + + int ans = nums[k], currMin = nums[k]; + + while(i > 0 or j < n-1) + { + int left = (i > 0 ? nums[i-1] : 0); + int right = (j < n-1 ? nums[j+1] : 0); + + if(left > right) + { + --i; + currMin = min(currMin, nums[i]); + } + else + { + ++j; + currMin = min(currMin,nums[j]); + } + + ans = max(ans, currMin * (j-i+1)); + } + + return ans; + } +}; \ No newline at end of file diff --git a/1793-maximum-score-of-a-good-subarray/NOTES.md b/1793-maximum-score-of-a-good-subarray/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1793-maximum-score-of-a-good-subarray/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1793-maximum-score-of-a-good-subarray/README.md b/1793-maximum-score-of-a-good-subarray/README.md new file mode 100644 index 00000000..d32c4f68 --- /dev/null +++ b/1793-maximum-score-of-a-good-subarray/README.md @@ -0,0 +1,30 @@ +

1793. Maximum Score of a Good Subarray

Hard


You are given an array of integers nums (0-indexed) and an integer k.

+ +

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.

+ +

Return the maximum possible score of a good subarray.

+ +

 

+

Example 1:

+ +
Input: nums = [1,4,3,7,4,5], k = 3
+Output: 15
+Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
+
+ +

Example 2:

+ +
Input: nums = [5,5,4,5,4,1,1,1], k = 0
+Output: 20
+Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 2 * 104
  • +
  • 0 <= k < nums.length
  • +
+
\ No newline at end of file diff --git a/1845-seat-reservation-manager/1845-seat-reservation-manager.cpp b/1845-seat-reservation-manager/1845-seat-reservation-manager.cpp new file mode 100644 index 00000000..1931d631 --- /dev/null +++ b/1845-seat-reservation-manager/1845-seat-reservation-manager.cpp @@ -0,0 +1,46 @@ +class SeatManager { +public: + + set st; + vector seat; + int counter = 1; + int N; + + SeatManager(int n) { + N = n; + seat.resize(n+1, 0); + } + + int reserve() { + + if(!st.empty()) + { + int least = *st.begin(); + st.erase(*st.begin()); + seat[least] = 1; + return least; + } + + if(counter <= N) + { + seat[counter] = 1; + int smallestUnreserved = counter; + ++counter; + return smallestUnreserved; + } + + return -1; + } + + void unreserve(int seatNumber) { + st.insert(seatNumber); + seat[seatNumber] = 0; + } +}; + +/** + * Your SeatManager object will be instantiated and called as such: + * SeatManager* obj = new SeatManager(n); + * int param_1 = obj->reserve(); + * obj->unreserve(seatNumber); + */ \ No newline at end of file diff --git a/1845-seat-reservation-manager/NOTES.md b/1845-seat-reservation-manager/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1845-seat-reservation-manager/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1845-seat-reservation-manager/README.md b/1845-seat-reservation-manager/README.md new file mode 100644 index 00000000..889dd702 --- /dev/null +++ b/1845-seat-reservation-manager/README.md @@ -0,0 +1,42 @@ +

1845. Seat Reservation Manager

Medium


Design a system that manages the reservation state of n seats that are numbered from 1 to n.

+ +

Implement the SeatManager class:

+ +
    +
  • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
  • +
  • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
  • +
  • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
  • +
+ +

 

+

Example 1:

+ +
Input
+["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
+[[5], [], [], [2], [], [], [], [], [5]]
+Output
+[null, 1, 2, null, 2, 3, 4, 5, null]
+
+Explanation
+SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
+seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
+seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
+seatManager.reserve();    // The only available seat is seat 5, so return 5.
+seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= seatNumber <= n
  • +
  • For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
  • +
  • For each call to unreserve, it is guaranteed that seatNumber will be reserved.
  • +
  • At most 105 calls in total will be made to reserve and unreserve.
  • +
+
\ No newline at end of file diff --git a/1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp b/1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp new file mode 100644 index 00000000..8aa5e6d0 --- /dev/null +++ b/1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int subsetXORSum(vector& nums) { + + int n = nums.size(); + + int range = (1 << n); + + int ans = 0; + + for(int i = 0; i < range; ++i) + { + int curXor = 0; + for(int j = 0; j < n; ++j) + { + if(i & (1 << j)) + curXor ^= nums[j]; + } + ans += curXor; + } + + return ans; + } +}; \ No newline at end of file diff --git a/1863-sum-of-all-subset-xor-totals/NOTES.md b/1863-sum-of-all-subset-xor-totals/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1863-sum-of-all-subset-xor-totals/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1863-sum-of-all-subset-xor-totals/README.md b/1863-sum-of-all-subset-xor-totals/README.md new file mode 100644 index 00000000..4468942a --- /dev/null +++ b/1863-sum-of-all-subset-xor-totals/README.md @@ -0,0 +1,56 @@ +

1863. Sum of All Subset XOR Totals

Easy


The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.

+ +
    +
  • For example, the XOR total of the array [2,5,6] is 2 XOR 5 XOR 6 = 1.
  • +
+ +

Given an array nums, return the sum of all XOR totals for every subset of nums

+ +

Note: Subsets with the same elements should be counted multiple times.

+ +

An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3]
+Output: 6
+Explanation: The 4 subsets of [1,3] are:
+- The empty subset has an XOR total of 0.
+- [1] has an XOR total of 1.
+- [3] has an XOR total of 3.
+- [1,3] has an XOR total of 1 XOR 3 = 2.
+0 + 1 + 3 + 2 = 6
+
+ +

Example 2:

+ +
Input: nums = [5,1,6]
+Output: 28
+Explanation: The 8 subsets of [5,1,6] are:
+- The empty subset has an XOR total of 0.
+- [5] has an XOR total of 5.
+- [1] has an XOR total of 1.
+- [6] has an XOR total of 6.
+- [5,1] has an XOR total of 5 XOR 1 = 4.
+- [5,6] has an XOR total of 5 XOR 6 = 3.
+- [1,6] has an XOR total of 1 XOR 6 = 7.
+- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
+0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
+
+ +

Example 3:

+ +
Input: nums = [3,4,5,6,7,8]
+Output: 480
+Explanation: The sum of all XOR totals for every subset is 480.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 12
  • +
  • 1 <= nums[i] <= 20
  • +
+
\ No newline at end of file diff --git a/1877-minimize-maximum-pair-sum-in-array/NOTES.md b/1877-minimize-maximum-pair-sum-in-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1877-minimize-maximum-pair-sum-in-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1877-minimize-maximum-pair-sum-in-array/README.md b/1877-minimize-maximum-pair-sum-in-array/README.md new file mode 100644 index 00000000..f49c9aa1 --- /dev/null +++ b/1877-minimize-maximum-pair-sum-in-array/README.md @@ -0,0 +1,41 @@ +

1877. Minimize Maximum Pair Sum in Array

Medium


The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs.

+ +
    +
  • For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8.
  • +
+ +

Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:

+ +
    +
  • Each element of nums is in exactly one pair, and
  • +
  • The maximum pair sum is minimized.
  • +
+ +

Return the minimized maximum pair sum after optimally pairing up the elements.

+ +

 

+

Example 1:

+ +
Input: nums = [3,5,2,3]
+Output: 7
+Explanation: The elements can be paired up into pairs (3,3) and (5,2).
+The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
+
+ +

Example 2:

+ +
Input: nums = [3,5,4,2,4,6]
+Output: 8
+Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
+The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 2 <= n <= 105
  • +
  • n is even.
  • +
  • 1 <= nums[i] <= 105
  • +
\ No newline at end of file diff --git a/1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp b/1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp new file mode 100644 index 00000000..894f7ffb --- /dev/null +++ b/1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int reductionOperations(vector& nums) { + + int n = nums.size(), ans = 0; + + sort(nums.begin(), nums.end()); + + for(int i = n-1; i > 0; --i) + { + if(nums[i] != nums[i-1]) + { + ans += (n - i); + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/1887-reduction-operations-to-make-the-array-elements-equal/README.md b/1887-reduction-operations-to-make-the-array-elements-equal/README.md new file mode 100644 index 00000000..a2eb1b38 --- /dev/null +++ b/1887-reduction-operations-to-make-the-array-elements-equal/README.md @@ -0,0 +1,47 @@ +

1887. Reduction Operations to Make the Array Elements Equal

Medium


Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:

+ +
    +
  1. Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
  2. +
  3. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
  4. +
  5. Reduce nums[i] to nextLargest.
  6. +
+ +

Return the number of operations to make all elements in nums equal.

+ +

 

+

Example 1:

+ +
Input: nums = [5,1,3]
+Output: 3
+Explanation: It takes 3 operations to make all elements in nums equal:
+1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
+2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
+3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
+
+ +

Example 2:

+ +
Input: nums = [1,1,1]
+Output: 0
+Explanation: All elements in nums are already equal.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2,2,3]
+Output: 4
+Explanation: It takes 4 operations to make all elements in nums equal:
+1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
+2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
+3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
+4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 1 <= nums[i] <= 5 * 104
  • +
+
\ No newline at end of file diff --git a/1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp b/1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp new file mode 100644 index 00000000..38a9b341 --- /dev/null +++ b/1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + bool makeEqual(vector& words) { + + int n = words.size(); + + bool ok = true; + + vector freq(26, 0); + + for(auto& word : words) + { + for(auto& ch : word) + ++freq[ch-'a']; + + } + + for(auto& f : freq) + { + if(f and f % n != 0) + { + ok = false; + break; + } + } + + return ok; + } +}; \ No newline at end of file diff --git a/1897-redistribute-characters-to-make-all-strings-equal/NOTES.md b/1897-redistribute-characters-to-make-all-strings-equal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1897-redistribute-characters-to-make-all-strings-equal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1897-redistribute-characters-to-make-all-strings-equal/README.md b/1897-redistribute-characters-to-make-all-strings-equal/README.md new file mode 100644 index 00000000..a493914d --- /dev/null +++ b/1897-redistribute-characters-to-make-all-strings-equal/README.md @@ -0,0 +1,32 @@ +

1897. Redistribute Characters to Make All Strings Equal

Easy


You are given an array of strings words (0-indexed).

+ +

In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j].

+ +

Return true if you can make every string in words equal using any number of operations, and false otherwise.

+ +

 

+

Example 1:

+ +
Input: words = ["abc","aabc","bc"]
+Output: true
+Explanation: Move the first 'a' in words[1] to the front of words[2],
+to make words[1] = "abc" and words[2] = "abc".
+All the strings are now equal to "abc", so return true.
+
+ +

Example 2:

+ +
Input: words = ["ab","a"]
+Output: false
+Explanation: It is impossible to make all the strings equal using the operation.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp b/1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp new file mode 100644 index 00000000..3422d3f7 --- /dev/null +++ b/1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + string largestOddNumber(string num) { + + int n = num.size(); + + for(int i = n-1; i >= 0; --i) + { + if((num[i] - '0') & 1) + return num.substr(0, i+1); + } + + return ""; + + } +}; \ No newline at end of file diff --git a/1903-largest-odd-number-in-string/NOTES.md b/1903-largest-odd-number-in-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1903-largest-odd-number-in-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1903-largest-odd-number-in-string/README.md b/1903-largest-odd-number-in-string/README.md new file mode 100644 index 00000000..0def66fa --- /dev/null +++ b/1903-largest-odd-number-in-string/README.md @@ -0,0 +1,34 @@ +

1903. Largest Odd Number in String

Easy


You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: num = "52"
+Output: "5"
+Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
+
+ +

Example 2:

+ +
Input: num = "4206"
+Output: ""
+Explanation: There are no odd numbers in "4206".
+
+ +

Example 3:

+ +
Input: num = "35427"
+Output: "35427"
+Explanation: "35427" is already an odd number.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num.length <= 105
  • +
  • num only consists of digits and does not contain any leading zeros.
  • +
+
\ No newline at end of file diff --git a/1905-count-sub-islands/1905-count-sub-islands.cpp b/1905-count-sub-islands/1905-count-sub-islands.cpp new file mode 100644 index 00000000..bd5ccebd --- /dev/null +++ b/1905-count-sub-islands/1905-count-sub-islands.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + int countSubIslands(vector>& grid1, vector>& grid2) { + + int n = grid1.size(); + int m = grid1[0].size(); + + vector> visited(n, vector(m, false)); + + function dfs = [&](int i, int j) -> bool{ + if(i < 0 or j < 0 or i >= n or j >= m or visited[i][j] or !grid2[i][j]) + return true; + + if(!grid1[i][j]) return false; + + visited[i][j] = true; + + bool up, left, right, down; + up = left = right = down = true; + + up = dfs(i-1, j); + left = dfs(i, j-1); + down = dfs(i+1, j); + right = dfs(i, j+1); + + return up and left and down and right; + }; + + int subIsland = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited[i][j] and grid2[i][j]) + { + if(dfs(i, j)) + { + ++subIsland; + } + } + } + } + + return subIsland; + } +}; \ No newline at end of file diff --git a/1905-count-sub-islands/NOTES.md b/1905-count-sub-islands/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1905-count-sub-islands/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1905-count-sub-islands/README.md b/1905-count-sub-islands/README.md new file mode 100644 index 00000000..b76cb8e8 --- /dev/null +++ b/1905-count-sub-islands/README.md @@ -0,0 +1,33 @@ +

1905. Count Sub Islands

Medium


You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

+ +

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

+ +

Return the number of islands in grid2 that are considered sub-islands.

+ +

 

+

Example 1:

+ +
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
+Output: 3
+Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
+The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
+
+ +

Example 2:

+ +
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
+Output: 2 
+Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
+The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid1.length == grid2.length
  • +
  • n == grid1[i].length == grid2[i].length
  • +
  • 1 <= m, n <= 500
  • +
  • grid1[i][j] and grid2[i][j] are either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp b/1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp new file mode 100644 index 00000000..7488c8e8 --- /dev/null +++ b/1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int maxProductDifference(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(),nums.end()); + + return ((nums[n-1] * nums[n-2]) - (nums[0] * nums[1])); + } +}; \ No newline at end of file diff --git a/1913-maximum-product-difference-between-two-pairs/NOTES.md b/1913-maximum-product-difference-between-two-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1913-maximum-product-difference-between-two-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1913-maximum-product-difference-between-two-pairs/README.md b/1913-maximum-product-difference-between-two-pairs/README.md new file mode 100644 index 00000000..3b05b9ed --- /dev/null +++ b/1913-maximum-product-difference-between-two-pairs/README.md @@ -0,0 +1,34 @@ +

1913. Maximum Product Difference Between Two Pairs

Easy


The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).

+ +
    +
  • For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16.
  • +
+ +

Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.

+ +

Return the maximum such product difference.

+ +

 

+

Example 1:

+ +
Input: nums = [5,6,2,7,4]
+Output: 34
+Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
+The product difference is (6 * 7) - (2 * 4) = 34.
+
+ +

Example 2:

+ +
Input: nums = [4,2,5,9,7,4,8]
+Output: 64
+Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
+The product difference is (9 * 8) - (2 * 4) = 64.
+
+ +

 

+

Constraints:

+ +
    +
  • 4 <= nums.length <= 104
  • +
  • 1 <= nums[i] <= 104
  • +
\ No newline at end of file diff --git a/1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp b/1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp new file mode 100644 index 00000000..f89c9d77 --- /dev/null +++ b/1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + long long wonderfulSubstrings(string word) { + + long cnt[1024] = { 1 }, mask = 0, res = 0; + for (auto ch : word) { + mask ^= 1 << (ch - 'a'); + res += cnt[mask]; + for (auto n = 0; n < 10; ++n) + res += cnt[mask ^ (1 << n)]; + ++cnt[mask]; + } + return res; + } +}; \ No newline at end of file diff --git a/1915-number-of-wonderful-substrings/README.md b/1915-number-of-wonderful-substrings/README.md new file mode 100644 index 00000000..c19d9e1f --- /dev/null +++ b/1915-number-of-wonderful-substrings/README.md @@ -0,0 +1,54 @@ +

1915. Number of Wonderful Substrings

Medium


A wonderful string is a string where at most one letter appears an odd number of times.

+ +
    +
  • For example, "ccjjc" and "abab" are wonderful, but "ab" is not.
  • +
+ +

Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.

+ +

A substring is a contiguous sequence of characters in a string.

+ +

 

+

Example 1:

+ +
Input: word = "aba"
+Output: 4
+Explanation: The four wonderful substrings are underlined below:
+- "aba" -> "a"
+- "aba" -> "b"
+- "aba" -> "a"
+- "aba" -> "aba"
+
+ +

Example 2:

+ +
Input: word = "aabb"
+Output: 9
+Explanation: The nine wonderful substrings are underlined below:
+- "aabb" -> "a"
+- "aabb" -> "aa"
+- "aabb" -> "aab"
+- "aabb" -> "aabb"
+- "aabb" -> "a"
+- "aabb" -> "abb"
+- "aabb" -> "b"
+- "aabb" -> "bb"
+- "aabb" -> "b"
+
+ +

Example 3:

+ +
Input: word = "he"
+Output: 2
+Explanation: The two wonderful substrings are underlined below:
+- "he" -> "h"
+- "he" -> "e"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word.length <= 105
  • +
  • word consists of lowercase English letters from 'a' to 'j'.
  • +
\ No newline at end of file diff --git a/1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp b/1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp new file mode 100644 index 00000000..e7a702a0 --- /dev/null +++ b/1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int eliminateMaximum(vector& dist, vector& speed) { + + int n = dist.size(); + + vector vec; + + for(int i = 0; i < n; ++i) + { + vec.push_back((float)dist[i]/speed[i]); + } + + sort(vec.begin(), vec.end()); + + int counter = 1, ans = 1; + + for(int i = 1; i < n; ++i) + { + if(counter < vec[i]) + ++ans; + else + break; + ++counter; + } + + return ans; + } +}; \ No newline at end of file diff --git a/1921-eliminate-maximum-number-of-monsters/README.md b/1921-eliminate-maximum-number-of-monsters/README.md new file mode 100644 index 00000000..53ce3337 --- /dev/null +++ b/1921-eliminate-maximum-number-of-monsters/README.md @@ -0,0 +1,50 @@ +

1921. Eliminate Maximum Number of Monsters

Medium


You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.

+ +

The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.

+ +

You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.

+ +

You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.

+ +

Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.

+ +

 

+

Example 1:

+ +
Input: dist = [1,3,4], speed = [1,1,1]
+Output: 3
+Explanation:
+In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
+After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.
+All 3 monsters can be eliminated.
+ +

Example 2:

+ +
Input: dist = [1,1,2,3], speed = [1,1,1,1]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,1,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

Example 3:

+ +
Input: dist = [3,2,4], speed = [5,3,2]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

 

+

Constraints:

+ +
    +
  • n == dist.length == speed.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= dist[i], speed[i] <= 105
  • +
+
\ No newline at end of file diff --git a/1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp b/1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp new file mode 100644 index 00000000..48915358 --- /dev/null +++ b/1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + int countPalindromicSubsequence(string s) { + + int n = s.size(); + + unordered_map> mp; + unordered_map firstOccurence, lastOccurence; + int palindrome = 0; + + for(int i = 0; i < n; ++i) + { + if(firstOccurence.find(s[i]) == firstOccurence.end()) + { + firstOccurence[s[i]] = i; + } + + lastOccurence[s[i]] = i; + + mp[s[i]].push_back(i); + } + + for(int ch = 'a'; ch <= 'z'; ++ch) + { + int left = firstOccurence[ch]; + int right = lastOccurence[ch]; + + if(right - left <= 1) + continue; + + for(char x = 'a'; x <= 'z'; ++x) + { + if(mp.find(x) != mp.end()) + { + int ub = upper_bound(mp[x].begin(), mp[x].end(), left) - mp[x].begin(); + + if(ub == mp[x].size()) + continue; + + int idx = mp[x][ub]; + + if(idx < right) + ++palindrome; + } + } + } + + return palindrome; + } +}; \ No newline at end of file diff --git a/1930-unique-length-3-palindromic-subsequences/NOTES.md b/1930-unique-length-3-palindromic-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1930-unique-length-3-palindromic-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1930-unique-length-3-palindromic-subsequences/README.md b/1930-unique-length-3-palindromic-subsequences/README.md new file mode 100644 index 00000000..099bc405 --- /dev/null +++ b/1930-unique-length-3-palindromic-subsequences/README.md @@ -0,0 +1,49 @@ +

1930. Unique Length-3 Palindromic Subsequences

Medium


Given a string s, return the number of unique palindromes of length three that are a subsequence of s.

+ +

Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.

+ +

A palindrome is a string that reads the same forwards and backwards.

+ +

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

+ +
    +
  • For example, "ace" is a subsequence of "abcde".
  • +
+ +

 

+

Example 1:

+ +
Input: s = "aabca"
+Output: 3
+Explanation: The 3 palindromic subsequences of length 3 are:
+- "aba" (subsequence of "aabca")
+- "aaa" (subsequence of "aabca")
+- "aca" (subsequence of "aabca")
+
+ +

Example 2:

+ +
Input: s = "adc"
+Output: 0
+Explanation: There are no palindromic subsequences of length 3 in "adc".
+
+ +

Example 3:

+ +
Input: s = "bbcbaba"
+Output: 4
+Explanation: The 4 palindromic subsequences of length 3 are:
+- "bbb" (subsequence of "bbcbaba")
+- "bcb" (subsequence of "bbcbaba")
+- "bab" (subsequence of "bbcbaba")
+- "aba" (subsequence of "bbcbaba")
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 105
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/1937-maximum-number-of-points-with-cost/README.md b/1937-maximum-number-of-points-with-cost/README.md new file mode 100644 index 00000000..d0709862 --- /dev/null +++ b/1937-maximum-number-of-points-with-cost/README.md @@ -0,0 +1,49 @@ +

1937. Maximum Number of Points with Cost

Medium


You are given an m x n integer matrix points (0-indexed). Starting with 0 points, you want to maximize the number of points you can get from the matrix.

+ +

To gain points, you must pick one cell in each row. Picking the cell at coordinates (r, c) will add points[r][c] to your score.

+ +

However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows r and r + 1 (where 0 <= r < m - 1), picking cells at coordinates (r, c1) and (r + 1, c2) will subtract abs(c1 - c2) from your score.

+ +

Return the maximum number of points you can achieve.

+ +

abs(x) is defined as:

+ +
    +
  • x for x >= 0.
  • +
  • -x for x < 0.
  • +
+ +

 

+

Example 1:

+ +
Input: points = [[1,2,3],[1,5,1],[3,1,1]]
+Output: 9
+Explanation:
+The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
+You add 3 + 5 + 3 = 11 to your score.
+However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
+Your final score is 11 - 2 = 9.
+
+ +

Example 2:

+ +
Input: points = [[1,5],[2,3],[4,2]]
+Output: 11
+Explanation:
+The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
+You add 5 + 3 + 4 = 12 to your score.
+However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
+Your final score is 12 - 1 = 11.
+
+ +

 

+

Constraints:

+ +
    +
  • m == points.length
  • +
  • n == points[r].length
  • +
  • 1 <= m, n <= 105
  • +
  • 1 <= m * n <= 105
  • +
  • 0 <= points[r][c] <= 105
  • +
+
\ No newline at end of file diff --git a/1971-find-if-path-exists-in-graph/README.md b/1971-find-if-path-exists-in-graph/README.md new file mode 100644 index 00000000..d95edbe4 --- /dev/null +++ b/1971-find-if-path-exists-in-graph/README.md @@ -0,0 +1,37 @@ +

1971. Find if Path Exists in Graph

Easy


There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.

+ +

You want to determine if there is a valid path that exists from vertex source to vertex destination.

+ +

Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
+Output: true
+Explanation: There are two paths from vertex 0 to vertex 2:
+- 0 → 1 → 2
+- 0 → 2
+
+ +

Example 2:

+ +
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
+Output: false
+Explanation: There is no path from vertex 0 to vertex 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 105
  • +
  • 0 <= edges.length <= 2 * 105
  • +
  • edges[i].length == 2
  • +
  • 0 <= ui, vi <= n - 1
  • +
  • ui != vi
  • +
  • 0 <= source, destination <= n - 1
  • +
  • There are no duplicate edges.
  • +
  • There are no self edges.
  • +
+
\ No newline at end of file diff --git a/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp new file mode 100644 index 00000000..776c6783 --- /dev/null +++ b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + string findDifferentBinaryString(vector& nums) { + + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + if(nums[0][i] = nums[i][i] == '0' ? '1' : '0'); + } + + return nums[0]; + } +}; \ No newline at end of file diff --git a/1980-find-unique-binary-string/NOTES.md b/1980-find-unique-binary-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1980-find-unique-binary-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/1980-find-unique-binary-string/README.md b/1980-find-unique-binary-string/README.md new file mode 100644 index 00000000..8cdd0a25 --- /dev/null +++ b/1980-find-unique-binary-string/README.md @@ -0,0 +1,35 @@ +

1980. Find Unique Binary String

Medium


Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.

+ +

 

+

Example 1:

+ +
Input: nums = ["01","10"]
+Output: "11"
+Explanation: "11" does not appear in nums. "00" would also be correct.
+
+ +

Example 2:

+ +
Input: nums = ["00","01"]
+Output: "11"
+Explanation: "11" does not appear in nums. "10" would also be correct.
+
+ +

Example 3:

+ +
Input: nums = ["111","011","001"]
+Output: "101"
+Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 16
  • +
  • nums[i].length == n
  • +
  • nums[i] is either '0' or '1'.
  • +
  • All the strings of nums are unique.
  • +
+
\ No newline at end of file diff --git a/1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp b/1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp new file mode 100644 index 00000000..add477bd --- /dev/null +++ b/1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp @@ -0,0 +1,145 @@ +class DSU{ + private: + vector size, rank, parent; + public: + DSU(int n) + { + int N = n; + size.resize(N, 1); + rank.resize(N, 0); + parent.resize(N); + for(int i = 0; i < N; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(rank[parU] < rank[parV]) + { + parent[parU] = parV; + } + else if(rank[parU] > rank[parV]) + { + parent[parV] = parU; + } + else + { + parent[parV] = parU; + ++rank[parU]; + } + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else{ + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } +}; + +class Solution { +public: + vector> findFarmland(vector>& land) { + + int n = land.size(); + int m = land[0].size(); + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1, 0}; + + auto isValid = [&](int x, int y)->bool + { + return (x >= 0 and y >= 0 and x < n and y < m); + }; + + DSU dsu((n*m) + 1); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(land[i][j] == 1) + { + int node = (i * m) + j; + + for(int k = 0; k < 4; ++k) + { + int nx = dx[k] + i; + int ny = dy[k] + j; + + if(isValid(nx, ny) and land[nx][ny] == 1) + { + int adjNode = (nx * m) + ny; + + if(!dsu.isSame(node, adjNode)) + dsu.unionBySize(node, adjNode); + } + } + } + } + } + + map> mp; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(land[i][j] == 1) + { + int node = (i * m) + j; + int par = dsu.findParent(node); + + if(mp.find(par) == mp.end()) + { + mp[par] = {i, j, i, j}; + } + else + { + mp[par][2] = i; + mp[par][3] = j; + } + + } + } + } + + vector> res; + + for(auto&[_, vec] : mp) + res.push_back(vec); + + return res; + + } +}; \ No newline at end of file diff --git a/1992-find-all-groups-of-farmland/README.md b/1992-find-all-groups-of-farmland/README.md new file mode 100644 index 00000000..783ad402 --- /dev/null +++ b/1992-find-all-groups-of-farmland/README.md @@ -0,0 +1,45 @@ +

1992. Find All Groups of Farmland

Medium


You are given a 0-indexed m x n binary matrix land where a 0 represents a hectare of forested land and a 1 represents a hectare of farmland.

+ +

To keep the land organized, there are designated rectangular areas of hectares that consist entirely of farmland. These rectangular areas are called groups. No two groups are adjacent, meaning farmland in one group is not four-directionally adjacent to another farmland in a different group.

+ +

land can be represented by a coordinate system where the top left corner of land is (0, 0) and the bottom right corner of land is (m-1, n-1). Find the coordinates of the top left and bottom right corner of each group of farmland. A group of farmland with a top left corner at (r1, c1) and a bottom right corner at (r2, c2) is represented by the 4-length array [r1, c1, r2, c2].

+ +

Return a 2D array containing the 4-length arrays described above for each group of farmland in land. If there are no groups of farmland, return an empty array. You may return the answer in any order.

+ +

 

+

Example 1:

+ +
Input: land = [[1,0,0],[0,1,1],[0,1,1]]
+Output: [[0,0,0,0],[1,1,2,2]]
+Explanation:
+The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].
+The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
+
+ +

Example 2:

+ +
Input: land = [[1,1],[1,1]]
+Output: [[0,0,1,1]]
+Explanation:
+The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
+
+ +

Example 3:

+ +
Input: land = [[0]]
+Output: []
+Explanation:
+There are no groups of farmland.
+
+ +

 

+

Constraints:

+ +
    +
  • m == land.length
  • +
  • n == land[i].length
  • +
  • 1 <= m, n <= 300
  • +
  • land consists of only 0's and 1's.
  • +
  • Groups of farmland are rectangular in shape.
  • +
+
\ No newline at end of file diff --git a/2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp b/2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp new file mode 100644 index 00000000..b6cb4401 --- /dev/null +++ b/2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + string reversePrefix(string word, char ch) { + + int n = word.size(); + string pref, ans; + bool isfound = false; + + for(int i = 0; i < n; ++i) + { + if(!isfound) + pref += word[i]; + else + ans += word[i]; + if(word[i] == ch) + isfound = true; + } + + if(!isfound) + return pref; + + reverse(pref.begin(), pref.end()); + + return pref + ans; + + } +}; \ No newline at end of file diff --git a/2000-reverse-prefix-of-word/NOTES.md b/2000-reverse-prefix-of-word/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2000-reverse-prefix-of-word/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp new file mode 100644 index 00000000..ffdf2002 --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minMovesToSeat(vector& seats, vector& students) { + + int n = seats.size(); + + sort(seats.begin(), seats.end()); + sort(students.begin(), students.end()); + + int moves = 0; + + for(int i = 0; i < n; ++i) + { + moves += abs(seats[i] - students[i]); + } + + return moves; + + } +}; \ No newline at end of file diff --git a/2037-minimum-number-of-moves-to-seat-everyone/NOTES.md b/2037-minimum-number-of-moves-to-seat-everyone/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2037-minimum-number-of-moves-to-seat-everyone/README.md b/2037-minimum-number-of-moves-to-seat-everyone/README.md new file mode 100644 index 00000000..04a8e685 --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/README.md @@ -0,0 +1,58 @@ +

2037. Minimum Number of Moves to Seat Everyone

Easy


There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.

+ +

You may perform the following move any number of times:

+ +
    +
  • Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)
  • +
+ +

Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.

+ +

Note that there may be multiple seats or students in the same position at the beginning.

+ +

 

+

Example 1:

+ +
Input: seats = [3,1,5], students = [2,7,4]
+Output: 4
+Explanation: The students are moved as follows:
+- The first student is moved from from position 2 to position 1 using 1 move.
+- The second student is moved from from position 7 to position 5 using 2 moves.
+- The third student is moved from from position 4 to position 3 using 1 move.
+In total, 1 + 2 + 1 = 4 moves were used.
+
+ +

Example 2:

+ +
Input: seats = [4,1,5,9], students = [1,3,2,6]
+Output: 7
+Explanation: The students are moved as follows:
+- The first student is not moved.
+- The second student is moved from from position 3 to position 4 using 1 move.
+- The third student is moved from from position 2 to position 5 using 3 moves.
+- The fourth student is moved from from position 6 to position 9 using 3 moves.
+In total, 0 + 1 + 3 + 3 = 7 moves were used.
+
+ +

Example 3:

+ +
Input: seats = [2,2,6,6], students = [1,3,2,6]
+Output: 4
+Explanation: Note that there are two seats at position 2 and two seats at position 6.
+The students are moved as follows:
+- The first student is moved from from position 1 to position 2 using 1 move.
+- The second student is moved from from position 3 to position 6 using 3 moves.
+- The third student is not moved.
+- The fourth student is not moved.
+In total, 1 + 3 + 0 + 0 = 4 moves were used.
+
+ +

 

+

Constraints:

+ +
    +
  • n == seats.length == students.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= seats[i], students[j] <= 100
  • +
+
\ No newline at end of file diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp new file mode 100644 index 00000000..adbdb99b --- /dev/null +++ b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool winnerOfGame(string colors) { + + int n = colors.size(); + + int aliceMoves = 0, bobMoves = 0; + + for(int i = 1; i < n-1; ++i) + { + char prev = colors[i-1]; + char curr = colors[i]; + char next = colors[i+1]; + + if(prev == curr and curr == next) + { + if(curr == 'A') + ++aliceMoves; + else + ++bobMoves; + } + } + + return (aliceMoves >= bobMoves + 1 ? 1 : 0); + + } +}; \ No newline at end of file diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md new file mode 100644 index 00000000..a5034a96 --- /dev/null +++ b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md @@ -0,0 +1,63 @@ +

2038. Remove Colored Pieces if Both Neighbors are the Same Color

Medium


There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.

+ +

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

+ +
    +
  • Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
  • +
  • Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
  • +
  • Alice and Bob cannot remove pieces from the edge of the line.
  • +
  • If a player cannot make a move on their turn, that player loses and the other player wins.
  • +
+ +

Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.

+ +

 

+

Example 1:

+ +
Input: colors = "AAABABB"
+Output: true
+Explanation:
+AAABABB -> AABABB
+Alice moves first.
+She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
+
+Now it's Bob's turn.
+Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
+Thus, Alice wins, so return true.
+
+ +

Example 2:

+ +
Input: colors = "AA"
+Output: false
+Explanation:
+Alice has her turn first.
+There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
+Thus, Bob wins, so return false.
+
+ +

Example 3:

+ +
Input: colors = "ABBBBBBBAAA"
+Output: false
+Explanation:
+ABBBBBBBAAA -> ABBBBBBBAA
+Alice moves first.
+Her only option is to remove the second to last 'A' from the right.
+
+ABBBBBBBAA -> ABBBBBBAA
+Next is Bob's turn.
+He has many options for which 'B' piece to remove. He can pick any.
+
+On Alice's second turn, she has no more pieces that she can remove.
+Thus, Bob wins, so return false.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= colors.length <= 105
  • +
  • colors consists of only the letters 'A' and 'B'
  • +
+
\ No newline at end of file diff --git a/2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp b/2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp new file mode 100644 index 00000000..37b28c23 --- /dev/null +++ b/2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int secondMinimum(int n, vector>& edges, int time, int change) { + + vector> dis (n); + for (int i=0;i adj[n]; + for (auto it:edges){ + adj[it[0]-1].push_back(it[1]-1); + adj[it[1]-1].push_back(it[0]-1); + } + + priority_queue , vector>, greater>> pq; + pq.push({0,0}); // time=0, node=0 + dis[0].pop(); + dis[0].push(0); + + while (pq.empty()==false){ + int dist=pq.top().first; + int node=pq.top().second; + pq.pop(); + int k=dist/change; + if (k%2==1) { + dist=dist+(change-dist%change); + } + + for (auto adjnode:adj[node]){ + int adjdis=dist+time; + + if (dis[adjnode].top()==1e8){ + dis[adjnode].pop(); + dis[adjnode].push(adjdis); + pq.push({adjdis,adjnode}); + } + else if (dis[adjnode].size()<2 && dis[adjnode].top()!= adjdis){ + dis[adjnode].push(adjdis); + pq.push({adjdis,adjnode}); + } + else { + if (dis[adjnode].top() > adjdis){ + dis[adjnode].pop(); + dis[adjnode].push(adjdis); + pq.push({adjdis,adjnode}); + } + } + } + } + + return dis[n-1].top(); + } +}; \ No newline at end of file diff --git a/2045-second-minimum-time-to-reach-destination/NOTES.md b/2045-second-minimum-time-to-reach-destination/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2045-second-minimum-time-to-reach-destination/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp new file mode 100644 index 00000000..fa609201 --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + string kthDistinct(vector& arr, int k) { + + map mp; + vector vec; + + for(auto& str : arr) + ++mp[str]; + + for(auto& str : arr) + { + if(mp[str] == 1) + { + vec.push_back(str); + } + } + + return (vec.size() < k ? "" : vec[k-1]); + + } +}; \ No newline at end of file diff --git a/2053-kth-distinct-string-in-an-array/NOTES.md b/2053-kth-distinct-string-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2053-kth-distinct-string-in-an-array/README.md b/2053-kth-distinct-string-in-an-array/README.md new file mode 100644 index 00000000..30e7edf5 --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/README.md @@ -0,0 +1,43 @@ +

2053. Kth Distinct String in an Array

Easy


A distinct string is a string that is present only once in an array.

+ +

Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".

+ +

Note that the strings are considered in the order in which they appear in the array.

+ +

 

+

Example 1:

+ +
Input: arr = ["d","b","c","b","c","a"], k = 2
+Output: "a"
+Explanation:
+The only distinct strings in arr are "d" and "a".
+"d" appears 1st, so it is the 1st distinct string.
+"a" appears 2nd, so it is the 2nd distinct string.
+Since k == 2, "a" is returned. 
+
+ +

Example 2:

+ +
Input: arr = ["aaa","aa","a"], k = 1
+Output: "aaa"
+Explanation:
+All strings in arr are distinct, so the 1st string "aaa" is returned.
+
+ +

Example 3:

+ +
Input: arr = ["a","b","a"], k = 3
+Output: ""
+Explanation:
+The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= arr.length <= 1000
  • +
  • 1 <= arr[i].length <= 5
  • +
  • arr[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp new file mode 100644 index 00000000..5e5ca5a6 --- /dev/null +++ b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp @@ -0,0 +1,49 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + vector nodesBetweenCriticalPoints(ListNode* head) { + + ListNode* prev = nullptr; + + int leftEnd = -1, nearestEnd = -1; + int minDist = INT_MAX, maxDist = INT_MIN; + int pos = 1; + + while(head) + { + if(head->next and prev) + { + if((prev->val < head->val and head->next->val < head->val) or (prev->val > head->val and head->next->val > head->val)) + { + if(leftEnd == -1) + { + leftEnd = pos; + nearestEnd = pos; + } + else + { + minDist = min(minDist, pos - nearestEnd); + maxDist = max(maxDist, pos - leftEnd); + nearestEnd = pos; + } + } + } + prev = head; + head = head->next; + ++pos; + } + + if(minDist == INT_MAX or maxDist == INT_MIN) + return {-1, -1}; + return {minDist, maxDist}; + } +}; \ No newline at end of file diff --git a/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp b/2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp new file mode 100644 index 00000000..3f1fb25d --- /dev/null +++ b/2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int timeRequiredToBuy(vector& tickets, int k) { + + int n = tickets.size(); + int time = 0; + + for(int i = 0; i < n; ++i) + { + if(i <= k) + { + time += (tickets[i] <= tickets[k] ? tickets[i] : tickets[k]); + } + else + { + time += (tickets[i] < tickets[k] ? tickets[i] : max(0, tickets[k] - 1)); + } + } + + return time; + } +}; \ No newline at end of file diff --git a/2073-time-needed-to-buy-tickets/NOTES.md b/2073-time-needed-to-buy-tickets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2073-time-needed-to-buy-tickets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2073-time-needed-to-buy-tickets/README.md b/2073-time-needed-to-buy-tickets/README.md new file mode 100644 index 00000000..5c998ffc --- /dev/null +++ b/2073-time-needed-to-buy-tickets/README.md @@ -0,0 +1,39 @@ +

2073. Time Needed to Buy Tickets

Easy


There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line.

+ +

You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i].

+ +

Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.

+ +

Return the time taken for the person at position k (0-indexed) to finish buying tickets.

+ +

 

+

Example 1:

+ +
Input: tickets = [2,3,2], k = 2
+Output: 6
+Explanation: 
+- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
+- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
+The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
+
+ +

Example 2:

+ +
Input: tickets = [5,1,1,1], k = 0
+Output: 8
+Explanation:
+- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
+- In the next 4 passes, only the person in position 0 is buying tickets.
+The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
+
+ +

 

+

Constraints:

+ +
    +
  • n == tickets.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= tickets[i] <= 100
  • +
  • 0 <= k < n
  • +
+
\ No newline at end of file diff --git a/2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp b/2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp new file mode 100644 index 00000000..a5443361 --- /dev/null +++ b/2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + vector findAllPeople(int n, vector>& meetings, int firstPerson) { + + vector> adj[n+1]; + + int currTime = 0; + + for(auto& meet : meetings) + { + int person1 = meet[0]; + int person2 = meet[1]; + int meetingTime = meet[2]; + + adj[person1].push_back({person2, meetingTime}); + adj[person2].push_back({person1, meetingTime}); + } + + priority_queue, vector>, greater>> q; // currTime node + + vector visited(n+1, false); + + vector ans; + + q.push({0, 0}); + q.push({0, firstPerson}); + + while(!q.empty()) + { + int currTime = q.top().first; + int person1 = q.top().second; + + q.pop(); + + if(visited[person1]) + continue; + + visited[person1] = true; + + for(auto& meets : adj[person1]) + { + int person2 = meets.first; + int meetingTime = meets.second; + + if(!visited[person2] and currTime <= meetingTime) + { + q.push({meetingTime, person2}); + } + } + } + + for(int i = 0; i < n; ++i) + { + if(visited[i]) + ans.push_back(i); + } + + return ans; + } +}; \ No newline at end of file diff --git a/2092-find-all-people-with-secret/NOTES.md b/2092-find-all-people-with-secret/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2092-find-all-people-with-secret/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2092-find-all-people-with-secret/README.md b/2092-find-all-people-with-secret/README.md new file mode 100644 index 00000000..3a4f6ecb --- /dev/null +++ b/2092-find-all-people-with-secret/README.md @@ -0,0 +1,57 @@ +

2092. Find All People With Secret

Hard


You are given an integer n indicating there are n people numbered from 0 to n - 1. You are also given a 0-indexed 2D integer array meetings where meetings[i] = [xi, yi, timei] indicates that person xi and person yi have a meeting at timei. A person may attend multiple meetings at the same time. Finally, you are given an integer firstPerson.

+ +

Person 0 has a secret and initially shares the secret with a person firstPerson at time 0. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person xi has the secret at timei, then they will share the secret with person yi, and vice versa.

+ +

The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.

+ +

Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.

+ +

 

+

Example 1:

+ +
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
+Output: [0,1,2,3,5]
+Explanation:
+At time 0, person 0 shares the secret with person 1.
+At time 5, person 1 shares the secret with person 2.
+At time 8, person 2 shares the secret with person 3.
+At time 10, person 1 shares the secret with person 5.​​​​
+Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
+
+ +

Example 2:

+ +
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
+Output: [0,1,3]
+Explanation:
+At time 0, person 0 shares the secret with person 3.
+At time 2, neither person 1 nor person 2 know the secret.
+At time 3, person 3 shares the secret with person 0 and person 1.
+Thus, people 0, 1, and 3 know the secret after all the meetings.
+
+ +

Example 3:

+ +
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
+Output: [0,1,2,3,4]
+Explanation:
+At time 0, person 0 shares the secret with person 1.
+At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
+Note that person 2 can share the secret at the same time as receiving it.
+At time 2, person 3 shares the secret with person 4.
+Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 105
  • +
  • 1 <= meetings.length <= 105
  • +
  • meetings[i].length == 3
  • +
  • 0 <= xi, yi <= n - 1
  • +
  • xi != yi
  • +
  • 1 <= timei <= 105
  • +
  • 1 <= firstPerson <= n - 1
  • +
+
\ No newline at end of file diff --git a/2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp new file mode 100644 index 00000000..54cbc44b --- /dev/null +++ b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp @@ -0,0 +1,56 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + +private: + void rootToNode(TreeNode* root,int &n,int &m,string &temp,string &s,string &d){ + if(!root) return; + + if(root->val == n) s=temp; + if(root->val == m) d=temp; + + temp.push_back('L'); + rootToNode(root->left, n, m, temp, s, d); + temp.pop_back(); + + temp.push_back('R'); + rootToNode(root->right, n, m, temp, s, d); + temp.pop_back(); + } + + +public: + string getDirections(TreeNode* root, int n, int m) { + + string s,d,temp; + rootToNode(root,n,m,temp,s,d); + + int ind = 0; + + for(int i=0;i2096. Step-By-Step Directions From a Binary Tree Node to Another

Medium


You are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue representing the value of the start node s, and a different integer destValue representing the value of the destination node t.

+ +

Find the shortest path starting from node s and ending at node t. Generate step-by-step directions of such path as a string consisting of only the uppercase letters 'L', 'R', and 'U'. Each letter indicates a specific direction:

+ +
    +
  • 'L' means to go from a node to its left child node.
  • +
  • 'R' means to go from a node to its right child node.
  • +
  • 'U' means to go from a node to its parent node.
  • +
+ +

Return the step-by-step directions of the shortest path from node s to node t.

+ +

 

+

Example 1:

+ +
Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
+Output: "UURL"
+Explanation: The shortest path is: 3 → 1 → 5 → 2 → 6.
+
+ +

Example 2:

+ +
Input: root = [2,1], startValue = 2, destValue = 1
+Output: "L"
+Explanation: The shortest path is: 2 → 1.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is n.
  • +
  • 2 <= n <= 105
  • +
  • 1 <= Node.val <= n
  • +
  • All the values in the tree are unique.
  • +
  • 1 <= startValue, destValue <= n
  • +
  • startValue != destValue
  • +
+
\ No newline at end of file diff --git a/2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp b/2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp new file mode 100644 index 00000000..51c0d7d2 --- /dev/null +++ b/2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + string firstPalindrome(vector& words) { + + for(auto& word : words) + { + int i = 0, j = word.size()-1; + bool ok = true; + + while(i < j) + { + if(word[i] != word[j]) + { + ok = false; + break; + } + ++i, --j; + } + + if(ok) return word; + } + + return ""; + } +}; \ No newline at end of file diff --git a/2108-find-first-palindromic-string-in-the-array/README.md b/2108-find-first-palindromic-string-in-the-array/README.md new file mode 100644 index 00000000..cdaba2f0 --- /dev/null +++ b/2108-find-first-palindromic-string-in-the-array/README.md @@ -0,0 +1,36 @@ +

2108. Find First Palindromic String in the Array

Easy


Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".

+ +

A string is palindromic if it reads the same forward and backward.

+ +

 

+

Example 1:

+ +
Input: words = ["abc","car","ada","racecar","cool"]
+Output: "ada"
+Explanation: The first string that is palindromic is "ada".
+Note that "racecar" is also palindromic, but it is not the first.
+
+ +

Example 2:

+ +
Input: words = ["notapalindrome","racecar"]
+Output: "racecar"
+Explanation: The first and only string that is palindromic is "racecar".
+
+ +

Example 3:

+ +
Input: words = ["def","ghi"]
+Output: ""
+Explanation: There are no palindromic strings, so the empty string is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists only of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp b/2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp new file mode 100644 index 00000000..371b3993 --- /dev/null +++ b/2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int numberOfBeams(vector& bank) { + + int n = bank.size(); + + int ans = 0, i = 0; + + while(i < n) + { + int cnt = count(bank[i].begin(), bank[i].end(), '1'); + + int j = i+1; + + while(j < n) + { + int cnt2 = count(bank[j].begin(), bank[j].end(), '1'); + + if(cnt2 >= 1) + { + ans += (cnt * cnt2); + break; + } + ++j; + } + + i = j++; + } + + return ans; + } +}; \ No newline at end of file diff --git a/2125-number-of-laser-beams-in-a-bank/NOTES.md b/2125-number-of-laser-beams-in-a-bank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2125-number-of-laser-beams-in-a-bank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2125-number-of-laser-beams-in-a-bank/README.md b/2125-number-of-laser-beams-in-a-bank/README.md new file mode 100644 index 00000000..6b3c5a3f --- /dev/null +++ b/2125-number-of-laser-beams-in-a-bank/README.md @@ -0,0 +1,48 @@ +

2125. Number of Laser Beams in a Bank

Medium


Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the ith row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.

+ +

There is one laser beam between any two security devices if both conditions are met:

+ +
    +
  • The two devices are located on two different rows: r1 and r2, where r1 < r2.
  • +
  • For each row i where r1 < i < r2, there are no security devices in the ith row.
  • +
+ +

Laser beams are independent, i.e., one beam does not interfere nor join with another.

+ +

Return the total number of laser beams in the bank.

+ +

 

+

Example 1:

+ +
Input: bank = ["011001","000000","010100","001000"]
+Output: 8
+Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
+ * bank[0][1] -- bank[2][1]
+ * bank[0][1] -- bank[2][3]
+ * bank[0][2] -- bank[2][1]
+ * bank[0][2] -- bank[2][3]
+ * bank[0][5] -- bank[2][1]
+ * bank[0][5] -- bank[2][3]
+ * bank[2][1] -- bank[3][2]
+ * bank[2][3] -- bank[3][2]
+Note that there is no beam between any device on the 0th row with any on the 3rd row.
+This is because the 2nd row contains security devices, which breaks the second condition.
+
+ +

Example 2:

+ +
Input: bank = ["000","111","000"]
+Output: 0
+Explanation: There does not exist two devices located on two different rows.
+
+ +

 

+

Constraints:

+ +
    +
  • m == bank.length
  • +
  • n == bank[i].length
  • +
  • 1 <= m, n <= 500
  • +
  • bank[i][j] is either '0' or '1'.
  • +
+
\ No newline at end of file diff --git a/2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp b/2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp new file mode 100644 index 00000000..276805cd --- /dev/null +++ b/2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minSwaps(vector& nums) { + + int n = nums.size(); + int cnt = 0; + + for(auto& ele : nums) + cnt += ele; + + for(int i = 0; i < n; ++i) + nums.push_back(nums[i]); + + int ans = INT_MAX, curCnt = 0, j = 0, i = 0; + + while(j < 2*n) + { + curCnt += nums[j]; + + if(j - i + 1 == cnt) + { + ans = min(ans, cnt - curCnt); + curCnt -= nums[i++]; + } + ++j; + } + + return ans == INT_MAX ? 0 : ans; + } +}; \ No newline at end of file diff --git a/2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md b/2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2134-minimum-swaps-to-group-all-1s-together-ii/README.md b/2134-minimum-swaps-to-group-all-1s-together-ii/README.md new file mode 100644 index 00000000..2c4dc53e --- /dev/null +++ b/2134-minimum-swaps-to-group-all-1s-together-ii/README.md @@ -0,0 +1,46 @@ +

2134. Minimum Swaps to Group All 1's Together II

Medium


A swap is defined as taking two distinct positions in an array and swapping the values in them.

+ +

A circular array is defined as an array where we consider the first element and the last element to be adjacent.

+ +

Given a binary circular array nums, return the minimum number of swaps required to group all 1's present in the array together at any location.

+ +

 

+

Example 1:

+ +
Input: nums = [0,1,0,1,1,0,0]
+Output: 1
+Explanation: Here are a few of the ways to group all the 1's together:
+[0,0,1,1,1,0,0] using 1 swap.
+[0,1,1,1,0,0,0] using 1 swap.
+[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
+There is no way to group all 1's together with 0 swaps.
+Thus, the minimum number of swaps required is 1.
+
+ +

Example 2:

+ +
Input: nums = [0,1,1,1,0,0,1,1,0]
+Output: 2
+Explanation: Here are a few of the ways to group all the 1's together:
+[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
+[1,1,1,1,1,0,0,0,0] using 2 swaps.
+There is no way to group all 1's together with 0 or 1 swaps.
+Thus, the minimum number of swaps required is 2.
+
+ +

Example 3:

+ +
Input: nums = [1,1,0,0,1]
+Output: 0
+Explanation: All the 1's are already grouped together due to the circular property of the array.
+Thus, the minimum number of swaps required is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • nums[i] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp new file mode 100644 index 00000000..156fbe4d --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector rearrangeArray(vector& nums) { + + int n = nums.size(); + vector ans(n); + + int even = 0, odd = 1; + + for(int i = 0; i < n; ++i) + { + if(nums[i] < 0) + { + ans[odd] = nums[i]; + odd += 2; + } + else + { + ans[even] = nums[i]; + even += 2; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/2149-rearrange-array-elements-by-sign/NOTES.md b/2149-rearrange-array-elements-by-sign/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2149-rearrange-array-elements-by-sign/README.md b/2149-rearrange-array-elements-by-sign/README.md new file mode 100644 index 00000000..06869c95 --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/README.md @@ -0,0 +1,42 @@ +

2149. Rearrange Array Elements by Sign

Medium


You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.

+ +

You should rearrange the elements of nums such that the modified array follows the given conditions:

+ +
    +
  1. Every consecutive pair of integers have opposite signs.
  2. +
  3. For all integers with the same sign, the order in which they were present in nums is preserved.
  4. +
  5. The rearranged array begins with a positive integer.
  6. +
+ +

Return the modified array after rearranging the elements to satisfy the aforementioned conditions.

+ +

 

+

Example 1:

+ +
Input: nums = [3,1,-2,-5,2,-4]
+Output: [3,-2,1,-5,2,-4]
+Explanation:
+The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
+The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
+Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.  
+
+ +

Example 2:

+ +
Input: nums = [-1,1]
+Output: [1,-1]
+Explanation:
+1 is the only positive integer and -1 the only negative integer in nums.
+So nums is rearranged to [1,-1].
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 2 * 105
  • +
  • nums.length is even
  • +
  • 1 <= |nums[i]| <= 105
  • +
  • nums consists of equal number of positive and negative integers.
  • +
+
\ No newline at end of file diff --git a/2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp b/2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp new file mode 100644 index 00000000..26bc8244 --- /dev/null +++ b/2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeNodes(ListNode* head) { + + ListNode* dummy = new ListNode(), *ptr = dummy; + + int curSum = 0; + + head = head->next; + + while(head) + { + curSum += head->val; + if(head->val == 0) + { + head->val = curSum; + ptr->next = head; + ptr = ptr->next; + curSum = 0 ; + } + head = head->next; + } + + ptr->next = nullptr; + + return dummy->next; + + } +}; \ No newline at end of file diff --git a/2181-merge-nodes-in-between-zeros/README.md b/2181-merge-nodes-in-between-zeros/README.md new file mode 100644 index 00000000..b70d21cf --- /dev/null +++ b/2181-merge-nodes-in-between-zeros/README.md @@ -0,0 +1,38 @@ +

2181. Merge Nodes in Between Zeros

Medium


You are given the head of a linked list, which contains a series of integers separated by 0's. The beginning and end of the linked list will have Node.val == 0.

+ +

For every two consecutive 0's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0's.

+ +

Return the head of the modified linked list.

+ +

 

+

Example 1:

+ +
Input: head = [0,3,1,0,4,5,2,0]
+Output: [4,11]
+Explanation: 
+The above figure represents the given linked list. The modified list contains
+- The sum of the nodes marked in green: 3 + 1 = 4.
+- The sum of the nodes marked in red: 4 + 5 + 2 = 11.
+
+ +

Example 2:

+ +
Input: head = [0,1,0,3,0,2,2,0]
+Output: [1,3,4]
+Explanation: 
+The above figure represents the given linked list. The modified list contains
+- The sum of the nodes marked in green: 1 = 1.
+- The sum of the nodes marked in red: 3 = 3.
+- The sum of the nodes marked in yellow: 2 + 2 = 4.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [3, 2 * 105].
  • +
  • 0 <= Node.val <= 1000
  • +
  • There are no two consecutive nodes with Node.val == 0.
  • +
  • The beginning and end of the linked list have Node.val == 0.
  • +
+
\ No newline at end of file diff --git a/2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp b/2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp new file mode 100644 index 00000000..1cc4d93d --- /dev/null +++ b/2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + vector sortJumbled(vector& mapping, vector& nums) { + + map mp; + + for(int i = 0; i < mapping.size(); ++i) + mp[i+'0'] = mapping[i]+'0'; + + vector> vp; + + for(int i = 0; i < nums.size(); ++i) + { + string str = to_string(nums[i]); + for(int i = 0; i < str.size(); ++i) + str[i] = mp[str[i]]; + vp.push_back({stoi(str), i}); + } + + sort(vp.begin(), vp.end()); + + vector ans; + + for(auto&[f,e] : vp) + { + ans.push_back(nums[e]); + } + + return ans; + + } +}; \ No newline at end of file diff --git a/2191-sort-the-jumbled-numbers/README.md b/2191-sort-the-jumbled-numbers/README.md new file mode 100644 index 00000000..c230b1a8 --- /dev/null +++ b/2191-sort-the-jumbled-numbers/README.md @@ -0,0 +1,47 @@ +

2191. Sort the Jumbled Numbers

Medium


You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.

+ +

The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.

+ +

You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements.

+ +

Notes:

+ +
    +
  • Elements with the same mapped values should appear in the same relative order as in the input.
  • +
  • The elements of nums should only be sorted based on their mapped values and not be replaced by them.
  • +
+ +

 

+

Example 1:

+ +
Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
+Output: [338,38,991]
+Explanation: 
+Map the number 991 as follows:
+1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
+2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
+Therefore, the mapped value of 991 is 669.
+338 maps to 007, or 7 after removing the leading zeros.
+38 maps to 07, which is also 7 after removing leading zeros.
+Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
+Thus, the sorted array is [338,38,991].
+
+ +

Example 2:

+ +
Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
+Output: [123,456,789]
+Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].
+
+ +

 

+

Constraints:

+ +
    +
  • mapping.length == 10
  • +
  • 0 <= mapping[i] <= 9
  • +
  • All the values of mapping[i] are unique.
  • +
  • 1 <= nums.length <= 3 * 104
  • +
  • 0 <= nums[i] < 109
  • +
+
\ No newline at end of file diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp new file mode 100644 index 00000000..f2780c4f --- /dev/null +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + vector> getAncestors(int n, vector>& edges) { + + + vector adj[n]; + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + + adj[v].push_back(u); + } + + vector> ans(n); + + vector visited(n, false); + + function dfs = [&](int sv, int idx) -> void{ + + visited[sv] = true; + + for(auto& node : adj[sv]) + { + if(!visited[node]) + { + ans[idx].push_back(node); + dfs(node, idx); + } + } + }; + + for(int i = 0; i < n; ++i) + { + dfs(i, i); + + fill(visited.begin(), visited.end(), false); + + sort(ans[i].begin(), ans[i].end()); + } + + return ans; + } +}; \ No newline at end of file diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md new file mode 100644 index 00000000..8831f995 --- /dev/null +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md @@ -0,0 +1,49 @@ +

2192. All Ancestors of a Node in a Directed Acyclic Graph

Medium


You are given a positive integer n representing the number of nodes of a Directed Acyclic Graph (DAG). The nodes are numbered from 0 to n - 1 (inclusive).

+ +

You are also given a 2D integer array edges, where edges[i] = [fromi, toi] denotes that there is a unidirectional edge from fromi to toi in the graph.

+ +

Return a list answer, where answer[i] is the list of ancestors of the ith node, sorted in ascending order.

+ +

A node u is an ancestor of another node v if u can reach v via a set of edges.

+ +

 

+

Example 1:

+ +
Input: n = 8, edgeList = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]
+Output: [[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]
+Explanation:
+The above diagram represents the input graph.
+- Nodes 0, 1, and 2 do not have any ancestors.
+- Node 3 has two ancestors 0 and 1.
+- Node 4 has two ancestors 0 and 2.
+- Node 5 has three ancestors 0, 1, and 3.
+- Node 6 has five ancestors 0, 1, 2, 3, and 4.
+- Node 7 has four ancestors 0, 1, 2, and 3.
+
+ +

Example 2:

+ +
Input: n = 5, edgeList = [[0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
+Output: [[],[0],[0,1],[0,1,2],[0,1,2,3]]
+Explanation:
+The above diagram represents the input graph.
+- Node 0 does not have any ancestor.
+- Node 1 has one ancestor 0.
+- Node 2 has two ancestors 0 and 1.
+- Node 3 has three ancestors 0, 1, and 2.
+- Node 4 has four ancestors 0, 1, 2, and 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
  • 0 <= edges.length <= min(2000, n * (n - 1) / 2)
  • +
  • edges[i].length == 2
  • +
  • 0 <= fromi, toi <= n - 1
  • +
  • fromi != toi
  • +
  • There are no duplicate edges.
  • +
  • The graph is directed and acyclic.
  • +
+
\ No newline at end of file diff --git a/2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp b/2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp new file mode 100644 index 00000000..ac310fa9 --- /dev/null +++ b/2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* createBinaryTree(vector>& descriptions) { + TreeNode* map[100001] = {}; + bool child[100001] = {}; + for (auto& d : descriptions){ + if (map[d[0]] == nullptr) map[d[0]] = new TreeNode(d[0]); + TreeNode* node = (map[d[1]] == nullptr ? new TreeNode(d[1]) : map[d[1]]); + if (d[2]) + map[d[0]]->left = node; + else + map[d[0]]->right = node; + map[node->val] = node; + child[d[1]] = true; + } + for (auto& d : descriptions) + if (!child[d[0]]) + return map[d[0]]; + return nullptr; + } +}; \ No newline at end of file diff --git a/2196-create-binary-tree-from-descriptions/README.md b/2196-create-binary-tree-from-descriptions/README.md new file mode 100644 index 00000000..3d44829b --- /dev/null +++ b/2196-create-binary-tree-from-descriptions/README.md @@ -0,0 +1,39 @@ +

2196. Create Binary Tree From Descriptions

Medium


You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore,

+ +
    +
  • If isLefti == 1, then childi is the left child of parenti.
  • +
  • If isLefti == 0, then childi is the right child of parenti.
  • +
+ +

Construct the binary tree described by descriptions and return its root.

+ +

The test cases will be generated such that the binary tree is valid.

+ +

 

+

Example 1:

+ +
Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
+Output: [50,20,80,15,17,19]
+Explanation: The root node is the node with value 50 since it has no parent.
+The resulting binary tree is shown in the diagram.
+
+ +

Example 2:

+ +
Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]]
+Output: [1,2,null,null,3,4]
+Explanation: The root node is the node with value 1 since it has no parent.
+The resulting binary tree is shown in the diagram.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= descriptions.length <= 104
  • +
  • descriptions[i].length == 3
  • +
  • 1 <= parenti, childi <= 105
  • +
  • 0 <= isLefti <= 1
  • +
  • The binary tree described by descriptions is valid.
  • +
+
\ No newline at end of file diff --git a/2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp b/2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp new file mode 100644 index 00000000..b4d8e152 --- /dev/null +++ b/2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& persons) { + vector start, end; + for (auto& t : flowers) + start.push_back(t[0]), end.push_back(t[1]); + sort(start.begin(), start.end()); + sort(end.begin(), end.end()); + vector res; + for (int t : persons) { + int started = upper_bound(start.begin(), start.end(), t) - start.begin(); + int ended = lower_bound(end.begin(), end.end(), t) - end.begin(); + res.push_back(started - ended); + } + return res; + } +}; \ No newline at end of file diff --git a/2251-number-of-flowers-in-full-bloom/NOTES.md b/2251-number-of-flowers-in-full-bloom/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2251-number-of-flowers-in-full-bloom/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2251-number-of-flowers-in-full-bloom/README.md b/2251-number-of-flowers-in-full-bloom/README.md new file mode 100644 index 00000000..e6a39513 --- /dev/null +++ b/2251-number-of-flowers-in-full-bloom/README.md @@ -0,0 +1,32 @@ +

2251. Number of Flowers in Full Bloom

Hard


You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.

+ +

Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.

+ +

 

+

Example 1:

+ +
Input: flowers = [[1,6],[3,7],[9,12],[4,13]], poeple = [2,3,7,11]
+Output: [1,2,2,2]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

Example 2:

+ +
Input: flowers = [[1,10],[3,3]], poeple = [3,3,2]
+Output: [2,2,1]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= flowers.length <= 5 * 104
  • +
  • flowers[i].length == 2
  • +
  • 1 <= starti <= endi <= 109
  • +
  • 1 <= people.length <= 5 * 104
  • +
  • 1 <= people[i] <= 109
  • +
+
\ No newline at end of file diff --git a/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp new file mode 100644 index 00000000..2dc69092 --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + string largestGoodInteger(string num) { + + int n = num.size(); + + string ans; + + int i = 0, j = 0, k = 3; + + string curr; + + while(j < n) + { + curr += num[j]; + + if(curr.size() == 3) + { + if(j>=2 and num[j] == num[j-1] and num[j-1] == num[j-2]) + { + ans = max(ans, curr); + } + curr.erase(curr.begin()); + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/2264-largest-3-same-digit-number-in-string/NOTES.md b/2264-largest-3-same-digit-number-in-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2264-largest-3-same-digit-number-in-string/README.md b/2264-largest-3-same-digit-number-in-string/README.md new file mode 100644 index 00000000..6dfcc6a6 --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/README.md @@ -0,0 +1,47 @@ +

2264. Largest 3-Same-Digit Number in String

Easy


You are given a string num representing a large integer. An integer is good if it meets the following conditions:

+ +
    +
  • It is a substring of num with length 3.
  • +
  • It consists of only one unique digit.
  • +
+ +

Return the maximum good integer as a string or an empty string "" if no such integer exists.

+ +

Note:

+ +
    +
  • A substring is a contiguous sequence of characters within a string.
  • +
  • There may be leading zeroes in num or a good integer.
  • +
+ +

 

+

Example 1:

+ +
Input: num = "6777133339"
+Output: "777"
+Explanation: There are two distinct good integers: "777" and "333".
+"777" is the largest, so we return "777".
+
+ +

Example 2:

+ +
Input: num = "2300019"
+Output: "000"
+Explanation: "000" is the only good integer.
+
+ +

Example 3:

+ +
Input: num = "42352338"
+Output: ""
+Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= num.length <= 1000
  • +
  • num only consists of digits.
  • +
+
\ No newline at end of file diff --git a/2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp b/2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp new file mode 100644 index 00000000..19d7070b --- /dev/null +++ b/2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp @@ -0,0 +1,47 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int ans = 0; + + pair helper(TreeNode* root) + { + if(!root) + { + return {0, 0}; + } + + auto left = helper(root->left); + auto right = helper(root->right); + + int sum = left.first + right.first + root->val; + int cnt = left.second + right.second + 1; + + // cout<val<<" " <val) + { + ++ans; + } + + return {sum, cnt}; + } + + int averageOfSubtree(TreeNode* root) { + + helper(root); + + return ans; + + } +}; \ No newline at end of file diff --git a/2265-count-nodes-equal-to-average-of-subtree/README.md b/2265-count-nodes-equal-to-average-of-subtree/README.md new file mode 100644 index 00000000..d9972242 --- /dev/null +++ b/2265-count-nodes-equal-to-average-of-subtree/README.md @@ -0,0 +1,37 @@ +

2265. Count Nodes Equal to Average of Subtree

Medium


Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.

+ +

Note:

+ +
    +
  • The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
  • +
  • A subtree of root is a tree consisting of root and all of its descendants.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,8,5,0,1,null,6]
+Output: 5
+Explanation: 
+For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
+For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
+For the node with value 0: The average of its subtree is 0 / 1 = 0.
+For the node with value 1: The average of its subtree is 1 / 1 = 1.
+For the node with value 6: The average of its subtree is 6 / 1 = 6.
+
+ +

Example 2:

+ +
Input: root = [1]
+Output: 1
+Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
+
\ No newline at end of file diff --git a/2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp b/2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp new file mode 100644 index 00000000..b992faad --- /dev/null +++ b/2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + long long maximumImportance(int n, vector>& roads) { + + vector adj[n+1]; + vector indegree(n, 0); + map, int> have; + + for(auto& edge : roads) + { + ++indegree[edge[0]]; + ++indegree[edge[1]]; + adj[edge[0]].push_back(edge[1]); + adj[edge[1]].push_back(edge[0]); + } + + vector> vp; + + for(int i = 0; i < n; ++i) + { + vp.push_back({indegree[i], i}); + } + + sort(vp.begin(), vp.end()); + + vector vals(n, 0); + + for(int i = 0; i < n; ++i) + { + vals[vp[i].second] = i+1; + } + + vector visited(n, false); + + long long ans = 0; + + for(int i = 0; i < n; ++i) + { + for(auto& node : adj[i]) + { + int a = i, b = node; + if(a > b) swap(a, b); + + if(have.find({a, b}) == have.end()) + { + ans += vals[a]; + ans += vals[b]; + ++have[{a, b}]; + } + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/2285-maximum-total-importance-of-roads/README.md b/2285-maximum-total-importance-of-roads/README.md new file mode 100644 index 00000000..c5b07133 --- /dev/null +++ b/2285-maximum-total-importance-of-roads/README.md @@ -0,0 +1,48 @@ +

2285. Maximum Total Importance of Roads

Medium


You are given an integer n denoting the number of cities in a country. The cities are numbered from 0 to n - 1.

+ +

You are also given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

+ +

You need to assign each city with an integer value from 1 to n, where each value can only be used once. The importance of a road is then defined as the sum of the values of the two cities it connects.

+ +

Return the maximum total importance of all roads possible after assigning the values optimally.

+ +

 

+

Example 1:

+ +
Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
+Output: 43
+Explanation: The figure above shows the country and the assigned values of [2,4,5,3,1].
+- The road (0,1) has an importance of 2 + 4 = 6.
+- The road (1,2) has an importance of 4 + 5 = 9.
+- The road (2,3) has an importance of 5 + 3 = 8.
+- The road (0,2) has an importance of 2 + 5 = 7.
+- The road (1,3) has an importance of 4 + 3 = 7.
+- The road (2,4) has an importance of 5 + 1 = 6.
+The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
+It can be shown that we cannot obtain a greater total importance than 43.
+
+ +

Example 2:

+ +
Input: n = 5, roads = [[0,3],[2,4],[1,3]]
+Output: 20
+Explanation: The figure above shows the country and the assigned values of [4,3,2,5,1].
+- The road (0,3) has an importance of 4 + 5 = 9.
+- The road (2,4) has an importance of 2 + 1 = 3.
+- The road (1,3) has an importance of 3 + 5 = 8.
+The total importance of all roads is 9 + 3 + 8 = 20.
+It can be shown that we cannot obtain a greater total importance than 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 5 * 104
  • +
  • 1 <= roads.length <= 5 * 104
  • +
  • roads[i].length == 2
  • +
  • 0 <= ai, bi <= n - 1
  • +
  • ai != bi
  • +
  • There are no duplicate roads.
  • +
+
\ No newline at end of file diff --git a/2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp b/2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp new file mode 100644 index 00000000..4b1f5932 --- /dev/null +++ b/2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + bool helper(TreeNode* root) + { + if(!root->left and !root->right) + { + return root->val; + } + + bool left = helper(root->left); + bool right = helper(root->right); + + return (root->val == 2 ? left | right : left & right); + } + + bool evaluateTree(TreeNode* root) { + + return helper(root); + } +}; \ No newline at end of file diff --git a/2331-evaluate-boolean-binary-tree/NOTES.md b/2331-evaluate-boolean-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2331-evaluate-boolean-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2331-evaluate-boolean-binary-tree/README.md b/2331-evaluate-boolean-binary-tree/README.md new file mode 100644 index 00000000..d563a4b9 --- /dev/null +++ b/2331-evaluate-boolean-binary-tree/README.md @@ -0,0 +1,48 @@ +

2331. Evaluate Boolean Binary Tree

Easy


You are given the root of a full binary tree with the following properties:

+ +
    +
  • Leaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True.
  • +
  • Non-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.
  • +
+ +

The evaluation of a node is as follows:

+ +
    +
  • If the node is a leaf node, the evaluation is the value of the node, i.e. True or False.
  • +
  • Otherwise, evaluate the node's two children and apply the boolean operation of its value with the children's evaluations.
  • +
+ +

Return the boolean result of evaluating the root node.

+ +

A full binary tree is a binary tree where each node has either 0 or 2 children.

+ +

A leaf node is a node that has zero children.

+ +

 

+

Example 1:

+ +
Input: root = [2,1,3,null,null,0,1]
+Output: true
+Explanation: The above diagram illustrates the evaluation process.
+The AND node evaluates to False AND True = False.
+The OR node evaluates to True OR False = True.
+The root node evaluates to True, so we return true.
+ +

Example 2:

+ +
Input: root = [0]
+Output: false
+Explanation: The root node is a leaf node and it evaluates to false, so we return false.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 3
  • +
  • Every node has either 0 or 2 children.
  • +
  • Leaf nodes have a value of 0 or 1.
  • +
  • Non-leaf nodes have a value of 2 or 3.
  • +
+
\ No newline at end of file diff --git a/2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp b/2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp new file mode 100644 index 00000000..f865a9a6 --- /dev/null +++ b/2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp @@ -0,0 +1,34 @@ +class FoodRatings { +public: + map>> s; + unordered_map cus; + unordered_map rat; + FoodRatings(vector& foods, vector& cuisines, vector& ratings) { + for(int i=0;i p= *(s[cuisine].begin()); + return p.second; + } +}; + +/** + * Your FoodRatings object will be instantiated and called as such: + * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings); + * obj->changeRating(food,newRating); + * string param_2 = obj->highestRated(cuisine); + */ \ No newline at end of file diff --git a/2353-design-a-food-rating-system/NOTES.md b/2353-design-a-food-rating-system/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2353-design-a-food-rating-system/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2353-design-a-food-rating-system/README.md b/2353-design-a-food-rating-system/README.md new file mode 100644 index 00000000..085588a8 --- /dev/null +++ b/2353-design-a-food-rating-system/README.md @@ -0,0 +1,63 @@ +

2353. Design a Food Rating System

Medium


Design a food rating system that can do the following:

+ +
    +
  • Modify the rating of a food item listed in the system.
  • +
  • Return the highest-rated food item for a type of cuisine in the system.
  • +
+ +

Implement the FoodRatings class:

+ +
    +
  • FoodRatings(String[] foods, String[] cuisines, int[] ratings) Initializes the system. The food items are described by foods, cuisines and ratings, all of which have a length of n. + +
      +
    • foods[i] is the name of the ith food,
    • +
    • cuisines[i] is the type of cuisine of the ith food, and
    • +
    • ratings[i] is the initial rating of the ith food.
    • +
    +
  • +
  • void changeRating(String food, int newRating) Changes the rating of the food item with the name food.
  • +
  • String highestRated(String cuisine) Returns the name of the food item that has the highest rating for the given type of cuisine. If there is a tie, return the item with the lexicographically smaller name.
  • +
+ +

Note that a string x is lexicographically smaller than string y if x comes before y in dictionary order, that is, either x is a prefix of y, or if i is the first position such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order.

+ +

 

+

Example 1:

+ +
Input
+["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
+[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
+Output
+[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
+
+Explanation
+FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
+foodRatings.highestRated("korean"); // return "kimchi"
+                                    // "kimchi" is the highest rated korean food with a rating of 9.
+foodRatings.highestRated("japanese"); // return "ramen"
+                                      // "ramen" is the highest rated japanese food with a rating of 14.
+foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
+foodRatings.highestRated("japanese"); // return "sushi"
+                                      // "sushi" is the highest rated japanese food with a rating of 16.
+foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
+foodRatings.highestRated("japanese"); // return "ramen"
+                                      // Both "sushi" and "ramen" have a rating of 16.
+                                      // However, "ramen" is lexicographically smaller than "sushi".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 104
  • +
  • n == foods.length == cuisines.length == ratings.length
  • +
  • 1 <= foods[i].length, cuisines[i].length <= 10
  • +
  • foods[i], cuisines[i] consist of lowercase English letters.
  • +
  • 1 <= ratings[i] <= 108
  • +
  • All the strings in foods are distinct.
  • +
  • food will be the name of a food item in the system across all calls to changeRating.
  • +
  • cuisine will be a type of cuisine of at least one food item in the system across all calls to highestRated.
  • +
  • At most 2 * 104 calls in total will be made to changeRating and highestRated.
  • +
+
\ No newline at end of file diff --git a/2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp b/2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp new file mode 100644 index 00000000..d54a17f5 --- /dev/null +++ b/2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + int helper(int idx, int prev, int k, int n, string& s, vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][prev+1] != -1) + return dp[idx][prev+1]; + + int notTake = helper(idx+1, prev, k, n, s, dp); + + int take = 0; + int curr = s[idx]; + if(prev == -1 or abs(prev - curr) <= k) + take = max(1 + helper(idx+1, curr, k, n, s, dp), notTake); + + return dp[idx][prev+1] = max(take, notTake); + } + + int longestIdealString(string s, int k) { + + int n = s.size(); + + vector> dp(n+1, vector(150, -1)); + + return helper(0, -1, k, n, s, dp); + + } +}; \ No newline at end of file diff --git a/2370-longest-ideal-subsequence/README.md b/2370-longest-ideal-subsequence/README.md new file mode 100644 index 00000000..17c7257e --- /dev/null +++ b/2370-longest-ideal-subsequence/README.md @@ -0,0 +1,37 @@ +

2370. Longest Ideal Subsequence

Medium


You are given a string s consisting of lowercase letters and an integer k. We call a string t ideal if the following conditions are satisfied:

+ +
    +
  • t is a subsequence of the string s.
  • +
  • The absolute difference in the alphabet order of every two adjacent letters in t is less than or equal to k.
  • +
+ +

Return the length of the longest ideal string.

+ +

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

+ +

Note that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of 'a' and 'z' is 25, not 1.

+ +

 

+

Example 1:

+ +
Input: s = "acfgbd", k = 2
+Output: 4
+Explanation: The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
+Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.
+ +

Example 2:

+ +
Input: s = "abcd", k = 3
+Output: 4
+Explanation: The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • 0 <= k <= 25
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp b/2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp new file mode 100644 index 00000000..1620dba7 --- /dev/null +++ b/2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector> largestLocal(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector> ans; + + for(int i = 0; i < n-2; ++i) + { + vector curr; + for(int j = 0; j < n-2; ++j) + { + int maxi = -1; + for(int row = i; row < i+3; ++row) + { + for(int col = j; col < j+3; ++col) + { + maxi = max(maxi, grid[row][col]); + } + } + curr.push_back(maxi); + } + ans.push_back(curr); + } + + return ans; + } +}; \ No newline at end of file diff --git a/2373-largest-local-values-in-a-matrix/README.md b/2373-largest-local-values-in-a-matrix/README.md new file mode 100644 index 00000000..b70175ae --- /dev/null +++ b/2373-largest-local-values-in-a-matrix/README.md @@ -0,0 +1,36 @@ +

2373. Largest Local Values in a Matrix

Easy


You are given an n x n integer matrix grid.

+ +

Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that:

+ +
    +
  • maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.
  • +
+ +

In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid.

+ +

Return the generated matrix.

+ +

 

+

Example 1:

+ +
Input: grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
+Output: [[9,9],[8,6]]
+Explanation: The diagram above shows the original matrix and the generated matrix.
+Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.
+ +

Example 2:

+ +
Input: grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
+Output: [[2,2,2],[2,2,2],[2,2,2]]
+Explanation: Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 3 <= n <= 100
  • +
  • 1 <= grid[i][j] <= 100
  • +
+
\ No newline at end of file diff --git a/2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp b/2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp new file mode 100644 index 00000000..c9bdd38b --- /dev/null +++ b/2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp @@ -0,0 +1,86 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + map> graph; + + int amountOfTime(TreeNode* root, int start) { + + queue q; + + q.push(root); + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0; i < size; ++i) + { + TreeNode* parent = q.front(); + q.pop(); + + if(parent->left) + { + q.push(parent->left); + graph[parent->val].push_back(parent->left->val); + graph[parent->left->val].push_back(parent->val); + } + + if(parent->right) + { + q.push(parent->right); + graph[parent->val].push_back(parent->right->val); + graph[parent->right->val].push_back(parent->val); + } + } + } + + int n = graph.size(); + + queue q2; + + set visited; + + visited.insert(start); + + for(auto& itr : graph[start]) + { + q2.push(itr); + visited.insert(itr); + } + + int cnt = 0; + + while(!q2.empty()) + { + int size = q2.size(); + for(int i = 0; i < size; ++i) + { + int curr = q2.front(); + q2.pop(); + + for(auto& itr : graph[curr]) + { + if(!visited.count(itr)) + { + q2.push(itr); + visited.insert(itr); + } + } + } + ++cnt; + } + + return cnt; + } +}; \ No newline at end of file diff --git a/2385-amount-of-time-for-binary-tree-to-be-infected/README.md b/2385-amount-of-time-for-binary-tree-to-be-infected/README.md new file mode 100644 index 00000000..4c9f31b3 --- /dev/null +++ b/2385-amount-of-time-for-binary-tree-to-be-infected/README.md @@ -0,0 +1,42 @@ +

2385. Amount of Time for Binary Tree to Be Infected

Medium


You are given the root of a binary tree with unique values, and an integer start. At minute 0, an infection starts from the node with value start.

+ +

Each minute, a node becomes infected if:

+ +
    +
  • The node is currently uninfected.
  • +
  • The node is adjacent to an infected node.
  • +
+ +

Return the number of minutes needed for the entire tree to be infected.

+ +

 

+

Example 1:

+ +
Input: root = [1,5,3,null,4,10,6,9,2], start = 3
+Output: 4
+Explanation: The following nodes are infected during:
+- Minute 0: Node 3
+- Minute 1: Nodes 1, 10 and 6
+- Minute 2: Node 5
+- Minute 3: Node 4
+- Minute 4: Nodes 9 and 2
+It takes 4 minutes for the whole tree to be infected so we return 4.
+
+ +

Example 2:

+ +
Input: root = [1], start = 1
+Output: 0
+Explanation: At minute 0, the only node in the tree is infected so we return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 105].
  • +
  • 1 <= Node.val <= 105
  • +
  • Each node has a unique value.
  • +
  • A node with a value of start exists in the tree.
  • +
+
\ No newline at end of file diff --git a/2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp b/2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp new file mode 100644 index 00000000..80607765 --- /dev/null +++ b/2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int garbageCollection(vector& garbage, vector& travel) { + + int n = garbage.size(); + + int totalTime = 0; + + unordered_map mp; + + for(int i = 0; i < n; ++i) + { + for(auto& gar : garbage[i]) + ++mp[gar]; + } + + for(int i = 0; i < n; ++i) + { + int p = 0, g = 0, m = 0; + + for(auto& gar : garbage[i]) + { + if(gar == 'P') + ++p; + else if(gar == 'G') + ++g; + else + ++m; + } + + totalTime += (p+g+m); + + if(i-1 >= 0) + { + if(mp['P']) totalTime += travel[i-1]; + if(mp['G']) totalTime += travel[i-1]; + if(mp['M']) totalTime += travel[i-1]; + } + + mp['P'] -= p; + mp['M'] -= m; + mp['G'] -= g; + + if(mp['P'] == 0) + mp.erase('P'); + if(mp['G'] == 0) + mp.erase('G'); + if(mp['M'] == 0) + mp.erase('M'); + + } + + return totalTime; + + } +}; \ No newline at end of file diff --git a/2391-minimum-amount-of-time-to-collect-garbage/NOTES.md b/2391-minimum-amount-of-time-to-collect-garbage/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2391-minimum-amount-of-time-to-collect-garbage/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2391-minimum-amount-of-time-to-collect-garbage/README.md b/2391-minimum-amount-of-time-to-collect-garbage/README.md new file mode 100644 index 00000000..8a44ad8c --- /dev/null +++ b/2391-minimum-amount-of-time-to-collect-garbage/README.md @@ -0,0 +1,56 @@ +

2391. Minimum Amount of Time to Collect Garbage

Medium


You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the ith house. garbage[i] consists only of the characters 'M', 'P' and 'G' representing one unit of metal, paper and glass garbage respectively. Picking up one unit of any type of garbage takes 1 minute.

+ +

You are also given a 0-indexed integer array travel where travel[i] is the number of minutes needed to go from house i to house i + 1.

+ +

There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order; however, they do not need to visit every house.

+ +

Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.

+ +

Return the minimum number of minutes needed to pick up all the garbage.

+ +

 

+

Example 1:

+ +
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
+Output: 21
+Explanation:
+The paper garbage truck:
+1. Travels from house 0 to house 1
+2. Collects the paper garbage at house 1
+3. Travels from house 1 to house 2
+4. Collects the paper garbage at house 2
+Altogether, it takes 8 minutes to pick up all the paper garbage.
+The glass garbage truck:
+1. Collects the glass garbage at house 0
+2. Travels from house 0 to house 1
+3. Travels from house 1 to house 2
+4. Collects the glass garbage at house 2
+5. Travels from house 2 to house 3
+6. Collects the glass garbage at house 3
+Altogether, it takes 13 minutes to pick up all the glass garbage.
+Since there is no metal garbage, we do not need to consider the metal garbage truck.
+Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
+
+ +

Example 2:

+ +
Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
+Output: 37
+Explanation:
+The metal garbage truck takes 7 minutes to pick up all the metal garbage.
+The paper garbage truck takes 15 minutes to pick up all the paper garbage.
+The glass garbage truck takes 15 minutes to pick up all the glass garbage.
+It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= garbage.length <= 105
  • +
  • garbage[i] consists of only the letters 'M', 'P', and 'G'.
  • +
  • 1 <= garbage[i].length <= 10
  • +
  • travel.length == garbage.length - 1
  • +
  • 1 <= travel[i] <= 100
  • +
+
\ No newline at end of file diff --git a/2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp b/2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp new file mode 100644 index 00000000..356083ec --- /dev/null +++ b/2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp @@ -0,0 +1,65 @@ +class Solution { +public: + vector findOrder(int k, vector>& dependencies) { + + vector> adj(k + 1); + vector indegree(k + 1); + for(auto dependency: dependencies) { + adj[dependency[0]].push_back(dependency[1]); + indegree[dependency[1]]++; + } + + queue q; + for(int i = 1; i <= k; i++) { + if(indegree[i] == 0) { + q.push(i); + } + } + + int cnt = 0; + vector order; + while(!q.empty()) { + + int cur = q.front(); + q.pop(); + + cnt++; + order.push_back(cur); + + for(int nbr: adj[cur]) { + indegree[nbr]--; + + if(indegree[nbr] == 0) { + q.push(nbr); + } + } + } + + if(cnt == k) return order; + return {}; + } + + vector> buildMatrix(int k, vector>& rowConditions, vector>& colConditions) { + vector rowArray = findOrder(k, rowConditions); + vector colArray = findOrder(k, colConditions); + + + if(rowArray.size() == 0 || colArray.size() == 0) { + return {}; + } + + vector> ind(k); + + for(int i = 0; i < k; i++) { + ind[rowArray[i] - 1].first = i; + ind[colArray[i] - 1].second = i; + } + + vector> result(k, vector(k, 0)); + + for(int i = 0; i < k; i++) { + result[ind[i].first][ind[i].second] = i + 1; + } + return result; + } +}; \ No newline at end of file diff --git a/2392-build-a-matrix-with-conditions/README.md b/2392-build-a-matrix-with-conditions/README.md new file mode 100644 index 00000000..83a8ca6c --- /dev/null +++ b/2392-build-a-matrix-with-conditions/README.md @@ -0,0 +1,55 @@ +

2392. Build a Matrix With Conditions

Hard


You are given a positive integer k. You are also given:

+ +
    +
  • a 2D integer array rowConditions of size n where rowConditions[i] = [abovei, belowi], and
  • +
  • a 2D integer array colConditions of size m where colConditions[i] = [lefti, righti].
  • +
+ +

The two arrays contain integers from 1 to k.

+ +

You have to build a k x k matrix that contains each of the numbers from 1 to k exactly once. The remaining cells should have the value 0.

+ +

The matrix should also satisfy the following conditions:

+ +
    +
  • The number abovei should appear in a row that is strictly above the row at which the number belowi appears for all i from 0 to n - 1.
  • +
  • The number lefti should appear in a column that is strictly left of the column at which the number righti appears for all i from 0 to m - 1.
  • +
+ +

Return any matrix that satisfies the conditions. If no answer exists, return an empty matrix.

+ +

 

+

Example 1:

+ +
Input: k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
+Output: [[3,0,0],[0,0,1],[0,2,0]]
+Explanation: The diagram above shows a valid example of a matrix that satisfies all the conditions.
+The row conditions are the following:
+- Number 1 is in row 1, and number 2 is in row 2, so 1 is above 2 in the matrix.
+- Number 3 is in row 0, and number 2 is in row 2, so 3 is above 2 in the matrix.
+The column conditions are the following:
+- Number 2 is in column 1, and number 1 is in column 2, so 2 is left of 1 in the matrix.
+- Number 3 is in column 0, and number 2 is in column 1, so 3 is left of 2 in the matrix.
+Note that there may be multiple correct answers.
+
+ +

Example 2:

+ +
Input: k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]]
+Output: []
+Explanation: From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied.
+No matrix can satisfy all the conditions, so we return the empty matrix.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= k <= 400
  • +
  • 1 <= rowConditions.length, colConditions.length <= 104
  • +
  • rowConditions[i].length == colConditions[i].length == 2
  • +
  • 1 <= abovei, belowi, lefti, righti <= k
  • +
  • abovei != belowi
  • +
  • lefti != righti
  • +
+
\ No newline at end of file diff --git a/2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp b/2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp new file mode 100644 index 00000000..bc237167 --- /dev/null +++ b/2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp @@ -0,0 +1,66 @@ + +class Solution { +public: + int mostBooked(int n, vector>& meetings) { + + int m = meetings.size(); + + int roomOccupied = 0; + + sort(meetings.begin(), meetings.end()); + + vector roomCount(n, 0); + + priority_queue, vector>, greater>> meetingRooms; // EndTime, RoomNumber + + priority_queue, greater> availableRooms; + + for(int room = 0; room < n; ++room) + availableRooms.push(room); + + for(auto& meet : meetings) + { + long long start = meet[0]; + long long end = meet[1]; + long long duration = end - start; + + while(!meetingRooms.empty() and meetingRooms.top().first <= start) + { + int room = meetingRooms.top().second; + availableRooms.push(room); + meetingRooms.pop(); + } + + if(!availableRooms.empty()) + { + int room = availableRooms.top(); + availableRooms.pop(); + meetingRooms.push({end, room}); + ++roomCount[room]; + } + else + { + int room = meetingRooms.top().second; + long long endTime = meetingRooms.top().first; + meetingRooms.pop(); + endTime += duration; + meetingRooms.push({endTime, room}); + ++roomCount[room]; + } + } + + int resultRoom = -1; + int maxUse = 0; + + for(int i = 0; i < n; ++i) + { + if(roomCount[i] > maxUse) + { + resultRoom = i; + maxUse = roomCount[i]; + } + } + + return resultRoom; + } +}; \ No newline at end of file diff --git a/2402-meeting-rooms-iii/NOTES.md b/2402-meeting-rooms-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2402-meeting-rooms-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2402-meeting-rooms-iii/README.md b/2402-meeting-rooms-iii/README.md new file mode 100644 index 00000000..fffc9ee8 --- /dev/null +++ b/2402-meeting-rooms-iii/README.md @@ -0,0 +1,57 @@ +

2402. Meeting Rooms III

Hard


You are given an integer n. There are n rooms numbered from 0 to n - 1.

+ +

You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

+ +

Meetings are allocated to rooms in the following manner:

+ +
    +
  1. Each meeting will take place in the unused room with the lowest number.
  2. +
  3. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
  4. +
  5. When a room becomes unused, meetings that have an earlier original start time should be given the room.
  6. +
+ +

Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

+ +

A half-closed interval [a, b) is the interval between a and b including a and not including b.

+ +

 

+

Example 1:

+ +
Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
+Output: 0
+Explanation:
+- At time 0, both rooms are not being used. The first meeting starts in room 0.
+- At time 1, only room 1 is not being used. The second meeting starts in room 1.
+- At time 2, both rooms are being used. The third meeting is delayed.
+- At time 3, both rooms are being used. The fourth meeting is delayed.
+- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
+- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
+Both rooms 0 and 1 held 2 meetings, so we return 0. 
+
+ +

Example 2:

+ +
Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
+Output: 1
+Explanation:
+- At time 1, all three rooms are not being used. The first meeting starts in room 0.
+- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
+- At time 3, only room 2 is not being used. The third meeting starts in room 2.
+- At time 4, all three rooms are being used. The fourth meeting is delayed.
+- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
+- At time 6, all three rooms are being used. The fifth meeting is delayed.
+- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
+Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 1 <= meetings.length <= 105
  • +
  • meetings[i].length == 2
  • +
  • 0 <= starti < endi <= 5 * 105
  • +
  • All the values of starti are unique.
  • +
+
\ No newline at end of file diff --git a/2418-sort-the-people/2418-sort-the-people.cpp b/2418-sort-the-people/2418-sort-the-people.cpp new file mode 100644 index 00000000..eb04256d --- /dev/null +++ b/2418-sort-the-people/2418-sort-the-people.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector sortPeople(vector& names, vector& heights) { + + vector> here; + + for(int i = 0; i < heights.size(); ++i) + here.push_back({heights[i], i}); + + sort(here.rbegin(), here.rend()); + + vector ans; + + for(int i = 0; i < names.size(); ++i) + ans.push_back(names[here[i].second]); + + return ans; + + } +}; \ No newline at end of file diff --git a/2418-sort-the-people/NOTES.md b/2418-sort-the-people/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2418-sort-the-people/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2418-sort-the-people/README.md b/2418-sort-the-people/README.md new file mode 100644 index 00000000..2f291942 --- /dev/null +++ b/2418-sort-the-people/README.md @@ -0,0 +1,33 @@ +

2418. Sort the People

Easy


You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

+ +

For each index i, names[i] and heights[i] denote the name and height of the ith person.

+ +

Return names sorted in descending order by the people's heights.

+ +

 

+

Example 1:

+ +
Input: names = ["Mary","John","Emma"], heights = [180,165,170]
+Output: ["Mary","Emma","John"]
+Explanation: Mary is the tallest, followed by Emma and John.
+
+ +

Example 2:

+ +
Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
+Output: ["Bob","Alice","Bob"]
+Explanation: The first Bob is the tallest, followed by Alice and the second Bob.
+
+ +

 

+

Constraints:

+ +
    +
  • n == names.length == heights.length
  • +
  • 1 <= n <= 103
  • +
  • 1 <= names[i].length <= 20
  • +
  • 1 <= heights[i] <= 105
  • +
  • names[i] consists of lower and upper case English letters.
  • +
  • All the values of heights are distinct.
  • +
+
\ No newline at end of file diff --git a/2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp b/2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp new file mode 100644 index 00000000..d6984685 --- /dev/null +++ b/2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + vector findArray(vector& pref) { + + int n = pref.size(); + + vector ans(n); + + ans[0] = pref[0]; + + int currXor = pref[0]; + + for(int i = 1; i < n; ++i) + { + int mask = 0; + int newXor = pref[i]; + + for(int i = 0; i < 32; ++i) + { + if(!(currXor & (1 << i)) and (newXor & (1 << i))) + mask |= (1<2433. Find The Original Array of Prefix Xor

Medium


You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

+ +
    +
  • pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
  • +
+ +

Note that ^ denotes the bitwise-xor operation.

+ +

It can be proven that the answer is unique.

+ +

 

+

Example 1:

+ +
Input: pref = [5,2,0,3,1]
+Output: [5,7,2,3,2]
+Explanation: From the array [5,7,2,3,2] we have the following:
+- pref[0] = 5.
+- pref[1] = 5 ^ 7 = 2.
+- pref[2] = 5 ^ 7 ^ 2 = 0.
+- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
+- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
+
+ +

Example 2:

+ +
Input: pref = [13]
+Output: [13]
+Explanation: We have pref[0] = arr[0] = 13.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= pref.length <= 105
  • +
  • 0 <= pref[i] <= 106
  • +
+
\ No newline at end of file diff --git a/2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp b/2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp new file mode 100644 index 00000000..03f004a3 --- /dev/null +++ b/2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int findMaxK(vector& nums) { + + set st(nums.begin(), nums.end()); + int ans = -1; + + for(auto& ele : nums) + { + if(ele > 0) + { + if(st.count(-ele)) + ans = max(ans, ele); + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md b/2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2441-largest-positive-integer-that-exists-with-its-negative/README.md b/2441-largest-positive-integer-that-exists-with-its-negative/README.md new file mode 100644 index 00000000..6a824672 --- /dev/null +++ b/2441-largest-positive-integer-that-exists-with-its-negative/README.md @@ -0,0 +1,35 @@ +

2441. Largest Positive Integer That Exists With Its Negative

Easy


Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

+ +

Return the positive integer k. If there is no such integer, return -1.

+ +

 

+

Example 1:

+ +
Input: nums = [-1,2,-3,3]
+Output: 3
+Explanation: 3 is the only valid k we can find in the array.
+
+ +

Example 2:

+ +
Input: nums = [-1,10,6,7,-7,1]
+Output: 7
+Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
+
+ +

Example 3:

+ +
Input: nums = [-10,8,6,7,-2,-3]
+Output: -1
+Explanation: There is no a single valid k, we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -1000 <= nums[i] <= 1000
  • +
  • nums[i] != 0
  • +
+
\ No newline at end of file diff --git a/2444-count-subarrays-with-fixed-bounds/README.md b/2444-count-subarrays-with-fixed-bounds/README.md new file mode 100644 index 00000000..ab1ef885 --- /dev/null +++ b/2444-count-subarrays-with-fixed-bounds/README.md @@ -0,0 +1,36 @@ +

2444. Count Subarrays With Fixed Bounds

Hard


You are given an integer array nums and two integers minK and maxK.

+ +

A fixed-bound subarray of nums is a subarray that satisfies the following conditions:

+ +
    +
  • The minimum value in the subarray is equal to minK.
  • +
  • The maximum value in the subarray is equal to maxK.
  • +
+ +

Return the number of fixed-bound subarrays.

+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
+Output: 2
+Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1], minK = 1, maxK = 1
+Output: 10
+Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i], minK, maxK <= 106
  • +
+
\ No newline at end of file diff --git a/2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp b/2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp new file mode 100644 index 00000000..97f982b3 --- /dev/null +++ b/2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector> onesMinusZeros(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector row(n, 0), col(m, 0); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + ++row[i], ++col[j]; + } + } + + vector> diff(n, vector(m)); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + diff[i][j] = (row[i] + col[j] - (m - row[i]) - (n - col[j])); + } + + return diff; + } +}; \ No newline at end of file diff --git a/2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md b/2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2482-difference-between-ones-and-zeros-in-row-and-column/README.md b/2482-difference-between-ones-and-zeros-in-row-and-column/README.md new file mode 100644 index 00000000..3353ff56 --- /dev/null +++ b/2482-difference-between-ones-and-zeros-in-row-and-column/README.md @@ -0,0 +1,55 @@ +

2482. Difference Between Ones and Zeros in Row and Column

Medium


You are given a 0-indexed m x n binary matrix grid.

+ +

A 0-indexed m x n difference matrix diff is created with the following procedure:

+ +
    +
  • Let the number of ones in the ith row be onesRowi.
  • +
  • Let the number of ones in the jth column be onesColj.
  • +
  • Let the number of zeros in the ith row be zerosRowi.
  • +
  • Let the number of zeros in the jth column be zerosColj.
  • +
  • diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
  • +
+ +

Return the difference matrix diff.

+ +

 

+

Example 1:

+ +
Input: grid = [[0,1,1],[1,0,1],[0,0,1]]
+Output: [[0,0,4],[0,0,4],[-2,-2,2]]
+Explanation:
+- diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 2 + 1 - 1 - 2 = 0 
+- diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 2 + 1 - 1 - 2 = 0 
+- diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 2 + 3 - 1 - 0 = 4 
+- diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 2 + 1 - 1 - 2 = 0 
+- diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 2 + 1 - 1 - 2 = 0 
+- diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 2 + 3 - 1 - 0 = 4 
+- diff[2][0] = onesRow2 + onesCol0 - zerosRow2 - zerosCol0 = 1 + 1 - 2 - 2 = -2
+- diff[2][1] = onesRow2 + onesCol1 - zerosRow2 - zerosCol1 = 1 + 1 - 2 - 2 = -2
+- diff[2][2] = onesRow2 + onesCol2 - zerosRow2 - zerosCol2 = 1 + 3 - 2 - 0 = 2
+
+ +

Example 2:

+ +
Input: grid = [[1,1,1],[1,1,1]]
+Output: [[5,5,5],[5,5,5]]
+Explanation:
+- diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 3 + 2 - 0 - 0 = 5
+- diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 3 + 2 - 0 - 0 = 5
+- diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 3 + 2 - 0 - 0 = 5
+- diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 3 + 2 - 0 - 0 = 5
+- diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 3 + 2 - 0 - 0 = 5
+- diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 3 + 2 - 0 - 0 = 5
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 105
  • +
  • 1 <= m * n <= 105
  • +
  • grid[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file diff --git a/2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp b/2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp new file mode 100644 index 00000000..2c01acd0 --- /dev/null +++ b/2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int pivotInteger(int n) { + + int tot = (n*(n+1))/2; + + for(int i = 1; i <= n; ++i) + { + int left = (i*(i+1))/2; + int right = tot - left + i; + + if(left == right) + return i; + } + + return -1; + } +}; \ No newline at end of file diff --git a/2485-find-the-pivot-integer/NOTES.md b/2485-find-the-pivot-integer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2485-find-the-pivot-integer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2485-find-the-pivot-integer/README.md b/2485-find-the-pivot-integer/README.md new file mode 100644 index 00000000..12c9294c --- /dev/null +++ b/2485-find-the-pivot-integer/README.md @@ -0,0 +1,37 @@ +

2485. Find the Pivot Integer

Easy


Given a positive integer n, find the pivot integer x such that:

+ +
    +
  • The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively.
  • +
+ +

Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.

+ +

 

+

Example 1:

+ +
Input: n = 8
+Output: 6
+Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
+
+ +

Example 2:

+ +
Input: n = 1
+Output: 1
+Explanation: 1 is the pivot integer since: 1 = 1.
+
+ +

Example 3:

+ +
Input: n = 4
+Output: -1
+Explanation: It can be proved that no such integer exist.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file diff --git a/2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp b/2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp new file mode 100644 index 00000000..36e9a014 --- /dev/null +++ b/2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int appendCharacters(string s, string t) { + + int idx = 0; + + for(int i = 0; i < s.size(); ++i) + { + if(t[idx] == s[i]) + ++idx; + if(idx == t.size()) + return 0; + } + + return t.size() - idx; + + } +}; \ No newline at end of file diff --git a/2486-append-characters-to-string-to-make-subsequence/README.md b/2486-append-characters-to-string-to-make-subsequence/README.md new file mode 100644 index 00000000..3029053c --- /dev/null +++ b/2486-append-characters-to-string-to-make-subsequence/README.md @@ -0,0 +1,40 @@ +

2486. Append Characters to String to Make Subsequence

Medium


You are given two strings s and t consisting of only lowercase English letters.

+ +

Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s.

+ +

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

+ +

 

+

Example 1:

+ +
Input: s = "coaching", t = "coding"
+Output: 4
+Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
+Now, t is a subsequence of s ("coachingding").
+It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
+
+ +

Example 2:

+ +
Input: s = "abcde", t = "a"
+Output: 0
+Explanation: t is already a subsequence of s ("abcde").
+
+ +

Example 3:

+ +
Input: s = "z", t = "abcde"
+Output: 5
+Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
+Now, t is a subsequence of s ("zabcde").
+It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 105
  • +
  • s and t consist only of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/2540-minimum-common-value/2540-minimum-common-value.cpp b/2540-minimum-common-value/2540-minimum-common-value.cpp new file mode 100644 index 00000000..116f4f9c --- /dev/null +++ b/2540-minimum-common-value/2540-minimum-common-value.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int getCommon(vector& nums1, vector& nums2) { + + int i = nums1.size() - 1, j = nums2.size() - 1; + + int cnt = 0, ans = -1; + + while(i >= 0 and j >= 0) + { + if(nums1[i] == nums2[j]) + { + ans = nums1[i]; + --i, --j; + } + else if(nums1[i] > nums2[j]) + --i; + else + --j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/2540-minimum-common-value/README.md b/2540-minimum-common-value/README.md new file mode 100644 index 00000000..acdefbbf --- /dev/null +++ b/2540-minimum-common-value/README.md @@ -0,0 +1,28 @@ +

2540. Minimum Common Value

Easy


Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.

+ +

Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,3], nums2 = [2,4]
+Output: 2
+Explanation: The smallest element common to both arrays is 2, so we return 2.
+
+ +

Example 2:

+ +
Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
+Output: 2
+Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 105
  • +
  • 1 <= nums1[i], nums2[j] <= 109
  • +
  • Both nums1 and nums2 are sorted in non-decreasing order.
  • +
+
\ No newline at end of file diff --git a/2582-pass-the-pillow/2582-pass-the-pillow.cpp b/2582-pass-the-pillow/2582-pass-the-pillow.cpp new file mode 100644 index 00000000..52a89766 --- /dev/null +++ b/2582-pass-the-pillow/2582-pass-the-pillow.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int passThePillow(int n, int time) { + + int fullChunks = time / (n-1); + int extTime = time % (n-1); + + return (fullChunks & 1 ? n - extTime : extTime + 1); + + } +}; \ No newline at end of file diff --git a/2582-pass-the-pillow/NOTES.md b/2582-pass-the-pillow/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2582-pass-the-pillow/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2582-pass-the-pillow/README.md b/2582-pass-the-pillow/README.md new file mode 100644 index 00000000..fcea9416 --- /dev/null +++ b/2582-pass-the-pillow/README.md @@ -0,0 +1,32 @@ +

2582. Pass the Pillow

Easy


There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.

+ +
    +
  • For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on.
  • +
+ +

Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.

+

 

+

Example 1:

+ +
Input: n = 4, time = 5
+Output: 2
+Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
+After five seconds, the 2nd person is holding the pillow.
+
+ +

Example 2:

+ +
Input: n = 3, time = 2
+Output: 3
+Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
+After two seconds, the 3rd person is holding the pillow.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 1000
  • +
  • 1 <= time <= 1000
  • +
+
\ No newline at end of file diff --git a/2597-the-number-of-beautiful-subsets/NOTES.md b/2597-the-number-of-beautiful-subsets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2597-the-number-of-beautiful-subsets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp b/2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp new file mode 100644 index 00000000..5dd5bc13 --- /dev/null +++ b/2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + vector> findMatrix(vector& nums) { + + map mp; + + for(auto& itr : nums) + ++mp[itr]; + + vector> ans; + + bool ok = true; + + while(ok) + { + vector currRow; + ok = false; + for(auto& [f, e] : mp) + { + if(mp[f] > 0) + currRow.push_back(f); + --e; + if(e > 0) + ok = true; + } + ans.push_back(currRow); + } + + return ans; + } +}; \ No newline at end of file diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md b/2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions/README.md b/2610-convert-an-array-into-a-2d-array-with-conditions/README.md new file mode 100644 index 00000000..58b3233b --- /dev/null +++ b/2610-convert-an-array-into-a-2d-array-with-conditions/README.md @@ -0,0 +1,39 @@ +

2610. Convert an Array Into a 2D Array With Conditions

Medium


You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:

+ +
    +
  • The 2D array should contain only the elements of the array nums.
  • +
  • Each row in the 2D array contains distinct integers.
  • +
  • The number of rows in the 2D array should be minimal.
  • +
+ +

Return the resulting array. If there are multiple answers, return any of them.

+ +

Note that the 2D array can have a different number of elements on each row.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,4,1,2,3,1]
+Output: [[1,3,4,2],[1,3],[1]]
+Explanation: We can create a 2D array that contains the following rows:
+- 1,3,4,2
+- 1,3
+- 1
+All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
+It can be shown that we cannot have less than 3 rows in a valid array.
+ +

Example 2:

+ +
Input: nums = [1,2,3,4]
+Output: [[4,3,2,1]]
+Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 200
  • +
  • 1 <= nums[i] <= nums.length
  • +
+
\ No newline at end of file diff --git a/2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp b/2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp new file mode 100644 index 00000000..b46998f3 --- /dev/null +++ b/2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp @@ -0,0 +1,76 @@ +class Graph { +public: + + vector > adj[105]; + + int dijistras(int src, int dest) + { + vector dist(105, INT_MAX); + + priority_queue, vector>, greater> > pq; + + dist[src] = 0; + + pq.push({dist[src], src}); + + while(!pq.empty()) + { + pair curr = pq.top(); + pq.pop(); + + int distance = curr.first; + int node = curr.second; + + if(node == dest) + { + return distance; + } + + for(auto& child : adj[node]) + { + if(distance + child.second < dist[child.first]) + { + dist[child.first] = distance + child.second; + + pq.push({dist[child.first], child.first}); + } + } + } + return -1; + } + + Graph(int n, vector>& edges) { + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + } + } + + void addEdge(vector edge) { + + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + + } + + int shortestPath(int node1, int node2) { + + return dijistras(node1, node2); + + } +}; + +/** + * Your Graph object will be instantiated and called as such: + * Graph* obj = new Graph(n, edges); + * obj->addEdge(edge); + * int param_2 = obj->shortestPath(node1,node2); + */ \ No newline at end of file diff --git a/2642-design-graph-with-shortest-path-calculator/NOTES.md b/2642-design-graph-with-shortest-path-calculator/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2642-design-graph-with-shortest-path-calculator/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2642-design-graph-with-shortest-path-calculator/README.md b/2642-design-graph-with-shortest-path-calculator/README.md new file mode 100644 index 00000000..151185c6 --- /dev/null +++ b/2642-design-graph-with-shortest-path-calculator/README.md @@ -0,0 +1,41 @@ +

2642. Design Graph With Shortest Path Calculator

Hard


There is a directed weighted graph that consists of n nodes numbered from 0 to n - 1. The edges of the graph are initially represented by the given array edges where edges[i] = [fromi, toi, edgeCosti] meaning that there is an edge from fromi to toi with the cost edgeCosti.

+ +

Implement the Graph class:

+ +
    +
  • Graph(int n, int[][] edges) initializes the object with n nodes and the given edges.
  • +
  • addEdge(int[] edge) adds an edge to the list of edges where edge = [from, to, edgeCost]. It is guaranteed that there is no edge between the two nodes before adding this one.
  • +
  • int shortestPath(int node1, int node2) returns the minimum cost of a path from node1 to node2. If no path exists, return -1. The cost of a path is the sum of the costs of the edges in the path.
  • +
+ +

 

+

Example 1:

+ +
Input
+["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
+[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
+Output
+[null, 6, -1, null, 6]
+
+Explanation
+Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
+g.shortestPath(3, 2); // return 6. The shortest path from 3 to 2 in the first diagram above is 3 -> 0 -> 1 -> 2 with a total cost of 3 + 2 + 1 = 6.
+g.shortestPath(0, 3); // return -1. There is no path from 0 to 3.
+g.addEdge([1, 3, 4]); // We add an edge from node 1 to node 3, and we get the second diagram above.
+g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -> 1 -> 3 with a total cost of 2 + 4 = 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= edges.length <= n * (n - 1)
  • +
  • edges[i].length == edge.length == 3
  • +
  • 0 <= fromi, toi, from, to, node1, node2 <= n - 1
  • +
  • 1 <= edgeCosti, edgeCost <= 106
  • +
  • There are no repeated edges and no self-loops in the graph at any point.
  • +
  • At most 100 calls will be made for addEdge.
  • +
  • At most 100 calls will be made for shortestPath.
  • +
+
\ No newline at end of file diff --git a/2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp b/2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp new file mode 100644 index 00000000..d02e9832 --- /dev/null +++ b/2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int countSeniors(vector& details) { + + int cnt = 0; + + for(auto& str : details) + { + cnt += (stoi(str.substr(11, 2)) > 60); + } + + return cnt; + } +}; \ No newline at end of file diff --git a/2678-number-of-senior-citizens/README.md b/2678-number-of-senior-citizens/README.md new file mode 100644 index 00000000..f48784b3 --- /dev/null +++ b/2678-number-of-senior-citizens/README.md @@ -0,0 +1,37 @@ +

2678. Number of Senior Citizens

Easy


You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:

+ +
    +
  • The first ten characters consist of the phone number of passengers.
  • +
  • The next character denotes the gender of the person.
  • +
  • The following two characters are used to indicate the age of the person.
  • +
  • The last two characters determine the seat allotted to that person.
  • +
+ +

Return the number of passengers who are strictly more than 60 years old.

+ +

 

+

Example 1:

+ +
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
+Output: 2
+Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
+
+ +

Example 2:

+ +
Input: details = ["1313579440F2036","2921522980M5644"]
+Output: 0
+Explanation: None of the passengers are older than 60.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= details.length <= 100
  • +
  • details[i].length == 15
  • +
  • details[i] consists of digits from '0' to '9'.
  • +
  • details[i][10] is either 'M' or 'F' or 'O'.
  • +
  • The phone numbers and seat numbers of the passengers are distinct.
  • +
+
\ No newline at end of file diff --git a/2680-maximum-or/2680-maximum-or.cpp b/2680-maximum-or/2680-maximum-or.cpp new file mode 100644 index 00000000..21840949 --- /dev/null +++ b/2680-maximum-or/2680-maximum-or.cpp @@ -0,0 +1,32 @@ + +using ll = long long; +class Solution { +public: + long long maximumOr(vector& nums, int k) { + + int n = nums.size(); + + vector pref(n, 0), suff(n, 0); + + pref[0] = nums[0], suff[n-1] = nums[n-1]; + + for(int i = 1; i < n; ++i) + pref[i] = pref[i-1] | nums[i]; + + for(int i = n-2; i >= 0; --i) + suff[i] = suff[i+1] | nums[i]; + + ll res = 0, mult = 1 << k; + + for(int i = 0; i < n; ++i) + { + ll left = (i == 0 ? 0 : pref[i-1]); + ll right = (i+1 == n ? 0 : suff[i+1]); + + ll ans = left | (mult*nums[i]) | right; + res = max(res, ans); + } + + return res; + } +}; \ No newline at end of file diff --git a/2680-maximum-or/NOTES.md b/2680-maximum-or/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2680-maximum-or/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2680-maximum-or/README.md b/2680-maximum-or/README.md new file mode 100644 index 00000000..1d8976a3 --- /dev/null +++ b/2680-maximum-or/README.md @@ -0,0 +1,30 @@ +

2680. Maximum OR

Medium


You are given a 0-indexed integer array nums of length n and an integer k. In an operation, you can choose an element and multiply it by 2.

+ +

Return the maximum possible value of nums[0] | nums[1] | ... | nums[n - 1] that can be obtained after applying the operation on nums at most k times.

+ +

Note that a | b denotes the bitwise or between two integers a and b.

+ +

 

+

Example 1:

+ +
Input: nums = [12,9], k = 1
+Output: 30
+Explanation: If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
+
+ +

Example 2:

+ +
Input: nums = [8,1,2], k = 2
+Output: 35
+Explanation: If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= k <= 15
  • +
+
\ No newline at end of file diff --git a/2706-buy-two-chocolates/2706-buy-two-chocolates.cpp b/2706-buy-two-chocolates/2706-buy-two-chocolates.cpp new file mode 100644 index 00000000..f26c009f --- /dev/null +++ b/2706-buy-two-chocolates/2706-buy-two-chocolates.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + int buyChoco(vector& prices, int money) { + sort(prices.begin(), prices.end()); + int sum = prices[0] + prices[1]; + return (sum > money)?money: (money - sum); + } +}; \ No newline at end of file diff --git a/2706-buy-two-chocolates/NOTES.md b/2706-buy-two-chocolates/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2706-buy-two-chocolates/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2706-buy-two-chocolates/README.md b/2706-buy-two-chocolates/README.md new file mode 100644 index 00000000..c6497e3f --- /dev/null +++ b/2706-buy-two-chocolates/README.md @@ -0,0 +1,30 @@ +

2706. Buy Two Chocolates

Easy


You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.

+ +

You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.

+ +

Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.

+ +

 

+

Example 1:

+ +
Input: prices = [1,2,2], money = 3
+Output: 0
+Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
+
+ +

Example 2:

+ +
Input: prices = [3,2,3], money = 3
+Output: 3
+Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= prices.length <= 50
  • +
  • 1 <= prices[i] <= 100
  • +
  • 1 <= money <= 100
  • +
+
\ No newline at end of file diff --git a/2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp b/2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp new file mode 100644 index 00000000..242430ac --- /dev/null +++ b/2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp @@ -0,0 +1,58 @@ +class Solution { + int getf(vector &f, int x) { + return f[x] == x ? x : (f[x] = getf(f, f[x])); + } + + void merge(vector &f, vector &num, int x, int y) { + x = getf(f, x); + y = getf(f, y); + if (x == y) { + return; + } + if (num[x] < num[y]) { + swap(x, y); + } + f[y] = x; + num[x] += num[y]; + } +public: + bool canTraverseAllPairs(vector& nums) { + const int n = nums.size(); + if (n == 1) { + return true; + } + vector f(n), num(n); + for (int i = 0; i < n; ++i) { + f[i] = i; + num[i] = 1; + } + unordered_map have; + for (int i = 0; i < n; ++i) { + int x = nums[i]; + if (x == 1) { + return false; + } + for (int d = 2; d * d <= x; ++d) { + if (x % d == 0) { + if (have.count(d)) { + merge(f, num, i, have[d]); + } else { + have[d] = i; + } + while (x % d == 0) { + x /= d; + } + } + } + if (x > 1) { + if (have.count(x)) { + merge(f, num, i, have[x]); + } else { + have[x] = i; + } + } + } + return num[getf(f, 0)] == n; + + } +}; \ No newline at end of file diff --git a/2709-greatest-common-divisor-traversal/NOTES.md b/2709-greatest-common-divisor-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2709-greatest-common-divisor-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2709-greatest-common-divisor-traversal/README.md b/2709-greatest-common-divisor-traversal/README.md new file mode 100644 index 00000000..b66d140a --- /dev/null +++ b/2709-greatest-common-divisor-traversal/README.md @@ -0,0 +1,38 @@ +

2709. Greatest Common Divisor Traversal

Hard


You are given a 0-indexed integer array nums, and you are allowed to traverse between its indices. You can traverse between index i and index j, i != j, if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor.

+ +

Your task is to determine if for every pair of indices i and j in nums, where i < j, there exists a sequence of traversals that can take us from i to j.

+ +

Return true if it is possible to traverse between all such pairs of indices, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,6]
+Output: true
+Explanation: In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).
+To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1.
+To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.
+
+ +

Example 2:

+ +
Input: nums = [3,9,5]
+Output: false
+Explanation: No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
+
+ +

Example 3:

+ +
Input: nums = [4,3,12,8]
+Output: true
+Explanation: There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
+
\ No newline at end of file diff --git a/2785-sort-vowels-in-a-string/NOTES.md b/2785-sort-vowels-in-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2785-sort-vowels-in-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2785-sort-vowels-in-a-string/README.md b/2785-sort-vowels-in-a-string/README.md new file mode 100644 index 00000000..e6652348 --- /dev/null +++ b/2785-sort-vowels-in-a-string/README.md @@ -0,0 +1,34 @@ +

2785. Sort Vowels in a String

Medium


Given a 0-indexed string s, permute s to get a new string t such that:

+ +
    +
  • All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
  • +
  • The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
  • +
+ +

Return the resulting string.

+ +

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

+ +

 

+

Example 1:

+ +
Input: s = "lEetcOde"
+Output: "lEOtcede"
+Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
+
+ +

Example 2:

+ +
Input: s = "lYmpH"
+Output: "lYmpH"
+Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists only of letters of the English alphabet in uppercase and lowercase.
  • +
+
\ No newline at end of file diff --git a/2812-find-the-safest-path-in-a-grid/NOTES.md b/2812-find-the-safest-path-in-a-grid/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2812-find-the-safest-path-in-a-grid/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp b/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp new file mode 100644 index 00000000..d5ac5ea5 --- /dev/null +++ b/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + int add(ListNode* head) + { + if(!head) + return 0; + + int sum = (head->val * 2) + add(head->next); + + head->val = sum % 10; + + return sum / 10; + } + + ListNode* doubleIt(ListNode* head) { + + int carry = add(head); + + if(carry) + head = new ListNode(carry, head); + + return head; + + } +}; \ No newline at end of file diff --git a/2816-double-a-number-represented-as-a-linked-list/NOTES.md b/2816-double-a-number-represented-as-a-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2816-double-a-number-represented-as-a-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2816-double-a-number-represented-as-a-linked-list/README.md b/2816-double-a-number-represented-as-a-linked-list/README.md new file mode 100644 index 00000000..57ca97b0 --- /dev/null +++ b/2816-double-a-number-represented-as-a-linked-list/README.md @@ -0,0 +1,28 @@ +

2816. Double a Number Represented as a Linked List

Medium


You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.

+ +

Return the head of the linked list after doubling it.

+ +

 

+

Example 1:

+ +
Input: head = [1,8,9]
+Output: [3,7,8]
+Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
+
+ +

Example 2:

+ +
Input: head = [9,9,9]
+Output: [1,9,9,8]
+Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. 
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 104]
  • +
  • 0 <= Node.val <= 9
  • +
  • The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself.
  • +
+
\ No newline at end of file diff --git a/2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp b/2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp new file mode 100644 index 00000000..e87e582f --- /dev/null +++ b/2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { + + int xDiff = abs(sx - fx); + int yDiff = abs(sy - fy); + + int diagonalDist = min(xDiff, yDiff); + + if(sx == fx and sy == fy and t == 1) + return false; + + int minDist = diagonalDist + abs(xDiff - yDiff); + + return minDist <= t; + } +}; \ No newline at end of file diff --git a/2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md b/2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md b/2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md new file mode 100644 index 00000000..b806424d --- /dev/null +++ b/2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md @@ -0,0 +1,31 @@ +

2849. Determine if a Cell Is Reachable at a Given Time

Medium


You are given four integers sx, sy, fx, fy, and a non-negative integer t.

+ +

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

+ +

Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.

+ +

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

+ +

 

+

Example 1:

+ +
Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
+Output: true
+Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. 
+
+ +

Example 2:

+ +
Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
+Output: false
+Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= sx, sy, fx, fy <= 109
  • +
  • 0 <= t <= 109
  • +
+
\ No newline at end of file diff --git a/2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp b/2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp new file mode 100644 index 00000000..949cc660 --- /dev/null +++ b/2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string maximumOddBinaryNumber(string s) { + + int n = s.size(); + int start = 0, end = n-1; + + while(start <= end) + { + if(s[start] == '1') + ++start; + else if(s[end] == '1') + { + swap(s[start], s[end]); + --end; + ++start; + } + else + --end; + } + swap(s[start-1], s[n-1]); + + return s; + + } +}; \ No newline at end of file diff --git a/2864-maximum-odd-binary-number/NOTES.md b/2864-maximum-odd-binary-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2864-maximum-odd-binary-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2864-maximum-odd-binary-number/README.md b/2864-maximum-odd-binary-number/README.md new file mode 100644 index 00000000..598ac213 --- /dev/null +++ b/2864-maximum-odd-binary-number/README.md @@ -0,0 +1,32 @@ +

2864. Maximum Odd Binary Number

Easy


You are given a binary string s that contains at least one '1'.

+ +

You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.

+ +

Return a string representing the maximum odd binary number that can be created from the given combination.

+ +

Note that the resulting string can have leading zeros.

+ +

 

+

Example 1:

+ +
Input: s = "010"
+Output: "001"
+Explanation: Because there is just one '1', it must be in the last position. So the answer is "001".
+
+ +

Example 2:

+ +
Input: s = "0101"
+Output: "1001"
+Explanation: One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s consists only of '0' and '1'.
  • +
  • s contains at least one '1'.
  • +
+
\ No newline at end of file diff --git a/2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp b/2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp new file mode 100644 index 00000000..f86d744f --- /dev/null +++ b/2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minOperations(vector& nums) { + + map mp; + + for(auto& itr : nums) + ++mp[itr]; + + int ans = 0; + + for(auto&[_, e] : mp) + { + if(e == 1) + return -1; + + if(e % 3 == 0) + ans += e/3; + else{ + ans += e/3; + ++ans; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/2870-minimum-number-of-operations-to-make-array-empty/README.md b/2870-minimum-number-of-operations-to-make-array-empty/README.md new file mode 100644 index 00000000..e32a8716 --- /dev/null +++ b/2870-minimum-number-of-operations-to-make-array-empty/README.md @@ -0,0 +1,39 @@ +

2870. Minimum Number of Operations to Make Array Empty

Medium


You are given a 0-indexed array nums consisting of positive integers.

+ +

There are two types of operations that you can apply on the array any number of times:

+ +
    +
  • Choose two elements with equal values and delete them from the array.
  • +
  • Choose three elements with equal values and delete them from the array.
  • +
+ +

Return the minimum number of operations required to make the array empty, or -1 if it is not possible.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,3,2,2,4,2,3,4]
+Output: 4
+Explanation: We can apply the following operations to make the array empty:
+- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
+- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
+- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
+- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
+It can be shown that we cannot make the array empty in less than 4 operations.
+
+ +

Example 2:

+ +
Input: nums = [2,1,2,2,3,3]
+Output: -1
+Explanation: It is impossible to empty the array.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
+
\ No newline at end of file diff --git a/2919-minimum-increment-operations-to-make-array-beautiful/README.md b/2919-minimum-increment-operations-to-make-array-beautiful/README.md new file mode 100644 index 00000000..ea73772b --- /dev/null +++ b/2919-minimum-increment-operations-to-make-array-beautiful/README.md @@ -0,0 +1,60 @@ +

2919. Minimum Increment Operations to Make Array Beautiful

Medium


You are given a 0-indexed integer array nums having length n, and an integer k.

+ +

You can perform the following increment operation any number of times (including zero):

+ +
    +
  • Choose an index i in the range [0, n - 1], and increase nums[i] by 1.
  • +
+ +

An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.

+ +

Return an integer denoting the minimum number of increment operations needed to make nums beautiful.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,0,0,2], k = 4
+Output: 3
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
+The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
+In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
+Hence, the answer is 3.
+
+ +

Example 2:

+ +
Input: nums = [0,1,3,3], k = 5
+Output: 2
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
+The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
+In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
+Hence, the answer is 2.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2], k = 1
+Output: 0
+Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
+The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
+Hence, the answer is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n == nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= k <= 109
  • +
+
\ No newline at end of file diff --git a/2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp b/2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp new file mode 100644 index 00000000..53c005cb --- /dev/null +++ b/2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp @@ -0,0 +1,42 @@ +#define ll long long + +class Solution { +public: + long long beautifulSubstrings(string s, int k) { + + int n = s.size(); + + function isVowel = [&](char ch) + { + return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'); + }; + + unordered_map > mp; + + ll ans = 0; + + ++mp[0][0]; + + ll pref = 0, vow = 0; + + for(int i = 0; i < n; ++i) + { + if(isVowel(s[i])) + ++pref, ++vow; + else + --pref; + + for(auto& [f, cnt] : mp[pref]) + { + ll curr = (vow%k) - f; + if((curr * curr) % k == 0) + ans += cnt; + } + + ++mp[pref][vow%k]; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/2949-count-beautiful-substrings-ii/NOTES.md b/2949-count-beautiful-substrings-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2949-count-beautiful-substrings-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp b/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp new file mode 100644 index 00000000..46200095 --- /dev/null +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int maxSubarrayLength(vector& nums, int k) { + + int i = 0, j = 0, n = nums.size(); + + int ans = 0; + + map mp; + + while(j < n) + { + ++mp[nums[j]]; + + while(mp[nums[j]] > k) + { + --mp[nums[i]]; + if(mp[nums[i]] == 0) + mp.erase(nums[i]); + ++i; + } + + ans = max(ans, j-i+1); + ++j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md b/2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/README.md b/2958-length-of-longest-subarray-with-at-most-k-frequency/README.md new file mode 100644 index 00000000..768756e8 --- /dev/null +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/README.md @@ -0,0 +1,44 @@ +

2958. Length of Longest Subarray With at Most K Frequency

Medium


You are given an integer array nums and an integer k.

+ +

The frequency of an element x is the number of times it occurs in an array.

+ +

An array is called good if the frequency of each element in this array is less than or equal to k.

+ +

Return the length of the longest good subarray of nums.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1,2,3,1,2], k = 2
+Output: 6
+Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
+It can be shown that there are no good subarrays with length more than 6.
+
+ +

Example 2:

+ +
Input: nums = [1,2,1,2,1,2,1,2], k = 1
+Output: 2
+Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
+It can be shown that there are no good subarrays with length more than 2.
+
+ +

Example 3:

+ +
Input: nums = [5,5,5,5,5,5,5], k = 4
+Output: 4
+Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
+It can be shown that there are no good subarrays with length more than 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= k <= nums.length
  • +
+
\ No newline at end of file diff --git a/2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp b/2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp new file mode 100644 index 00000000..f4926b3b --- /dev/null +++ b/2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long countSubarrays(vector& nums, int k) { + + int maxElement = *max_element(nums.begin(), nums.end()); + + int i = 0, j = 0, n = nums.size(); + + long long cnt = 0, ans = 0; + + while(j < n) + { + if(nums[j] == maxElement) + ++cnt; + + while(cnt >= k) + { + ans += (n-j); + if(nums[i] == maxElement) + --cnt; + ++i; + } + ++j; + } + + return ans; + } +}; \ No newline at end of file diff --git a/2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md b/2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md b/2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md new file mode 100644 index 00000000..499c8675 --- /dev/null +++ b/2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md @@ -0,0 +1,30 @@ +

2962. Count Subarrays Where Max Element Appears at Least K Times

Medium


You are given an integer array nums and a positive integer k.

+ +

Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.

+ +

A subarray is a contiguous sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,2,3,3], k = 2
+Output: 6
+Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
+
+ +

Example 2:

+ +
Input: nums = [1,4,2,1], k = 3
+Output: 0
+Explanation: No subarray contains the element 4 at least 3 times.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file diff --git a/2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp b/2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp new file mode 100644 index 00000000..70e9c842 --- /dev/null +++ b/2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp @@ -0,0 +1,52 @@ +#define ll long long + +class Solution { +public: + + const int mod = 1e9 + 7; + + ll expo(ll x, ll y, ll mod) + { + ll res = 1; + + while(y > 0) + { + if(y & 1) + res = (res * x) % mod; + x = (x * x) % mod; + y >>= 1; + } + + return res; + } + + int numberOfGoodPartitions(vector& nums) { + + int n = nums.size(); + + int maxGotSoFar = 0; + + unordered_map mp; + + for(int i = 0; i < n; ++i) + { + mp[nums[i]] = i; + } + + int nonOverLappingPartitions = 0; + + for(int i = 0; i < n; ++i) + { + maxGotSoFar = max(maxGotSoFar, mp[nums[i]]); + + if(mp[nums[i]] == i and maxGotSoFar == i) + { + ++nonOverLappingPartitions; + } + + } + + return expo(2, nonOverLappingPartitions - 1, mod); + + } +}; \ No newline at end of file diff --git a/2963-count-the-number-of-good-partitions/NOTES.md b/2963-count-the-number-of-good-partitions/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2963-count-the-number-of-good-partitions/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2963-count-the-number-of-good-partitions/README.md b/2963-count-the-number-of-good-partitions/README.md new file mode 100644 index 00000000..057c9ef1 --- /dev/null +++ b/2963-count-the-number-of-good-partitions/README.md @@ -0,0 +1,38 @@ +

2963. Count the Number of Good Partitions

Hard


You are given a 0-indexed array nums consisting of positive integers.

+ +

A partition of an array into one or more contiguous subarrays is called good if no two subarrays contain the same number.

+ +

Return the total number of good partitions of nums.

+ +

Since the answer may be large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: 8
+Explanation: The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1]
+Output: 1
+Explanation: The only possible good partition is: ([1,1,1,1]).
+
+ +

Example 3:

+ +
Input: nums = [1,2,1,3]
+Output: 2
+Explanation: The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp b/2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp new file mode 100644 index 00000000..0353e6b5 --- /dev/null +++ b/2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + vector> divideArray(vector& nums, int k) { + + int n = nums.size(); + + if(n < 3) + return {}; + + vector> output; + + sort(nums.begin(), nums.end()); + + int part = n/3; + + for(int i = 0; i < n-2; i += 3) + { + vector curr; + + if(nums[i+1] - nums[i] <= k and nums[i+2] - nums[i+1] <= k and nums[i+2] - nums[i] <= k) + { + curr.push_back(nums[i]); + curr.push_back(nums[i+1]); + curr.push_back(nums[i+2]); + } + + if(!curr.empty()) + output.push_back(curr); + } + + if((int)output.size() != part) + return {}; + return output; + } +}; \ No newline at end of file diff --git a/2966-divide-array-into-arrays-with-max-difference/NOTES.md b/2966-divide-array-into-arrays-with-max-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2966-divide-array-into-arrays-with-max-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2966-divide-array-into-arrays-with-max-difference/README.md b/2966-divide-array-into-arrays-with-max-difference/README.md new file mode 100644 index 00000000..8e0024aa --- /dev/null +++ b/2966-divide-array-into-arrays-with-max-difference/README.md @@ -0,0 +1,39 @@ +

2966. Divide Array Into Arrays With Max Difference

Medium


You are given an integer array nums of size n and a positive integer k.

+ +

Divide the array into one or more arrays of size 3 satisfying the following conditions:

+ +
    +
  • Each element of nums should be in exactly one array.
  • +
  • The difference between any two elements in one array is less than or equal to k.
  • +
+ +

Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
+Output: [[1,1,3],[3,4,5],[7,8,9]]
+Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
+The difference between any two elements in each array is less than or equal to 2.
+Note that the order of elements is not important.
+
+ +

Example 2:

+ +
Input: nums = [1,3,3,2,7,3], k = 3
+Output: []
+Explanation: It is not possible to divide the array satisfying all the conditions.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 105
  • +
  • n is a multiple of 3.
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file diff --git a/2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp b/2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp new file mode 100644 index 00000000..7acb465d --- /dev/null +++ b/2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + long long largestPerimeter(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector pref(n, 0); + + pref[0] = nums[0]; + + for(int i = 1; i < n; ++i) + pref[i] = (pref[i-1] + nums[i]); + + for(int i = n-2; i >= 1; --i) + { + if(pref[i] > (pref[i+1] - pref[i])) + return pref[i+1]; + } + + return -1; + + } +}; \ No newline at end of file diff --git a/2971-find-polygon-with-the-largest-perimeter/NOTES.md b/2971-find-polygon-with-the-largest-perimeter/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2971-find-polygon-with-the-largest-perimeter/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2971-find-polygon-with-the-largest-perimeter/README.md b/2971-find-polygon-with-the-largest-perimeter/README.md new file mode 100644 index 00000000..c7d742d1 --- /dev/null +++ b/2971-find-polygon-with-the-largest-perimeter/README.md @@ -0,0 +1,42 @@ +

2971. Find Polygon With the Largest Perimeter

Medium


You are given an array of positive integers nums of length n.

+ +

A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.

+ +

Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.

+ +

The perimeter of a polygon is the sum of lengths of its sides.

+ +

Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.

+ +

 

+

Example 1:

+ +
Input: nums = [5,5,5]
+Output: 15
+Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
+
+ +

Example 2:

+ +
Input: nums = [1,12,1,2,5,50,3]
+Output: 12
+Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
+We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
+It can be shown that the largest possible perimeter is 12.
+
+ +

Example 3:

+ +
Input: nums = [5,5,50]
+Output: -1
+Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file diff --git a/2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp b/2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp new file mode 100644 index 00000000..31ba790f --- /dev/null +++ b/2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp @@ -0,0 +1,41 @@ +using ll = long long; + +class Solution { +public: + long long minimumCost(string source, string target, vector& original, vector& changed, vector& cost) { + + vector> mat(26, vector(26, INT_MAX)); + + for(int i = 0; i < original.size(); ++i) + { + int a = original[i] - 'a'; + int b = changed[i] - 'a'; + mat[a][b] = min(mat[a][b], (ll)cost[i]); + } + + for(int k = 0; k < 26; ++k) + { + for(int i = 0; i < 26; ++i) + { + for(int j = 0; j < 26; ++j) + { + mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]); + } + } + } + + ll ans = 0; + + for(int i = 0; i < source.size(); ++i) + { + if(source[i] != target[i]) + { + ll val = mat[source[i]-'a'][target[i]-'a']; + if(val >= INT_MAX) return -1; + ans += val; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/2976-minimum-cost-to-convert-string-i/README.md b/2976-minimum-cost-to-convert-string-i/README.md new file mode 100644 index 00000000..cc8d73c0 --- /dev/null +++ b/2976-minimum-cost-to-convert-string-i/README.md @@ -0,0 +1,48 @@ +

2976. Minimum Cost to Convert String I

Medium


You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].

+ +

You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.

+ +

Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.

+ +

Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].

+ +

 

+

Example 1:

+ +
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
+Output: 28
+Explanation: To convert the string "abcd" to string "acbe":
+- Change value at index 1 from 'b' to 'c' at a cost of 5.
+- Change value at index 2 from 'c' to 'e' at a cost of 1.
+- Change value at index 2 from 'e' to 'b' at a cost of 2.
+- Change value at index 3 from 'd' to 'e' at a cost of 20.
+The total cost incurred is 5 + 1 + 2 + 20 = 28.
+It can be shown that this is the minimum possible cost.
+
+ +

Example 2:

+ +
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
+Output: 12
+Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
+
+ +

Example 3:

+ +
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
+Output: -1
+Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= source.length == target.length <= 105
  • +
  • source, target consist of lowercase English letters.
  • +
  • 1 <= cost.length == original.length == changed.length <= 2000
  • +
  • original[i], changed[i] are lowercase English letters.
  • +
  • 1 <= cost[i] <= 106
  • +
  • original[i] != changed[i]
  • +
+
\ No newline at end of file diff --git a/2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp b/2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp new file mode 100644 index 00000000..aa1bf6d7 --- /dev/null +++ b/2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp @@ -0,0 +1,81 @@ +class Solution { +public: + int maximumLength(string str) { + + unordered_map mp, mp2, mp3; + + map, int> mp4; + + vector freq(26, 0); + + int n = str.size(); + + int ans = -1; + + for(int i = 0; i < n; ++i) + { + ++mp[str[i]]; + if(mp[str[i]] >= 3) + ans = 1; + } + + if(ans == -1) + return ans; + + int cnt = 1; + mp[str[0]] = 1; + for(int i = 1; i < n; ++i) + { + if(str[i] == str[i-1]) + { + ++cnt; + // int al = mp[str[i]]; + mp2[str[i]] = max(mp2[str[i]], cnt); + int have = mp2[str[i]]; + if(cnt < have) + { + if(cnt == have-1) + { + mp3[str[i]] = max(mp3[str[i]], cnt); + } + } + + ++mp4[{str[i], cnt}]; + if(mp4[{str[i], cnt}] >= 3) + freq[str[i]-'a'] = max(freq[str[i]-'a'],cnt); + } + else + { + cnt = 1; + mp2[str[i]] = max(mp2[str[i]], cnt); + } + } + for(auto& [f, e] : mp2) + { + // cout<= 3) + { + // cout<= 3) + // { + // ans = max(ans, itr.first.second); + // } + // } + + for(auto& itr : freq) + ans = max(ans, itr); + + return ans; + } +}; \ No newline at end of file diff --git a/2982-find-longest-special-substring-that-occurs-thrice-ii/README.md b/2982-find-longest-special-substring-that-occurs-thrice-ii/README.md new file mode 100644 index 00000000..2a1c8241 --- /dev/null +++ b/2982-find-longest-special-substring-that-occurs-thrice-ii/README.md @@ -0,0 +1,40 @@ +

2982. Find Longest Special Substring That Occurs Thrice II

Medium


You are given a string s that consists of lowercase English letters.

+ +

A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.

+ +

Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.

+ +

A substring is a contiguous non-empty sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "aaaa"
+Output: 2
+Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
+It can be shown that the maximum length achievable is 2.
+
+ +

Example 2:

+ +
Input: s = "abcdef"
+Output: -1
+Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
+
+ +

Example 3:

+ +
Input: s = "abcaba"
+Output: 1
+Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
+It can be shown that the maximum length achievable is 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 5 * 105
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp new file mode 100644 index 00000000..2c31aa57 --- /dev/null +++ b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int minOperations(vector& nums, int k) { + + int n = nums.size(); + + vector bits(32, 0); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < 32; ++j) + { + if(nums[i] & (1 << j)) + ++bits[j]; + } + } + + int op = 0; + + for(int i = 0; i < 32; ++i) + { + if(k & (1 << i)) + { + if(bits[i] % 2 == 0) + ++op; + } + else + { + if(bits[i] & 1) + ++op; + } + } + + return op; + + } +}; \ No newline at end of file diff --git a/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md new file mode 100644 index 00000000..8da44784 --- /dev/null +++ b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md @@ -0,0 +1,40 @@ +

2997. Minimum Number of Operations to Make Array XOR Equal to K

Medium


You are given a 0-indexed integer array nums and a positive integer k.

+ +

You can apply the following operation on the array any number of times:

+ +
    +
  • Choose any element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.
  • +
+ +

Return the minimum number of operations required to make the bitwise XOR of all elements of the final array equal to k.

+ +

Note that you can flip leading zero bits in the binary representation of elements. For example, for the number (101)2 you can flip the fourth bit and obtain (1101)2.

+ +

 

+

Example 1:

+ +
Input: nums = [2,1,3,4], k = 1
+Output: 2
+Explanation: We can do the following operations:
+- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
+- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
+The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
+It can be shown that we cannot make the XOR equal to k in less than 2 operations.
+
+ +

Example 2:

+ +
Input: nums = [2,0,2,0], k = 0
+Output: 0
+Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 106
  • +
  • 0 <= k <= 106
  • +
+
\ No newline at end of file diff --git a/3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp b/3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp new file mode 100644 index 00000000..9b517485 --- /dev/null +++ b/3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int maxFrequencyElements(vector& nums) { + + int n = nums.size(); + + vector freq(101, 0); + + int sameFrequencyNum = 0, maxi = 0; + + for(int i = 0; i < n; ++i) + { + ++freq[nums[i]]; + maxi = max(maxi, freq[nums[i]]); + } + + for(int i = 0; i < 101; ++i) + { + if(freq[i] == maxi) + sameFrequencyNum += freq[i]; + } + + return sameFrequencyNum; + } +}; \ No newline at end of file diff --git a/3005-count-elements-with-maximum-frequency/README.md b/3005-count-elements-with-maximum-frequency/README.md new file mode 100644 index 00000000..2b9d7d58 --- /dev/null +++ b/3005-count-elements-with-maximum-frequency/README.md @@ -0,0 +1,31 @@ +

3005. Count Elements With Maximum Frequency

Easy


You are given an array nums consisting of positive integers.

+ +

Return the total frequencies of elements in nums such that those elements all have the maximum frequency.

+ +

The frequency of an element is the number of occurrences of that element in the array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,2,3,1,4]
+Output: 4
+Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
+So the number of elements in the array with maximum frequency is 4.
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,4,5]
+Output: 5
+Explanation: All elements of the array have a frequency of 1 which is the maximum.
+So the number of elements in the array with maximum frequency is 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
+
\ No newline at end of file diff --git a/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp new file mode 100644 index 00000000..da125d45 --- /dev/null +++ b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int maxPalindromesAfterOperations(vector& words) { + + vector length; + map mp; + + for(auto& word : words) + { + for(auto& ch : word) + ++mp[ch]; + length.push_back((int)word.size()/2); + } + + int matching = 0, cnt = 0; + + for(auto&[f, e] : mp) + matching += e/2; + + sort(length.begin(), length.end()); + + for(int i = 0; i < length.size(); ++i) + { + if(matching >= length[i]) + { + matching -= length[i]; + ++cnt; + } + } + + return cnt; + } +}; \ No newline at end of file diff --git a/3035-maximum-palindromes-after-operations/NOTES.md b/3035-maximum-palindromes-after-operations/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3035-maximum-palindromes-after-operations/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3035-maximum-palindromes-after-operations/README.md b/3035-maximum-palindromes-after-operations/README.md new file mode 100644 index 00000000..d4aba112 --- /dev/null +++ b/3035-maximum-palindromes-after-operations/README.md @@ -0,0 +1,51 @@ +

3035. Maximum Palindromes After Operations

Medium


You are given a 0-indexed string array words having length n and containing 0-indexed strings.

+ +

You are allowed to perform the following operation any number of times (including zero):

+ +
    +
  • Choose integers i, j, x, and y such that 0 <= i, j < n, 0 <= x < words[i].length, 0 <= y < words[j].length, and swap the characters words[i][x] and words[j][y].
  • +
+ +

Return an integer denoting the maximum number of palindromes words can contain, after performing some operations.

+ +

Note: i and j may be equal during an operation.

+ +

 

+

Example 1:

+ +
Input: words = ["abbb","ba","aa"]
+Output: 3
+Explanation: In this example, one way to get the maximum number of palindromes is:
+Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].
+All strings in words are now palindromes.
+Hence, the maximum number of palindromes achievable is 3.
+ +

Example 2:

+ +
Input: words = ["abc","ab"]
+Output: 2
+Explanation: In this example, one way to get the maximum number of palindromes is: 
+Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].
+Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].
+Both strings are now palindromes.
+Hence, the maximum number of palindromes achievable is 2.
+
+ +

Example 3:

+ +
Input: words = ["cd","ef","a"]
+Output: 1
+Explanation: In this example, there is no need to perform any operation.
+There is one palindrome in words "a".
+It can be shown that it is not possible to get more than one palindrome after any number of operations.
+Hence, the answer is 1.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists only of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp new file mode 100644 index 00000000..704e11a4 --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + long long maximumValueSum(vector& nums, int k, vector>& edges) { + + int mini = 1e9; + long long cnt = 0, ans = 0; + + for(auto& ele : nums) + { + int xorVal = ele ^ k; + + if(xorVal > ele) + { + ++cnt; + ans += xorVal; + } + else + { + ans += ele; + } + + mini = min(mini, abs(xorVal - ele)); + } + + if(cnt & 1) + { + return ans - mini; + } + return ans; + } +}; \ No newline at end of file diff --git a/3068-find-the-maximum-sum-of-node-values/NOTES.md b/3068-find-the-maximum-sum-of-node-values/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3068-find-the-maximum-sum-of-node-values/README.md b/3068-find-the-maximum-sum-of-node-values/README.md new file mode 100644 index 00000000..f988eb9a --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/README.md @@ -0,0 +1,57 @@ +

3068. Find the Maximum Sum of Node Values

Hard


There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 0-indexed 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. You are also given a positive integer k, and a 0-indexed array of non-negative integers nums of length n, where nums[i] represents the value of the node numbered i.

+ +

Alice wants the sum of values of tree nodes to be maximum, for which Alice can perform the following operation any number of times (including zero) on the tree:

+ +
    +
  • Choose any edge [u, v] connecting the nodes u and v, and update their values as follows: + +
      +
    • nums[u] = nums[u] XOR k
    • +
    • nums[v] = nums[v] XOR k
    • +
    +
  • +
+ +

Return the maximum possible sum of the values Alice can achieve by performing the operation any number of times.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
+Output: 6
+Explanation: Alice can achieve the maximum sum of 6 using a single operation:
+- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
+The total sum of values is 2 + 2 + 2 = 6.
+It can be shown that 6 is the maximum achievable sum of values.
+
+ +

Example 2:

+ +
Input: nums = [2,3], k = 7, edges = [[0,1]]
+Output: 9
+Explanation: Alice can achieve the maximum sum of 9 using a single operation:
+- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
+The total sum of values is 5 + 4 = 9.
+It can be shown that 9 is the maximum achievable sum of values.
+
+ +

Example 3:

+ +
Input: nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
+Output: 42
+Explanation: The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n == nums.length <= 2 * 104
  • +
  • 1 <= k <= 109
  • +
  • 0 <= nums[i] <= 109
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= edges[i][0], edges[i][1] <= n - 1
  • +
  • The input is generated such that edges represent a valid tree.
  • +
+
\ No newline at end of file diff --git a/3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp b/3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp new file mode 100644 index 00000000..23cbcd93 --- /dev/null +++ b/3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + long long maximumHappinessSum(vector& happiness, int k) { + + int n = happiness.size(); + + sort(happiness.rbegin(), happiness.rend()); + + int cap = 0; + long long ans = 0; + + for(int i = 0; i < n, i < k; ++i, ++cap) + { + if(happiness[i] - cap > 0) + ans += (happiness[i] - cap); + } + + return ans; + } +}; \ No newline at end of file diff --git a/3075-maximize-happiness-of-selected-children/README.md b/3075-maximize-happiness-of-selected-children/README.md new file mode 100644 index 00000000..16567a73 --- /dev/null +++ b/3075-maximize-happiness-of-selected-children/README.md @@ -0,0 +1,47 @@ +

3075. Maximize Happiness of Selected Children

Medium


You are given an array happiness of length n, and a positive integer k.

+ +

There are n children standing in a queue, where the ith child has happiness value happiness[i]. You want to select k children from these n children in k turns.

+ +

In each turn, when you select a child, the happiness value of all the children that have not been selected till now decreases by 1. Note that the happiness value cannot become negative and gets decremented only if it is positive.

+ +

Return the maximum sum of the happiness values of the selected children you can achieve by selecting k children.

+ +

 

+

Example 1:

+ +
Input: happiness = [1,2,3], k = 2
+Output: 4
+Explanation: We can pick 2 children in the following way:
+- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
+- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
+The sum of the happiness values of the selected children is 3 + 1 = 4.
+
+ +

Example 2:

+ +
Input: happiness = [1,1,1,1], k = 2
+Output: 1
+Explanation: We can pick 2 children in the following way:
+- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
+- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
+The sum of the happiness values of the selected children is 1 + 0 = 1.
+
+ +

Example 3:

+ +
Input: happiness = [2,3,4,5], k = 1
+Output: 5
+Explanation: We can pick 1 child in the following way:
+- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
+The sum of the happiness values of the selected children is 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == happiness.length <= 2 * 105
  • +
  • 1 <= happiness[i] <= 108
  • +
  • 1 <= k <= n
  • +
+
\ No newline at end of file diff --git a/3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp b/3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp new file mode 100644 index 00000000..9e5a32b9 --- /dev/null +++ b/3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp @@ -0,0 +1,131 @@ +class Node{ + public: + Node* child[26] = {nullptr}; + pair p; + bool isWord; + + public: + + Node(){ + p.first = 1e9; + p.second = 1e9; + isWord = false; + } + bool containsKey(char ch) + { + return child[ch-'a'] != nullptr; + } + + void put(char ch, Node* node) + { + child[ch - 'a'] = node; + } + + Node* get(char ch) + { + return child[ch - 'a']; + } + + void setEnd() + { + isWord = true; + } + + void updateMin(int idx, int len) + { + if(len < p.second) + { + p.first = idx; + p.second = len; + } + else if(len == p.second) + p.first = min(p.first, idx); + } + + int getIdx() + { + return p.first; + } +}; + +class Trie{ + private: + Node* root; + + public: + Trie() + { + root = new Node(); + } + + void insert(string& word, int idx) + { + Node* temp = root; + + int n = word.size(); + + for(int i = 0; i < n; ++i) + { + char ch = word[i]; + temp->updateMin(idx, n); + if(!temp->containsKey(ch)) + { + temp->put(ch, new Node()); + + } + temp = temp->get(ch); + } + temp->updateMin(idx, n); + temp->setEnd(); + } + + int search(string& word) + { + Node* temp = root; + int n = word.size(); + int ansIdx = -1; + + for(int i = 0; i < n; ++i) + { + char ch = word[i]; + if(!temp->containsKey(ch)) + { + break; + } + ansIdx= temp->getIdx(); + temp = temp->get(ch); + } + + return temp->getIdx(); + } +}; + +class Solution { +public: + vector stringIndices(vector& wordsContainer, vector& wordsQuery) { + + Trie *trie = new Trie(); + + vector ans; + + int idx = 0, minLength = 1e9;; + for(auto& word : wordsContainer) + { + string rev = word; + reverse(rev.begin(), rev.end()); + minLength = min(minLength, (int)rev.size()); + trie->insert(rev, idx); + ++idx; + } + + for(auto& word : wordsQuery) + { + string rev = word; + reverse(rev.begin(), rev.end()); + int ansIdx = trie->search(rev); + ans.push_back(ansIdx); + } + + return ans; + } +}; \ No newline at end of file diff --git a/3093-longest-common-suffix-queries/README.md b/3093-longest-common-suffix-queries/README.md new file mode 100644 index 00000000..14bd5b40 --- /dev/null +++ b/3093-longest-common-suffix-queries/README.md @@ -0,0 +1,56 @@ +

3093. Longest Common Suffix Queries

Hard


You are given two arrays of strings wordsContainer and wordsQuery.

+ +

For each wordsQuery[i], you need to find a string from wordsContainer that has the longest common suffix with wordsQuery[i]. If there are two or more strings in wordsContainer that share the longest common suffix, find the string that is the smallest in length. If there are two or more such strings that have the same smallest length, find the one that occurred earlier in wordsContainer.

+ +

Return an array of integers ans, where ans[i] is the index of the string in wordsContainer that has the longest common suffix with wordsQuery[i].

+ +

 

+

Example 1:

+ +
+

Input: wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]

+ +

Output: [1,1,1]

+ +

Explanation:

+ +

Let's look at each wordsQuery[i] separately:

+ +
    +
  • For wordsQuery[0] = "cd", strings from wordsContainer that share the longest common suffix "cd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
  • +
  • For wordsQuery[1] = "bcd", strings from wordsContainer that share the longest common suffix "bcd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
  • +
  • For wordsQuery[2] = "xyz", there is no string from wordsContainer that shares a common suffix. Hence the longest common suffix is "", that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
  • +
+
+ +

Example 2:

+ +
+

Input: wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]

+ +

Output: [2,0,2]

+ +

Explanation:

+ +

Let's look at each wordsQuery[i] separately:

+ +
    +
  • For wordsQuery[0] = "gh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
  • +
  • For wordsQuery[1] = "acbfgh", only the string at index 0 shares the longest common suffix "fgh". Hence it is the answer, even though the string at index 2 is shorter.
  • +
  • For wordsQuery[2] = "acbfegh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= wordsContainer.length, wordsQuery.length <= 104
  • +
  • 1 <= wordsContainer[i].length <= 5 * 103
  • +
  • 1 <= wordsQuery[i].length <= 5 * 103
  • +
  • wordsContainer[i] consists only of lowercase English letters.
  • +
  • wordsQuery[i] consists only of lowercase English letters.
  • +
  • Sum of wordsContainer[i].length is at most 5 * 105.
  • +
  • Sum of wordsQuery[i].length is at most 5 * 105.
  • +
+
\ No newline at end of file diff --git a/3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp b/3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp new file mode 100644 index 00000000..193b48fc --- /dev/null +++ b/3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp @@ -0,0 +1,112 @@ +class DSU{ + public: + int N; + vector parent, size; + + DSU(int n) + { + N = n; + parent.resize(N); + size.resize(N, 1); + + for(int i = 0; i < n; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + { + return u; + } + return parent[u] = findParent(parent[u]); + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } +}; + +class Solution { +public: + + vector minimumCost(int n, vector>& edges, vector>& query) { + + vector ans; + + DSU dsu(n+1); + + vector bitAnd(n, -1); + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + if(bitAnd[u] == -1) + bitAnd[u] = wt; + else + bitAnd[u] &= wt; + + if(bitAnd[v] == -1) + bitAnd[v] = wt; + else + bitAnd[v] &= wt; + + if(!dsu.isSame(u, v)) + { + dsu.unionBySize(u, v); + } + } + + map mp; + + for(int i = 0; i < n; ++i) + { + int ultPar = dsu.findParent(i); + if(bitAnd[ultPar] == -1) + continue; + if(mp.find(ultPar) == mp.end()) + mp[ultPar] = (bitAnd[i] & bitAnd[ultPar]); + else + mp[ultPar] &= bitAnd[i]; + } + + for(auto& q : query) + { + int u = q[0]; + int v = q[1]; + + if(u == v) + ans.push_back(0); + else if(dsu.isSame(u, v)) + ans.push_back(mp[dsu.findParent(u)]); + else + ans.push_back(-1); + } + + return ans; + } +}; \ No newline at end of file diff --git a/3108-minimum-cost-walk-in-weighted-graph/README.md b/3108-minimum-cost-walk-in-weighted-graph/README.md new file mode 100644 index 00000000..e48a0a2f --- /dev/null +++ b/3108-minimum-cost-walk-in-weighted-graph/README.md @@ -0,0 +1,54 @@ +

3108. Minimum Cost Walk in Weighted Graph

Hard


There is an undirected weighted graph with n vertices labeled from 0 to n - 1.

+ +

You are given the integer n and an array edges, where edges[i] = [ui, vi, wi] indicates that there is an edge between vertices ui and vi with a weight of wi.

+ +

A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or vertex more than once.

+ +

The cost of a walk starting at node u and ending at node v is defined as the bitwise AND of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk is w0, w1, w2, ..., wk, then the cost is calculated as w0 & w1 & w2 & ... & wk, where & denotes the bitwise AND operator.

+ +

You are also given a 2D array query, where query[i] = [si, ti]. For each query, you need to find the minimum cost of the walk starting at vertex si and ending at vertex ti. If there exists no such walk, the answer is -1.

+ +

Return the array answer, where answer[i] denotes the minimum cost of a walk for query i.

+ +

 

+

Example 1:

+ +
+

Input: n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]

+ +

Output: [1,-1]

+ +

Explanation:

+ +

To achieve the cost of 1 in the first query, we need to move on the following edges: 0->1 (weight 7), 1->2 (weight 1), 2->1 (weight 1), 1->3 (weight 7).

+ +

In the second query, there is no walk between nodes 3 and 4, so the answer is -1.

+ +

Example 2:

+
+ +
+

Input: n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], query = [[1,2]]

+ +

Output: [0]

+ +

Explanation:

+ +

To achieve the cost of 0 in the first query, we need to move on the following edges: 1->2 (weight 1), 2->1 (weight 6), 1->2 (weight 1).

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 0 <= edges.length <= 105
  • +
  • edges[i].length == 3
  • +
  • 0 <= ui, vi <= n - 1
  • +
  • ui != vi
  • +
  • 0 <= wi <= 105
  • +
  • 1 <= query.length <= 105
  • +
  • query[i].length == 2
  • +
  • 0 <= si, ti <= n - 1
  • +
+
\ No newline at end of file diff --git a/3110-score-of-a-string/3110-score-of-a-string.cpp b/3110-score-of-a-string/3110-score-of-a-string.cpp new file mode 100644 index 00000000..cbfc766d --- /dev/null +++ b/3110-score-of-a-string/3110-score-of-a-string.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int scoreOfString(string s) { + + int n = s.size(); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + ans += abs((s[i] - 'a') - (s[i-1] - 'a')); + } + + return ans; + } +}; \ No newline at end of file diff --git a/3110-score-of-a-string/README.md b/3110-score-of-a-string/README.md new file mode 100644 index 00000000..5859fc38 --- /dev/null +++ b/3110-score-of-a-string/README.md @@ -0,0 +1,37 @@ +

3110. Score of a String

Easy


You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.

+ +

Return the score of s.

+ +

 

+

Example 1:

+ +
+

Input: s = "hello"

+ +

Output: 13

+ +

Explanation:

+ +

The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.

+
+ +

Example 2:

+ +
+

Input: s = "zaz"

+ +

Output: 50

+ +

Explanation:

+ +

The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 100
  • +
  • s consists only of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp b/3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp new file mode 100644 index 00000000..dbb896c6 --- /dev/null +++ b/3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int minimumOperationsToMakeKPeriodic(string word, int k) { + + int n = word.size(); + + vector factors; + + for(int i = 0; i < n; ++i) + { + if(i == 0 or i % k == 0) + factors.push_back(i); + } + + map mp; + + int maxi = 0; + + for(auto& idx : factors) + { + string st = word.substr(idx, k); + ++mp[st]; + maxi = max(maxi, mp[st]); + } + + return (int)factors.size() - maxi; + } +}; \ No newline at end of file diff --git a/3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md b/3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3137-minimum-number-of-operations-to-make-word-k-periodic/README.md b/3137-minimum-number-of-operations-to-make-word-k-periodic/README.md new file mode 100644 index 00000000..cc5c44d8 --- /dev/null +++ b/3137-minimum-number-of-operations-to-make-word-k-periodic/README.md @@ -0,0 +1,84 @@ +

3137. Minimum Number of Operations to Make Word K-Periodic

Medium


You are given a string word of size n, and an integer k such that k divides n.

+ +

In one operation, you can pick any two indices i and j, that are divisible by k, then replace the substring of length k starting at i with the substring of length k starting at j. That is, replace the substring word[i..i + k - 1] with the substring word[j..j + k - 1].

+ +

Return the minimum number of operations required to make word k-periodic.

+ +

We say that word is k-periodic if there is some string s of length k such that word can be obtained by concatenating s an arbitrary number of times. For example, if word == “ababab”, then word is 2-periodic for s = "ab".

+ +

 

+

Example 1:

+ +
+

Input: word = "leetcodeleet", k = 4

+ +

Output: 1

+ +

Explanation:

+ +

We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".

+
+ +

Example 2:

+ +
+

Input: word = "leetcoleet", k = 2

+ +

Output: 3

+ +

Explanation:

+ +

We can obtain a 2-periodic string by applying the operations in the table below.

+ + + + + + + + + + + + + + + + + + + + + + + + +
ijword
02etetcoleet
40etetetleet
60etetetetet
+
+ +
+
 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == word.length <= 105
  • +
  • 1 <= k <= word.length
  • +
  • k divides word.length.
  • +
  • word consists only of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp b/3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp new file mode 100644 index 00000000..083fb850 --- /dev/null +++ b/3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + int minAnagramLength(string s) { + + int n = s.size(); + + map mp; + + for(auto&ch : s) + ++mp[ch]; + + int minFreq = INT_MAX; + + for(auto&[_, e] : mp) + minFreq = min(minFreq, e); + + int ans = n; + + for(int i = 1; i <= minFreq; ++i) + { + bool ok = true; + int len = 0; + + for(auto&[_, e] : mp) + { + ok &= (e % i == 0); + len += (e / i); + } + + if(ok) + { + ans = len; + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/3138-minimum-length-of-anagram-concatenation/NOTES.md b/3138-minimum-length-of-anagram-concatenation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3138-minimum-length-of-anagram-concatenation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3138-minimum-length-of-anagram-concatenation/README.md b/3138-minimum-length-of-anagram-concatenation/README.md new file mode 100644 index 00000000..5d4c31a9 --- /dev/null +++ b/3138-minimum-length-of-anagram-concatenation/README.md @@ -0,0 +1,39 @@ +

3138. Minimum Length of Anagram Concatenation

Medium


You are given a string s, which is known to be a concatenation of anagrams of some string t.

+ +

Return the minimum possible length of the string t.

+ +

An anagram is a word or phrase formed by rearranging the letters of a word or phrase, typically using all the original letters exactly once.

+ +

 

+

Example 1:

+ +
+

Input: s = "abba"

+ +

Output: 2

+ +

Explanation:

+ +

One possible string t could be "ba".

+
+ +

Example 2:

+ +
+

Input: s = "cdef"

+ +

Output: 4

+ +

Explanation:

+ +

One possible string t could be "cdef", notice that t can be equal to s.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consist only of lowercase English letters.
  • +
+
\ No newline at end of file diff --git a/3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp b/3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp new file mode 100644 index 00000000..a3da90c1 --- /dev/null +++ b/3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp @@ -0,0 +1,52 @@ +using lli = long long; + +class Solution { +public: + int minCostToEqualizeArray(vector& nums, int cost1, int cost2) { + + int n = nums.size(); + + lli maxi = 0; + lli mini = 1e9; + lli ans = 1e18; + lli sum = 0; + const int mod = 1e9+7; + + for(auto& ele : nums) + { + maxi = max(maxi, 1LL * ele); + mini = min(mini, 1LL * ele); + sum += ele; + } + + for(int curMax = maxi; curMax <= 2 * maxi; ++curMax) + { + lli total = (n * 1LL* curMax) - sum; + lli curr = 0; + if(2 * cost1 <= cost2) + { + curr = (total * cost1); + ans = min(ans, curr); + } + else + { + lli maxDiff = curMax - mini; + lli pair = total - maxDiff; + + if(pair < maxDiff) + { + lli nonPair = maxDiff - pair; + curr = (nonPair * cost1) + (pair * cost2); + ans = min(ans, curr); + continue; + } + + curr = ((total/2) * cost2) + ((total%2) * cost1); + + ans = min(ans, curr); + } + } + + return ans%mod; + } +}; \ No newline at end of file diff --git a/3139-minimum-cost-to-equalize-array/NOTES.md b/3139-minimum-cost-to-equalize-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3139-minimum-cost-to-equalize-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp b/3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp new file mode 100644 index 00000000..9049bed3 --- /dev/null +++ b/3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + + map>> dp; + + int helper(int i, int backStep, int jump, int target) + { + int ans = 0; + + if(dp[i][backStep].find(jump) != dp[i][backStep].end()) + { + return dp[i][backStep][jump]; + } + + if(i == target) + { + ++ans; + } + + if(backStep and i > 0) + { + ans += helper(i-1, 0, jump, target); + } + + if(i + (1LL<3154. Find Number of Ways to Reach the K-th Stair

Hard


You are given a non-negative integer k. There exists a staircase with an infinite number of stairs, with the lowest stair numbered 0.

+ +

Alice has an integer jump, with an initial value of 0. She starts on stair 1 and wants to reach stair k using any number of operations. If she is on stair i, in one operation she can:

+ +
    +
  • Go down to stair i - 1. This operation cannot be used consecutively or on stair 0.
  • +
  • Go up to stair i + 2jump. And then, jump becomes jump + 1.
  • +
+ +

Return the total number of ways Alice can reach stair k.

+ +

Note that it is possible that Alice reaches the stair k, and performs some operations to reach the stair k again.

+ +

 

+

Example 1:

+ +
+

Input: k = 0

+ +

Output: 2

+ +

Explanation:

+ +

The 2 possible ways of reaching stair 0 are:

+ +
    +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    +
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 1.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    +
  • +
+
+ +

Example 2:

+ +
+

Input: k = 1

+ +

Output: 4

+ +

Explanation:

+ +

The 4 possible ways of reaching stair 1 are:

+ +
    +
  • Alice starts at stair 1. Alice is at stair 1.
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 1.
    • +
    +
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 2.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 1.
    • +
    +
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 1.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 21 stairs to reach stair 2.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 1.
    • +
    +
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= k <= 109
  • +
+
\ No newline at end of file diff --git a/3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp b/3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp new file mode 100644 index 00000000..e50652d3 --- /dev/null +++ b/3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + long long numberOfPairs(vector& nums1, vector& nums2, int k) { + + map fact; + + for(auto& ele : nums1) + { + int num = ele; + + for(int i = 1; i * i <= num; ++i) + { + if(num % i == 0) + { + ++fact[i]; + if(num/i != i) + ++fact[num/i]; + } + } + } + + long long ans = 0; + + for(auto& ele : nums2) + { + long long num = ele * k; + + ans += fact[num]; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/3164-find-the-number-of-good-pairs-ii/README.md b/3164-find-the-number-of-good-pairs-ii/README.md new file mode 100644 index 00000000..b81081a4 --- /dev/null +++ b/3164-find-the-number-of-good-pairs-ii/README.md @@ -0,0 +1,38 @@ +

3164. Find the Number of Good Pairs II

Medium


You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.

+ +

A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1).

+ +

Return the total number of good pairs.

+ +

 

+

Example 1:

+ +
+

Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1

+ +

Output: 5

+ +

Explanation:

+The 5 good pairs are (0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).
+ +

Example 2:

+ +
+

Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3

+ +

Output: 2

+ +

Explanation:

+ +

The 2 good pairs are (3, 0) and (3, 1).

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n, m <= 105
  • +
  • 1 <= nums1[i], nums2[j] <= 106
  • +
  • 1 <= k <= 103
  • +
+
\ No newline at end of file diff --git a/3169-count-days-without-meetings/3169-count-days-without-meetings.cpp b/3169-count-days-without-meetings/3169-count-days-without-meetings.cpp new file mode 100644 index 00000000..e82c9dde --- /dev/null +++ b/3169-count-days-without-meetings/3169-count-days-without-meetings.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + int countDays(int days, vector>& meetings) { + + int n = meetings.size(); + + vector> events; + + for(auto& e : meetings) + { + events.push_back({e[0], +1}); + events.push_back({e[1], -1}); + } + + events.push_back({0, 0}); + events.push_back({days+1, 0}); + + sort(events.begin(), events.end()); + + int ans = 0, currMeetings = 0; + + for(int i = 0; i < events.size()-1; ++i) + { + currMeetings += events[i].second; + + if(currMeetings == 0) + { + ans += max(0, events[i+1].first - events[i].first - 1); + } + + } + + return ans; + + } +}; \ No newline at end of file diff --git a/3169-count-days-without-meetings/NOTES.md b/3169-count-days-without-meetings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3169-count-days-without-meetings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3169-count-days-without-meetings/README.md b/3169-count-days-without-meetings/README.md new file mode 100644 index 00000000..ecd8d5e6 --- /dev/null +++ b/3169-count-days-without-meetings/README.md @@ -0,0 +1,53 @@ +

3169. Count Days Without Meetings

Medium


You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).

+ +

Return the count of days when the employee is available for work but no meetings are scheduled.

+ +

Note: The meetings may overlap.

+ +

 

+

Example 1:

+ +
+

Input: days = 10, meetings = [[5,7],[1,3],[9,10]]

+ +

Output: 2

+ +

Explanation:

+ +

There is no meeting scheduled on the 4th and 8th days.

+
+ +

Example 2:

+ +
+

Input: days = 5, meetings = [[2,4],[1,3]]

+ +

Output: 1

+ +

Explanation:

+ +

There is no meeting scheduled on the 5th day.

+
+ +

Example 3:

+ +
+

Input: days = 6, meetings = [[1,6]]

+ +

Output: 0

+ +

Explanation:

+ +

Meetings are scheduled for all working days.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= days <= 109
  • +
  • 1 <= meetings.length <= 105
  • +
  • meetings[i].length == 2
  • +
  • 1 <= meetings[i][0] <= meetings[i][1] <= days
  • +
+
\ No newline at end of file diff --git a/3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp b/3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp new file mode 100644 index 00000000..1bc8d5f5 --- /dev/null +++ b/3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int minimumDifference(vector& nums, int k) { + + set curSet; + + int ans = INT_MAX; + + for(auto& ele : nums) + { + set newSet; + + for(auto& vals : curSet) + { + newSet.insert(vals & ele); + } + + newSet.insert(ele); + + for(auto& vals : newSet) + { + ans = min(ans, abs(k - vals)); + } + + curSet = newSet; + } + + return ans; + + } +}; \ No newline at end of file diff --git a/3171-find-subarray-with-bitwise-and-closest-to-k/README.md b/3171-find-subarray-with-bitwise-and-closest-to-k/README.md new file mode 100644 index 00000000..8e2e15f6 --- /dev/null +++ b/3171-find-subarray-with-bitwise-and-closest-to-k/README.md @@ -0,0 +1,52 @@ +

3171. Find Subarray With Bitwise AND Closest to K

Hard


You are given an array nums and an integer k. You need to find a subarray of nums such that the absolute difference between k and the bitwise AND of the subarray elements is as small as possible. In other words, select a subarray nums[l..r] such that |k - (nums[l] AND nums[l + 1] ... AND nums[r])| is minimum.

+ +

Return the minimum possible value of the absolute difference.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
+

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

+ +

Output: 1

+ +

Explanation:

+ +

The subarray nums[2..3] has AND value 4, which gives the minimum absolute difference |3 - 4| = 1.

+
+ +

Example 2:

+ +
+

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

+ +

Output: 0

+ +

Explanation:

+ +

The subarray nums[1..1] has AND value 2, which gives the minimum absolute difference |2 - 2| = 0.

+
+ +

Example 3:

+ +
+

Input: nums = [1], k = 10

+ +

Output: 9

+ +

Explanation:

+ +

There is a single subarray with AND value 1, which gives the minimum absolute difference |10 - 1| = 9.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= k <= 109
  • +
+
\ No newline at end of file diff --git a/3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp b/3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp new file mode 100644 index 00000000..a7c22a40 --- /dev/null +++ b/3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp @@ -0,0 +1,34 @@ +using ll = long long; + +class Solution { +public: + + long long maximumTotalDamage(vector& power) { + + map mp; + + for(auto& ele : power) + ++mp[ele]; + + vector vec; + + for(auto& [f, _] : mp) + vec.push_back(f); + + vector dp(vec.size()+1, 0); + + for(int i = vec.size()-1; i >= 0; --i) + { + ll notTake = dp[i+1]; + + int ubIdx = upper_bound(vec.begin(), vec.end(), vec[i] + 2) - vec.begin(); + + ll take = vec[i] * 1LL * mp[vec[i]] + dp[ubIdx]; + + dp[i] = max(take, notTake); + } + + return dp[0]; + + } +}; \ No newline at end of file diff --git a/3186-maximum-total-damage-with-spell-casting/NOTES.md b/3186-maximum-total-damage-with-spell-casting/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3186-maximum-total-damage-with-spell-casting/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3186-maximum-total-damage-with-spell-casting/README.md b/3186-maximum-total-damage-with-spell-casting/README.md new file mode 100644 index 00000000..3408ac47 --- /dev/null +++ b/3186-maximum-total-damage-with-spell-casting/README.md @@ -0,0 +1,43 @@ +

3186. Maximum Total Damage With Spell Casting

Medium


A magician has various spells.

+ +

You are given an array power, where each element represents the damage of a spell. Multiple spells can have the same damage value.

+ +

It is a known fact that if a magician decides to cast a spell with a damage of power[i], they cannot cast any spell with a damage of power[i] - 2, power[i] - 1, power[i] + 1, or power[i] + 2.

+ +

Each spell can be cast only once.

+ +

Return the maximum possible total damage that a magician can cast.

+ +

 

+

Example 1:

+ +
+

Input: power = [1,1,3,4]

+ +

Output: 6

+ +

Explanation:

+ +

The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.

+
+ +

Example 2:

+ +
+

Input: power = [7,1,6,6]

+ +

Output: 13

+ +

Explanation:

+ +

The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= power.length <= 105
  • +
  • 1 <= power[i] <= 109
  • +
+
\ No newline at end of file diff --git a/3187-peaks-in-array/3187-peaks-in-array.cpp b/3187-peaks-in-array/3187-peaks-in-array.cpp new file mode 100644 index 00000000..f3beefc1 --- /dev/null +++ b/3187-peaks-in-array/3187-peaks-in-array.cpp @@ -0,0 +1,133 @@ +template +class SEG +{ +public: + vector tree, lazy, arr; + T N; + SEG() {} + SEG(vector v) + { + N = v.size(); + arr.resize(N); + for (int i = 0; i < N; ++i) + arr[i] = v[i]; + tree.resize(4 * N + 5); + lazy.resize(4 * N + 5, 0); + + buildTree(0, 0, N - 1); + } + + bool isPeak(int idx) + { + if(idx <= 0 or idx >= N-1) + return false; + + return (arr[idx] > arr[idx-1] and arr[idx] > arr[idx+1]); + } + + void buildTree(int idx, int low, int high) + { + if (low == high) + { + tree[idx] = (isPeak(low) ? 1 : 0); + return; + } + + int mid = (low + high) >> 1; + buildTree(2 * idx + 1, low, mid); + buildTree(2 * idx + 2, mid + 1, high); + + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; + + return; + } + + + int queryTree(int idx, int low, int high, int l, int r) + { + // no overlap + // l r low high or low high l r + if (r < low or l > high) + return 0; + + // complete overlap + // [l low high r] + if (low >= l and high <= r) + return tree[idx]; + + // partial overlap + int mid = (low + high) / 2; + int left = queryTree(2 * idx + 1, low, mid, l, r); + int right = queryTree(2 * idx + 2, mid + 1, high, l, r); + + return left + right; + } + +public: + void updateTree(int idx, int low, int high, int ind, int val) + { + if (low == high) + { + tree[idx] = (isPeak(low) ? 1 : 0); + return; + } + int mid = (low + high) >> 1; + if (ind <= mid) + updateTree(2 * idx + 1, low, mid, ind, val); + else + updateTree(2 * idx + 2, mid + 1, high, ind, val); + + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; + } + + int query(int l, int r) + { + return queryTree(0, 0, N - 1, l, r); + } + + void update(int ind, int val) + { + updateTree(0, 0, N - 1, ind, val); + } + + void build() + { + buildTree(0, 0, N - 1); + } +}; + + +class Solution { +public: + vector countOfPeaks(vector& nums, vector>& queries) { + + vector ans; + + int n = nums.size(); + + SEG seg(nums); + + for(auto& q : queries) + { + int type = q[0]; + int x = q[1]; + int y = q[2]; + + if(type == 1) + { + ans.push_back(seg.query(x+1, y-1)); + } + else + { + seg.arr[x] = y; + seg.update(x, y); + if(x - 1 > 0) + seg.update(x-1, nums[x-1]); + if(x + 1 < n-1) + seg.update(x+1, nums[x+1]); + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/3187-peaks-in-array/README.md b/3187-peaks-in-array/README.md new file mode 100644 index 00000000..822bb1d9 --- /dev/null +++ b/3187-peaks-in-array/README.md @@ -0,0 +1,66 @@ +

3187. Peaks in Array

Hard


A peak in an array arr is an element that is greater than its previous and next element in arr.

+ +

You are given an integer array nums and a 2D integer array queries.

+ +

You have to process queries of two types:

+ +
    +
  • queries[i] = [1, li, ri], determine the count of peak elements in the subarray nums[li..ri].
  • +
  • queries[i] = [2, indexi, vali], change nums[indexi] to vali.
  • +
+ +

Return an array answer containing the results of the queries of the first type in order.

+ +

Notes:

+ +
    +
  • The first and the last element of an array or a subarray cannot be a peak.
  • +
+ +

 

+

Example 1:

+ +
+

Input: nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]

+ +

Output: [0]

+ +

Explanation:

+ +

First query: We change nums[3] to 4 and nums becomes [3,1,4,4,5].

+ +

Second query: The number of peaks in the [3,1,4,4,5] is 0.

+
+ +

Example 2:

+ +
+

Input: nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]

+ +

Output: [0,1]

+ +

Explanation:

+ +

First query: nums[2] should become 4, but it is already set to 4.

+ +

Second query: The number of peaks in the [4,1,4] is 0.

+ +

Third query: The second 4 is a peak in the [4,1,4,2,1].

+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= queries.length <= 105
  • +
  • queries[i][0] == 1 or queries[i][0] == 2
  • +
  • For all i that: +
      +
    • queries[i][0] == 1: 0 <= queries[i][1] <= queries[i][2] <= nums.length - 1
    • +
    • queries[i][0] == 2: 0 <= queries[i][1] <= nums.length - 1, 1 <= queries[i][2] <= 105
    • +
    +
  • +
+
\ No newline at end of file diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp b/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp new file mode 100644 index 00000000..88f28610 --- /dev/null +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maximumLength(vector& nums, int k) { + + int ans = 0; + + for(int i = 0; i < k; ++i) + { + vector dp(k, 0); + + for(auto& x : nums) + { + int rem = x % k; + + dp[rem] = max(dp[rem], dp[(i - rem + k)%k] + 1); + + ans = max(ans, dp[rem]); + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md b/3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/README.md b/3202-find-the-maximum-length-of-valid-subsequence-ii/README.md new file mode 100644 index 00000000..d8d412fe --- /dev/null +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/README.md @@ -0,0 +1,41 @@ +

3202. Find the Maximum Length of Valid Subsequence II

Medium


You are given an integer array nums and a positive integer k. +

A subsequence sub of nums with length x is called valid if it satisfies:

+ +
    +
  • (sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.
  • +
+Return the length of the longest valid subsequence of nums. +

 

+

Example 1:

+ +
+

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

+ +

Output: 5

+ +

Explanation:

+ +

The longest valid subsequence is [1, 2, 3, 4, 5].

+
+ +

Example 2:

+ +
+

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

+ +

Output: 4

+ +

Explanation:

+ +

The longest valid subsequence is [1, 4, 1, 4].

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 103
  • +
  • 1 <= nums[i] <= 107
  • +
  • 1 <= k <= 103
  • +
+
\ No newline at end of file diff --git a/3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp b/3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp new file mode 100644 index 00000000..0ad2e2d1 --- /dev/null +++ b/3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + + void constructingList(vector>& edges, vector adj[]) + { + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + + adj[u].push_back(v); + adj[v].push_back(u); + } + } + + int maximumDiameter(vector adj[], int n) + { + vector dist(n, 0); + + function dfs = [&](int sv, int d) + { + dist[sv] = d; + for(auto& node : adj[sv]) + { + if(dist[node] == 0) + dfs(node, d+1); + } + }; + + dfs(0, 1); + int farthest = max_element(dist.begin(), dist.end()) - dist.begin(); + fill(dist.begin(), dist.end(), 0); + dfs(farthest, 1); + return *max_element(dist.begin(), dist.end()) - 1; + } + + int minimumDiameterAfterMerge(vector>& edges1, vector>& edges2) { + + int n = edges1.size(); + int m = edges2.size(); + + vector adj[n+1], adj2[m+1]; + + constructingList(edges1, adj); + constructingList(edges2, adj2); + + int diameter1 = maximumDiameter(adj, n+1); + int diameter2 = maximumDiameter(adj2, m+1); + + return max({diameter1, diameter2, (diameter1 + 1)/2 + (diameter2 + 1)/2 + 1}); + } +}; \ No newline at end of file diff --git a/3203-find-minimum-diameter-after-merging-two-trees/README.md b/3203-find-minimum-diameter-after-merging-two-trees/README.md new file mode 100644 index 00000000..97f18b2d --- /dev/null +++ b/3203-find-minimum-diameter-after-merging-two-trees/README.md @@ -0,0 +1,48 @@ +

3203. Find Minimum Diameter After Merging Two Trees

Hard


There exist two undirected trees with n and m nodes, numbered from 0 to n - 1 and from 0 to m - 1, respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree.

+ +

You must connect one node from the first tree with another node from the second tree with an edge.

+ +

Return the minimum possible diameter of the resulting tree.

+ +

The diameter of a tree is the length of the longest path between any two nodes in the tree.

+ +

 

+

Example 1:

+ +
+

Input: edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]

+ +

Output: 3

+ +

Explanation:

+ +

We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.

+
+ +

Example 2:

+ +
+

Input: edges1 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]], edges2 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]]

+ +

Output: 5

+ +

Explanation:

+ +

We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n, m <= 105
  • +
  • edges1.length == n - 1
  • +
  • edges2.length == m - 1
  • +
  • edges1[i].length == edges2[i].length == 2
  • +
  • edges1[i] = [ai, bi]
  • +
  • 0 <= ai, bi < n
  • +
  • edges2[i] = [ui, vi]
  • +
  • 0 <= ui, vi < m
  • +
  • The input is generated such that edges1 and edges2 represent valid trees.
  • +
+
\ No newline at end of file diff --git a/3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp b/3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp new file mode 100644 index 00000000..edd38009 --- /dev/null +++ b/3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int minChanges(vector& nums, int k) { + + int n = nums.size(); + + map mp; + + for(int i = 0; i < n/2; ++i) + { + int diff = abs(nums[i] - nums[n-i-1]); + int maxDiff = max(k - min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])); + ++mp[0]; + --mp[diff]; + ++mp[diff+1]; + ++mp[maxDiff + 1]; + } + + int ans = INT_MAX, sum = 0; + + for(auto&[f, e] : mp) + { + sum += e; + ans = min(ans, sum); + } + + return ans; + } +}; \ No newline at end of file diff --git a/3224-minimum-array-changes-to-make-differences-equal/README.md b/3224-minimum-array-changes-to-make-differences-equal/README.md new file mode 100644 index 00000000..a40cf925 --- /dev/null +++ b/3224-minimum-array-changes-to-make-differences-equal/README.md @@ -0,0 +1,58 @@ +

3224. Minimum Array Changes to Make Differences Equal

Medium


You are given an integer array nums of size n where n is even, and an integer k.

+ +

You can perform some changes on the array, where in one change you can replace any element in the array with any integer in the range from 0 to k.

+ +

You need to perform some changes (possibly none) such that the final array satisfies the following condition:

+ +
    +
  • There exists an integer X such that abs(a[i] - a[n - i - 1]) = X for all (0 <= i < n).
  • +
+ +

Return the minimum number of changes required to satisfy the above condition.

+ +

 

+

Example 1:

+ +
+

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

+ +

Output: 2

+ +

Explanation:
+We can perform the following changes:

+ +
    +
  • Replace nums[1] by 2. The resulting array is nums = [1,2,1,2,4,3].
  • +
  • Replace nums[3] by 3. The resulting array is nums = [1,2,1,3,4,3].
  • +
+ +

The integer X will be 2.

+
+ +

Example 2:

+ +
+

Input: nums = [0,1,2,3,3,6,5,4], k = 6

+ +

Output: 2

+ +

Explanation:
+We can perform the following operations:

+ +
    +
  • Replace nums[3] by 0. The resulting array is nums = [0,1,2,0,3,6,5,4].
  • +
  • Replace nums[4] by 4. The resulting array is nums = [0,1,2,0,4,6,5,4].
  • +
+ +

The integer X will be 4.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n == nums.length <= 105
  • +
  • n is even.
  • +
  • 0 <= nums[i] <= k <= 105
  • +
+
\ No newline at end of file diff --git a/Boolean Matrix - GFG/README.md b/Boolean Matrix - GFG/README.md new file mode 100644 index 00000000..5c0cc0a5 --- /dev/null +++ b/Boolean Matrix - GFG/README.md @@ -0,0 +1,35 @@ +# Boolean Matrix +## Medium +

Given a boolean matrix of size RxC where each cell contains either 0 or 1, modify it such that if a matrix cell matrix[i][j] is 1 then all the cells in its ith row and jth column will become 1.

+

Example 1:

+
Input:
+R = 2, C = 2
+matrix[][] = {{1, 0},
+              {0, 0}}
+Output: 
+1 1
+1 0 
+Explanation:
+Only cell that has 1 is at (0,0) so all 
+cells in row 0 are modified to 1 and all 
+cells in column 0 are modified to 1.
+


Example 2:

+
Input:
+R = 4, C = 3
+matrix[][] = {{ 1, 0, 0},
+              { 1, 0, 0},
+              { 1, 0, 0},
+              { 0, 0, 0}}
+Output: 
+1 1 1
+1 1 1
+1 1 1
+1 0 0 
+Explanation:
+The position of cells that have 1 in
+the original matrix are (0,0), (1,0)
+and (2,0). Therefore, all cells in row
+0,1,2 are and column 0 are modified to 1. 
+

Your Task:
You dont need to read input or print anything. Complete the function booleanMatrix() that takes the matrix as input parameter and modifies it in-place.

+

Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R + C) 

+

Constraints:
1 ≤ R, C ≤ 1000
0 ≤ matrix[i][j] ≤ 1

\ No newline at end of file diff --git a/Boolean Matrix - GFG/boolean-matrix.cpp b/Boolean Matrix - GFG/boolean-matrix.cpp new file mode 100644 index 00000000..fd1cca9b --- /dev/null +++ b/Boolean Matrix - GFG/boolean-matrix.cpp @@ -0,0 +1,81 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + + +class Solution +{ + public: + //Function to modify the matrix such that if a matrix cell matrix[i][j] + //is 1 then all the cells in its ith row and jth column will become 1. + void booleanMatrix(vector > &matrix) + { + // code here + + int n = matrix.size(); + int m = matrix[0].size(); + + vector row(n, 0), col(m, 0); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(matrix[i][j] == 1) + { + row[i] = col[j] = 1; + } + } + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(row[i] or col[j]) + matrix[i][j] = 1; + } + } + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin>>t; + while(t--) + { + int row, col; + cin>> row>> col; + vector > matrix(row); + for(int i=0; i>matrix[i][j]; + } + } + + Solution ob; + ob.booleanMatrix(matrix); + + + for (int i = 0; i < row; ++i) + { + for (int j = 0; j < col; ++j) + { + cout<

You are given a matrix of dimensions n x m. The task is to perform boundary traversal on the matrix in a clockwise manner.

Example 1:

+
Input:
+n = 4, m = 4
+matrix[][] = {{1, 2, 3, 4},
+         {5, 6, 7, 8},
+         {9, 10, 11, 12},
+         {13, 14, 15,16}}
+Output: 1 2 3 4 8 12 16 15 14 13 9 5
+Explanation:
+The matrix is:
+1 2 3 4
+5 6 7 8
+9 10 11 12
+13 14 15 16
+The boundary traversal is:
+1 2 3 4 8 12 16 15 14 13 9 5
+
+

Example 2:

+
Input:
+n = 3, m = 4
+matrrix[][] = {{12, 11, 10, 9},
+         {8, 7, 6, 5},
+         {4, 3, 2, 1}}
+Output: 12 11 10 9 5 1 2 3 4 8
+
+

Your Task:
Complete the function boundaryTraversal() that takes matrix, n and m as input parameters and returns the list of integers that form the boundary traversal of the matrix in a clockwise manner.


Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)


Constraints:
1 <= n, m<= 1000
0 <= matrixi <= 1000

\ No newline at end of file diff --git a/Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp b/Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp new file mode 100644 index 00000000..9688b6c2 --- /dev/null +++ b/Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp @@ -0,0 +1,69 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + +class Solution +{ + public: + //Function to return list of integers that form the boundary + //traversal of the matrix in a clockwise manner. + vector boundaryTraversal(vector > matrix, int n, int m) + { + // code here + + vector ans; + + for(int i = 0; i < m; ++i) + ans.push_back(matrix[0][i]); + + if(n > 1) + { + for(int i = 1; i < n; ++i) + ans.push_back(matrix[i][m-1]); + + for(int i = m-2; i >= 0; --i) + ans.push_back(matrix[n-1][i]); + + if(m > 1) + { + for(int i = n-2; i >= 1; --i) + ans.push_back(matrix[i][0]); + } + } + + return ans; + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin>>t; + + while(t--) + { + int n,m; + cin>>n>>m; + vector > matrix(n); + + for(int i=0; i>matrix[i][j]; + } + } + + Solution ob; + vector result = ob.boundaryTraversal(matrix, n, m); + for (int i = 0; i < result.size(); ++i) + cout<

Given a binary tree, find if it is height balanced or not. 
A tree is height balanced if difference between heights of left and right subtrees is not more than one for all nodes of tree. 

+

A height balanced tree
        1
     /     \
   10      39
  /
5

+

An unbalanced tree
        1
     /    
   10   
  /
5

+

Example 1:

+
Input:
+      1
+    /
+   2
+    \
+     3 
+Output: 0
+Explanation: The max difference in height
+of left subtree and right subtree is 2,
+which is greater than 1. Hence unbalanced
+
+

Example 2:

+
Input:
+       10
+     /   \
+    20   30 
+  /   \
+ 40   60
+Output: 1
+Explanation: The max difference in height
+of left subtree and right subtree is 1.
+Hence balanced. 
+
+

Your Task:
You don't need to take input. Just complete the function isBalanced() that takes root node as parameter and returns true, if the tree is balanced else returns false.

+

Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 109

+

Expected time complexity: O(N)
Expected auxiliary space: O(h) , where h = height of tree

\ No newline at end of file diff --git a/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp b/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp new file mode 100644 index 00000000..df84409c --- /dev/null +++ b/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp @@ -0,0 +1,152 @@ +//{ Driver Code Starts +//Initial Template for C++ + + +#include +using namespace std; + + +// Tree Node +struct Node { + int data; + Node* left; + Node* right; +}; + +// Utility function to create a new Tree Node +Node* newNode(int val) { + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + + +// Function to Build Tree +Node* buildTree(string str) { + // Corner Case + if (str.length() == 0 || str[0] == 'N') return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +// } Driver Code Ends +/* A binary tree node structure + +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; + */ + +class Solution{ + public: + //Function to check whether a binary tree is balanced or not. + + int height(Node* root) + { + if(!root) + return 0; + return 1 + max(height(root->left), height(root->right)); + } + + bool isBalanced(Node *root) + { + // Your Code here + + if(!root) + return true; + + int left = height(root->left); + int right = height(root->right); + + return abs(left-right) <=1 and isBalanced(root->left) and isBalanced(root->right); + } +}; + + +//{ Driver Code Starts. + +/* Driver program to test size function*/ + + + +int main() { + + + int t; + scanf("%d ", &t); + while (t--) { + string s, ch; + getline(cin, s); + + Node* root = buildTree(s); + Solution ob; + cout << ob.isBalanced(root) << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Check whether K-th bit is set or not - GFG/README.md b/Check whether K-th bit is set or not - GFG/README.md new file mode 100644 index 00000000..89eef55a --- /dev/null +++ b/Check whether K-th bit is set or not - GFG/README.md @@ -0,0 +1,17 @@ +# Check whether K-th bit is set or not +## Easy +

Given a number N and a bit number K, check if Kth index bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number.
Note: Index is starting from 0. You just need to return true or false, driver code will take care of printing "Yes" and "No".

+

Example 1:

+
Input: 
N = 4
K = 0 +Output:
No +Explanation:
Binary representation of 4 is 100, in which 0th index bit from LSB is not set. So, return false.
+

Example 2:

+
Input: 
N = 4
K = 2 +Output:
Yes +Explanation:
Binary representation of 4 is 100, in which 2nd index bit from LSB is set. So, return true.
+

Example 3:

+
Input: 
N = 500
K = 3 +Output:
No +Explanation:
Binary representation of 500 is 111110100, in which 3rd index bit from LSB is not set. So, return false.
+
Your task:
+
You don't have to read input or print anything. Your task is to complete the function checkKthbit that takes n and k as parameters and returns either true(if kth bit is set) or false(if kth bit is not set).

Expected Time Complexity:
 O(1).
Expected Auxiliary Space: O(1).


Constraints:
1 ≤ N ≤ 109
0 ≤ K ≤ 31
\ No newline at end of file diff --git a/Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp b/Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp new file mode 100644 index 00000000..8a7874e7 --- /dev/null +++ b/Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp @@ -0,0 +1,46 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +//User function Template for C++ + + +class Solution +{ + public: + // Function to check if Kth bit is set or not. + bool checkKthBit(int n, int k) + { + // Your code here + // It can be a one liner logic!! Think of it!! + + return n & (1 << k); + } +}; + +//{ Driver Code Starts. + +// Driver Code +int main() +{ + int t; + cin>>t;//taking testcases + while(t--) + { + long long n; + cin>>n;//input n + int k; + cin>>k;//bit number k + Solution obj; + if(obj.checkKthBit(n, k)) + cout << "Yes" << endl; + else + cout << "No" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Column name from a given column number - GFG/README.md b/Column name from a given column number - GFG/README.md new file mode 100644 index 00000000..eb24562d --- /dev/null +++ b/Column name from a given column number - GFG/README.md @@ -0,0 +1,21 @@ +# Column name from a given column number +## Medium +

Given a positive integer, return its corresponding column title as appear in an Excel sheet.
Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA” and so on.

+

Example 1:

+
Input:
+N = 28
+Output: AB
+Explanation: 1 to 26 are A to Z.
+Then, 27 is AA and 28 = AB.
+
+
+

Example 2:

+
Input: 
+N = 13
+Output: M
+Explanation: M is the 13th character of
+alphabet.
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function colName() which takes the column number N as input and returns the column name represented as a string.

Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(1).

+

Constraints:
1 <= N <= 1018

+

 

\ No newline at end of file diff --git a/Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp b/Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp new file mode 100644 index 00000000..c1086872 --- /dev/null +++ b/Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp @@ -0,0 +1,41 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + + +class Solution{ + public: + string colName (long long int n) + { + // your code here + + string ans; + + while(n--) + { + int rem = n%26; + ans.push_back(rem + 'A'); + n /= 26; + } + + reverse(ans.begin(), ans.end()); + + return ans; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; cin >> t; + while (t--) + { + long long int n; cin >> n; + Solution ob; + cout << ob.colName (n) << '\n'; + } +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Eventual Safe States - GFG/README.md b/Eventual Safe States - GFG/README.md new file mode 100644 index 00000000..59c21c34 --- /dev/null +++ b/Eventual Safe States - GFG/README.md @@ -0,0 +1,51 @@ +# Eventual Safe States +## Medium +

A directed graph of V vertices and E edges is given in the form of an adjacency list adj. Each node of the graph is labelled with a distinct integer in the range 0 to V - 1.

+ +

A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node.

+ +

You have to return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.

+ +

Example 1:

+ +
Input:
+
+
+Output:
+2 4 5 6
+Explanation:
+The given graph is shown above.
+Nodes 5 and 6 are terminal nodes as there are no 
+outgoing edges from either of them. 
+Every path starting at nodes 2, 4, 5, and 6 all 
+lead to either node 5 or 6.
+
+ +

Example 2:

+ +
Input:
+
+
+Output:
+3
+Explanation:
+Only node 3 is a terminal node, and every path 
+starting at node 3 leads to node 3.
+
+ +

Your Task:
+You don't need to read or print anything. Your task is to complete the function eventualSafeNodes() which takes an integer V denoting no. of vertices and adj denoting adjacency list of the graph and returns an array of safe nodes.

+ +

Expected Time Complexity: O(V + E)

+ +

Expected Space Complexity: O(V)

+ +

Constraints:

+ +
    +
  • 1 <= V <= 104
  • +
  • 0 <= E <= 104
  • +
  • The graph won't contain self loops.
  • +
  • Each node in the graph has a distinct value in the range 0 to V - 1.
  • +
+
\ No newline at end of file diff --git a/Eventual Safe States - GFG/eventual-safe-states.cpp b/Eventual Safe States - GFG/eventual-safe-states.cpp new file mode 100644 index 00000000..e6f97a13 --- /dev/null +++ b/Eventual Safe States - GFG/eventual-safe-states.cpp @@ -0,0 +1,95 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + vector eventualSafeNodes(int V, vector adj[]) { + // code here + + + vector revAdj[V+1]; + + for(int i = 0; i < V; ++i) + { + for(auto& itr : adj[i]) + { + revAdj[itr].push_back(i); + } + } + + vector indegree(V+1,0); + + for(int i = 0; i < V; ++i) + { + for(auto& itr : revAdj[i]) + ++indegree[itr]; + } + + queue q; + + for(int i = 0; i < V; ++i) + { + if(indegree[i] == 0) + q.push(i); + } + + + vector safe; + + while(!q.empty()) + { + int curr = q.front(); + q.pop(); + + safe.push_back(curr); + + for(auto& itr : revAdj[curr]) + { + --indegree[itr]; + + if(indegree[itr] == 0) + q.push(itr); + } + } + + sort(safe.begin(), safe.end()); + + return safe; + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + + int V, E; + cin >> V >> E; + vector adj[V]; + + for (int i = 0; i < E; i++) { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + } + + Solution obj; + vector safeNodes = obj.eventualSafeNodes(V, adj); + for (auto i : safeNodes) { + cout << i << " "; + } + cout << endl; + } +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Find Common Nodes in two BSTs - GFG/README.md b/Find Common Nodes in two BSTs - GFG/README.md new file mode 100644 index 00000000..be9f1877 --- /dev/null +++ b/Find Common Nodes in two BSTs - GFG/README.md @@ -0,0 +1,40 @@ +# Find Common Nodes in two BSTs +## Easy +

Given two Binary Search Trees. Find the nodes that are common in both of them, ie- find the intersection of the two BSTs.

+

Note: Return the common nodes in sorted order.

+

Example 1:

+
Input:
+BST1:
+                  5
+               /     \
+             1        10
+           /   \      /
+          0     4    7
+                      \
+                       9
+BST2:
+                10 
+              /    \
+             7     20
+           /   \ 
+          4     9
+Output: 4 7 9 10
+
+
+

Example 2:

+
Input:
+BST1:
+     10
+    /  \
+   2   11
+  /  \
+ 1   3
+BST2:
+       2
+     /  \
+    1    3
+Output: 1 2 3
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function findCommon() that takes roots of the two BSTs as input parameters and returns a list of integers containing the common nodes in sorted order. 

+

Expected Time Complexity: O(N1 + N2) where N1 and N2 are the sizes of the 2 BSTs.
Expected Auxiliary Space: O(H1 + H2) where H1 and H2 are the heights of the 2 BSTs.

+

Constraints:
1 <= Number of Nodes <= 105
1 <= Node data <= 109

\ No newline at end of file diff --git a/Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp b/Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp new file mode 100644 index 00000000..d57d7cf2 --- /dev/null +++ b/Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp @@ -0,0 +1,149 @@ +//{ Driver Code Starts +#include +using namespace std; + +// Tree Node +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +// } Driver Code Ends + + +class Solution +{ + public: + //Function to find the nodes that are common in both BST. + + map mp; + + void helper(Node* root) + { + if(root) + { + helper(root->left); + ++mp[root->data]; + helper(root->right); + } + } + + vector findCommon(Node *root1, Node *root2) + { + //Your code here + + helper(root1); + helper(root2); + + vector ans; + + for(auto& itr : mp) + { + if(itr.second > 1) + ans.push_back(itr.first); + } + + return ans; + + } +}; + + + + +//{ Driver Code Starts. + +int main() +{ + int t; + cin>>t; + getchar(); + while(t--) + { + string s; + getline(cin,s); + Node* root1 = buildTree(s); + + getline(cin,s); + Node* root2 = buildTree(s); + Solution ob; + vector res = ob.findCommon(root1, root2); + for (int i : res) + cout << i << " "; + cout<< endl; + } + + return 1; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Find duplicates in an array - GFG/README.md b/Find duplicates in an array - GFG/README.md new file mode 100644 index 00000000..81f21dc6 --- /dev/null +++ b/Find duplicates in an array - GFG/README.md @@ -0,0 +1,19 @@ +# Find duplicates in an array +## Easy +

Given an array a of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array. Return the answer in ascending order. If no such element is found, return list containing [-1]

+

Note: The extra space is only for the array to be returned. Try and perform all operations within the provided array. 

+

Example 1:

+
Input:
+N = 4
+a[] = {0,3,1,2}
+Output: 
-1 +Explanation:
There is no repeating element in the array. Therefore output is -1.
+

Example 2:

+
Input:
+N = 5
+a[] = {2,3,1,2,3}
+Output: 
2 3  +Explanation:
2 and 3 occur more than once in the given array.
+

Your Task:
Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in a sorted manner. 

+

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).

+

Constraints:
1 <= N <= 105
0 <= A[i] <= N-1, for each valid i

\ No newline at end of file diff --git a/Find duplicates in an array - GFG/find-duplicates-in-an-array.java b/Find duplicates in an array - GFG/find-duplicates-in-an-array.java new file mode 100644 index 00000000..e60293f5 --- /dev/null +++ b/Find duplicates in an array - GFG/find-duplicates-in-an-array.java @@ -0,0 +1,54 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; +import java.util.Map.Entry; + +class GFG { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = sc.nextInt(); + Solution g = new Solution(); + ArrayList ans = g.duplicates(a, n); + for (Integer val : ans) System.out.print(val + " "); + System.out.println(); + } + } +} + +// } Driver Code Ends + + +class Solution { + public static ArrayList duplicates(int arr[], int n) { + // code here + + Map mp = new TreeMap(); + + for(int i = 0; i < n; ++i) + { + if(mp.containsKey(arr[i])) + { + mp.put(arr[i], mp.get(arr[i]) + 1); + } + else + mp.put(arr[i], 1); + } + + ArrayList ans = new ArrayList(); + + for(Map.Entry ele : mp.entrySet()) + { + if(ele.getValue() > 1) + ans.add(ele.getKey()); + } + + if(ans.isEmpty()) + ans.add(-1); + + return ans; + } +} diff --git a/Form a number divisible by 3 using array digits - GFG/README.md b/Form a number divisible by 3 using array digits - GFG/README.md new file mode 100644 index 00000000..cacbddfd --- /dev/null +++ b/Form a number divisible by 3 using array digits - GFG/README.md @@ -0,0 +1,19 @@ +# Form a number divisible by 3 using array digits +## Easy +

You will be given an array arr of integers of length N. You can construct an integer from two integers by treating the integers as strings, and then concatenating them. For example, 19 and 4 can be used to construct 194 and 419.

+

The task is to find whether it’s possible to construct an integer using all the digits of these numbers such that it would be divisible by 3.
If it is possible then print 1 and if not print 0.

+

Example 1:

+
Input: N = 3
+arr = {40, 50, 90}
+Output: 1
+Explanation: One such number is 405090.
+

Example 2:

+
Input: N = 2
+arr = {1, 4}
+Output: 0
+Explanation: The numbers we can form 
+are 14 and 41. But neither of them are 
+divisible by 3.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function isPossible() which takes N and arr as input parameters and returns 1 if we can form a number by the digits of the given number, that would be divisible by 3, otherwise returns 0.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 ≤ N, arr[i] ≤ 105

\ No newline at end of file diff --git a/Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp b/Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp new file mode 100644 index 00000000..50b25f4d --- /dev/null +++ b/Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp @@ -0,0 +1,42 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + int isPossible(int N, int arr[]) { + // code here + + int sum = 0; + + for(int i = 0; i < N; ++i) + { + sum += (arr[i] % 3); + } + + return (sum % 3 == 0 ? 1 : 0); + } +}; + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int N; + cin >> N; + int arr[N]; + for (int i = 0; i < N; i++) cin >> arr[i]; + + Solution ob; + cout << ob.isPossible(N, arr) << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Height of Binary Tree - GFG/README.md b/Height of Binary Tree - GFG/README.md new file mode 100644 index 00000000..ca23b059 --- /dev/null +++ b/Height of Binary Tree - GFG/README.md @@ -0,0 +1,21 @@ +# Height of Binary Tree +## Easy +

Given a binary tree, find its height.

+

Example 1:

+
Input:
+     1
+    /  \
+   2    3
+Output: 2
+
+

Example 2:

+
Input:
+  2
+   \
+    1
+   /
+ 3
+Output: 3   
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function height() which takes root node of the tree as input parameter and returns an integer denoting the height of the tree. If the tree is empty, return 0. 

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

+

Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 109

\ No newline at end of file diff --git a/Height of Binary Tree - GFG/height-of-binary-tree.cpp b/Height of Binary Tree - GFG/height-of-binary-tree.cpp new file mode 100644 index 00000000..325faf43 --- /dev/null +++ b/Height of Binary Tree - GFG/height-of-binary-tree.cpp @@ -0,0 +1,127 @@ +//{ Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +// } Driver Code Ends +//User function template for C++ + +/* +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; +*/ +class Solution{ + public: + //Function to find the height of a binary tree. + int height(struct Node* node){ + // code here + + if(!node) + return 0; + + return 1 + max(height(node->left), height(node->right)); + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + string treeString; + getline(cin,treeString); + Node* root = buildTree(treeString); + Solution ob; + cout<

Given a linked list sorted in ascending order and an integer called data, insert data in the linked list such that the list remains sorted.

+

Example 1:

+
Input:
+LinkedList: 25->36->47->58->69->80
+data: 19
+Output: 
19 25 36 47 58 69 80
Explanation:

After inserting 19 the sorted linked list will look like the one in the output.
+

Example 2:

+
Input:
+LinkedList: 50->100
+data: 75
+Output: 
50 75 100
Explanation:
After inserting 75 the sorted linked list will look like the one in the output.
+

Your Task:
The task is to complete the function sortedInsert() which should insert the element in sorted Linked List and return the head of the linked list

+

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

+

Constraints:
1 <= N <= 104
-99999 <= A[i] <= 99999, for each valid i

\ No newline at end of file diff --git a/Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp b/Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp new file mode 100644 index 00000000..2f15ec45 --- /dev/null +++ b/Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp @@ -0,0 +1,114 @@ +//{ Driver Code Starts +// + +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} + +// } Driver Code Ends +/* +structure of the node of the list is as +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; +*/ + +class Solution{ + public: + // Should return head of the modified linked list + Node *sortedInsert(struct Node* head, int data) { + // Code + + + Node* newNode = new Node(data); + + Node* prev = nullptr, *temp = head; + + if(data <= temp->data) + { + newNode->next = head; + return newNode; + } + + while(temp) + { + if(temp->data >= data) + { + prev->next = newNode; + newNode->next = temp; + break; + } + prev = temp; + temp = temp->next; + } + + if(prev->data < data) + { + prev->next = newNode; + } + + return head; + + } +}; + + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + + int data; + cin>>data; + + struct Node *head = new Node(data); + struct Node *tail = head; + for (int i = 0; i < n-1; ++i) + { + cin>>data; + tail->next = new Node(data); + tail = tail->next; + } + + int k; + cin>>k; + Solution obj; + head = obj.sortedInsert(head,k); + printList(head); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Knapsack with Duplicate Items - GFG/README.md b/Knapsack with Duplicate Items - GFG/README.md new file mode 100644 index 00000000..07f5effd --- /dev/null +++ b/Knapsack with Duplicate Items - GFG/README.md @@ -0,0 +1,21 @@ +# Knapsack with Duplicate Items +## Medium +

Given a set of N items, each with a weight and a value, represented by the array w and val respectively. Also, a knapsack with weight limit W.
The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit.
Note: Each item can be taken any number of times.

+

Example 1:

+
Input: 
N = 2
W = 3 +val = {1, 1} +wt = {2, 1} +Output:
3 +Explanation: +1.Pick the 2nd element thrice. +2.Total profit = 1 + 1 + 1 = 3. Also the total weight = 1 + 1 + 1 = 3 which is <= 3.
+
+

Example 2:

+
Input: 
N = 4
W = 8 +val[] = {6, 1, 7, 7} +wt[] = {1, 3, 4, 5} +Output:
48 +Explanation:
The optimal choice is to pick the 1st element 8 times.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function knapSack() which takes the values N, W and the arrays val and wt as input parameters and returns the maximum possible value.

+

Expected Time Complexity: O(N*W)
Expected Auxiliary Space: O(W)

+

Constraints:
1 ≤ N, W ≤ 1000
1 ≤ val[i], wt[i] ≤ 100

\ No newline at end of file diff --git a/Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp b/Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp new file mode 100644 index 00000000..d158438b --- /dev/null +++ b/Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp @@ -0,0 +1,60 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution{ +public: + + int helper(int idx, int n, int W, int val[], int wt[], vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][W] != -1) + return dp[idx][W]; + + int notTake = helper(idx+1, n, W, val, wt, dp); + + int take = 0; + + if(wt[idx] <= W) + take = val[idx] + helper(idx, n, W - wt[idx], val, wt, dp); + + return dp[idx][W] = max(take, notTake); + } + + int knapSack(int N, int W, int val[], int wt[]) + { + // code here + + vector> dp(N+1, vector(W+1, -1)); + + return helper(0, N, W, val, wt, dp); + } +}; + +//{ Driver Code Starts. + +int main(){ + int t; + cin>>t; + while(t--){ + int N, W; + cin>>N>>W; + int val[N], wt[N]; + for(int i = 0;i < N;i++) + cin>>val[i]; + for(int i = 0;i < N;i++) + cin>>wt[i]; + + Solution ob; + cout<

Thank you for consistently taking part in the POTD.
Feel free to share your feedback and suggestions by filling up the Form below.
https://forms.gle/Ga6fVJBzEtDSvEop9
____________________________________________________________________________________________________________________________________________

Given two integer array A and B of size N each.
A sum combination is made by adding one element from array A and another element of array B.
Return the maximum K valid sum combinations from all the possible sum combinations.

+

Note : Output array must be sorted in non-increasing order.

+

Example 1:

+
Input:
N = 2
K = 2
A [ ] = {3, 2}
B [ ] = {1, 4}
Output: {7, 6}
Explanation: 
7 -> (A : 3) + (B : 4)
6 -> (A : 2) + (B : 4)
+

Example 2:

+
Input:
N = 4
K = 3
A [ ] = {1, 4, 2, 3}
B [ ] = {2, 5, 1, 6}
Output: {10, 9, 9}
Explanation: 
10 -> (A : 4) + (B : 6)
9 -> (A : 4) + (B : 5)
9 -> (A : 3) + (B : 6)
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function maxCombinations() which takes the interger N,integer K and two integer arrays A [ ] and B [ ] as parameters and returns the maximum K valid distinct sum combinations .

+

Expected Time Complexity: O(Nlog(N))
Expected Auxiliary Space: O(N)

+

Constraints:
1 ≤ N ≤  105
1 ≤ K ≤  N
1 ≤ A [ i ] , B [ i ] ≤ 104

\ No newline at end of file diff --git a/Maximum Sum Combination - GFG/maximum-sum-combination.cpp b/Maximum Sum Combination - GFG/maximum-sum-combination.cpp new file mode 100644 index 00000000..a13755ac --- /dev/null +++ b/Maximum Sum Combination - GFG/maximum-sum-combination.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +class Solution { + public: + vector maxCombinations(int N, int K, vector &A, vector &B) { + // code here + + priority_queue, greater > pq; + + sort(A.rbegin(), A.rend()); + sort(B.rbegin(), B.rend()); + + for(int i = 0; i < K; ++i) + { + for(int j = 0; j < K; ++j) + { + int sum = A[i] + B[j]; + + if(pq.size() < K) + pq.push(sum); + else + { + int curr = pq.top(); + + if(curr < sum) + { + pq.pop(); + pq.push(sum); + } + else + break; + } + } + } + + vector ans; + + while(!pq.empty()) + { + ans.push_back(pq.top()); + pq.pop(); + } + + reverse(ans.begin(), ans.end()); + + return ans; + } +}; + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int N, K; + cin >> N >> K; + + vector A(N), B(N); + for (int i = 0; i < N; i++) { + cin >> A[i]; + } + for (int i = 0; i < N; i++) { + cin >> B[i]; + } + Solution obj; + vector ans = obj.maxCombinations(N, K, A, B); + for (auto &it : ans) cout << it << ' '; + cout << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Minimum Deletions - GFG/README.md b/Minimum Deletions - GFG/README.md new file mode 100644 index 00000000..c4358768 --- /dev/null +++ b/Minimum Deletions - GFG/README.md @@ -0,0 +1,13 @@ +# Minimum Deletions +## Medium +

Given a string of S as input. Your task is to write a program to delete the minimum number of characters from the string so that the resultant string is a palindrome.
Note: The order of characters in the string should be maintained.

+

Example 1:

+
Input: 
S = "aebcbda" +Output:
2 +Explanation:
Remove characters 'e' and 'd'.
+

Example 2:

+
Input: 
S = "geeksforgeeks" +Output:
8 +Explanation:
One of the possible result string can be "eefee", so answer is 13 - 5 = 8. +
+

Your Task:  
You don't need to read input or print anything. Your task is to complete the function minimumNumberOfDeletions() which takes the string S as inputs and returns the minimum number of deletions required to convert S into a pallindrome.

Expected Time Complexity: O(|S|2)
Expected Auxiliary Space: O(|S|2)

Constraints:
1 ≤ |S| ≤ 103

\ No newline at end of file diff --git a/Minimum Deletions - GFG/minimum-deletions.cpp b/Minimum Deletions - GFG/minimum-deletions.cpp new file mode 100644 index 00000000..82c5f770 --- /dev/null +++ b/Minimum Deletions - GFG/minimum-deletions.cpp @@ -0,0 +1,60 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +//User function template for C++ + +class Solution{ + public: + + int helper(int i , int j, int n, string& s, string& t, vector>& dp) + { + if(i == n and j == n) + return 0; + + if(i >= n or j >= n) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + if(s[i] == t[j]) + return dp[i][j] = 1 + helper(i+1, j+1, n, s, t, dp); + else + { + return dp[i][j] = max(helper(i+1, j, n, s, t, dp), helper(i, j+1, n, s, t, dp)); + } + } + + int minimumNumberOfDeletions(string S) { + // code here + + int n = S.size(); + + string t = S; + + reverse(t.begin(), t.end()); + + if(t == S) return 0; + + vector> dp(n+1, vector(n+1, -1)); + + return n - helper(0, 0, n, S, t, dp); + } +}; + +//{ Driver Code Starts. +int main(){ + int t; + cin >> t; + while(t--){ + string S; + cin >> S; + Solution obj; + cout << obj.minimumNumberOfDeletions(S) << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Minimum Operations - GFG/README.md b/Minimum Operations - GFG/README.md new file mode 100644 index 00000000..664241f7 --- /dev/null +++ b/Minimum Operations - GFG/README.md @@ -0,0 +1,22 @@ +# Minimum Operations +## Easy +

Given a number N. Find the minimum number of operations required to reach N starting from 0. You have 2 operations available:

+
    +
  • Double the number
  • +
  • Add one to the number
  • +
+

Example 1:

+
Input:
+N = 8
+Output: 4
+Explanation: 
0 + 1 = 1 --> 1 + 1 = 2 --> 2 * 2 = 4 --> 4 * 2 = 8. +
+

Example 2:

+
Input: 
+N = 7
+Output: 5
+Explanation: 
0 + 1 = 1 --> 1 + 1 = 2 --> 1 + 2 = 3 --> 3 * 2 = 6 --> 6 + 1 = 7. +
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function minOperation() which accepts an integer N and return number of minimum operations required to reach N from 0.

+

Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 106

\ No newline at end of file diff --git a/Minimum Operations - GFG/minimum-operations.cpp b/Minimum Operations - GFG/minimum-operations.cpp new file mode 100644 index 00000000..96d6a112 --- /dev/null +++ b/Minimum Operations - GFG/minimum-operations.cpp @@ -0,0 +1,42 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution +{ + public: + int minOperation(int n) + { + //code here. + + int cnt = 0; + + while(n > 0) + { + if(n & 1) + --n; + else + n /= 2; + + ++cnt; + } + + return cnt; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + Solution ob; + cout<

Given a Binary Search Tree, modify the given BST such that it is balanced and has minimum possible height. Return the balanced BST.

+

Example1:

+
Input:
+       30
+      /
+     20
+    /
+   10
Output: + 20 + / \ + 10 30 +
+

Example2:

+
Input:
+         4
+        /
+       3
+      /
+     2
+    /
+   1
+Output:
+      3            3           2
+    /  \         /  \        /  \
+   1    4   OR  2    4  OR  1    3   
+    \          /                  \ 
2 1 4
+


Your Task:

The task is to complete the function buildBalancedTree() which takes root as the input argument and returns the root of tree after converting the given BST into a balanced BST with minimum possible height. The driver code will print the height of the updated tree in output itself.

 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Here N denotes total number of nodes in given BST.

+

Constraints:
1 <= N <= 105
1 <= Node data <= 109

\ No newline at end of file diff --git a/Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp b/Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp new file mode 100644 index 00000000..0a4ca566 --- /dev/null +++ b/Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp @@ -0,0 +1,186 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x){ + data = x; + left = NULL; + right = NULL; + } +}; + + +// } Driver Code Ends +/*Structure of the Node of the BST is as +struct Node +{ + int data; + Node* left, *right; +}; +*/ + +class Solution{ + + public: + // Your are required to complete this function + // function should return root of the modified BST + + vector inorder; + + void helper(Node* root) + { + if(root) + { + helper(root->left); + inorder.push_back(root->data); + helper(root->right); + } + } + + Node* BST(int start, int end) + { + if(start <= end) + { + int mid = (start + end) >> 1; + + Node* root = new Node(inorder[mid]); + root->left = BST(start, mid-1); + root->right = BST(mid+1, end); + return root; + } + + return nullptr; + } + Node* buildBalancedTree(Node* root) + { + // Code here + + helper(root); + + return BST(0, inorder.size()-1); + + } +}; + + +//{ Driver Code Starts. + +Node* insert(struct Node* node, int key){ + if (node == NULL) return new Node(key); + if (key < node->data) + node->left = insert(node->left, key); + else if (key > node->data) + node->right = insert(node->right, key); + return node; +} + +void preOrder(Node* node) +{ + if (node == NULL)return; + printf("%d ", node->data); + preOrder(node->left); + preOrder(node->right); +} + +int height(struct Node* node) +{ + if (node==NULL) + return 0; + int lDepth = height(node->left); + int rDepth = height(node->right); + if (lDepth > rDepth) + return(lDepth+1); + else + return(rDepth+1); +} +Node *buildTree(string str) { + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +Node* buildBalancedTree(Node* root); + +int main() +{ + int t; + cin>>t; + getchar(); + while(t--) + { + struct Node *root = NULL; + int n, temp; + string tree; + getline(cin,tree); + root = buildTree(tree); + // cout<

You are given an n x m binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

+

A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.

+

Find the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

+

Example 1:

+
Input:
+grid[][] = {{0, 0, 0, 0},
+            {1, 0, 1, 0},
+            {0, 1, 1, 0},
+            {0, 0, 0, 0}}
+Output:
+3
+Explanation:
+0 0 0 0
+1 0 1 0
+0 1 1 0
+0 0 0 0
+The highlighted cells represents the land cells.
+
+

Example 2:

+
Input:
+grid[][] = {{0, 0, 0, 1},
+            {0, 1, 1, 0},
+            {0, 1, 1, 0},
+            {0, 0, 0, 1},
+            {0, 1, 1, 0}}
+Output:
+4
+Explanation:
+0 0 0 1
+0 1 1 0
+0 1 1 0
+0 0 0 1
+0 1 1 0
+The highlighted cells represents the land cells.
+

Your Task:

+

You don't need to print or input anything. Complete the function numberOfEnclaves() which takes a 2D integer matrix grid as the input parameter and returns an integer, denoting the number of land cells.

+

Expected Time Complexity: O(n * m)

+

Expected Space Complexity: O(n * m)

+

Constraints:

+
    +
  • 1 <= n, m <= 500
  • +
  • grid[i][j] == 0 or 1
  • +
\ No newline at end of file diff --git a/Number Of Enclaves - GFG/number-of-enclaves.cpp b/Number Of Enclaves - GFG/number-of-enclaves.cpp new file mode 100644 index 00000000..e61366c8 --- /dev/null +++ b/Number Of Enclaves - GFG/number-of-enclaves.cpp @@ -0,0 +1,86 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + int numberOfEnclaves(vector> &grid) { + // Code here + + int n = grid.size(); + int m = grid[0].size(); + + int landCell = 0; + + vector dx = {0, +1, 0, -1}; + vector dy = {+1, 0, -1, 0}; + + vector> visited(n, vector(m, false)); + + function dfs = [&](int i, int j){ + + visited[i][j] = true; + + for(int x = 0; x < 4; ++x) + { + int newX = dx[x] + i; + int newY = dy[x] + j; + + if(newX >= 0 and newY >= 0 and newX < n and newY < m and !visited[newX][newY] and grid[newX][newY] == 1) + { + dfs(newX, newY); + } + } + + }; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if((i == 0 or j == 0 or i == n-1 or j == m-1) and (grid[i][j] == 1 and !visited[i][j])) + { + dfs(i,j); + } + } + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited[i][j] and grid[i][j] == 1) + ++landCell; + } + } + + return landCell; + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + Solution obj; + cout << obj.numberOfEnclaves(grid) << endl; + } +} +// } Driver Code Ends \ No newline at end of file diff --git a/Number of paths - GFG/README.md b/Number of paths - GFG/README.md new file mode 100644 index 00000000..bd1a9a3c --- /dev/null +++ b/Number of paths - GFG/README.md @@ -0,0 +1,28 @@ +# Number of paths +## Medium +

The problem is to count all the possible paths from top left to bottom right of an MxN matrix with the constraints that from each cell you can either move to right or down.

+

Return answer modulo 109+7.

+

Example 1:

+
Input:
+M = 3 and N = 3
+Output: 6
+Explanation:
+Let the given input 3*3 matrix is filled 
+as such:
+A B C
+D E F
+G H I
+The possible paths which exists to reach 
+'I' from 'A' following above conditions 
+are as follows:ABCFI, ABEHI, ADGHI, ADEFI, 
+ADEHI, ABEFI
+
+

Example 2:

+
Input:
+M = 1 and N = 4
+Output: 1
+Explanation:
+There is only one direction to go in,
and thus, there is only one path possible.
+

Your Task
You don't need to read input or print anything. Your task is to complete the function numberOfPaths() which takes the integer M and integer N as input parameters and returns an integer, the number of paths.

+

Expected Time Complexity: O(M)
Expected Space Complexity: O(1)

+

Constraints:
1 ≤ N ≤ 108
1 ≤ M ≤ 105

\ No newline at end of file diff --git a/Number of paths - GFG/number-of-paths.cpp b/Number of paths - GFG/number-of-paths.cpp new file mode 100644 index 00000000..4d0291c2 --- /dev/null +++ b/Number of paths - GFG/number-of-paths.cpp @@ -0,0 +1,73 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +class Solution +{ + public: + + const int mod = 1e9+7; + + long long expo(long long x, long long y, long long mod) + { + long long res = 1; + + while(y > 0) + { + if(y & 1) + { + res = (res * x) % mod; + } + + x = (x * x) % mod; + + y >>= 1; + } + + return res; + } + + long long modInverse(long long x, long long m) + { + return expo(x, m-2, m); + } + + long long numberOfPaths(int M, int N) + { + // Code Here + + int n = M + N - 2; + int r = M-1; + + long long ans = 1; + + for(int i = 1; i <= r; ++i) + { + ans = (ans * (n-i+1)) % mod; + ans = (ans * modInverse(i , mod))%mod; + } + + return ans; + } +}; + + +//{ Driver Code Starts. + + +int main() +{ + int t; + cin>>t; + while(t--) + { + int N, M; + cin>>M>>N; + Solution ob; + cout << ob.numberOfPaths(M, N)<

Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.
Note: You need to swap the nodes, not only the data. If only data is swapped then driver will print -1.

+

Example 1:

+
Input:
+LinkedList: 1->2->2->4->5->6->7->8
+Output: 
2 1 4 2 6 5 8 7 +Explanation:
After swapping each pair considering (1,2), (2, 4), (5, 6).. so on as pairs, we get 2, 1, 4, 2, 6, 5, 8, 7 as a new linked list.
+
+

Example 2:

+
Input:
+LinkedList: 1->3->4->7->9->10->1
+Output: 
3 1 7 4 10 9 1 +Explanation:
After swapping each pair considering (1,3), (4, 7), (9, 10).. so on as pairs, we get 3, 1, 7, 4, 10, 9, 1 as a new linked list.
+

Your Task:
The task is to complete the function pairWiseSwap() which takes the head node as the only argument and returns the head of modified linked list.

+

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

+

Constraints:
1 ≤ N ≤ 105

\ No newline at end of file diff --git a/Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp b/Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp new file mode 100644 index 00000000..dc962e68 --- /dev/null +++ b/Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp @@ -0,0 +1,135 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + + +// } Driver Code Ends +/* + Pairwise swap a linked list + The input list will have at least one element + node is defined as + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } + +}*head; +*/ +class Solution +{ + public: + Node* pairWiseSwap(struct Node* head) + { + // The task is to complete this method + + if(!head or !head->next) + return head; + + Node* prev = nullptr, *temp = head, *prevPrev = nullptr; + Node* newHead = nullptr; + + bool ok = false; + + while(temp) + { + if(ok) + { + Node* nextNode = (temp->next ? temp->next : nullptr); + temp->next = prev; + prev->next = nextNode; + + if(prevPrev) + { + prevPrev->next = temp; + } + prevPrev = prev; + + if(!newHead) + newHead = temp; + + temp = prev; + } + + if(!ok) + prev = temp; + + temp = temp->next; + + ok ^= 1; + } + + return newHead; + } +}; + +//{ Driver Code Starts. + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} + +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + + int data; + cin>>data; + struct Node *head = new Node(data); + struct Node *tail = head; + map mp; + mp[head] = head->data; + for (int i = 0; i>data; + tail->next = new Node(data); + tail = tail->next; + mp[tail] = tail->data; + } + struct Node *failure = new Node(-1); + Solution ob; + head = ob.pairWiseSwap(head); + int flag = 0; + struct Node *temp = head; + while(temp){ + if(mp[temp] != temp->data){ + flag = 1; + break; + } + temp = temp->next; + } + if(flag) + printList(failure); + else + printList(head); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/README.md b/README.md index c84a2351..eec66b90 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,24 @@ # Leetcode This Repository Contains All My Solved Leetcode Problems. + 2023 + +leetcode.com/rewind/?ref=KnockCat + +![LeetCode_Rewind_2023](https://github.com/knockcat/Temp/assets/85362504/ceb0a224-1036-478d-9d21-805c14ed6389) + +![image](https://github.com/knockcat/Temp/assets/85362504/7e954346-c8a9-4786-89f4-949f2406e3b9) + +![image](https://github.com/knockcat/Temp/assets/85362504/2c7a62b7-221f-4493-9111-a5d7bb5ba85b) + +![Screenshot (965)](https://github.com/knockcat/Temp/assets/85362504/d06eb3c4-8ba9-4d4e-8b79-fea5eca506ca) + +![image](https://github.com/knockcat/Temp/assets/85362504/d835a19b-f770-4946-9b3e-cc909bf50e10) + +![image](https://github.com/knockcat/Temp/assets/85362504/249fc2eb-1410-4ee8-aaf2-8f8bda6db33b) + + 2022 + ![image](https://user-images.githubusercontent.com/85362504/210123821-c3c67c36-5321-4b00-88de-faa8d3b0d30f.png) diff --git a/Reverse alternate nodes in Link List - GFG/README.md b/Reverse alternate nodes in Link List - GFG/README.md new file mode 100644 index 00000000..9ba82f5b --- /dev/null +++ b/Reverse alternate nodes in Link List - GFG/README.md @@ -0,0 +1,23 @@ +# Reverse alternate nodes in Link List +## Medium +

Given a linked list, you have to perform the following task:

+
    +
  1. Extract the alternative nodes starting from second node.
  2. +
  3. Reverse the extracted list.
  4. +
  5. Append the extracted list at the end of the original list.
  6. +
+

Note: Try to solve the problem without using any extra memory.

+

Example 1:

+
Input:
+LinkedList = 10->4->9->1->3->5->9->4
+Output: 
10 9 3 9 4 5 1 4 +Explanation:
Alternative nodes in the given linked list are 4,1,5,4. Reversing the alternative nodes from the given list, and then appending them to the end of the list results in a list 10->9->3->9->4->5->1->4. +
+

Example 2:

+
Input:
+LinkedList = 1->2->3->4->5
+Output: 
1 3 5 4 2  +Explanation:
Alternative nodes in the given linked list are 2 and 4. Reversing the alternative nodes from the given list, and then appending them to the end of the list results in a list 1->3->5->4->2.
+

Your Task:
You don't have to read input or print anything. Your task is to complete the function rearrange() which takes the head of the linked list as input and rearranges the list as required.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 105
0 <= Node_value <= 109

\ No newline at end of file diff --git a/Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp b/Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp new file mode 100644 index 00000000..13df7d24 --- /dev/null +++ b/Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp @@ -0,0 +1,150 @@ +//{ Driver Code Starts +#include +#include +#include +using namespace std; +/* A linked list node */ + + +struct Node +{ + int data; + struct Node *next; + + Node(int x){ + data = x; + next = NULL; + } + +}; + +struct Node *start = NULL; + +/* Function to print nodes in a given linked list */ +void printList(struct Node *node) +{ + while (node != NULL) + { + printf("%d ", node->data); + node = node->next; + } + printf("\n"); + +} + +void insert() +{ + int n,value; + cin>>n; + struct Node *temp; + for(int i=0;i>value; + if(i==0) + { + start = new Node(value); + temp = start; + continue; + } + else + { + temp->next = new Node(value); + temp = temp->next; + } + } +} + + +// } Driver Code Ends +/* + reverse alternate nodes and append at the end + The input list will have at least one element + Node is defined as + struct Node + { + int data; + struct Node *next; + + Node(int x){ + data = x; + next = NULL; + } + + }; + +*/ +class Solution +{ + public: + + Node* reverse(struct Node* head) + { + Node* prev = nullptr; + + while(head) + { + Node* nextNode = head->next; + head->next = prev; + prev = head; + head = nextNode; + } + + return prev; + } + + void rearrange(struct Node *odd) + { + //add code here + + Node* dummy = new Node(0), *ptr = dummy; + + Node* temp = odd, *prev = nullptr; + + bool ok = false; + + while(temp) + { + if(ok) + { + prev->next = (temp->next ? temp->next : nullptr); + ptr ->next = temp; + ptr = ptr->next; + } + + if(!ok) + prev = temp; + + temp = temp->next; + + ok ^= 1; + } + + + if(ptr->next) + ptr->next = nullptr; + + Node* rev = reverse(dummy->next); + + + prev->next = rev; + + } +}; + + + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while (t--) { + insert(); + Solution ob; + ob.rearrange(start); + printList(start); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Sum of all divisors from 1 to n - GFG/README.md b/Sum of all divisors from 1 to n - GFG/README.md new file mode 100644 index 00000000..b0863e36 --- /dev/null +++ b/Sum of all divisors from 1 to n - GFG/README.md @@ -0,0 +1,32 @@ +# Sum of all divisors from 1 to n +## Easy +

Given a positive integer N., The task is to find the value of    \sum_{i=1}^{i=n} F(i)  where function F(i) for the number i is defined as the sum of all divisors of i.

+

Example 1:

+
Input:
+N = 4
+Output:
+15
+Explanation:
+F(1) = 1
+F(2) = 1 + 2 = 3
+F(3) = 1 + 3 = 4
+F(4) = 1 + 2 + 4 = 7
+ans = F(1) + F(2) + F(3) + F(4)
+    = 1 + 3 + 4 + 7
+    = 15
+

Example 2:

+
Input:
+N = 5
+Output:
+21
+Explanation:
+F(1) = 1
+F(2) = 1 + 2 = 3
+F(3) = 1 + 3 = 4
+F(4) = 1 + 2 + 4 = 7
+F(5) = 1 + 5 = 6
+ans = F(1) + F(2) + F(3) + F(4) + F(5)
+    = 1 + 3 + 4 + 7 + 6
+    = 21
+

Your Task:  
You don't need to read input or print anything. Your task is to complete the function sumOfDivisors() which takes an integer N as an input parameter and returns an integer.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 106

\ No newline at end of file diff --git a/Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp b/Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp new file mode 100644 index 00000000..324dcc03 --- /dev/null +++ b/Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp @@ -0,0 +1,40 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +//User function Template for C++ +class Solution +{ +public: + long long sumOfDivisors(int N) + { + // Write Your Code here + + long long ans = 0; + + for(int i = 1; i <= N; ++i) + { + ans += (i * (N/i)); + } + + return ans; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + cin >> t; + while (t--) + { + int N; + cin>>N; + Solution ob; + long long ans = ob.sumOfDivisors(N); + cout<

Given a directed graph, determine whether a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Here, vertex j is reachable from another vertex i means that there is a path from vertex i to j. The reachability matrix is called the transitive closure of a graph. The directed graph is represented by an adjacency matrix where there are N vertices. 

+

Example 1:

+
Input: N = 4
+graph = {{1, 1, 0, 1}, 
+         {0, 1, 1, 0}, 
+         {0, 0, 1, 1}, 
+         {0, 0, 0, 1}}
+Output: {{1, 1, 1, 1}, 
+         {0, 1, 1, 1}, 
+         {0, 0, 1, 1}, 
+         {0, 0, 0, 1}}
+Explanation: 
The output list shows the reachable indexes.
+

Example 2:

+
Input: N = 3
+graph = {{1, 0, 0}, 
+         {0, 1, 0}, 
+         {0, 0, 1}}
+Output: {{1, 0, 0}, 
+         {0, 1, 0}, 
+         {0, 0, 1}}
+Explanation: 
The output list shows the reachable indexes.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function transitiveClosure() which takes an integer N and a 2D array graph(adjacency matrix of the graph) as input parameters and returns the transitive closure of the graph in the form of 2D array.

+

Expected Time Complexity: O(N3)
Expected Auxiliary Space: O(N2)

+

Constraints:
1 ≤ N ≤ 100  

\ No newline at end of file diff --git a/Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp b/Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp new file mode 100644 index 00000000..84151909 --- /dev/null +++ b/Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp @@ -0,0 +1,75 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// Back-end complete function Template for C++ + +class Solution{ +public: + + void dfs(int node, int sv, vector adj[], vector& visited, vector>& res) + { + visited[sv] = true; + res[node][sv] = true; + + for(auto& itr : adj[sv]) + { + if(!visited[itr]) + dfs(node, itr, adj, visited, res); + } + } + + vector> transitiveClosure(int N, vector> graph) + { + // code here + + vector adj[N+1]; + + for(int i = 0; i < N; ++i) + { + for(int j = 0; j < N; ++j) + { + if(graph[i][j] == 1) + adj[i].push_back(j); + } + } + + vector> res(N, vector(N, 0)); + + for(int i = 0; i < N; ++i) + { + vector visited(N, false); + dfs(i, i, adj, visited, res); + } + + return res; + + } +}; + +//{ Driver Code Starts. + +int main(){ + int t; + cin>>t; + while(t--){ + int N; + cin>>N; + vector> graph(N, vector(N, -1)); + for(int i = 0;i < N;i++) + for(int j=0;j>graph[i][j]; + + Solution ob; + vector> ans = ob.transitiveClosure(N, graph); + for(int i = 0;i < N;i++) + {for(int j = 0;j < N;j++) + cout<

Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array(In Place).
In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....

+

If there are multiple solutions, find the lexicographically smallest one.

+

Note:The given array is sorted in ascending order, and you don't need to return anything to make changes in the original array itself.

+

Example 1:

+
Input:
+n = 5
+arr[] = {1,2,3,4,5}
+Output: 2 1 4 3 5
+Explanation: Array elements after 
+sorting it in wave form are 
+2 1 4 3 5.
+

Example 2:

+
Input:
+n = 6
+arr[] = {2,4,7,8,9,10}
+Output: 4 2 8 7 10 9
+Explanation: Array elements after 
+sorting it in wave form are 
+4 2 8 7 10 9.
+

Your Task:
The task is to complete the function convertToWave(), which converts the given array to a wave array.

+

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).

+

Constraints:
1 ≤ n ≤ 106
0 ≤ arr[i] ≤107

\ No newline at end of file diff --git a/Wave Array - GFG/wave-array.cpp b/Wave Array - GFG/wave-array.cpp new file mode 100644 index 00000000..21ee8fe1 --- /dev/null +++ b/Wave Array - GFG/wave-array.cpp @@ -0,0 +1,47 @@ +//{ Driver Code Starts +#include +using namespace std; +// #include + + +// } Driver Code Ends +class Solution{ + public: + // arr: input array + // n: size of array + //Function to sort the array into a wave-like array. + void convertToWave(int n, vector& arr){ + + // Your code here + + for(int i = 0; i < n-1; i += 2) + { + swap(arr[i], arr[i+1]); + } + + } +}; + +//{ Driver Code Starts. + +int main() +{ + int t,n; + cin>>t; //Input testcases + while(t--) //While testcases exist + { + cin>>n; //input size of array + vector a(n); //declare vector of size n + for(int i=0;i>a[i]; //input elements of array + sort(a.begin(),a.end()); + Solution ob; + ob.convertToWave(n, a); + + for(int i=0;i