From 3dcaa6345a9d44c007c78e6fc07d47c4c105f0e1 Mon Sep 17 00:00:00 2001 From: VICKY Date: Sat, 30 Jul 2022 22:29:37 +0530 Subject: [PATCH 0001/3167] Solution as on 30-07-2022 10:30 pm --- 0890. Find and Replace Pattern.cpp | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0890. Find and Replace Pattern.cpp diff --git a/0890. Find and Replace Pattern.cpp b/0890. Find and Replace Pattern.cpp new file mode 100644 index 00000000..1822e4ef --- /dev/null +++ b/0890. Find and Replace Pattern.cpp @@ -0,0 +1,46 @@ +// 890.✅ Find and Replace Pattern + +class Solution +{ +public: + vector found_Pattern(string pattern) + { + if (pattern.size() == 0) + return {}; + + vector v; + int ind = 0; + unordered_map mp; + for (int i = 0; i < pattern.size(); ++i) + { + if (mp.find(pattern[i]) == mp.end()) + { + mp.insert({pattern[i], ind++}); + v.push_back(mp[pattern[i]]); + } + else + { + v.push_back(mp[pattern[i]]); + } + } + return v; + } + vector findAndReplacePattern(vector &words, string pattern) + { + + vector v = found_Pattern(pattern); + + int n = words.size(); + vector ans; + + for (int i = 0; i < n; ++i) + { + vector pattern_word = found_Pattern(words[i]); + + if (v == pattern_word) + ans.push_back(words[i]); + } + + return ans; + } +}; \ No newline at end of file From 1293ec295e7ff49c25a48c691bed0c6751cb56b9 Mon Sep 17 00:00:00 2001 From: VICKY Date: Sun, 31 Jul 2022 22:47:08 +0530 Subject: [PATCH 0002/3167] Solution as on 31-07-2022 10:45 pm --- 0916. Word Subsets.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 0916. Word Subsets.cpp diff --git a/0916. Word Subsets.cpp b/0916. Word Subsets.cpp new file mode 100644 index 00000000..06d5eef2 --- /dev/null +++ b/0916. Word Subsets.cpp @@ -0,0 +1,98 @@ +// 916. Word Subsets + +class Solution +{ +public: + bool check(unordered_map m, unordered_map mp, string str) + { + for (int i = 0; i < str.size(); ++i) + { + if (mp[str[i]] < m[str[i]]) + return false; + } + return true; + } + + vector wordSubsets(vector &words1, vector &words2) + { + + vector ans; + + unordered_map m; + for (auto itr : words2) + { + unordered_map m1; + for (auto x : itr) + { + ++m1[x]; + m[x] = max(m[x], m1[x]); + } + } + + string str = ""; + for (auto itr : m) + { + for (int i = 0; i < itr.second; ++i) + str += itr.first; + } + + cout << str; + + // unordered_map mp = generate(str); + + for (int i = 0; i < words1.size(); ++i) + { + unordered_map mp; + for (auto itr : words1[i]) + ++mp[itr]; + + if (check(m, mp, str)) + ans.push_back(words1[i]); + } + + return ans; + } +}; + +class Solution +{ +public: + vector wordSubsets(vector &words1, vector &words2) + { + + vector freq(26, 0); + + for (auto itr : words2) + { + vector temp(26, 0); + for (auto x : itr) + { + ++temp[x - 'a']; + freq[x - 'a'] = max(temp[x - 'a'], freq[x - 'a']); + } + } + + vector ans; + + for (auto itr : words1) + { + vector temp(26, 0); + for (auto x : itr) + ++temp[x - 'a']; + + bool flag = true; + + for (int i = 0; i < 26; ++i) + { + if (freq[i] > temp[i]) + { + flag = false; + break; + } + } + if (flag) + ans.push_back(itr); + } + return ans; + } +}; From 62cd981e49833ed06b08f2f0c9adcc73ba5da6a1 Mon Sep 17 00:00:00 2001 From: VICKY Date: Wed, 3 Aug 2022 22:41:47 +0530 Subject: [PATCH 0003/3167] Solution as on 03-08-2022 10:45 pm --- ...rray Zero by Subtracting Equal Amounts.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2357. Make Array Zero by Subtracting Equal Amounts.cpp diff --git a/2357. Make Array Zero by Subtracting Equal Amounts.cpp b/2357. Make Array Zero by Subtracting Equal Amounts.cpp new file mode 100644 index 00000000..0ffa5c9b --- /dev/null +++ b/2357. Make Array Zero by Subtracting Equal Amounts.cpp @@ -0,0 +1,38 @@ +// 2357. Make Array Zero by Subtracting Equal Amounts + +class Solution +{ +public: + int minimumOperations(vector &nums) + { + + int count = 0; + sort(nums.begin(), nums.end()); + + for (int i = 0; i < nums.size(); ++i) + { + if (nums[i] == 0) + continue; + else + { + int a = nums[i]; + for (int j = i; j < nums.size(); ++j) + { + // nums[j] -= nums[i]; + nums[j] -= a; + } + + cout << endl; + + for (auto itr : nums) + cout << itr << " "; + + ++count; + + cout << endl; + cout << count; + } + } + return count; + } +}; \ No newline at end of file From c26400f549021418809d292e65c0dcdd8d1c844c Mon Sep 17 00:00:00 2001 From: VICKY Date: Thu, 4 Aug 2022 23:10:30 +0530 Subject: [PATCH 0004/3167] Solution as on 04-08-2022 11:00 pm --- ...umber of Groups Entering a Competition.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2358. Maximum Number of Groups Entering a Competition.cpp diff --git a/2358. Maximum Number of Groups Entering a Competition.cpp b/2358. Maximum Number of Groups Entering a Competition.cpp new file mode 100644 index 00000000..b39b012b --- /dev/null +++ b/2358. Maximum Number of Groups Entering a Competition.cpp @@ -0,0 +1,21 @@ +// 2358.✅ Maximum Number of Groups Entering a Competition + +class Solution +{ +public: + int maximumGroups(vector &grades) + { + + int n = grades.size(); + int count = 0; + + int i = 1; + while (n > 0) + { + n -= i++; + ++count; + } + + return n == 0 ? count : count - 1; + } +}; \ No newline at end of file From 2f24e10b2311c63d7f70d38ebade7dbaa951f766 Mon Sep 17 00:00:00 2001 From: VICKY Date: Fri, 5 Aug 2022 22:03:26 +0530 Subject: [PATCH 0005/3167] Solution as on 05-08-2022 10:00 pm --- 0307. Range Sum Query - Mutable.cpp | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 0307. Range Sum Query - Mutable.cpp diff --git a/0307. Range Sum Query - Mutable.cpp b/0307. Range Sum Query - Mutable.cpp new file mode 100644 index 00000000..ffd454f5 --- /dev/null +++ b/0307. Range Sum Query - Mutable.cpp @@ -0,0 +1,127 @@ +class NumArray +{ +public: + int arr[100005], seg[100005 * 4]; // for segment Tree + vector ans; + + void build(int ind, int low, int high) + { + if (high == low) + { + seg[ind] = arr[low]; + return; + } + int mid = (low + high) / 2; + build(2 * ind + 1, low, mid); + build(2 * ind + 2, mid + 1, high); + + // find max in range + // seg[ind] = max(seg[2 * ind + 1],seg[2 * ind + 2]); + + // find sum in range + seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2]; + } + + int query(int ind, int low, int high, int l, int r) + { + if (low >= l && high <= r) // [ 1 - 1 ] + return seg[ind]; + + if (high < l || low > r) + // return INT_MIN; ( when we doing for max) + return 0; + + int mid = (low + high) / 2; + int left = query(2 * ind + 1, low, mid, l, r); + int right = query(2 * ind + 2, mid + 1, high, l, r); + // return max(left, right); + return left + right; + } + + void updateSeg(int idx, int low, int high, int index, int val) + { + // no overlap + if (index < low || index > high) + return; + + // total overlap + if (low == high) + { + if (low == index) + seg[idx] = val; + return; + } + + // partial overlap + int mid = (low + high) / 2; + updateSeg(2 * idx + 1, low, mid, index, val); // low child + updateSeg(2 * idx + 2, mid + 1, high, index, val); // high child + seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; + } + + NumArray(vector &nums) + { + ans = nums; + for (int i = 0; i < nums.size(); ++i) + arr[i] = nums[i]; + build(0, 0, nums.size() - 1); + } + + void update(int index, int val) + { + updateSeg(0, 0, ans.size() - 1, index, val); + } + + int sumRange(int left, int right) + { + return query(0, 0, ans.size() - 1, left, right); + } +}; + +/** + * Your NumArray object will be instantiated and called as such: + * NumArray* obj = new NumArray(nums); + * obj->update(index,val); + * int param_2 = obj->sumRange(left,right); + */ + +// Another Approach + +class NumArray +{ +public: + vector ans; + int sum = 0; + NumArray(vector &nums) + { + sum = 0; + ans = nums; + for (int i = 0; i < nums.size(); ++i) + { + sum += nums[i]; + } + } + + void update(int index, int val) + { + sum -= ans[index]; + ans[index] = val; + sum += val; + } + + int sumRange(int left, int right) + { + int res = sum; + + for (int i = 0; i < left; ++i) + { + res -= ans[i]; + } + for (int i = right + 1; i < ans.size(); ++i) + { + res -= ans[i]; + } + + return res; + } +}; From ec447beab3b82cc06a8ab8bd7a6cff3454eaf26c Mon Sep 17 00:00:00 2001 From: VICKY Date: Sat, 6 Aug 2022 22:37:53 +0530 Subject: [PATCH 0006/3167] Solution as on 06-08-2022 10:40 pm --- 0377. Combination Sum IV.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0377. Combination Sum IV.cpp diff --git a/0377. Combination Sum IV.cpp b/0377. Combination Sum IV.cpp new file mode 100644 index 00000000..69579857 --- /dev/null +++ b/0377. Combination Sum IV.cpp @@ -0,0 +1,30 @@ +// 377.✅ Combination Sum IV + +class Solution +{ +public: + vector dp; + + Solution() + { + dp.resize(1001); + fill(dp.begin(), dp.end(), -1); + } + + int combinationSum4(vector &nums, int target, int currSum = 0) + { + if (currSum > target) + return 0; + if (currSum == target) + return 1; + if (dp[currSum] != -1) + return dp[currSum]; + int ways = 0; + for (int i = 0; i < nums.size(); i++) + { + if (currSum + nums[i] <= target) + ways += combinationSum4(nums, target, currSum + nums[i]); + } + return dp[currSum] = ways; + } +}; \ No newline at end of file From 2c958e00ac3afb9883cda1fd1c20b6de3d731f2b Mon Sep 17 00:00:00 2001 From: VICKY Date: Sun, 14 Aug 2022 23:18:19 +0530 Subject: [PATCH 0007/3167] Solution as on 14-08-2022 11:20 pm --- 0858. Mirror Reflection.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0858. Mirror Reflection.cpp diff --git a/0858. Mirror Reflection.cpp b/0858. Mirror Reflection.cpp new file mode 100644 index 00000000..3bcadbf0 --- /dev/null +++ b/0858. Mirror Reflection.cpp @@ -0,0 +1,15 @@ +// 858.✅ Mirror Reflection + +class Solution +{ +public: + int mirrorReflection(int p, int q) + { + while (p % 2 == 0 && q % 2 == 0) + { + p /= 2; + q /= 2; + } + return 1 - p % 2 + q % 2; + } +}; \ No newline at end of file From 7d41c277fd39170b6445ab2efbde7c674bbb0823 Mon Sep 17 00:00:00 2001 From: VICKY Date: Fri, 19 Aug 2022 23:56:49 +0530 Subject: [PATCH 0008/3167] Solution as on 19-08-2022 11:56 pm --- 0458. Poor Pigs.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 0458. Poor Pigs.cpp diff --git a/0458. Poor Pigs.cpp b/0458. Poor Pigs.cpp new file mode 100644 index 00000000..f9d748de --- /dev/null +++ b/0458. Poor Pigs.cpp @@ -0,0 +1,10 @@ +// 458.✅ Poor Pigs + +class Solution +{ +public: + int poorPigs(int buckets, int poisonTime, int totalTime) + { + return ceil(log(buckets) / log(totalTime / poisonTime + 1)); + } +}; \ No newline at end of file From 1bbe5b62fad7598c97b32bfbd4b7b295029c4b0e Mon Sep 17 00:00:00 2001 From: VICKY Date: Sat, 20 Aug 2022 22:21:39 +0530 Subject: [PATCH 0009/3167] Solution as on 20-08-2022 10:21 pm --- 2363. Merge Similar Items.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2363. Merge Similar Items.cpp diff --git a/2363. Merge Similar Items.cpp b/2363. Merge Similar Items.cpp new file mode 100644 index 00000000..58306501 --- /dev/null +++ b/2363. Merge Similar Items.cpp @@ -0,0 +1,34 @@ +// 2363.✅ Merge Similar Items + +class Solution +{ +public: + vector> mergeSimilarItems(vector> &items1, vector> &items2) + { + + map mp; + + for (int i = 0; i < items1.size(); ++i) + { + mp.insert({items1[i][0], items1[i][1]}); + } + + for (int i = 0; i < items2.size(); ++i) + { + if (mp[items2[i][0]] >= 0) + { + mp[items2[i][0]] += items2[i][1]; + } + else + mp.insert({items2[i][0], items2[1][1]}); + } + + vector> ans; + for (auto it = mp.cbegin(); it != mp.cend(); ++it) + { + ans.push_back({it->first, it->second}); + } + + return ans; + } +}; \ No newline at end of file From ab50a1bf4b4f6dd775095b01ea1c467078af9767 Mon Sep 17 00:00:00 2001 From: VICKY Date: Wed, 31 Aug 2022 23:50:07 +0530 Subject: [PATCH 0010/3167] Solution as on 31-08-2022 11:50 pm --- 1220. Count Vowels Permutation.cpp | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 1220. Count Vowels Permutation.cpp diff --git a/1220. Count Vowels Permutation.cpp b/1220. Count Vowels Permutation.cpp new file mode 100644 index 00000000..01d3b860 --- /dev/null +++ b/1220. Count Vowels Permutation.cpp @@ -0,0 +1,57 @@ +// 1220.✅ Count Vowels Permutation + +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 From a1055204e720c9323ce4258fcf293c63d445028b Mon Sep 17 00:00:00 2001 From: VICKY Date: Sun, 4 Sep 2022 21:22:17 +0530 Subject: [PATCH 0011/3167] Solution as on 04-09-2022 09:20 pm --- 2367. Number of Arithmetic Triplets.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2367. Number of Arithmetic Triplets.cpp diff --git a/2367. Number of Arithmetic Triplets.cpp b/2367. Number of Arithmetic Triplets.cpp new file mode 100644 index 00000000..425a4dd7 --- /dev/null +++ b/2367. Number of Arithmetic Triplets.cpp @@ -0,0 +1,22 @@ +// 2367.✅ Number of Arithmetic Triplets + +class Solution +{ +public: + int arithmeticTriplets(vector &nums, int diff) + { + int count = 0; + for (int i = 0; i < nums.size(); ++i) + { + for (int j = i + 1; j < nums.size(); ++j) + { + for (int k = j + 1; k < nums.size(); ++k) + { + if (nums[j] - nums[i] == diff && nums[k] - nums[j] == diff) + ++count; + } + } + } + return count; + } +}; \ No newline at end of file From 7d97c411b1136703fc4b5015fbb4727ec90ccb84 Mon Sep 17 00:00:00 2001 From: VICKY Date: Mon, 12 Sep 2022 23:34:09 +0530 Subject: [PATCH 0012/3167] Solution as on 12-09-2022 09:30 pm --- 2368. Reachable Nodes With Restrictions.cpp | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 2368. Reachable Nodes With Restrictions.cpp diff --git a/2368. Reachable Nodes With Restrictions.cpp b/2368. Reachable Nodes With Restrictions.cpp new file mode 100644 index 00000000..478338d5 --- /dev/null +++ b/2368. Reachable Nodes With Restrictions.cpp @@ -0,0 +1,65 @@ +// 2368.✅ Reachable Nodes With Restrictions + +class Solution +{ +public: + void adjacencylist(vector adj[], int V) + { + for (int i = 0; i < V; i++) + { + cout << i << "->"; + for (int &x : adj[i]) + { + cout << x << " "; + } + cout << endl; + } + } + + void dfsOfGraph(int sv, vector &visited, vector adj[]) + { + visited[sv] = true; + + for (auto itr : adj[sv]) + { + if (!visited[itr]) + dfsOfGraph(itr, visited, adj); + } + } + + int reachableNodes(int n, vector> &edges, vector &restricted) + { + + vector graph[n + 1]; + for (int i = 0; i < edges.size(); ++i) + { + graph[edges[i][0]].push_back(edges[i][1]); + graph[edges[i][1]].push_back(edges[i][0]); + } + + vector visited(n + 1, 0); + int count = 0; + for (int i = 0; i < restricted.size(); ++i) + { + visited[restricted[i]] = true; + } + + // for(auto itr : visited) + // cout< Date: Mon, 12 Sep 2022 23:35:05 +0530 Subject: [PATCH 0013/3167] Solution as on 12-09-2022 11:30 pm --- ...ere is a Valid Partition For The Array.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2369. Check if There is a Valid Partition For The Array.cpp diff --git a/2369. Check if There is a Valid Partition For The Array.cpp b/2369. Check if There is a Valid Partition For The Array.cpp new file mode 100644 index 00000000..dc783b5e --- /dev/null +++ b/2369. Check if There is a Valid Partition For The Array.cpp @@ -0,0 +1,27 @@ +class Solution +{ + // 2369.✅ Check if There is a Valid Partition For The Array + +public: + bool validPartition(vector &nums) + { + int n = nums.size(); + vector dp(n + 1); + + dp[0] = 1; + + for (int i = 0; i < n; ++i) + { + if (dp[i]) + { + if (i + 1 < n && nums[i] == nums[i + 1]) + dp[i + 2] = true; + if (i + 2 < n && nums[i] == nums[i + 1] && nums[i] == nums[i + 2]) + dp[i + 3] = true; + if (i + 2 < n && nums[i] == nums[i + 1] - 1 && nums[i] == nums[i + 2] - 2) + dp[i + 3] = true; + } + } + return dp[n]; + } +}; \ No newline at end of file From 043756f20430ce63c5e7586a3c12740c598302f7 Mon Sep 17 00:00:00 2001 From: VICKY Date: Mon, 12 Sep 2022 23:36:04 +0530 Subject: [PATCH 0014/3167] Solution as on 12-09-2022 11:30 pm --- 2365. Task Scheduler II.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2365. Task Scheduler II.cpp diff --git a/2365. Task Scheduler II.cpp b/2365. Task Scheduler II.cpp new file mode 100644 index 00000000..8e92ef0d --- /dev/null +++ b/2365. Task Scheduler II.cpp @@ -0,0 +1,28 @@ +// 2365.✅ Task Scheduler II + +class Solution +{ +public: + long long taskSchedulerII(vector &tasks, int space) + { + map mp; + long long day = 0; + + for (int i = 0; i < tasks.size(); ++i) + { + if (mp.find(tasks[i]) == mp.end()) + { + ++day; + mp[tasks[i]] = day; + continue; + } + + long long last_day = mp[tasks[i]]; + + day = max(day + 1, last_day + space + 1); + + mp[tasks[i]] = day; + } + return day; + } +}; \ No newline at end of file From 202166f8a83624fe60ecd25dc2dcf13be6da660b Mon Sep 17 00:00:00 2001 From: VICKY Date: Fri, 30 Sep 2022 23:32:57 +0530 Subject: [PATCH 0015/3167] Solution as on 30-09-2022 11:32 pm --- 0823. Binary Trees With Factors.cpp | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0823. Binary Trees With Factors.cpp diff --git a/0823. Binary Trees With Factors.cpp b/0823. Binary Trees With Factors.cpp new file mode 100644 index 00000000..80674a71 --- /dev/null +++ b/0823. Binary Trees With Factors.cpp @@ -0,0 +1,48 @@ +// 823.✅ Binary Trees With Factors + +class Solution +{ +public: + int numFactoredBinaryTrees(vector &arr) + { + + int n = arr.size(); + + long long int mod = 1e9 + 7; + + map mp; + + long long int sum = 0; + + sort(arr.begin(), arr.end()); + + for (int i = 0; i < n; ++i) + mp.insert({arr[i], 1}); + + for (int i = 0; i < n; ++i) + { + long long int count = 0; + + auto itr1 = mp.find(arr[i]); + + for (int j = 0; j < i; ++j) + { + if (arr[i] % arr[j] == 0) + { + if (mp[arr[i] / arr[j]]) + { + count += mp[arr[j]] % mod * mp[arr[i] / arr[j]] % mod; + } + } + } + mp[arr[i]] += count; + } + + for (auto itr : mp) + { + sum += itr.second; + } + + return sum % mod; + } +}; \ No newline at end of file From ff7ada174d9e45cd6165e4512151bb28e6f6bf86 Mon Sep 17 00:00:00 2001 From: VICKY Date: Thu, 14 Oct 2021 23:45:37 +0530 Subject: [PATCH 0016/3167] Solution as on 30-09-2022 11:45 pm --- 0098. Validate Binary Search Tree.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0098. Validate Binary Search Tree.cpp diff --git a/0098. Validate Binary Search Tree.cpp b/0098. Validate Binary Search Tree.cpp new file mode 100644 index 00000000..861586eb --- /dev/null +++ b/0098. Validate Binary Search Tree.cpp @@ -0,0 +1,21 @@ +// 98.✅ Validate Binary Search Tree + +class Solution +{ +public: + bool helper(TreeNode *root, long mn, long mx) + { + if (!root) + return true; + + if (root->val <= mn || root->val >= mx) + return false; + + return helper(root->left, mn, root->val) && helper(root->right, root->val, mx); + } + + bool isValidBST(TreeNode *root) + { + return helper(root, LONG_MIN, LONG_MAX); + } +}; From d1406fbaa9eccfb455d49bd4ee21c74b52e5c12a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:05:41 +0530 Subject: [PATCH 0017/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0446-arithmetic-slices-ii-subsequence/README.md diff --git a/0446-arithmetic-slices-ii-subsequence/README.md b/0446-arithmetic-slices-ii-subsequence/README.md new file mode 100644 index 00000000..af5ccfdd --- /dev/null +++ b/0446-arithmetic-slices-ii-subsequence/README.md @@ -0,0 +1,47 @@ +

446. Arithmetic Slices II - Subsequence

Hard


Given an integer array nums, return the number of all the arithmetic subsequences of nums.

+ +

A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

+ +
    +
  • For example, [1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences.
  • +
  • For example, [1, 1, 2, 5, 7] is not an arithmetic sequence.
  • +
+ +

A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.

+ +
    +
  • For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].
  • +
+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [2,4,6,8,10]
+Output: 7
+Explanation: All arithmetic subsequence slices are:
+[2,4,6]
+[4,6,8]
+[6,8,10]
+[2,4,6,8]
+[4,6,8,10]
+[2,4,6,8,10]
+[2,6,10]
+
+ +

Example 2:

+ +
Input: nums = [7,7,7,7,7]
+Output: 16
+Explanation: Any subsequence of this array is arithmetic.
+
+ +

 

+

Constraints:

+ +
    +
  • 1  <= nums.length <= 1000
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
+
\ No newline at end of file From 7b58484d47ec3107b1bde01ec83eccb8885aae83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:05:41 +0530 Subject: [PATCH 0018/3167] Attach NOTES - LeetHub --- 0446-arithmetic-slices-ii-subsequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0446-arithmetic-slices-ii-subsequence/NOTES.md diff --git a/0446-arithmetic-slices-ii-subsequence/NOTES.md b/0446-arithmetic-slices-ii-subsequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0446-arithmetic-slices-ii-subsequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4ba68168279bdc0da2aba7fca83f947ba05a774d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:05:45 +0530 Subject: [PATCH 0019/3167] Time: 1308 ms (61.32%), Space: 154.8 MB (51.65%) - LeetHub --- .../0446-arithmetic-slices-ii-subsequence.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp diff --git a/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp b/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp new file mode 100644 index 00000000..4d37934b --- /dev/null +++ b/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int numberOfArithmeticSlices(vector& nums) { + int n = nums.size(); + int ans = 0; + + vector> dp(n); + for(int i = 1; i Date: Sun, 27 Nov 2022 23:07:32 +0530 Subject: [PATCH 0020/3167] Attach NOTES - LeetHub From 7e7f9cf79e4b1849b16f095eb0a7ebfa80d109e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:07:35 +0530 Subject: [PATCH 0021/3167] Time: 1308 ms (61.32%), Space: 154.8 MB (51.65%) - LeetHub From ac3b87c66559bed44ffef13dbc43be4be06f7364 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:12:31 +0530 Subject: [PATCH 0022/3167] Attach NOTES - LeetHub From 667646b48193e02d7a7cf4fb7103b746c53252b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:12:34 +0530 Subject: [PATCH 0023/3167] Time: 1317 ms (59.54%), Space: 154.6 MB (60.56%) - LeetHub --- .../0446-arithmetic-slices-ii-subsequence.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp b/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp index 4d37934b..db1219f9 100644 --- a/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp +++ b/0446-arithmetic-slices-ii-subsequence/0446-arithmetic-slices-ii-subsequence.cpp @@ -4,10 +4,11 @@ class Solution { int n = nums.size(); int ans = 0; - vector> dp(n); - for(int i = 1; i> dp(n); + + for(int i = 1; i < n; ++i) { - for(int j = 0; j Date: Mon, 28 Nov 2022 08:48:43 +0530 Subject: [PATCH 0024/3167] Attach NOTES - LeetHub --- 0279-perfect-squares/NOTES.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0279-perfect-squares/NOTES.md diff --git a/0279-perfect-squares/NOTES.md b/0279-perfect-squares/NOTES.md new file mode 100644 index 00000000..54e85b40 --- /dev/null +++ b/0279-perfect-squares/NOTES.md @@ -0,0 +1,22 @@ +**Recursion + Memoization** +​ +class Solution { +public: +int helper(int n, vector& dp){ +if(n <= 3) +return n; +if(dp[n] != -1) +return dp[n]; +int ans = n; +for(int i = 1; i*i <= n; ++i){ +ans = min(ans, 1 + helper(n - (i*i), dp)); +dp[n] = ans; +} +return ans; +} +int numSquares(int n) { +vector dp(n+1,-1); +int ans = helper(n,dp); +return ans; +} +}; \ No newline at end of file From 79dbf6ed9025a4d104dc88e92fb535cc25e37be8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 08:48:46 +0530 Subject: [PATCH 0025/3167] Time: 289 ms (56.66%), Space: 9.6 MB (28.42%) - LeetHub --- 0279-perfect-squares/0279-perfect-squares.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0279-perfect-squares/0279-perfect-squares.cpp diff --git a/0279-perfect-squares/0279-perfect-squares.cpp b/0279-perfect-squares/0279-perfect-squares.cpp new file mode 100644 index 00000000..316df162 --- /dev/null +++ b/0279-perfect-squares/0279-perfect-squares.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + + int helper(int n, vector& dp){ + + if(n <= 3) + return n; + if(dp[n] != -1) + return dp[n]; + + int ans = n; + for(int i = 1; i*i <= n; ++i){ + ans = min(ans, 1 + helper(n - (i*i), dp)); + dp[n] = ans; + } + return ans; + } + + int numSquares(int n) { + + vector dp(n+1,-1); + int ans = helper(n,dp); + return ans; + + } +}; \ No newline at end of file From efd46c76af89231616287c62a5f5dec26f04eb6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 08:53:01 +0530 Subject: [PATCH 0026/3167] Attach NOTES - LeetHub --- 0279-perfect-squares/NOTES.md | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/0279-perfect-squares/NOTES.md b/0279-perfect-squares/NOTES.md index 54e85b40..5ec7be84 100644 --- a/0279-perfect-squares/NOTES.md +++ b/0279-perfect-squares/NOTES.md @@ -1,22 +1,25 @@ -**Recursion + Memoization** +return ans; +} +}; +​ +**Bottom Up** ​ +``` class Solution { public: -int helper(int n, vector& dp){ -if(n <= 3) -return n; -if(dp[n] != -1) -return dp[n]; -int ans = n; -for(int i = 1; i*i <= n; ++i){ -ans = min(ans, 1 + helper(n - (i*i), dp)); -dp[n] = ans; -} -return ans; -} int numSquares(int n) { vector dp(n+1,-1); -int ans = helper(n,dp); -return ans; +dp[0] = 0; +for(int i = 1; i<=n; ++i) +{ +dp[i] = i; +for(int j = 1; j*j <= i; ++j) +{ +int sq = j*j; +dp[i] = min(dp[i], 1 + dp[i-sq]); +} +} +return dp[n]; } -}; \ No newline at end of file +}; +``` \ No newline at end of file From c47282c62df7ea7bbe644a451d7589198888aa63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 08:53:04 +0530 Subject: [PATCH 0027/3167] Time: 246 ms (62.77%), Space: 9.1 MB (63.70%) - LeetHub --- 0279-perfect-squares/0279-perfect-squares.cpp | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/0279-perfect-squares/0279-perfect-squares.cpp b/0279-perfect-squares/0279-perfect-squares.cpp index 316df162..c00ae994 100644 --- a/0279-perfect-squares/0279-perfect-squares.cpp +++ b/0279-perfect-squares/0279-perfect-squares.cpp @@ -1,26 +1,20 @@ class Solution { public: - - int helper(int n, vector& dp){ - - if(n <= 3) - return n; - if(dp[n] != -1) - return dp[n]; - - int ans = n; - for(int i = 1; i*i <= n; ++i){ - ans = min(ans, 1 + helper(n - (i*i), dp)); - dp[n] = ans; - } - return ans; - } - int numSquares(int n) { vector dp(n+1,-1); - int ans = helper(n,dp); - return ans; + dp[0] = 0; + + for(int i = 1; i<=n; ++i) + { + dp[i] = i; + for(int j = 1; j*j <= i; ++j) + { + int sq = j*j; + dp[i] = min(dp[i], 1 + dp[i-sq]); + } + } + return dp[n]; } }; \ No newline at end of file From 4a4b2441af898237bfc0a0158488f210f5249d4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 08:58:58 +0530 Subject: [PATCH 0028/3167] Attach NOTES - LeetHub --- 0279-perfect-squares/NOTES.md | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/0279-perfect-squares/NOTES.md b/0279-perfect-squares/NOTES.md index 5ec7be84..9cbc5803 100644 --- a/0279-perfect-squares/NOTES.md +++ b/0279-perfect-squares/NOTES.md @@ -1,14 +1,3 @@ -return ans; -} -}; -​ -**Bottom Up** -​ -``` -class Solution { -public: -int numSquares(int n) { -vector dp(n+1,-1); dp[0] = 0; for(int i = 1; i<=n; ++i) { @@ -22,4 +11,23 @@ dp[i] = min(dp[i], 1 + dp[i-sq]); return dp[n]; } }; -``` \ No newline at end of file +``` +​ +**Legendres's Theorem** +class Solution { +public: +int numSquares(int n) { +if(ceil(sqrt(n)) == floor(sqrt(n))) +return 1; +while(n%4 == 0) +n /= 4; +if(n%8 == 7) +return 4; +for(int i = 1; i*i <= n; ++i){ +int base = sqrt(n - (i*i)); +if((base * base) == (n - (i*i))) +return 2; +} +return 3; +} +}; \ No newline at end of file From 5510a4f4952a351d553ebb2afcda2908f7f6be58 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 08:59:01 +0530 Subject: [PATCH 0029/3167] Time: 0 ms (100.00%), Space: 6 MB (92.58%) - LeetHub --- 0279-perfect-squares/0279-perfect-squares.cpp | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/0279-perfect-squares/0279-perfect-squares.cpp b/0279-perfect-squares/0279-perfect-squares.cpp index c00ae994..3760af3c 100644 --- a/0279-perfect-squares/0279-perfect-squares.cpp +++ b/0279-perfect-squares/0279-perfect-squares.cpp @@ -2,19 +2,22 @@ class Solution { public: int numSquares(int n) { - vector dp(n+1,-1); + if(ceil(sqrt(n)) == floor(sqrt(n))) + return 1; - dp[0] = 0; + while(n%4 == 0) + n /= 4; - for(int i = 1; i<=n; ++i) - { - dp[i] = i; - for(int j = 1; j*j <= i; ++j) - { - int sq = j*j; - dp[i] = min(dp[i], 1 + dp[i-sq]); - } + if(n%8 == 7) + return 4; + + for(int i = 1; i*i <= n; ++i){ + int base = sqrt(n - (i*i)); + if((base * base) == (n - (i*i))) + return 2; } - return dp[n]; + + return 3; + } }; \ No newline at end of file From 931a3b89b72130f33dcaa64e7491ee59734cf3e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 09:03:01 +0530 Subject: [PATCH 0030/3167] Attach NOTES - LeetHub --- 0279-perfect-squares/NOTES.md | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/0279-perfect-squares/NOTES.md b/0279-perfect-squares/NOTES.md index 9cbc5803..a4bb801a 100644 --- a/0279-perfect-squares/NOTES.md +++ b/0279-perfect-squares/NOTES.md @@ -1,9 +1,3 @@ -dp[0] = 0; -for(int i = 1; i<=n; ++i) -{ -dp[i] = i; -for(int j = 1; j*j <= i; ++j) -{ int sq = j*j; dp[i] = min(dp[i], 1 + dp[i-sq]); } @@ -11,9 +5,9 @@ dp[i] = min(dp[i], 1 + dp[i-sq]); return dp[n]; } }; -``` ​ -**Legendres's Theorem** +Legendres's Theorem +​ class Solution { public: int numSquares(int n) { @@ -30,4 +24,5 @@ return 2; } return 3; } -}; \ No newline at end of file +}; +``` \ No newline at end of file From 6ddc6482966b40f1ae0f7a766b57783fa1129d44 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 09:03:04 +0530 Subject: [PATCH 0031/3167] Time: 6 ms (92.75%), Space: 5.9 MB (97.72%) - LeetHub From 5674aa6274d94debbdd79d311c447281522029a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:02:33 +0530 Subject: [PATCH 0032/3167] Create README - LeetHub --- 0907-sum-of-subarray-minimums/README.md | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0907-sum-of-subarray-minimums/README.md diff --git a/0907-sum-of-subarray-minimums/README.md b/0907-sum-of-subarray-minimums/README.md new file mode 100644 index 00000000..95fd00a1 --- /dev/null +++ b/0907-sum-of-subarray-minimums/README.md @@ -0,0 +1,27 @@ +

907. Sum of Subarray Minimums

Medium


Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: arr = [3,1,2,4]
+Output: 17
+Explanation: 
+Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
+Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
+Sum is 17.
+
+ +

Example 2:

+ +
Input: arr = [11,81,94,43,3]
+Output: 444
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 3 * 104
  • +
  • 1 <= arr[i] <= 3 * 104
  • +
+
\ No newline at end of file From 0c479441cb3b76dc2c0281799d738bced07c7dda Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:02:36 +0530 Subject: [PATCH 0033/3167] Time: 280 ms (20.08%), Space: 42.9 MB (36.86%) - LeetHub --- .../0907-sum-of-subarray-minimums.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp diff --git a/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp b/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp new file mode 100644 index 00000000..edd52636 --- /dev/null +++ b/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + int sumSubarrayMins(vector& arr) { + + int n = arr.size(); + long long ans = 0; + long long mod = (int) 1e9+7; + + vector next_smaller(n), prev_smaller(n); + stack st1, st2; + + for(int i = 0; i arr[i]) + { + next_smaller[st1.top()] = i - st1.top() - 1; + st1.pop(); + } + st1.push(i); + } + + for(int i = n-1; i>= 0; --i){ + while(!st2.empty() and arr[st2.top()] >= arr[i]) + { + prev_smaller[st2.top()] = st2.top() - i - 1; + st2.pop(); + } + st2.push(i); + } + + for(int i= 0; i Date: Mon, 28 Nov 2022 17:05:48 +0530 Subject: [PATCH 0034/3167] Attach NOTES - LeetHub --- 0907-sum-of-subarray-minimums/NOTES.md | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0907-sum-of-subarray-minimums/NOTES.md diff --git a/0907-sum-of-subarray-minimums/NOTES.md b/0907-sum-of-subarray-minimums/NOTES.md new file mode 100644 index 00000000..840bd83c --- /dev/null +++ b/0907-sum-of-subarray-minimums/NOTES.md @@ -0,0 +1,38 @@ +{ +mini = arr[j]; +ans += arr[j]; +ans %= mod; +} +else +{ +ans += mini; +ans %= mod; +} +} +} +return ans%mod; +} +}; +​ +O(n) +class Solution { +public: +int sumSubarrayMins(vector& arr) { +int n = arr.size(); +long long ans = 0; +long long mod = (int) 1e9+7; +vector next_smaller(n), prev_smaller(n); +stack st1, st2; +for(int i = 0; i arr[i]) +{ +next_smaller[st1.top()] = i - st1.top() - 1; +st1.pop(); +} +st1.push(i); \ No newline at end of file From c7ab18bb62610d778511cb23e65eb6d8cbad0f4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:05:51 +0530 Subject: [PATCH 0035/3167] Time: 284 ms (18.42%), Space: 43 MB (21.43%) - LeetHub --- .../0907-sum-of-subarray-minimums.cpp | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp b/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp index edd52636..a9cbb15f 100644 --- a/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp +++ b/0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp @@ -42,4 +42,62 @@ class Solution { return ans; } -}; \ No newline at end of file +}; + +/* + O(n^2) + class Solution { + public: + int sumSubarrayMins(vector& arr) { + + int n = arr.size(); + long long ans = 0; + int mod = 1e9 + 7; + + + for(int i = 0; i < n; ++i) + { + int mini = INT_MAX; + for(int j = i; j < n; ++j){ + if(arr[j] < mini) + { + mini = arr[j]; + ans += arr[j]; + ans %= mod; + } + else + { + ans += mini; + ans %= mod; + } + } + } + return ans%mod; + } +}; + +*/ + +/* + O(n^3) + + class Solution { + public: + int sumSubarrayMins(vector& arr) { + int sum =0 ,n = arr.size(); + for(int i=0;i Date: Mon, 28 Nov 2022 17:44:00 +0530 Subject: [PATCH 0036/3167] Create README - LeetHub --- .../README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2225-find-players-with-zero-or-one-losses/README.md diff --git a/2225-find-players-with-zero-or-one-losses/README.md b/2225-find-players-with-zero-or-one-losses/README.md new file mode 100644 index 00000000..7b31cde1 --- /dev/null +++ b/2225-find-players-with-zero-or-one-losses/README.md @@ -0,0 +1,51 @@ +

2225. Find Players With Zero or One Losses

Medium


You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.

+ +

Return a list answer of size 2 where:

+ +
    +
  • answer[0] is a list of all players that have not lost any matches.
  • +
  • answer[1] is a list of all players that have lost exactly one match.
  • +
+ +

The values in the two lists should be returned in increasing order.

+ +

Note:

+ +
    +
  • You should only consider the players that have played at least one match.
  • +
  • The testcases will be generated such that no two matches will have the same outcome.
  • +
+ +

 

+

Example 1:

+ +
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
+Output: [[1,2,10],[4,5,7,8]]
+Explanation:
+Players 1, 2, and 10 have not lost any matches.
+Players 4, 5, 7, and 8 each have lost one match.
+Players 3, 6, and 9 each have lost two matches.
+Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
+
+ +

Example 2:

+ +
Input: matches = [[2,3],[1,3],[5,4],[6,4]]
+Output: [[1,2,5,6],[]]
+Explanation:
+Players 1, 2, 5, and 6 have not lost any matches.
+Players 3 and 4 each have lost two matches.
+Thus, answer[0] = [1,2,5,6] and answer[1] = [].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= matches.length <= 105
  • +
  • matches[i].length == 2
  • +
  • 1 <= winneri, loseri <= 105
  • +
  • winneri != loseri
  • +
  • All matches[i] are unique.
  • +
+
\ No newline at end of file From 3e42e37ac7aeec98e864ac3af7f52a2d4567ddad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:44:01 +0530 Subject: [PATCH 0037/3167] Attach NOTES - LeetHub --- 2225-find-players-with-zero-or-one-losses/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2225-find-players-with-zero-or-one-losses/NOTES.md diff --git a/2225-find-players-with-zero-or-one-losses/NOTES.md b/2225-find-players-with-zero-or-one-losses/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2225-find-players-with-zero-or-one-losses/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 246ad4ca56d76eccd77f9309a8b5e7296eda1e72 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:44:04 +0530 Subject: [PATCH 0038/3167] Time: 1532 ms (30.13%), Space: 168 MB (68.19%) - LeetHub --- ...5-find-players-with-zero-or-one-losses.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2225-find-players-with-zero-or-one-losses/2225-find-players-with-zero-or-one-losses.cpp diff --git a/2225-find-players-with-zero-or-one-losses/2225-find-players-with-zero-or-one-losses.cpp b/2225-find-players-with-zero-or-one-losses/2225-find-players-with-zero-or-one-losses.cpp new file mode 100644 index 00000000..106472ac --- /dev/null +++ b/2225-find-players-with-zero-or-one-losses/2225-find-players-with-zero-or-one-losses.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + vector> findWinners(vector>& matches) { + + vector not_lost, one_lost; + vector> ans; + map win, loss; + int n = matches.size(); + + for(int i = 0; i Date: Mon, 28 Nov 2022 17:51:09 +0530 Subject: [PATCH 0039/3167] Attach NOTES - LeetHub From f98d47304bfbc81e18c7e309fe1d315e47c16517 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:51:12 +0530 Subject: [PATCH 0040/3167] Time: 1532 ms (30.13%), Space: 168 MB (68.19%) - LeetHub From 1e92bf7fee469ce1452821f1790582003af0400e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 18:07:49 +0530 Subject: [PATCH 0041/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0236-lowest-common-ancestor-of-a-binary-tree/README.md diff --git a/0236-lowest-common-ancestor-of-a-binary-tree/README.md b/0236-lowest-common-ancestor-of-a-binary-tree/README.md new file mode 100644 index 00000000..33fa84b0 --- /dev/null +++ b/0236-lowest-common-ancestor-of-a-binary-tree/README.md @@ -0,0 +1,36 @@ +

236. Lowest Common Ancestor of a Binary Tree

Medium


Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

+ +

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

+ +

 

+

Example 1:

+ +
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
+Output: 3
+Explanation: The LCA of nodes 5 and 1 is 3.
+
+ +

Example 2:

+ +
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
+Output: 5
+Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [2, 105].
  • +
  • -109 <= Node.val <= 109
  • +
  • All Node.val are unique.
  • +
  • p != q
  • +
  • p and q will exist in the tree.
  • +
+
\ No newline at end of file From 03b3f625d6470067540aa4a233a409180df83d1d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 18:07:50 +0530 Subject: [PATCH 0042/3167] Attach NOTES - LeetHub --- 0236-lowest-common-ancestor-of-a-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0236-lowest-common-ancestor-of-a-binary-tree/NOTES.md diff --git a/0236-lowest-common-ancestor-of-a-binary-tree/NOTES.md b/0236-lowest-common-ancestor-of-a-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0236-lowest-common-ancestor-of-a-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b81c97a2d4052cfbcd8933b8e318a7f4413bf812 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 18:07:53 +0530 Subject: [PATCH 0043/3167] Time: 18 ms (89.36%), Space: 14.4 MB (25.28%) - LeetHub --- ...owest-common-ancestor-of-a-binary-tree.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.cpp diff --git a/0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.cpp b/0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.cpp new file mode 100644 index 00000000..75cb67eb --- /dev/null +++ b/0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.cpp @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + + if(!root or root == p or root == q) + return root; + + TreeNode *left = lowestCommonAncestor(root->left,p,q); + TreeNode *right = lowestCommonAncestor(root->right,p,q); + + if(!left) + return right; + if(!right) + return left; + + return root; + } +}; \ No newline at end of file From 38c9c63dd78baa485a27155934d2704fcbbc0b23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 18:58:18 +0530 Subject: [PATCH 0044/3167] Create README - LeetHub --- 2487-remove-nodes-from-linked-list/README.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2487-remove-nodes-from-linked-list/README.md diff --git a/2487-remove-nodes-from-linked-list/README.md b/2487-remove-nodes-from-linked-list/README.md new file mode 100644 index 00000000..85da83fc --- /dev/null +++ b/2487-remove-nodes-from-linked-list/README.md @@ -0,0 +1,32 @@ +

2487. Remove Nodes From Linked List

Medium


You are given the head of a linked list.

+ +

Remove every node which has a node with a strictly greater value anywhere to the right side of it.

+ +

Return the head of the modified linked list.

+ +

 

+

Example 1:

+ +
Input: head = [5,2,13,3,8]
+Output: [13,8]
+Explanation: The nodes that should be removed are 5, 2 and 3.
+- Node 13 is to the right of node 5.
+- Node 13 is to the right of node 2.
+- Node 8 is to the right of node 3.
+
+ +

Example 2:

+ +
Input: head = [1,1,1,1]
+Output: [1,1,1,1]
+Explanation: Every node has value 1, so no nodes are removed.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the given list is in the range [1, 105].
  • +
  • 1 <= Node.val <= 105
  • +
+
\ No newline at end of file From 3ec50f28b2196237793b656224358582d8870adb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 18:58:21 +0530 Subject: [PATCH 0045/3167] Time: 490 ms (25.00%), Space: 162.5 MB (12.50%) - LeetHub --- .../2487-remove-nodes-from-linked-list.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp diff --git a/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp b/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp new file mode 100644 index 00000000..60cd865c --- /dev/null +++ b/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp @@ -0,0 +1,63 @@ +/** + * 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* rev(ListNode* head){ + ListNode* prev = nullptr, *curr = head; + while(curr != nullptr) + { + swap(curr->next,prev); + swap(prev,curr); + } + return prev; + } + + ListNode* reverseList(ListNode* head){ + ListNode* prev = nullptr, *curr = head; + while(curr != nullptr) + { + ListNode* temp = curr->next; + curr->next = prev; + prev = curr; + curr = temp; + } + return prev; + } + + void delete_Node(ListNode* head) + { + ListNode *maxNode = head, *curr = head; + + while(curr && curr->next) + { + if(curr->next->val < maxNode->val) + { + ListNode* temp = curr->next; + curr->next = temp->next; + delete(temp); + } + else + { + curr = curr->next; + maxNode = curr; + } + } + } + + ListNode* removeNodes(ListNode* head) { + + head = reverseList(head); + delete_Node(head); + head = reverseList(head); + return head; + } +}; \ No newline at end of file From 2a602dc2f6c90c045e3fd092edf7eb6f4c11b77b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 19:00:16 +0530 Subject: [PATCH 0046/3167] Attach NOTES - LeetHub --- 2487-remove-nodes-from-linked-list/NOTES.md | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2487-remove-nodes-from-linked-list/NOTES.md diff --git a/2487-remove-nodes-from-linked-list/NOTES.md b/2487-remove-nodes-from-linked-list/NOTES.md new file mode 100644 index 00000000..4ebb8157 --- /dev/null +++ b/2487-remove-nodes-from-linked-list/NOTES.md @@ -0,0 +1,25 @@ +void delete_Node(ListNode* head) +{ +ListNode *maxNode = head, *curr = head; +while(curr && curr->next) +{ +if(curr->next->val < maxNode->val) +{ +ListNode* temp = curr->next; +curr->next = temp->next; +delete(temp); +} +else +{ +curr = curr->next; +maxNode = curr; +} +} +} +ListNode* removeNodes(ListNode* head) { +head = reverseList(head); +delete_Node(head); +head = reverseList(head); +return head; +} +}; \ No newline at end of file From dc2a06d333b44d9f4b3264d960b6ff56754c0b30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Nov 2022 19:00:19 +0530 Subject: [PATCH 0047/3167] Time: 994 ms (12.50%), Space: 160.9 MB (12.50%) - LeetHub --- .../2487-remove-nodes-from-linked-list.cpp | 53 +++---------------- 1 file changed, 6 insertions(+), 47 deletions(-) diff --git a/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp b/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp index 60cd865c..6a24a27a 100644 --- a/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp +++ b/2487-remove-nodes-from-linked-list/2487-remove-nodes-from-linked-list.cpp @@ -10,54 +10,13 @@ */ class Solution { public: - - ListNode* rev(ListNode* head){ - ListNode* prev = nullptr, *curr = head; - while(curr != nullptr) - { - swap(curr->next,prev); - swap(prev,curr); - } - return prev; - } - - ListNode* reverseList(ListNode* head){ - ListNode* prev = nullptr, *curr = head; - while(curr != nullptr) - { - ListNode* temp = curr->next; - curr->next = prev; - prev = curr; - curr = temp; - } - return prev; - } - - void delete_Node(ListNode* head) - { - ListNode *maxNode = head, *curr = head; - - while(curr && curr->next) - { - if(curr->next->val < maxNode->val) - { - ListNode* temp = curr->next; - curr->next = temp->next; - delete(temp); - } - else - { - curr = curr->next; - maxNode = curr; - } - } - } - ListNode* removeNodes(ListNode* head) { - - head = reverseList(head); - delete_Node(head); - head = reverseList(head); + if(!head or !head->next) + return head; + ListNode* newNode = removeNodes(head->next); + if(newNode->val > head->val) + return newNode; + head->next = newNode; return head; } }; \ No newline at end of file From 40013fb0638df16ceea7ea91a30f88db04c59f20 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:07:18 +0530 Subject: [PATCH 0048/3167] Create README - LeetHub --- 0380-insert-delete-getrandom-o1/README.md | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0380-insert-delete-getrandom-o1/README.md diff --git a/0380-insert-delete-getrandom-o1/README.md b/0380-insert-delete-getrandom-o1/README.md new file mode 100644 index 00000000..fc1cda7b --- /dev/null +++ b/0380-insert-delete-getrandom-o1/README.md @@ -0,0 +1,40 @@ +

380. Insert Delete GetRandom O(1)

Medium


Implement the RandomizedSet class:

+ +
    +
  • RandomizedSet() Initializes the RandomizedSet object.
  • +
  • bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
  • +
  • bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
  • +
  • int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
  • +
+ +

You must implement the functions of the class such that each function works in average O(1) time complexity.

+ +

 

+

Example 1:

+ +
Input
+["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
+[[], [1], [2], [2], [], [1], [2], []]
+Output
+[null, true, false, true, 2, true, false, 2]
+
+Explanation
+RandomizedSet randomizedSet = new RandomizedSet();
+randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
+randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
+randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
+randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
+randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
+randomizedSet.insert(2); // 2 was already in the set, so return false.
+randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= val <= 231 - 1
  • +
  • At most 2 * 105 calls will be made to insert, remove, and getRandom.
  • +
  • There will be at least one element in the data structure when getRandom is called.
  • +
+
\ No newline at end of file From 4023de2a74df1978ac9333f0b7a3df6196f4b3f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:07:22 +0530 Subject: [PATCH 0049/3167] Time: 762 ms (6.50%), Space: 97 MB (58.37%) - LeetHub --- .../0380-insert-delete-getrandom-o1.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0380-insert-delete-getrandom-o1/0380-insert-delete-getrandom-o1.cpp diff --git a/0380-insert-delete-getrandom-o1/0380-insert-delete-getrandom-o1.cpp b/0380-insert-delete-getrandom-o1/0380-insert-delete-getrandom-o1.cpp new file mode 100644 index 00000000..347d6302 --- /dev/null +++ b/0380-insert-delete-getrandom-o1/0380-insert-delete-getrandom-o1.cpp @@ -0,0 +1,42 @@ +class RandomizedSet { +public: + unordered_map mp; + vector v; + RandomizedSet() { + + } + + bool insert(int val) { + if(mp.find(val) == mp.end()) + { + v.push_back(val) ; + mp[val] = v.size()-1; + return true; + } + return false; + } + + bool remove(int val) { + if(mp.find(val) == mp.end()) + return false; + + int last = v.back() ; + mp[last] = mp[val]; + v[mp[val]] = last; + v.pop_back() ; + mp.erase(val) ; + return true; + } + + int getRandom() { + return v[rand()%v.size()]; + } +}; + +/** + * Your RandomizedSet object will be instantiated and called as such: + * RandomizedSet* obj = new RandomizedSet(); + * bool param_1 = obj->insert(val); + * bool param_2 = obj->remove(val); + * int param_3 = obj->getRandom(); + */ \ No newline at end of file From 679ccacac26c02f46aa96583d764b4ab993f7950 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Nov 2022 19:39:39 +0530 Subject: [PATCH 0050/3167] Create README - LeetHub --- 1207-unique-number-of-occurrences/README.md | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1207-unique-number-of-occurrences/README.md diff --git a/1207-unique-number-of-occurrences/README.md b/1207-unique-number-of-occurrences/README.md new file mode 100644 index 00000000..17a227c4 --- /dev/null +++ b/1207-unique-number-of-occurrences/README.md @@ -0,0 +1,29 @@ +

1207. Unique Number of Occurrences

Easy


Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: arr = [1,2,2,1,1,3]
+Output: true
+Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
+ +

Example 2:

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

Example 3:

+ +
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
+Output: true
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • -1000 <= arr[i] <= 1000
  • +
+
\ No newline at end of file From 6bcaba79e1e29bdbdf9abefdebbaacd0a81ee3ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Nov 2022 19:39:43 +0530 Subject: [PATCH 0051/3167] Time: 3 ms (86.20%), Space: 8.1 MB (63.54%) - LeetHub --- .../1207-unique-number-of-occurrences.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.cpp diff --git a/1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.cpp b/1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.cpp new file mode 100644 index 00000000..68291a5b --- /dev/null +++ b/1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + bool uniqueOccurrences(vector& arr) { + + unordered_map mp; + unordered_set s; + for(auto itr : arr) + ++mp[itr]; + + for(auto itr : mp) + s.insert(itr.second); + + return (mp.size() == s.size()); + } +}; \ No newline at end of file From d57999c5950807c8a4c6362da09aa31cd189e2d3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:59:14 +0530 Subject: [PATCH 0052/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1704-determine-if-string-halves-are-alike/README.md diff --git a/1704-determine-if-string-halves-are-alike/README.md b/1704-determine-if-string-halves-are-alike/README.md new file mode 100644 index 00000000..a3be6787 --- /dev/null +++ b/1704-determine-if-string-halves-are-alike/README.md @@ -0,0 +1,31 @@ +

1704. Determine if String Halves Are Alike

Easy


You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.

+ +

Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.

+ +

Return true if a and b are alike. Otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: s = "book"
+Output: true
+Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
+
+ +

Example 2:

+ +
Input: s = "textbook"
+Output: false
+Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
+Notice that the vowel o is counted twice.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 1000
  • +
  • s.length is even.
  • +
  • s consists of uppercase and lowercase letters.
  • +
+
\ No newline at end of file From ac7a0b4364408be0247dcea4f53bd7ea4dfc1cdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:59:17 +0530 Subject: [PATCH 0053/3167] Attach NOTES - LeetHub --- 1704-determine-if-string-halves-are-alike/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1704-determine-if-string-halves-are-alike/NOTES.md diff --git a/1704-determine-if-string-halves-are-alike/NOTES.md b/1704-determine-if-string-halves-are-alike/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1704-determine-if-string-halves-are-alike/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f3916696d7188d83e57cd57a2d7c0dae5d381163 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:59:20 +0530 Subject: [PATCH 0054/3167] Time: 8 ms (40.71%), Space: 6.7 MB (56.35%) - LeetHub --- ...4-determine-if-string-halves-are-alike.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1704-determine-if-string-halves-are-alike/1704-determine-if-string-halves-are-alike.cpp diff --git a/1704-determine-if-string-halves-are-alike/1704-determine-if-string-halves-are-alike.cpp b/1704-determine-if-string-halves-are-alike/1704-determine-if-string-halves-are-alike.cpp new file mode 100644 index 00000000..adb1de1d --- /dev/null +++ b/1704-determine-if-string-halves-are-alike/1704-determine-if-string-halves-are-alike.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + bool halvesAreAlike(string s) { + + vector vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; + int one = 0 ,two = 0; + + int n = s.size(); + + for(int i = 0; i Date: Thu, 1 Dec 2022 18:59:56 +0530 Subject: [PATCH 0055/3167] Attach NOTES - LeetHub From 66d47b2f42660ad251c8717396693101350280a4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:59:59 +0530 Subject: [PATCH 0056/3167] Time: 8 ms (40.71%), Space: 6.7 MB (56.35%) - LeetHub From 80fec17e9e9d376bf8927e6457068a2a97ad4626 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 19:56:19 +0530 Subject: [PATCH 0057/3167] Create README - LeetHub --- 0110-balanced-binary-tree/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0110-balanced-binary-tree/README.md diff --git a/0110-balanced-binary-tree/README.md b/0110-balanced-binary-tree/README.md new file mode 100644 index 00000000..60949b6e --- /dev/null +++ b/0110-balanced-binary-tree/README.md @@ -0,0 +1,29 @@ +

110. Balanced Binary Tree

Easy


Given a binary tree, determine if it is height-balanced.

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: true
+
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 5000].
  • +
  • -104 <= Node.val <= 104
  • +
+
\ No newline at end of file From 83217102e8cef778904d31dbea8a9157f0d600ee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 19:56:22 +0530 Subject: [PATCH 0058/3167] Time: 11 ms (95.50%), Space: 20.9 MB (81.26%) - LeetHub --- .../0110-balanced-binary-tree.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0110-balanced-binary-tree/0110-balanced-binary-tree.cpp diff --git a/0110-balanced-binary-tree/0110-balanced-binary-tree.cpp b/0110-balanced-binary-tree/0110-balanced-binary-tree.cpp new file mode 100644 index 00000000..7105be6b --- /dev/null +++ b/0110-balanced-binary-tree/0110-balanced-binary-tree.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: + + int height(TreeNode* root) + { + if(!root) + return 0; + return 1 + max(height(root->left),height(root->right)); + } + + bool isBalanced(TreeNode* root) { + if(!root) + return true; + int left = height(root->left); + int right = height(root->right); + + return abs(left - right) <= 1 and isBalanced(root->left) && isBalanced(root->right); + } +}; + \ No newline at end of file From fb385ab4eafb52989008aace4ad4902f756ff3cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 19:59:48 +0530 Subject: [PATCH 0059/3167] Attach NOTES - LeetHub --- 0110-balanced-binary-tree/NOTES.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0110-balanced-binary-tree/NOTES.md diff --git a/0110-balanced-binary-tree/NOTES.md b/0110-balanced-binary-tree/NOTES.md new file mode 100644 index 00000000..9824a60c --- /dev/null +++ b/0110-balanced-binary-tree/NOTES.md @@ -0,0 +1,16 @@ +class Solution { +public: +int height(TreeNode* root) +{ +if(!root) +return 0; +return 1 + max(height(root->left),height(root->right)); +} +bool isBalanced(TreeNode* root) { +if(!root) +return true; +int left = height(root->left); +int right = height(root->right); +return abs(left - right) <= 1 and isBalanced(root->left) && isBalanced(root->right); +} +}; \ No newline at end of file From 38a86f533b7135e579365f6b23a6717e6d3335f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 19:59:51 +0530 Subject: [PATCH 0060/3167] Time: 11 ms (95.50%), Space: 20.8 MB (95.13%) - LeetHub --- .../0110-balanced-binary-tree.cpp | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/0110-balanced-binary-tree/0110-balanced-binary-tree.cpp b/0110-balanced-binary-tree/0110-balanced-binary-tree.cpp index 7105be6b..a8599a87 100644 --- a/0110-balanced-binary-tree/0110-balanced-binary-tree.cpp +++ b/0110-balanced-binary-tree/0110-balanced-binary-tree.cpp @@ -12,20 +12,23 @@ class Solution { public: - int height(TreeNode* root) + int helper(TreeNode* root) { if(!root) return 0; - return 1 + max(height(root->left),height(root->right)); + int left = helper(root->left); + int right = helper(root->right); + + if(abs(left - right) > 1 or left == -1 or right == -1) + return -1; + + return 1 + max(left,right); } + bool isBalanced(TreeNode* root) { - if(!root) - return true; - int left = height(root->left); - int right = height(root->right); - - return abs(left - right) <= 1 and isBalanced(root->left) && isBalanced(root->right); + if(helper(root) == -1) + return false; + return true; } -}; - \ No newline at end of file +}; \ No newline at end of file From eee3f5ef14ec0864f08ce28cec81dbd841e38d6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:00:28 +0530 Subject: [PATCH 0061/3167] Attach NOTES - LeetHub From 2a7ab3b8cb27879118c4ab31e3adad0143d75243 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:00:31 +0530 Subject: [PATCH 0062/3167] Time: 11 ms (95.50%), Space: 20.8 MB (95.13%) - LeetHub From 9c8f1e93623e9f44e1addcc51ba628d3e6021c88 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:00:33 +0530 Subject: [PATCH 0063/3167] Attach NOTES - LeetHub From 0f35e6a0a921bd65c7447545a238c4b5ce16e2ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:00:36 +0530 Subject: [PATCH 0064/3167] Time: 11 ms (95.50%), Space: 20.8 MB (95.13%) - LeetHub From a7a668d83114108c18e61cb69b62edf1d3a753b7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:01:26 +0530 Subject: [PATCH 0065/3167] Attach NOTES - LeetHub From d7e8f9b1799fe298f887cf2bc2a1b4329cdde558 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:01:29 +0530 Subject: [PATCH 0066/3167] Time: 11 ms (95.50%), Space: 20.8 MB (95.13%) - LeetHub From 0278dc418aa42990571b6816da164b5a871ad404 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:11:55 +0530 Subject: [PATCH 0067/3167] Create README - LeetHub --- 0094-binary-tree-inorder-traversal/README.md | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0094-binary-tree-inorder-traversal/README.md diff --git a/0094-binary-tree-inorder-traversal/README.md b/0094-binary-tree-inorder-traversal/README.md new file mode 100644 index 00000000..ad2de4dc --- /dev/null +++ b/0094-binary-tree-inorder-traversal/README.md @@ -0,0 +1,31 @@ +

94. Binary Tree Inorder Traversal

Easy


Given the root of a binary tree, return the inorder traversal of its nodes' values.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

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

 

+Follow up: Recursive solution is trivial, could you do it iteratively?
\ No newline at end of file From 966996a31d05c29b4ab5d6c5671defb4aa71d8aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:11:56 +0530 Subject: [PATCH 0068/3167] Attach NOTES - LeetHub --- 0094-binary-tree-inorder-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0094-binary-tree-inorder-traversal/NOTES.md diff --git a/0094-binary-tree-inorder-traversal/NOTES.md b/0094-binary-tree-inorder-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0094-binary-tree-inorder-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From edda48fc8b4b4201ac8e42dd8ac4646a94934c3e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:11:59 +0530 Subject: [PATCH 0069/3167] Time: 3 ms (63.22%), Space: 8.4 MB (75.78%) - LeetHub --- .../0094-binary-tree-inorder-traversal.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.cpp diff --git a/0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.cpp b/0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.cpp new file mode 100644 index 00000000..e114d0a2 --- /dev/null +++ b/0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.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: + vector inorderTraversal(TreeNode* root) { + + vector inorder; + TreeNode * curr = root; + + while(curr) + { + if(!curr->left) + { + inorder.push_back(curr->val); + curr = curr->right; + } + else + { + TreeNode *prev = curr->left; + while(prev->right and prev->right != curr) + prev = prev->right; + + if(!prev->right) + { + prev->right = curr; + curr = curr->left; + } + else + { + prev->right = nullptr; + inorder.push_back(curr->val); + curr = curr->right; + } + } + } + return inorder; + } +}; \ No newline at end of file From 12d25db209cb6b43604f0295eec129ac458f8fb9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:15:35 +0530 Subject: [PATCH 0070/3167] Create README - LeetHub --- 0144-binary-tree-preorder-traversal/README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0144-binary-tree-preorder-traversal/README.md diff --git a/0144-binary-tree-preorder-traversal/README.md b/0144-binary-tree-preorder-traversal/README.md new file mode 100644 index 00000000..320ded4b --- /dev/null +++ b/0144-binary-tree-preorder-traversal/README.md @@ -0,0 +1,32 @@ +

144. Binary Tree Preorder Traversal

Easy


Given the root of a binary tree, return the preorder traversal of its nodes' values.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

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

 

+

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

+
\ No newline at end of file From a25057f04868aa97df1704c216f145f5ccca60d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:15:35 +0530 Subject: [PATCH 0071/3167] Attach NOTES - LeetHub --- 0144-binary-tree-preorder-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0144-binary-tree-preorder-traversal/NOTES.md diff --git a/0144-binary-tree-preorder-traversal/NOTES.md b/0144-binary-tree-preorder-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0144-binary-tree-preorder-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4ec727e15c9e5404f0b980a7580a79a213abcf15 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:15:38 +0530 Subject: [PATCH 0072/3167] Time: 0 ms (100.00%), Space: 8.2 MB (93.59%) - LeetHub --- .../0144-binary-tree-preorder-traversal.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp diff --git a/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp b/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp new file mode 100644 index 00000000..bcf2a442 --- /dev/null +++ b/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.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: + vector preorderTraversal(TreeNode* root) { + + vector preorder; + TreeNode* curr = root; + + while(curr) + { + if(!curr->left) + { + preorder.push_back(curr->val); + curr = curr->right; + } + else + { + TreeNode* prev = curr->left; + while(prev->right and prev->right != curr) + prev = prev->right; + + if(!prev->right) + { + prev->right = curr; + preorder.push_back(curr->val); + curr = curr->left; + } + else + { + prev->right = nullptr; + curr = curr->right; + } + } + } + return preorder; + } +}; \ No newline at end of file From c9f1e50e557818c1d71c88e96e0ba057cf4e7192 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:50:09 +0530 Subject: [PATCH 0073/3167] Create README - LeetHub --- 0208-implement-trie-prefix-tree/README.md | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0208-implement-trie-prefix-tree/README.md diff --git a/0208-implement-trie-prefix-tree/README.md b/0208-implement-trie-prefix-tree/README.md new file mode 100644 index 00000000..fb35aa46 --- /dev/null +++ b/0208-implement-trie-prefix-tree/README.md @@ -0,0 +1,39 @@ +

208. Implement Trie (Prefix Tree)

Medium


A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

+ +

Implement the Trie class:

+ +
    +
  • Trie() Initializes the trie object.
  • +
  • void insert(String word) Inserts the string word into the trie.
  • +
  • boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
  • +
  • boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.
  • +
+ +

 

+

Example 1:

+ +
Input
+["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
+[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
+Output
+[null, null, true, false, true, null, true]
+
+Explanation
+Trie trie = new Trie();
+trie.insert("apple");
+trie.search("apple");   // return True
+trie.search("app");     // return False
+trie.startsWith("app"); // return True
+trie.insert("app");
+trie.search("app");     // return True
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word.length, prefix.length <= 2000
  • +
  • word and prefix consist only of lowercase English letters.
  • +
  • At most 3 * 104 calls in total will be made to insert, search, and startsWith.
  • +
+
\ No newline at end of file From 75e829de0b8131ad20ed07c623f0d19f51148d56 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:50:15 +0530 Subject: [PATCH 0074/3167] Time: 80 ms (83.62%), Space: 45 MB (70.46%) - LeetHub --- .../0208-implement-trie-prefix-tree.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp diff --git a/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp b/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp new file mode 100644 index 00000000..4be1fc25 --- /dev/null +++ b/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp @@ -0,0 +1,55 @@ +class Trie { +public: + + struct Node{ + Node *Child[26] = {nullptr}; + bool isWord = false; + }; + + Node* root = new Node(); + + Trie() { + + } + + void insert(string word) { + Node * temp = root; + for(int i= 0; iChild[word[i]-'a']) + temp->Child[word[i]-'a'] = new Node(); + temp = temp->Child[word[i]-'a']; + } + temp->isWord = true; + } + + bool search(string word) { + Node *temp = root; + for(int i = 0; i < word.size(); ++i) + { + if(!temp->Child[word[i]-'a']) + return false; + temp = temp->Child[word[i]-'a']; + } + return temp->isWord; + } + + bool startsWith(string prefix) { + Node *temp = root; + for(int i = 0; iChild[prefix[i] - 'a']) + return false; + temp = temp->Child[prefix[i] - 'a']; + } + return true; + } +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ \ No newline at end of file From 9737ef800d33a9a78d844a1a5b178194d289c137 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Dec 2022 18:13:49 +0530 Subject: [PATCH 0075/3167] Create README - LeetHub --- .../README.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 1657-determine-if-two-strings-are-close/README.md diff --git a/1657-determine-if-two-strings-are-close/README.md b/1657-determine-if-two-strings-are-close/README.md new file mode 100644 index 00000000..eeaa6e03 --- /dev/null +++ b/1657-determine-if-two-strings-are-close/README.md @@ -0,0 +1,55 @@ +

1657. Determine if Two Strings Are Close

Medium


Two strings are considered close if you can attain one from the other using the following operations:

+ +
    +
  • Operation 1: Swap any two existing characters. + +
      +
    • For example, abcde -> aecdb
    • +
    +
  • +
  • Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character. +
      +
    • For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
    • +
    +
  • +
+ +

You can use the operations on either string as many times as necessary.

+ +

Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.

+ +

 

+

Example 1:

+ +
Input: word1 = "abc", word2 = "bca"
+Output: true
+Explanation: You can attain word2 from word1 in 2 operations.
+Apply Operation 1: "abc" -> "acb"
+Apply Operation 1: "acb" -> "bca"
+
+ +

Example 2:

+ +
Input: word1 = "a", word2 = "aa"
+Output: false
+Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
+
+ +

Example 3:

+ +
Input: word1 = "cabbba", word2 = "abbccc"
+Output: true
+Explanation: You can attain word2 from word1 in 3 operations.
+Apply Operation 1: "cabbba" -> "caabbb"
+Apply Operation 2: "caabbb" -> "baaccc"
+Apply Operation 2: "baaccc" -> "abbccc"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word1.length, word2.length <= 105
  • +
  • word1 and word2 contain only lowercase English letters.
  • +
+
\ No newline at end of file From 1185eb6980a9e6663e5c47dba489fdf3fc0da69d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Dec 2022 18:13:53 +0530 Subject: [PATCH 0076/3167] Time: 436 ms (7.19%), Space: 20.7 MB (32.93%) - LeetHub --- ...1657-determine-if-two-strings-are-close.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1657-determine-if-two-strings-are-close/1657-determine-if-two-strings-are-close.cpp diff --git a/1657-determine-if-two-strings-are-close/1657-determine-if-two-strings-are-close.cpp b/1657-determine-if-two-strings-are-close/1657-determine-if-two-strings-are-close.cpp new file mode 100644 index 00000000..d70b0dcd --- /dev/null +++ b/1657-determine-if-two-strings-are-close/1657-determine-if-two-strings-are-close.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + bool closeStrings(string word1, string word2) { + vectorw1(26,0),w2(26,0); + sets1,s2; + for(char c:word1){ + w1[c-'a']++; + s1.insert(c); + } + for(char c:word2){ + w2[c-'a']++; + s2.insert(c); + } + sort(begin(w1),end(w1)); + sort(begin(w2),end(w2)); + return w1==w2&&s1==s2; + } +}; \ No newline at end of file From ca1a880101aa7ce310c07778cb1cb1be16b27dae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Dec 2022 21:11:33 +0530 Subject: [PATCH 0077/3167] Create README - LeetHub --- 0451-sort-characters-by-frequency/README.md | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0451-sort-characters-by-frequency/README.md diff --git a/0451-sort-characters-by-frequency/README.md b/0451-sort-characters-by-frequency/README.md new file mode 100644 index 00000000..6eceb8b1 --- /dev/null +++ b/0451-sort-characters-by-frequency/README.md @@ -0,0 +1,37 @@ +

451. Sort Characters By Frequency

Medium


Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.

+ +

Return the sorted string. If there are multiple answers, return any of them.

+ +

 

+

Example 1:

+ +
Input: s = "tree"
+Output: "eert"
+Explanation: 'e' appears twice while 'r' and 't' both appear once.
+So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
+
+ +

Example 2:

+ +
Input: s = "cccaaa"
+Output: "aaaccc"
+Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
+Note that "cacaca" is incorrect, as the same characters must be together.
+
+ +

Example 3:

+ +
Input: s = "Aabb"
+Output: "bbAa"
+Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
+Note that 'A' and 'a' are treated as two different characters.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 105
  • +
  • s consists of uppercase and lowercase English letters and digits.
  • +
+
\ No newline at end of file From dd7dd7ff20b51c363c9c1cfe75da51b3d55b61f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Dec 2022 21:11:33 +0530 Subject: [PATCH 0078/3167] Attach NOTES - LeetHub --- 0451-sort-characters-by-frequency/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0451-sort-characters-by-frequency/NOTES.md diff --git a/0451-sort-characters-by-frequency/NOTES.md b/0451-sort-characters-by-frequency/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0451-sort-characters-by-frequency/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f7dbe396c4a3470a386376ccffe8e017b6855961 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Dec 2022 21:11:37 +0530 Subject: [PATCH 0079/3167] Time: 27 ms (51.76%), Space: 8.8 MB (19.08%) - LeetHub --- .../0451-sort-characters-by-frequency.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.cpp diff --git a/0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.cpp b/0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.cpp new file mode 100644 index 00000000..793ad7b1 --- /dev/null +++ b/0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + string frequencySort(string s) { + unordered_map mp; + + for(auto itr : s) + ++mp[itr]; + + priority_queue> pq; + for(auto itr : mp) + pq.push({itr.second,itr.first}); + + string ans; + + while(!pq.empty()) + { + ans += string(pq.top().first,pq.top().second); + pq.pop(); + } + + return ans; + } +}; \ No newline at end of file From 26e645526f965c44b9c781201660ba4a0f8800d1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 09:59:55 +0530 Subject: [PATCH 0080/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2492-minimum-score-of-a-path-between-two-cities/README.md diff --git a/2492-minimum-score-of-a-path-between-two-cities/README.md b/2492-minimum-score-of-a-path-between-two-cities/README.md new file mode 100644 index 00000000..e7bd11d5 --- /dev/null +++ b/2492-minimum-score-of-a-path-between-two-cities/README.md @@ -0,0 +1,44 @@ +

2492. Minimum Score of a Path Between Two Cities

Medium


You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance equal to distancei. The cities graph is not necessarily connected.

+ +

The score of a path between two cities is defined as the minimum distance of a road in this path.

+ +

Return the minimum possible score of a path between cities 1 and n.

+ +

Note:

+ +
    +
  • A path is a sequence of roads between two cities.
  • +
  • It is allowed for a path to contain the same road multiple times, and you can visit cities 1 and n multiple times along the path.
  • +
  • The test cases are generated such that there is at least one path between 1 and n.
  • +
+ +

 

+

Example 1:

+ +
Input: n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
+Output: 5
+Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5.
+It can be shown that no other path has less score.
+
+ +

Example 2:

+ +
Input: n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]
+Output: 2
+Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 105
  • +
  • 1 <= roads.length <= 105
  • +
  • roads[i].length == 3
  • +
  • 1 <= ai, bi <= n
  • +
  • ai != bi
  • +
  • 1 <= distancei <= 104
  • +
  • There are no repeated edges.
  • +
  • There is at least one path between 1 and n.
  • +
+
\ No newline at end of file From 2ce7bd753265ff3acde96d4f5f4c2130a38db37a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 09:59:55 +0530 Subject: [PATCH 0081/3167] Attach NOTES - LeetHub --- 2492-minimum-score-of-a-path-between-two-cities/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2492-minimum-score-of-a-path-between-two-cities/NOTES.md diff --git a/2492-minimum-score-of-a-path-between-two-cities/NOTES.md b/2492-minimum-score-of-a-path-between-two-cities/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2492-minimum-score-of-a-path-between-two-cities/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 43bc3c617413227dfa16cb66d15f4082b7e8a053 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 10:00:00 +0530 Subject: [PATCH 0082/3167] Time: 638 ms (76.19%), Space: 122.7 MB (76.19%) - LeetHub --- ...mum-score-of-a-path-between-two-cities.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp diff --git a/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp b/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp new file mode 100644 index 00000000..cb2ce42a --- /dev/null +++ b/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + + void dfs(int sv, vector& visited, vector> adj[]) + { + visited[sv] = true; + for(auto itr : adj[sv]) + { + if(!visited[itr.first]) + dfs(itr.first,visited,adj); + } + } + + int minScore(int n, vector>& roads) { + + vector> adj[n+1]; + + for(int i = 0; i visited(n+1,false); + int ans = INT_MAX; + dfs(1,visited,adj); + + for(int i = 0; i Date: Sun, 4 Dec 2022 11:01:26 +0530 Subject: [PATCH 0083/3167] Create README - LeetHub --- 2256-minimum-average-difference/README.md | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2256-minimum-average-difference/README.md diff --git a/2256-minimum-average-difference/README.md b/2256-minimum-average-difference/README.md new file mode 100644 index 00000000..e2317be7 --- /dev/null +++ b/2256-minimum-average-difference/README.md @@ -0,0 +1,46 @@ +

2256. Minimum Average Difference

Medium


You are given a 0-indexed integer array nums of length n.

+ +

The average difference of the index i is the absolute difference between the average of the first i + 1 elements of nums and the average of the last n - i - 1 elements. Both averages should be rounded down to the nearest integer.

+ +

Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.

+ +

Note:

+ +
    +
  • The absolute difference of two numbers is the absolute value of their difference.
  • +
  • The average of n elements is the sum of the n elements divided (integer division) by n.
  • +
  • The average of 0 elements is considered to be 0.
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [2,5,3,9,5,3]
+Output: 3
+Explanation:
+- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
+- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
+- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
+- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
+- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
+- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
+The average difference of index 3 is the minimum average difference so return 3.
+
+ +

Example 2:

+ +
Input: nums = [0]
+Output: 0
+Explanation:
+The only index is 0 so return 0.
+The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 105
  • +
+
\ No newline at end of file From e746093925562baa52bfe4f14f8d3eeac093c7d1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 11:01:26 +0530 Subject: [PATCH 0084/3167] Attach NOTES - LeetHub --- 2256-minimum-average-difference/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2256-minimum-average-difference/NOTES.md diff --git a/2256-minimum-average-difference/NOTES.md b/2256-minimum-average-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2256-minimum-average-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bca5edcc463ea2278ab4246fc9f04e7e4ea67992 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 11:01:29 +0530 Subject: [PATCH 0085/3167] Time: 272 ms (56.17%), Space: 78.4 MB (54.89%) - LeetHub --- .../2256-minimum-average-difference.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2256-minimum-average-difference/2256-minimum-average-difference.cpp diff --git a/2256-minimum-average-difference/2256-minimum-average-difference.cpp b/2256-minimum-average-difference/2256-minimum-average-difference.cpp new file mode 100644 index 00000000..0ce8d91b --- /dev/null +++ b/2256-minimum-average-difference/2256-minimum-average-difference.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minimumAverageDifference(vector& nums) { + + int n = nums.size(), ans = INT_MAX, idx; + + long long sumFromFront = 0, sumFromEnd = 0; + + for(auto itr : nums) + sumFromEnd += itr; + + for(int i= 0; i Date: Sun, 4 Dec 2022 19:37:09 +0530 Subject: [PATCH 0086/3167] Create README - LeetHub --- 0115-distinct-subsequences/README.md | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0115-distinct-subsequences/README.md diff --git a/0115-distinct-subsequences/README.md b/0115-distinct-subsequences/README.md new file mode 100644 index 00000000..9f536311 --- /dev/null +++ b/0115-distinct-subsequences/README.md @@ -0,0 +1,36 @@ +

115. Distinct Subsequences

Hard


Given two strings s and t, return the number of distinct subsequences of s which equals t.

+ +

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

+ +

 

+

Example 1:

+ +
Input: s = "rabbbit", t = "rabbit"
+Output: 3
+Explanation:
+As shown below, there are 3 ways you can generate "rabbit" from s.
+rabbbit
+rabbbit
+rabbbit
+
+ +

Example 2:

+ +
Input: s = "babgbag", t = "bag"
+Output: 5
+Explanation:
+As shown below, there are 5 ways you can generate "bag" from s.
+babgbag
+babgbag
+babgbag
+babgbag
+babgbag
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 1000
  • +
  • s and t consist of English letters.
  • +
+
\ No newline at end of file From 4519c229987d24549100ff1e7d64c7d8438eb796 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:37:09 +0530 Subject: [PATCH 0087/3167] Attach NOTES - LeetHub --- 0115-distinct-subsequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0115-distinct-subsequences/NOTES.md diff --git a/0115-distinct-subsequences/NOTES.md b/0115-distinct-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0115-distinct-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b7c131cd7d1f9beb3e791262f519d2765e8cdd1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:37:14 +0530 Subject: [PATCH 0088/3167] Time: 2685 ms (5.01%), Space: 422.6 MB (5.01%) - LeetHub --- .../0115-distinct-subsequences.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0115-distinct-subsequences/0115-distinct-subsequences.cpp diff --git a/0115-distinct-subsequences/0115-distinct-subsequences.cpp b/0115-distinct-subsequences/0115-distinct-subsequences.cpp new file mode 100644 index 00000000..1dad287b --- /dev/null +++ b/0115-distinct-subsequences/0115-distinct-subsequences.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + int helper(int i, int j ,string s, string t, vector>& dp) + { + if(j < 0) + return 1; + if(i < 0) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + if(s[i] == t[j]) + return dp[i][j] = helper(i-1,j-1,s,t,dp) + helper(i-1,j,s,t,dp); + return dp[i][j] = helper(i-1,j,s,t,dp); + } + + + + int numDistinct(string s, string t) { + + int n = s.size(), m = t.size(); + vector> dp(n+1,vector(m+1,-1)); + return helper(n-1,m-1,s,t,dp); + } +}; \ No newline at end of file From 57fc1bcd320685711ad40c0b50d2fd664aeab302 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:46:30 +0530 Subject: [PATCH 0089/3167] Attach NOTES - LeetHub From bb7007e953e90b7707dc92940a6147bc540753f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:46:33 +0530 Subject: [PATCH 0090/3167] Time: 57 ms (63.00%), Space: 6.5 MB (87.07%) - LeetHub --- .../0115-distinct-subsequences.cpp | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/0115-distinct-subsequences/0115-distinct-subsequences.cpp b/0115-distinct-subsequences/0115-distinct-subsequences.cpp index 1dad287b..c448881f 100644 --- a/0115-distinct-subsequences/0115-distinct-subsequences.cpp +++ b/0115-distinct-subsequences/0115-distinct-subsequences.cpp @@ -1,27 +1,24 @@ class Solution { public: - int helper(int i, int j ,string s, string t, vector>& dp) - { - if(j < 0) - return 1; - if(i < 0) - return 0; - - if(dp[i][j] != -1) - return dp[i][j]; - - if(s[i] == t[j]) - return dp[i][j] = helper(i-1,j-1,s,t,dp) + helper(i-1,j,s,t,dp); - return dp[i][j] = helper(i-1,j,s,t,dp); - } - - - int numDistinct(string s, string t) { - + int n = s.size(), m = t.size(); - vector> dp(n+1,vector(m+1,-1)); - return helper(n-1,m-1,s,t,dp); + vector prev(m+1,0); + + for(int i = 0; i<=n; ++i) + prev[0] = 1; + + for(int i= 1; i<=n; ++i) + { + for(int j= m; j>=1; --j) + { + if(s[i-1] == t[j-1]) + prev[j] = prev[j-1] + prev[j]; + else + prev[j] = prev[j]; + } + } + return (int)prev[m]; } }; \ No newline at end of file From 56940e8f9af4eb403b01b49137ba907bb0429f79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:58:36 +0530 Subject: [PATCH 0091/3167] Create README - LeetHub --- 0072-edit-distance/README.md | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0072-edit-distance/README.md diff --git a/0072-edit-distance/README.md b/0072-edit-distance/README.md new file mode 100644 index 00000000..1959a449 --- /dev/null +++ b/0072-edit-distance/README.md @@ -0,0 +1,41 @@ +

72. Edit Distance

Hard


Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.

+ +

You have the following three operations permitted on a word:

+ +
    +
  • Insert a character
  • +
  • Delete a character
  • +
  • Replace a character
  • +
+ +

 

+

Example 1:

+ +
Input: word1 = "horse", word2 = "ros"
+Output: 3
+Explanation: 
+horse -> rorse (replace 'h' with 'r')
+rorse -> rose (remove 'r')
+rose -> ros (remove 'e')
+
+ +

Example 2:

+ +
Input: word1 = "intention", word2 = "execution"
+Output: 5
+Explanation: 
+intention -> inention (remove 't')
+inention -> enention (replace 'i' with 'e')
+enention -> exention (replace 'n' with 'x')
+exention -> exection (replace 'n' with 'c')
+exection -> execution (insert 'u')
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= word1.length, word2.length <= 500
  • +
  • word1 and word2 consist of lowercase English letters.
  • +
+
\ No newline at end of file From c29eb18d924513d138aa66acb4e0424ca1c0411a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:58:36 +0530 Subject: [PATCH 0092/3167] Attach NOTES - LeetHub --- 0072-edit-distance/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0072-edit-distance/NOTES.md diff --git a/0072-edit-distance/NOTES.md b/0072-edit-distance/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0072-edit-distance/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0e258b1a069aef26b8c4b76d06afa11c6c491c5f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:58:40 +0530 Subject: [PATCH 0093/3167] Time: 16 ms (77.13%), Space: 9.1 MB (41.74%) - LeetHub --- 0072-edit-distance/0072-edit-distance.cpp | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0072-edit-distance/0072-edit-distance.cpp diff --git a/0072-edit-distance/0072-edit-distance.cpp b/0072-edit-distance/0072-edit-distance.cpp new file mode 100644 index 00000000..69e57c56 --- /dev/null +++ b/0072-edit-distance/0072-edit-distance.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + int helper(int i, int j, string& word1, string& word2, vector>& dp) + { + + if(i < 0) + return j+1; + if(j < 0) + return i+1; + + if(dp[i][j] != -1) + return dp[i][j]; + + if(word1[i] == word2[j]) + return dp[i][j] = helper(i-1,j-1,word1,word2, dp); + return dp[i][j] = 1 + min({helper(i-1,j-1,word1,word2, dp ) , helper(i,j-1,word1,word2,dp), helper(i-1,j,word1,word2,dp)}); + } + + int minDistance(string word1, string word2) { + + int n = word1.size(), m = word2.size(); + vector> dp(n+1,vector(m+1,-1)); + return helper(n-1,m-1,word1,word2,dp); + + } +}; \ No newline at end of file From 35fd65cdf618fab130fa8781afc6802fc97a4530 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 20:09:20 +0530 Subject: [PATCH 0094/3167] Attach NOTES - LeetHub From c22c5aee0b6574ac301ea7d76578a2b1c982ba35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 20:09:23 +0530 Subject: [PATCH 0095/3167] Time: 11 ms (90.08%), Space: 9.1 MB (23.56%) - LeetHub --- 0072-edit-distance/0072-edit-distance.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/0072-edit-distance/0072-edit-distance.cpp b/0072-edit-distance/0072-edit-distance.cpp index 69e57c56..d02cbb2a 100644 --- a/0072-edit-distance/0072-edit-distance.cpp +++ b/0072-edit-distance/0072-edit-distance.cpp @@ -20,8 +20,23 @@ class Solution { int minDistance(string word1, string word2) { int n = word1.size(), m = word2.size(); - vector> dp(n+1,vector(m+1,-1)); - return helper(n-1,m-1,word1,word2,dp); + vector> dp(n+1,vector(m+1,0)); + for(int j = 0; j<=m; ++j) + dp[0][j] = j; + for(int i = 0; i<=n; ++i) + dp[i][0] = i; + + for(int i = 1; i<=n; ++i) + { + for(int j = 1; j<=m; ++j) + { + if(word1[i-1] == word2[j-1]) + dp[i][j] = dp[i-1][j-1]; + else + dp[i][j] = 1 + min({dp[i-1][j-1], dp[i-1][j], dp[i][j-1]}); + } + } + return dp[n][m]; } }; \ No newline at end of file From 2c1f3b77feb520e1e74debb4f5c4ec3a4e35ea38 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 20:13:54 +0530 Subject: [PATCH 0096/3167] Attach NOTES - LeetHub From 8cf0642c0f2ce2d6eafdeb846e8fe4072a9f0a5f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 20:13:57 +0530 Subject: [PATCH 0097/3167] Time: 18 ms (72.04%), Space: 6.4 MB (93.89%) - LeetHub --- 0072-edit-distance/0072-edit-distance.cpp | 31 ++++++----------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/0072-edit-distance/0072-edit-distance.cpp b/0072-edit-distance/0072-edit-distance.cpp index d02cbb2a..fa77a66d 100644 --- a/0072-edit-distance/0072-edit-distance.cpp +++ b/0072-edit-distance/0072-edit-distance.cpp @@ -1,42 +1,27 @@ class Solution { public: - int helper(int i, int j, string& word1, string& word2, vector>& dp) - { - - if(i < 0) - return j+1; - if(j < 0) - return i+1; - - if(dp[i][j] != -1) - return dp[i][j]; - - if(word1[i] == word2[j]) - return dp[i][j] = helper(i-1,j-1,word1,word2, dp); - return dp[i][j] = 1 + min({helper(i-1,j-1,word1,word2, dp ) , helper(i,j-1,word1,word2,dp), helper(i-1,j,word1,word2,dp)}); - } - + int minDistance(string word1, string word2) { int n = word1.size(), m = word2.size(); - vector> dp(n+1,vector(m+1,0)); + vector prev(m+1,0), curr(m+1,0); for(int j = 0; j<=m; ++j) - dp[0][j] = j; - for(int i = 0; i<=n; ++i) - dp[i][0] = i; + prev[j] = j; for(int i = 1; i<=n; ++i) { + curr[0] = i; for(int j = 1; j<=m; ++j) { if(word1[i-1] == word2[j-1]) - dp[i][j] = dp[i-1][j-1]; + curr[j] = prev[j-1]; else - dp[i][j] = 1 + min({dp[i-1][j-1], dp[i-1][j], dp[i][j-1]}); + curr[j] = 1 + min({prev[j-1], prev[j], curr[j-1]}); } + prev = curr; } - return dp[n][m]; + return prev[m]; } }; \ No newline at end of file From b668f847ad0fdb1d1788c6f1e8579bb01ca6bdce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 22:38:58 +0530 Subject: [PATCH 0098/3167] Create README - LeetHub --- 0044-wildcard-matching/README.md | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0044-wildcard-matching/README.md diff --git a/0044-wildcard-matching/README.md b/0044-wildcard-matching/README.md new file mode 100644 index 00000000..cc08b89c --- /dev/null +++ b/0044-wildcard-matching/README.md @@ -0,0 +1,40 @@ +

44. Wildcard Matching

Hard


Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where:

+ +
    +
  • '?' Matches any single character.
  • +
  • '*' Matches any sequence of characters (including the empty sequence).
  • +
+ +

The matching should cover the entire input string (not partial).

+ +

 

+

Example 1:

+ +
Input: s = "aa", p = "a"
+Output: false
+Explanation: "a" does not match the entire string "aa".
+
+ +

Example 2:

+ +
Input: s = "aa", p = "*"
+Output: true
+Explanation: '*' matches any sequence.
+
+ +

Example 3:

+ +
Input: s = "cb", p = "?a"
+Output: false
+Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s.length, p.length <= 2000
  • +
  • s contains only lowercase English letters.
  • +
  • p contains only lowercase English letters, '?' or '*'.
  • +
+
\ No newline at end of file From ec01e0f04696ef697068e310d559773ab3757477 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 22:38:58 +0530 Subject: [PATCH 0099/3167] Attach NOTES - LeetHub --- 0044-wildcard-matching/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0044-wildcard-matching/NOTES.md diff --git a/0044-wildcard-matching/NOTES.md b/0044-wildcard-matching/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0044-wildcard-matching/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 78aae5a1f683aa5c8134d65b4b7d1f0156c198a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 22:39:01 +0530 Subject: [PATCH 0100/3167] Time: 91 ms (62.96%), Space: 28 MB (13.69%) - LeetHub --- .../0044-wildcard-matching.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0044-wildcard-matching/0044-wildcard-matching.cpp diff --git a/0044-wildcard-matching/0044-wildcard-matching.cpp b/0044-wildcard-matching/0044-wildcard-matching.cpp new file mode 100644 index 00000000..a2de5d7b --- /dev/null +++ b/0044-wildcard-matching/0044-wildcard-matching.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + int helper(int i, int j, string& s, string& p, vector>& dp){ + + if(i < 0 and j < 0) + return true; + if(j < 0 and i >= 0) + return false; + if(i < 0 and j >= 0) + { + for(int ii = 0; ii<=j; ++ii) + { + if(p[ii] != '*') + return false; + } + return true; + } + + if(dp[i][j] != -1) + return dp[i][j]; + + if(s[i] == p[j] or p[j] == '?') + return dp[i][j] = helper(i-1,j-1,s,p,dp); + if(p[j] == '*') + return dp[i][j] = helper(i-1,j,s,p,dp) | helper(i,j-1,s,p,dp); + return dp[i][j] = false; + } + + bool isMatch(string s, string p) { + + int n = s.size(), m = p.size(); + vector> dp(n+1,vector(m+1,-1)); + return helper(n-1,m-1,s,p,dp); + + } +}; \ No newline at end of file From 8004acde81bf0590687612febb40da7bf3c15d09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 23:06:30 +0530 Subject: [PATCH 0101/3167] Attach NOTES - LeetHub From 1018b4892b157498ab8fd244f604990882243e58 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 23:06:33 +0530 Subject: [PATCH 0102/3167] Time: 91 ms (62.96%), Space: 28 MB (13.69%) - LeetHub From 8173564950a7f049fa8191b96754f93d727f965b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 23:08:54 +0530 Subject: [PATCH 0103/3167] Attach NOTES - LeetHub From c325764b98facf5fd3d5d6d2f66592ba0d3486ae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 23:08:57 +0530 Subject: [PATCH 0104/3167] Time: 91 ms (62.96%), Space: 28 MB (13.69%) - LeetHub From 4e3a57a44e5875eaf6e6b11ad741189634a6042d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 23:14:47 +0530 Subject: [PATCH 0105/3167] Attach NOTES - LeetHub From 9009df1e10a764de1bfb15fb73ee7fd105bc9674 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Dec 2022 23:14:49 +0530 Subject: [PATCH 0106/3167] Time: 182 ms (19.83%), Space: 9.4 MB (76.14%) - LeetHub --- .../0044-wildcard-matching.cpp | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/0044-wildcard-matching/0044-wildcard-matching.cpp b/0044-wildcard-matching/0044-wildcard-matching.cpp index a2de5d7b..d746615d 100644 --- a/0044-wildcard-matching/0044-wildcard-matching.cpp +++ b/0044-wildcard-matching/0044-wildcard-matching.cpp @@ -1,37 +1,31 @@ class Solution { public: - - int helper(int i, int j, string& s, string& p, vector>& dp){ - - if(i < 0 and j < 0) - return true; - if(j < 0 and i >= 0) - return false; - if(i < 0 and j >= 0) - { - for(int ii = 0; ii<=j; ++ii) - { - if(p[ii] != '*') - return false; - } - return true; - } - - if(dp[i][j] != -1) - return dp[i][j]; - - if(s[i] == p[j] or p[j] == '?') - return dp[i][j] = helper(i-1,j-1,s,p,dp); - if(p[j] == '*') - return dp[i][j] = helper(i-1,j,s,p,dp) | helper(i,j-1,s,p,dp); - return dp[i][j] = false; - } - + bool isMatch(string s, string p) { - int n = s.size(), m = p.size(); - vector> dp(n+1,vector(m+1,-1)); - return helper(n-1,m-1,s,p,dp); + int n=p.size(); + int m=s.size(); + vector> dp(n+1,vector(m+1,false)); + dp[0][0]=true; + for(int j=1;j<=m;j++) dp[0][j]=false; + for(int i=1;i<=n;i++){ + bool flag=true; + for(int k=1;k<=i;k++){ + if(p[k-1]!='*'){ + flag=false; + break; + } + } + dp[i][0]=flag; + } + for(int i=1;i<=n;i++){ + for(int j=1;j<=m;j++){ + if(p[i-1]==s[j-1] || p[i-1]=='?') dp[i][j]=dp[i-1][j-1]; + else if(p[i-1]=='*') dp[i][j]=dp[i-1][j] || dp[i][j-1]; + else dp[i][j]=false; + } + } + return dp[n][m]; } }; \ No newline at end of file From b8fd1bfc16484222f15052a7db0c58b27b9e388a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:38:00 +0530 Subject: [PATCH 0107/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0122-best-time-to-buy-and-sell-stock-ii/README.md diff --git a/0122-best-time-to-buy-and-sell-stock-ii/README.md b/0122-best-time-to-buy-and-sell-stock-ii/README.md new file mode 100644 index 00000000..0cf1818e --- /dev/null +++ b/0122-best-time-to-buy-and-sell-stock-ii/README.md @@ -0,0 +1,39 @@ +

122. Best Time to Buy and Sell Stock II

Medium


You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

+ +

On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.

+ +

Find and return the maximum profit you can achieve.

+ +

 

+

Example 1:

+ +
Input: prices = [7,1,5,3,6,4]
+Output: 7
+Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
+Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
+Total profit is 4 + 3 = 7.
+
+ +

Example 2:

+ +
Input: prices = [1,2,3,4,5]
+Output: 4
+Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
+Total profit is 4.
+
+ +

Example 3:

+ +
Input: prices = [7,6,4,3,1]
+Output: 0
+Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= prices.length <= 3 * 104
  • +
  • 0 <= prices[i] <= 104
  • +
+
\ No newline at end of file From c4ac295d345693fcc08d749fcc1bab1bdb1a4a4a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:38:04 +0530 Subject: [PATCH 0108/3167] Time: 20 ms (27.18%), Space: 15.2 MB (23.70%) - LeetHub --- ...122-best-time-to-buy-and-sell-stock-ii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp diff --git a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp new file mode 100644 index 00000000..5d5a3b7d --- /dev/null +++ b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + + int maxProfit(vector& prices) { + + int n = prices.size(); + vector> dp(n+1,vector(2,0)); + + dp[n][0] = dp[n][1] = 0; + for(int i = n-1; i>=0; --i) + { + for(int j = 0; j<=1; ++j) + { + if(j) + dp[i][j] = max(-prices[i] + dp[i+1][0], dp[i+1][1]); + else + dp[i][j] = max(prices[i] + dp[i+1][1], dp[i+1][0]); + } + } + return dp[0][1]; + } +}; \ No newline at end of file From bace2b70aa4d1499eecbbd2d82a10a1122d17a3e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:40:45 +0530 Subject: [PATCH 0109/3167] Attach NOTES - LeetHub --- 0122-best-time-to-buy-and-sell-stock-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0122-best-time-to-buy-and-sell-stock-ii/NOTES.md diff --git a/0122-best-time-to-buy-and-sell-stock-ii/NOTES.md b/0122-best-time-to-buy-and-sell-stock-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0122-best-time-to-buy-and-sell-stock-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 161f93742dd9b539f77e3caf4afa73e3af3cce86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:40:49 +0530 Subject: [PATCH 0110/3167] Time: 12 ms (65.11%), Space: 13.1 MB (29.01%) - LeetHub --- .../0122-best-time-to-buy-and-sell-stock-ii.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp index 5d5a3b7d..959c45ae 100644 --- a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp +++ b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp @@ -4,19 +4,20 @@ class Solution { int maxProfit(vector& prices) { int n = prices.size(); - vector> dp(n+1,vector(2,0)); + vector ahead(2,0),curr(2,0); + ahead[0] = ahead[1] = 0; - dp[n][0] = dp[n][1] = 0; for(int i = n-1; i>=0; --i) { for(int j = 0; j<=1; ++j) { if(j) - dp[i][j] = max(-prices[i] + dp[i+1][0], dp[i+1][1]); + curr[j] = max(-prices[i] + ahead[0], ahead[1]); else - dp[i][j] = max(prices[i] + dp[i+1][1], dp[i+1][0]); + curr[j] = max(prices[i] + ahead[1], ahead[0]); } + ahead = curr; } - return dp[0][1]; + return ahead[1]; } }; \ No newline at end of file From 990359077174d4838dcbb3cd3e6e4d5209123fa9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:48:17 +0530 Subject: [PATCH 0111/3167] Attach NOTES - LeetHub From 2445dc58e98a8b91549dd39964eded7f4f103ab1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:48:20 +0530 Subject: [PATCH 0112/3167] Time: 19 ms (32.71%), Space: 13.1 MB (59.28%) - LeetHub --- ...122-best-time-to-buy-and-sell-stock-ii.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp index 959c45ae..78e7b9c0 100644 --- a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp +++ b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp @@ -4,20 +4,20 @@ class Solution { int maxProfit(vector& prices) { int n = prices.size(); - vector ahead(2,0),curr(2,0); - ahead[0] = ahead[1] = 0; + + int aheadBuy, aheadNotBuy, currBuy, currNotBuy; + aheadBuy = aheadNotBuy = 0; for(int i = n-1; i>=0; --i) { - for(int j = 0; j<=1; ++j) - { - if(j) - curr[j] = max(-prices[i] + ahead[0], ahead[1]); - else - curr[j] = max(prices[i] + ahead[1], ahead[0]); - } - ahead = curr; + currBuy = max(-prices[i] + aheadNotBuy, aheadBuy); + + currNotBuy = max(prices[i] + aheadBuy, aheadNotBuy); + + aheadBuy = currBuy; + aheadNotBuy = currNotBuy; } - return ahead[1]; + + return aheadBuy; } }; \ No newline at end of file From 456350f2f020ae4516f947326edc9a5f49ce06db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Dec 2022 18:48:36 +0530 Subject: [PATCH 0113/3167] Create README - LeetHub --- .../README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 1770-maximum-score-from-performing-multiplication-operations/README.md diff --git a/1770-maximum-score-from-performing-multiplication-operations/README.md b/1770-maximum-score-from-performing-multiplication-operations/README.md new file mode 100644 index 00000000..6a9e22d5 --- /dev/null +++ b/1770-maximum-score-from-performing-multiplication-operations/README.md @@ -0,0 +1,51 @@ +

1770. Maximum Score from Performing Multiplication Operations

Hard


You are given two 0-indexed integer arrays nums and multipliers of size n and m respectively, where n >= m.

+ +

You begin with a score of 0. You want to perform exactly m operations. On the ith operation (0-indexed) you will:

+ +
    +
  • Choose one integer x from either the start or the end of the array nums.
  • +
  • Add multipliers[i] * x to your score. +
      +
    • Note that multipliers[0] corresponds to the first operation, multipliers[1] to the second operation, and so on.
    • +
    +
  • +
  • Remove x from nums.
  • +
+ +

Return the maximum score after performing m operations.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3], multipliers = [3,2,1]
+Output: 14
+Explanation: An optimal solution is as follows:
+- Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.
+- Choose from the end, [1,2], adding 2 * 2 = 4 to the score.
+- Choose from the end, [1], adding 1 * 1 = 1 to the score.
+The total score is 9 + 4 + 1 = 14.
+ +

Example 2:

+ +
Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
+Output: 102
+Explanation: An optimal solution is as follows:
+- Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
+- Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.
+- Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.
+- Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.
+- Choose from the end, [-2,7], adding 7 * 6 = 42 to the score. 
+The total score is 50 + 15 - 9 + 4 + 42 = 102.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • m == multipliers.length
  • +
  • 1 <= m <= 300
  • +
  • m <= n <= 105
  • +
  • -1000 <= nums[i], multipliers[i] <= 1000
  • +
+
\ No newline at end of file From deff4ea50b0b02dd2838d581b22fea7deb1a89b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Dec 2022 18:48:39 +0530 Subject: [PATCH 0114/3167] Time: 556 ms (17.40%), Space: 676.8 MB (5.07%) - LeetHub --- ...m-performing-multiplication-operations.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1770-maximum-score-from-performing-multiplication-operations/1770-maximum-score-from-performing-multiplication-operations.cpp diff --git a/1770-maximum-score-from-performing-multiplication-operations/1770-maximum-score-from-performing-multiplication-operations.cpp b/1770-maximum-score-from-performing-multiplication-operations/1770-maximum-score-from-performing-multiplication-operations.cpp new file mode 100644 index 00000000..695cc6a0 --- /dev/null +++ b/1770-maximum-score-from-performing-multiplication-operations/1770-maximum-score-from-performing-multiplication-operations.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + + int helper(int index,vector& nums, vector& multipliers, int start, int end, vector>& dp) + { + if(index == multipliers.size()) + return 0; + if(dp[index][start] != INT_MIN) + return dp[index][start]; + + return dp[index][start] = max(nums[start] * multipliers[index] + helper(index + 1, nums, multipliers,start+1,end, dp ), nums[end] * multipliers[index] + helper(index + 1,nums,multipliers,start,end-1, dp)); + } + + int maximumScore(vector& nums, vector& multipliers) { + int n = nums.size(); + int m = multipliers.size(); + vector> dp(n+1,vector(m+1,INT_MIN)); + return helper(0,nums,multipliers,0,n-1, dp); + } +}; \ No newline at end of file From e94450414708a794b1cdd3dbc27104c5b5657e2b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Dec 2022 21:52:36 +0530 Subject: [PATCH 0115/3167] Create README - LeetHub --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1339-maximum-product-of-splitted-binary-tree/README.md diff --git a/1339-maximum-product-of-splitted-binary-tree/README.md b/1339-maximum-product-of-splitted-binary-tree/README.md new file mode 100644 index 00000000..06088e64 --- /dev/null +++ b/1339-maximum-product-of-splitted-binary-tree/README.md @@ -0,0 +1,29 @@ +

1339. Maximum Product of Splitted Binary Tree

Medium


Given the root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.

+ +

Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 109 + 7.

+ +

Note that you need to maximize the answer before taking the mod and not after taking it.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4,5,6]
+Output: 110
+Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
+
+ +

Example 2:

+ +
Input: root = [1,null,2,3,4,null,null,5,6]
+Output: 90
+Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [2, 5 * 104].
  • +
  • 1 <= Node.val <= 104
  • +
+
\ No newline at end of file From 3b9282c65b48e5200e2b6b0ab58d223d2335dc1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Dec 2022 21:52:38 +0530 Subject: [PATCH 0116/3167] Time: 136 ms (95.18%), Space: 77.2 MB (89.61%) - LeetHub --- ...aximum-product-of-splitted-binary-tree.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp diff --git a/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp b/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp new file mode 100644 index 00000000..6ae761bd --- /dev/null +++ b/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp @@ -0,0 +1,29 @@ +/** + * 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 rec(TreeNode* root) { + if (!root) return 0; + int subtree = rec(root->left) + rec(root->right) + root->val; + res = max(res, (total - subtree) * subtree); + return subtree; + } + + int maxProduct(TreeNode* root) { + total = rec(root); + rec(root); + return res % int(pow(10,9)+7); + } + +private: + long res = 0, total = 0; +}; \ No newline at end of file From c2a04fab90a93bdc934d07e894a43349c54f811e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 18:46:02 +0530 Subject: [PATCH 0117/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0123-best-time-to-buy-and-sell-stock-iii/README.md diff --git a/0123-best-time-to-buy-and-sell-stock-iii/README.md b/0123-best-time-to-buy-and-sell-stock-iii/README.md new file mode 100644 index 00000000..ecc42d25 --- /dev/null +++ b/0123-best-time-to-buy-and-sell-stock-iii/README.md @@ -0,0 +1,37 @@ +

123. Best Time to Buy and Sell Stock III

Hard


You are given an array prices where prices[i] is the price of a given stock on the ith day.

+ +

Find the maximum profit you can achieve. You may complete at most two transactions.

+ +

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

+ +

 

+

Example 1:

+ +
Input: prices = [3,3,5,0,0,3,1,4]
+Output: 6
+Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
+Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
+ +

Example 2:

+ +
Input: prices = [1,2,3,4,5]
+Output: 4
+Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
+Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
+
+ +

Example 3:

+ +
Input: prices = [7,6,4,3,1]
+Output: 0
+Explanation: In this case, no transaction is done, i.e. max profit = 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= prices.length <= 105
  • +
  • 0 <= prices[i] <= 105
  • +
+
\ No newline at end of file From 2cf49215d2740c60b282f6cfd0cb4559f6166887 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 18:46:06 +0530 Subject: [PATCH 0118/3167] Time: 1006 ms (30.18%), Space: 218 MB (15.11%) - LeetHub --- ...23-best-time-to-buy-and-sell-stock-iii.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp diff --git a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp new file mode 100644 index 00000000..30674c76 --- /dev/null +++ b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + + int helper(int index, int buy, vector& prices, int cap, int n, vector>>& dp) + { + if(cap == 0 or index == n) + return 0; + if(dp[index][buy][cap] != -1) + return dp[index][buy][cap]; + if(buy == 1) + return dp[index][buy][cap] = max(-prices[index] + helper(index+1, 0, prices, cap, n ,dp), helper(index+1,1,prices,cap, n, dp)); + + return dp[index][buy][cap] = max(prices[index] + helper(index + 1, 1, prices,cap - 1, n, dp), helper(index+1, 0, prices,cap, n, dp)); + } + + int maxProfit(vector& prices) { + + int n = prices.size(); + vector>> dp(n+1,vector>(2,vector(3,-1))); + return helper(0,1,prices,2, n, dp); + + } +}; \ No newline at end of file From be792c53ef2b5e4baaa592d1f926bdd693842802 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:01:02 +0530 Subject: [PATCH 0119/3167] Attach NOTES - LeetHub --- 0123-best-time-to-buy-and-sell-stock-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0123-best-time-to-buy-and-sell-stock-iii/NOTES.md diff --git a/0123-best-time-to-buy-and-sell-stock-iii/NOTES.md b/0123-best-time-to-buy-and-sell-stock-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0123-best-time-to-buy-and-sell-stock-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From dd1459e5de8ff399a87b4436b80003ea27bd2c51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:01:06 +0530 Subject: [PATCH 0120/3167] Time: 1596 ms (16.42%), Space: 207.1 MB (34.42%) - LeetHub --- ...23-best-time-to-buy-and-sell-stock-iii.cpp | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp index 30674c76..d998917b 100644 --- a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp +++ b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp @@ -16,8 +16,28 @@ class Solution { int maxProfit(vector& prices) { int n = prices.size(); - vector>> dp(n+1,vector>(2,vector(3,-1))); - return helper(0,1,prices,2, n, dp); + vector>> dp(n+1,vector>(2,vector(3,0))); + + for(int i = n-1; i>=0; --i) + { + for(int buy = 0; buy <= 1; ++buy) + { + for(int cap = 1; cap <= 2; ++cap) + { + if(buy == 1) + { + dp[i][buy][cap] = max(-prices[i] + dp[i+1][0][cap], dp[i+1][1][cap]); + } + else + { + dp[i][buy][cap] = max(prices[i] + dp[i+1][1][cap-1], dp[i+1][0][cap]); + } + } + } + } + + + return dp[0][1][2]; } }; \ No newline at end of file From 04af761eaa95ffe8d84f56258d9de8213c8f48d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:18:59 +0530 Subject: [PATCH 0121/3167] Attach NOTES - LeetHub From bf40c0c4bcd228d96d1b06fd2a8125df657a63ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:19:02 +0530 Subject: [PATCH 0122/3167] Time: 1596 ms (16.42%), Space: 207.1 MB (34.42%) - LeetHub From 38baa5c95bbbee047e98c455fc7d3f22e4dfdc4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:28:09 +0530 Subject: [PATCH 0123/3167] Attach NOTES - LeetHub From dfd4ca1c96f780c903483ee28d47313661cad961 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:28:12 +0530 Subject: [PATCH 0124/3167] Time: 696 ms (44.03%), Space: 75.3 MB (85.42%) - LeetHub --- .../0123-best-time-to-buy-and-sell-stock-iii.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp index d998917b..1966c903 100644 --- a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp +++ b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp @@ -16,7 +16,8 @@ class Solution { int maxProfit(vector& prices) { int n = prices.size(); - vector>> dp(n+1,vector>(2,vector(3,0))); + // vector>> dp(n+1,vector>(2,vector(3,0))); + vector> ahead(2,vector(3,0)), curr(2,vector(3,0)); for(int i = n-1; i>=0; --i) { @@ -26,18 +27,19 @@ class Solution { { if(buy == 1) { - dp[i][buy][cap] = max(-prices[i] + dp[i+1][0][cap], dp[i+1][1][cap]); + curr[buy][cap] = max(-prices[i] + ahead[0][cap], ahead[1][cap]); } else { - dp[i][buy][cap] = max(prices[i] + dp[i+1][1][cap-1], dp[i+1][0][cap]); + curr[buy][cap] = max(prices[i] + ahead[1][cap-1], ahead[0][cap]); } } } + ahead = curr; } - return dp[0][1][2]; + return ahead[1][2]; } }; \ No newline at end of file From 90d4ad274a6a0707575d673bf7c0b47956ec600f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:44:23 +0530 Subject: [PATCH 0125/3167] Attach NOTES - LeetHub From 6b1e36ea2e4ad147885e108a2731855becf104ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:44:26 +0530 Subject: [PATCH 0126/3167] Time: 848 ms (35.30%), Space: 134.1 MB (43.59%) - LeetHub --- ...23-best-time-to-buy-and-sell-stock-iii.cpp | 44 +++++-------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp index 1966c903..0a9adf60 100644 --- a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp +++ b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp @@ -1,45 +1,23 @@ class Solution { public: - int helper(int index, int buy, vector& prices, int cap, int n, vector>>& dp) + int helper(int index, vector& prices, int buy, int ok, int n, vector>& dp) { - if(cap == 0 or index == n) + if(index == n or ok == 4) return 0; - if(dp[index][buy][cap] != -1) - return dp[index][buy][cap]; - if(buy == 1) - return dp[index][buy][cap] = max(-prices[index] + helper(index+1, 0, prices, cap, n ,dp), helper(index+1,1,prices,cap, n, dp)); - - return dp[index][buy][cap] = max(prices[index] + helper(index + 1, 1, prices,cap - 1, n, dp), helper(index+1, 0, prices,cap, n, dp)); + if(dp[index][ok] != -1) + return dp[index][ok]; + if(buy or ok % 2 == 0) + return dp[index][ok] = max(-prices[index] + helper(index+1, prices,0,ok+1, n, dp) ,helper(index+1,prices,1,ok, n, dp)); + return dp[index][ok] = max(prices[index] + helper(index+1, prices,1,ok+1, n, dp), helper(index+1, prices,0,ok, n, dp)); + } int maxProfit(vector& prices) { - int n = prices.size(); - // vector>> dp(n+1,vector>(2,vector(3,0))); - vector> ahead(2,vector(3,0)), curr(2,vector(3,0)); - - for(int i = n-1; i>=0; --i) - { - for(int buy = 0; buy <= 1; ++buy) - { - for(int cap = 1; cap <= 2; ++cap) - { - if(buy == 1) - { - curr[buy][cap] = max(-prices[i] + ahead[0][cap], ahead[1][cap]); - } - else - { - curr[buy][cap] = max(prices[i] + ahead[1][cap-1], ahead[0][cap]); - } - } - } - ahead = curr; - } - - - return ahead[1][2]; + int n = prices.size(); + vector> dp(n,vector(4,-1)); + return helper(0,prices,1,0,n,dp); } }; \ No newline at end of file From b24e1fef39189043a755f59f503e66fa1bd75cb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:47:32 +0530 Subject: [PATCH 0127/3167] Attach NOTES - LeetHub From 91fe82964ceb2b4af9792ba85e9087f75a7cb368 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:47:36 +0530 Subject: [PATCH 0128/3167] Time: 848 ms (35.30%), Space: 134.1 MB (43.59%) - LeetHub From 2f79ea1fb56f497d18faa7ca2be2fe86ddd95006 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:04:19 +0530 Subject: [PATCH 0129/3167] Attach NOTES - LeetHub From b28db901c795cd5f28065c4b4688ce6edea034b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:04:23 +0530 Subject: [PATCH 0130/3167] Time: 431 ms (61.82%), Space: 132.7 MB (43.69%) - LeetHub --- .../0123-best-time-to-buy-and-sell-stock-iii.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp index 0a9adf60..4dda7ac2 100644 --- a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp +++ b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp @@ -1,15 +1,15 @@ class Solution { public: - int helper(int index, vector& prices, int buy, int ok, int n, vector>& dp) + int helper(int index, vector& prices,int ok, int n, vector>& dp) { if(index == n or ok == 4) return 0; if(dp[index][ok] != -1) return dp[index][ok]; - if(buy or ok % 2 == 0) - return dp[index][ok] = max(-prices[index] + helper(index+1, prices,0,ok+1, n, dp) ,helper(index+1,prices,1,ok, n, dp)); - return dp[index][ok] = max(prices[index] + helper(index+1, prices,1,ok+1, n, dp), helper(index+1, prices,0,ok, n, dp)); + if(ok % 2 == 0) + return dp[index][ok] = max(-prices[index] + helper(index+1, prices,ok+1, n, dp) ,helper(index+1,prices,ok, n, dp)); + return dp[index][ok] = max(prices[index] + helper(index+1, prices,ok+1, n, dp), helper(index+1, prices,ok, n, dp)); } @@ -17,7 +17,7 @@ class Solution { int n = prices.size(); vector> dp(n,vector(4,-1)); - return helper(0,prices,1,0,n,dp); + return helper(0,prices,0,n,dp); } }; \ No newline at end of file From 586b792ccb1a3ee238feecc098dcf1c1f1338e2f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:15:43 +0530 Subject: [PATCH 0131/3167] Attach NOTES - LeetHub From f77a0015c0e30b8b01cce5f0ff6d63fc1aba495c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:15:46 +0530 Subject: [PATCH 0132/3167] Time: 405 ms (66.95%), Space: 135.2 MB (42.75%) - LeetHub --- .../0123-best-time-to-buy-and-sell-stock-iii.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp index 4dda7ac2..a7297d11 100644 --- a/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp +++ b/0123-best-time-to-buy-and-sell-stock-iii/0123-best-time-to-buy-and-sell-stock-iii.cpp @@ -16,8 +16,19 @@ class Solution { int maxProfit(vector& prices) { int n = prices.size(); - vector> dp(n,vector(4,-1)); - return helper(0,prices,0,n,dp); + vector> dp(n+1,vector(5,0)); + for(int i = n - 1; i >= 0; --i) + { + for(int ok = 3; ok >= 0; --ok) + { + if(ok % 2 == 0) + dp[i][ok] = max(-prices[i] + dp[i+1][ok+1], dp[i+1][ok]); + else + dp[i][ok] = max(prices[i] + dp[i+1][ok+1], dp[i+1][ok]); + } + } + + return dp[0][0]; } }; \ No newline at end of file From 648f850b7a7b54899de827b6bb4979e7aa85f12e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:24:54 +0530 Subject: [PATCH 0133/3167] Create README - LeetHub --- 0112-path-sum/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0112-path-sum/README.md diff --git a/0112-path-sum/README.md b/0112-path-sum/README.md new file mode 100644 index 00000000..6939ed6b --- /dev/null +++ b/0112-path-sum/README.md @@ -0,0 +1,38 @@ +

112. Path Sum

Easy


Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

+ +

A leaf is a node with no children.

+ +

 

+

Example 1:

+ +
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
+Output: true
+Explanation: The root-to-leaf path with the target sum is shown.
+
+ +

Example 2:

+ +
Input: root = [1,2,3], targetSum = 5
+Output: false
+Explanation: There two root-to-leaf paths in the tree:
+(1 --> 2): The sum is 3.
+(1 --> 3): The sum is 4.
+There is no root-to-leaf path with sum = 5.
+
+ +

Example 3:

+ +
Input: root = [], targetSum = 0
+Output: false
+Explanation: Since the tree is empty, there are no root-to-leaf paths.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 5000].
  • +
  • -1000 <= Node.val <= 1000
  • +
  • -1000 <= targetSum <= 1000
  • +
+
\ No newline at end of file From ea226422888d2714b34e9979509fa337960c78c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:24:57 +0530 Subject: [PATCH 0134/3167] Time: 23 ms (45.67%), Space: 21.1 MB (92.41%) - LeetHub --- 0112-path-sum/0112-path-sum.cpp | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0112-path-sum/0112-path-sum.cpp diff --git a/0112-path-sum/0112-path-sum.cpp b/0112-path-sum/0112-path-sum.cpp new file mode 100644 index 00000000..2e8485fe --- /dev/null +++ b/0112-path-sum/0112-path-sum.cpp @@ -0,0 +1,36 @@ +/** + * 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, int currSum, int targetSum) + { + if(!root) + { + return false; + } + if(!root->left and !root->right) + return currSum+root->val == targetSum; + + bool one = helper(root->left,currSum + root->val, targetSum); + bool two = helper(root->right,currSum + root->val, targetSum); + + return one or two; + } + + bool hasPathSum(TreeNode* root, int targetSum) { + + if(!root) + return false; + return helper(root,0,targetSum); + } +}; \ No newline at end of file From 14e04db7fbf0ba22dd1cfee9adfa66c69c521409 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:25:05 +0530 Subject: [PATCH 0135/3167] Attach NOTES - LeetHub --- 0112-path-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0112-path-sum/NOTES.md diff --git a/0112-path-sum/NOTES.md b/0112-path-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0112-path-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2deb9186d6797346cbdcaab95f04d9995ff0045f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:25:08 +0530 Subject: [PATCH 0136/3167] Time: 23 ms (45.67%), Space: 21.1 MB (92.41%) - LeetHub From c591c6bf006b574cb6b1159c47eae4410e96965d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 21:05:17 +0530 Subject: [PATCH 0137/3167] Create README - LeetHub --- 0113-path-sum-ii/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0113-path-sum-ii/README.md diff --git a/0113-path-sum-ii/README.md b/0113-path-sum-ii/README.md new file mode 100644 index 00000000..e07242b1 --- /dev/null +++ b/0113-path-sum-ii/README.md @@ -0,0 +1,35 @@ +

113. Path Sum II

Medium


Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.

+ +

A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.

+ +

 

+

Example 1:

+ +
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
+Output: [[5,4,11,2],[5,8,4,5]]
+Explanation: There are two paths whose sum equals targetSum:
+5 + 4 + 11 + 2 = 22
+5 + 8 + 4 + 5 = 22
+
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 5000].
  • +
  • -1000 <= Node.val <= 1000
  • +
  • -1000 <= targetSum <= 1000
  • +
+
\ No newline at end of file From 176bde73af21523ebef3da894347a2979ae5c0bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 21:05:20 +0530 Subject: [PATCH 0138/3167] Time: 18 ms (63.62%), Space: 36.1 MB (29.99%) - LeetHub --- 0113-path-sum-ii/0113-path-sum-ii.cpp | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0113-path-sum-ii/0113-path-sum-ii.cpp diff --git a/0113-path-sum-ii/0113-path-sum-ii.cpp b/0113-path-sum-ii/0113-path-sum-ii.cpp new file mode 100644 index 00000000..cee5e478 --- /dev/null +++ b/0113-path-sum-ii/0113-path-sum-ii.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: + + void helper(TreeNode* root, vector v, vector>& res, int targetSum) + { + if(!root) + return; + + if(!root->left and !root->right) + { + if(targetSum - root->val == 0) + { + v.push_back(root->val); + res.push_back(v); + return ; + } + return ; + } + + v.push_back(root->val); + helper(root->left,v,res,targetSum - root->val); + helper(root->right,v,res,targetSum - root->val); + v.pop_back(); + } + + vector> pathSum(TreeNode* root, int targetSum) { + + vector v; + vector> res; + + helper(root,v,res,targetSum); + + return res; + + } +}; \ No newline at end of file From fd857b9d4da34949e8777f1146cc836bfa0477c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 23:10:37 +0530 Subject: [PATCH 0139/3167] Attach NOTES - LeetHub --- 0124-binary-tree-maximum-path-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0124-binary-tree-maximum-path-sum/NOTES.md diff --git a/0124-binary-tree-maximum-path-sum/NOTES.md b/0124-binary-tree-maximum-path-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0124-binary-tree-maximum-path-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3e4c375ab27a9d3dd8c916d05e4e18ec15b3ff44 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Dec 2022 23:10:40 +0530 Subject: [PATCH 0140/3167] Time: 33 ms (80.36%), Space: 28.1 MB (26.97%) - LeetHub --- .../0124-binary-tree-maximum-path-sum.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0124-binary-tree-maximum-path-sum/0124-binary-tree-maximum-path-sum.cpp diff --git a/0124-binary-tree-maximum-path-sum/0124-binary-tree-maximum-path-sum.cpp b/0124-binary-tree-maximum-path-sum/0124-binary-tree-maximum-path-sum.cpp new file mode 100644 index 00000000..d3c8220d --- /dev/null +++ b/0124-binary-tree-maximum-path-sum/0124-binary-tree-maximum-path-sum.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: + + int ans = INT_MIN; + + int helper(TreeNode* root) + { + if(!root) + return 0; + int left = helper(root->left); + int right = helper(root->right); + + ans = max(ans,left + right + root->val); + return max({root->val + left, root->val + right, 0}); + + } + + int maxPathSum(TreeNode* root) { + + helper(root); + return ans; + + } +}; \ No newline at end of file From 0723b9ec4f9bee63d2c11b69459ec86d0e7cbd86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Dec 2022 19:54:08 +0530 Subject: [PATCH 0141/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1192-critical-connections-in-a-network/README.md diff --git a/1192-critical-connections-in-a-network/README.md b/1192-critical-connections-in-a-network/README.md new file mode 100644 index 00000000..1851e0a3 --- /dev/null +++ b/1192-critical-connections-in-a-network/README.md @@ -0,0 +1,31 @@ +

1192. Critical Connections in a Network

Hard


There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network.

+ +

A critical connection is a connection that, if removed, will make some servers unable to reach some other server.

+ +

Return all critical connections in the network in any order.

+ +

 

+

Example 1:

+ +
Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
+Output: [[1,3]]
+Explanation: [[3,1]] is also accepted.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 2 <= n <= 105
  • +
  • n - 1 <= connections.length <= 105
  • +
  • 0 <= ai, bi <= n - 1
  • +
  • ai != bi
  • +
  • There are no repeated connections.
  • +
+
\ No newline at end of file From 9145f4e631a3625cb2fadefb1978db3c6737ee88 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Dec 2022 19:54:12 +0530 Subject: [PATCH 0142/3167] Time: 698 ms (87.31%), Space: 174.3 MB (71.89%) - LeetHub --- ...1192-critical-connections-in-a-network.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp diff --git a/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp b/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp new file mode 100644 index 00000000..ca191c63 --- /dev/null +++ b/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + + private: + int timer = 0; + + void dfs(int sv, int parent, vector& visited, vector& dis, vector& low, int timer, vector adj[],vector>& vp){ + visited[sv] = true; + dis[sv] = low[sv] = timer++; + + for(auto itr : adj[sv]) + { + if(itr == parent) + continue; + if(!visited[itr]) + { + dfs(itr,sv,visited,dis,low,timer,adj,vp); + low[sv] = min(low[sv],low[itr]); + + if(low[itr] > dis[sv]) + { + vp.push_back({itr,sv}); + } + } + else{ + low[sv] = min(low[sv],low[itr]); + } + } + } + public: + vector> criticalConnections(int n, vector>& connections) + { + vector adj[n]; + for(auto itr : connections) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + } + + vector visited(n,false); + vector dis(n,-1), low(n,-1); + vector> vp; + int timer = 0; + for(int i = 0; i Date: Tue, 13 Dec 2022 19:02:26 +0530 Subject: [PATCH 0143/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0714-best-time-to-buy-and-sell-stock-with-transaction-fee/README.md diff --git a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/README.md b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/README.md new file mode 100644 index 00000000..c93f20ad --- /dev/null +++ b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/README.md @@ -0,0 +1,34 @@ +

714. Best Time to Buy and Sell Stock with Transaction Fee

Medium


You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

+ +

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

+ +

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

+ +

 

+

Example 1:

+ +
Input: prices = [1,3,2,8,4,9], fee = 2
+Output: 8
+Explanation: The maximum profit can be achieved by:
+- Buying at prices[0] = 1
+- Selling at prices[3] = 8
+- Buying at prices[4] = 4
+- Selling at prices[5] = 9
+The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
+
+ +

Example 2:

+ +
Input: prices = [1,3,7,5,10,3], fee = 3
+Output: 6
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= prices.length <= 5 * 104
  • +
  • 1 <= prices[i] < 5 * 104
  • +
  • 0 <= fee < 5 * 104
  • +
+
\ No newline at end of file From 4fa1732c641fcc70caa7b5782262869671fe74c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:02:30 +0530 Subject: [PATCH 0144/3167] Time: 308 ms (49.85%), Space: 94.9 MB (23.33%) - LeetHub --- ...uy-and-sell-stock-with-transaction-fee.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp diff --git a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp new file mode 100644 index 00000000..b9ab6921 --- /dev/null +++ b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + + int helper(int index, int buy, vector& prices, int fee, vector>& dp) + { + if(index == prices.size()) + return 0; + if(dp[index][buy] != -1) + return dp[index][buy]; + if(buy) + return dp[index][buy] = max(-prices[index] + helper(index+1, 0, prices,fee,dp), helper(index+1, 1, prices,fee,dp)); + return dp[index][buy] = max(prices[index] - fee + helper(index+1,1,prices,fee,dp), helper(index+1,0,prices,fee,dp)); + } + + int maxProfit(vector& prices, int fee) { + + int n = prices.size(); + vector> dp(n,vector(2,-1)); + return helper(0,1,prices,fee, dp); + } +}; \ No newline at end of file From b95f00333286e98bbba4ac30735f328e725bc5ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:05:24 +0530 Subject: [PATCH 0145/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0714-best-time-to-buy-and-sell-stock-with-transaction-fee/NOTES.md diff --git a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/NOTES.md b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4151154170217f2b5394ba82d29cb8b0ce4553cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:05:27 +0530 Subject: [PATCH 0146/3167] Time: 531 ms (25.69%), Space: 90.9 MB (35.86%) - LeetHub --- ...to-buy-and-sell-stock-with-transaction-fee.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp index b9ab6921..58cfe5c4 100644 --- a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp +++ b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp @@ -15,7 +15,18 @@ class Solution { int maxProfit(vector& prices, int fee) { int n = prices.size(); - vector> dp(n,vector(2,-1)); - return helper(0,1,prices,fee, dp); + vector> dp(n+1,vector(2,0)); + + for(int i = n-1; i>=0; --i) + { + for(int buy = 1; buy>=0; --buy) + { + if(buy == 1) + dp[i][buy] = max(-prices[i] + dp[i+1][0], dp[i+1][1]); + else + dp[i][buy] = max(prices[i] - fee + dp[i+1][1], dp[i+1][0]); + } + } + return dp[0][1]; } }; \ No newline at end of file From 7fbbb9ee2505c31ae945c8e7fe7c6e6b3a884941 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:07:01 +0530 Subject: [PATCH 0147/3167] Attach NOTES - LeetHub From 3ba72bce98fb5e44a75e915d8d20c2f9804978ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:07:04 +0530 Subject: [PATCH 0148/3167] Time: 241 ms (73.08%), Space: 55 MB (74.68%) - LeetHub --- ...uy-and-sell-stock-with-transaction-fee.cpp | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp index 58cfe5c4..94e64e4d 100644 --- a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp +++ b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp @@ -1,32 +1,22 @@ class Solution { public: - int helper(int index, int buy, vector& prices, int fee, vector>& dp) - { - if(index == prices.size()) - return 0; - if(dp[index][buy] != -1) - return dp[index][buy]; - if(buy) - return dp[index][buy] = max(-prices[index] + helper(index+1, 0, prices,fee,dp), helper(index+1, 1, prices,fee,dp)); - return dp[index][buy] = max(prices[index] - fee + helper(index+1,1,prices,fee,dp), helper(index+1,0,prices,fee,dp)); - } - int maxProfit(vector& prices, int fee) { int n = prices.size(); - vector> dp(n+1,vector(2,0)); + vector curr(2,0), ahead(2,0); for(int i = n-1; i>=0; --i) { for(int buy = 1; buy>=0; --buy) { if(buy == 1) - dp[i][buy] = max(-prices[i] + dp[i+1][0], dp[i+1][1]); + curr[buy] = max(-prices[i] + ahead[0], ahead[1]); else - dp[i][buy] = max(prices[i] - fee + dp[i+1][1], dp[i+1][0]); + curr[buy] = max(prices[i] - fee + ahead[1], ahead[0]); } + ahead = curr; } - return dp[0][1]; + return ahead[1]; } }; \ No newline at end of file From 41212c6aebd95fa4f2adc4c923480d0bab6d2e82 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:07:47 +0530 Subject: [PATCH 0149/3167] Attach NOTES - LeetHub From b9c5bb3c599aa5f1b11b57356990447c02c7bdc8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Dec 2022 19:07:51 +0530 Subject: [PATCH 0150/3167] Time: 241 ms (73.08%), Space: 55 MB (74.68%) - LeetHub From ab76f67d93eea155ba449ad32a1d0767ca9d90a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:15:54 +0530 Subject: [PATCH 0151/3167] Create README - LeetHub --- 0300-longest-increasing-subsequence/README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0300-longest-increasing-subsequence/README.md diff --git a/0300-longest-increasing-subsequence/README.md b/0300-longest-increasing-subsequence/README.md new file mode 100644 index 00000000..74241c8e --- /dev/null +++ b/0300-longest-increasing-subsequence/README.md @@ -0,0 +1,33 @@ +

300. Longest Increasing Subsequence

Medium


Given an integer array nums, return the length of the longest strictly increasing subsequence.

+ +

 

+

Example 1:

+ +
Input: nums = [10,9,2,5,3,7,101,18]
+Output: 4
+Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
+
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 2500
  • +
  • -104 <= nums[i] <= 104
  • +
+ +

 

+

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

+
\ No newline at end of file From 07bb675448c3330b8cef48f2f672983a4dcf6c9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:15:54 +0530 Subject: [PATCH 0152/3167] Attach NOTES - LeetHub --- 0300-longest-increasing-subsequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0300-longest-increasing-subsequence/NOTES.md diff --git a/0300-longest-increasing-subsequence/NOTES.md b/0300-longest-increasing-subsequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0300-longest-increasing-subsequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 55de3d4461780441381513de4ef0f9d75692ee72 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:15:58 +0530 Subject: [PATCH 0153/3167] Time: 1470 ms (13.44%), Space: 292.4 MB (5.38%) - LeetHub --- .../0300-longest-increasing-subsequence.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp diff --git a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp new file mode 100644 index 00000000..1618c72b --- /dev/null +++ b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + + int helper(int index, vector& nums, int prev, vector>& dp) + { + if(index == nums.size()) + return 0; + if(dp[index][prev + 1] != -1) + return dp[index][prev+1]; + int len = 0 + helper(index+1, nums,prev,dp); + if(prev == -1 or nums[index] > nums[prev]) + len = max(len , 1 + helper(index+1, nums, index, dp)); + + return dp[index][prev+1] = len; + } + + int lengthOfLIS(vector& nums) { + int n = nums.size(); + vector> dp(n,vector(n+1,-1)); + return helper(0,nums,-1, dp); + } +}; \ No newline at end of file From 93150e8d27e7bcdf651435ecb022f0bcf125c879 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:22:30 +0530 Subject: [PATCH 0154/3167] Attach NOTES - LeetHub From 1a054cab3842323eee7074c234d675cb470508f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:22:33 +0530 Subject: [PATCH 0155/3167] Time: 1470 ms (13.44%), Space: 292.4 MB (5.38%) - LeetHub From 23c946290e459de7a785c6fe1bbe9102fac3cc24 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:24:52 +0530 Subject: [PATCH 0156/3167] Attach NOTES - LeetHub From 824f153023863375a98859543cb6001a4fe92b6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 08:24:55 +0530 Subject: [PATCH 0157/3167] Time: 635 ms (40.54%), Space: 292.3 MB (10.68%) - LeetHub --- .../0300-longest-increasing-subsequence.cpp | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp index 1618c72b..179f6029 100644 --- a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp +++ b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp @@ -1,22 +1,24 @@ class Solution { public: - - int helper(int index, vector& nums, int prev, vector>& dp) - { - if(index == nums.size()) - return 0; - if(dp[index][prev + 1] != -1) - return dp[index][prev+1]; - int len = 0 + helper(index+1, nums,prev,dp); - if(prev == -1 or nums[index] > nums[prev]) - len = max(len , 1 + helper(index+1, nums, index, dp)); - - return dp[index][prev+1] = len; - } + int lengthOfLIS(vector& nums) { int n = nums.size(); - vector> dp(n,vector(n+1,-1)); - return helper(0,nums,-1, dp); + vector> dp(n+1,vector(n+1,0)); + + for(int i = n-1; i>=0; --i) + { + for(int prev = i-1; prev >= -1; --prev) + { + int len = dp[i+1][prev+1]; + if(prev == -1 or nums[i] > nums[prev]) + len = max(len , 1 + dp[i+1][i+1]); + dp[i][prev + 1] = len; + } + } + + return dp[0][0]; } -}; \ No newline at end of file +}; + + \ No newline at end of file From b0e5ade8572a035a6ce1a1e4b9807a2114b3c4e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 09:36:02 +0530 Subject: [PATCH 0158/3167] Attach NOTES - LeetHub From 2e8f49ac1bcaa49aa7317c9829551bad321a78e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 09:36:05 +0530 Subject: [PATCH 0159/3167] Time: 509 ms (48.63%), Space: 10.7 MB (24.53%) - LeetHub --- .../0300-longest-increasing-subsequence.cpp | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp index 179f6029..2a780c05 100644 --- a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp +++ b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp @@ -1,24 +1,22 @@ class Solution { public: - - int lengthOfLIS(vector& nums) { int n = nums.size(); - vector> dp(n+1,vector(n+1,0)); + // vector> dp(n+1,vector(n+1,0)); + vector next(n+1,0), curr(n+1,0); - for(int i = n-1; i>=0; --i) + for(int i = n-1; i>= 0; --i) { for(int prev = i-1; prev >= -1; --prev) { - int len = dp[i+1][prev+1]; + int len = 0 + next[prev+1]; if(prev == -1 or nums[i] > nums[prev]) - len = max(len , 1 + dp[i+1][i+1]); - dp[i][prev + 1] = len; + len = max(len , 1 + next[i+1]); + curr[prev+1] = len; } + next = curr; } - return dp[0][0]; + return next[0]; } -}; - - \ No newline at end of file +}; \ No newline at end of file From 235e42547a6e65c66ce8452207db71675dfb9afa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 09:51:11 +0530 Subject: [PATCH 0160/3167] Attach NOTES - LeetHub From d29fa8f75eca83907e2e297e1a6f1744fe92484c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 09:51:15 +0530 Subject: [PATCH 0161/3167] Time: 364 ms (61.87%), Space: 10.6 MB (29.00%) - LeetHub --- .../0300-longest-increasing-subsequence.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp index 2a780c05..63fe08a2 100644 --- a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp +++ b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp @@ -1,22 +1,20 @@ class Solution { public: int lengthOfLIS(vector& nums) { + int n = nums.size(); - // vector> dp(n+1,vector(n+1,0)); - vector next(n+1,0), curr(n+1,0); + vector dp(n+1,1); - for(int i = n-1; i>= 0; --i) + for(int i= 0; i= -1; --prev) + for(int j = 0; j nums[prev]) - len = max(len , 1 + next[i+1]); - curr[prev+1] = len; + if(nums[i] > nums[j]) + dp[i] = max(dp[i], dp[j] + 1); } - next = curr; + } - return next[0]; + return *max_element(dp.begin(),dp.end()); } }; \ No newline at end of file From a73891915b2d1f6f2abb7de493ca8aef543ef82b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 10:49:09 +0530 Subject: [PATCH 0162/3167] Attach NOTES - LeetHub --- 0300-longest-increasing-subsequence/NOTES.md | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/0300-longest-increasing-subsequence/NOTES.md b/0300-longest-increasing-subsequence/NOTES.md index 38c1374a..fcc3fdab 100644 --- a/0300-longest-increasing-subsequence/NOTES.md +++ b/0300-longest-increasing-subsequence/NOTES.md @@ -1 +1,30 @@ -​ \ No newline at end of file +vector dp(n+1,1),hash(n); +int ans = 1, lastIndex = 0; +for(int i= 0; i nums[j] and dp[j] + 1 > dp[i]) +{ +dp[i] = dp[j] + 1; +hash[i] = j; +} +} +if(dp[i] > ans) +{ +ans = dp[i]; +lastIndex = i; +} +} +vector lis; +lis.push_back(nums[lastIndex]); +while(hash[lastIndex] != lastIndex) +{ +lastIndex = hash[lastIndex]; +lis.push_back(nums[lastIndex]); +} +reverse(lis.begin(),lis.end()); +for(auto itr : lis) +cout< Date: Wed, 14 Dec 2022 10:49:15 +0530 Subject: [PATCH 0163/3167] Time: 364 ms (61.87%), Space: 10.8 MB (24.53%) - LeetHub --- .../0300-longest-increasing-subsequence.cpp | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp index 63fe08a2..ad538d86 100644 --- a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp +++ b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp @@ -3,18 +3,41 @@ class Solution { int lengthOfLIS(vector& nums) { int n = nums.size(); - vector dp(n+1,1); + vector dp(n+1,1),hash(n); + int ans = 1, lastIndex = 0; for(int i= 0; i nums[j]) - dp[i] = max(dp[i], dp[j] + 1); + if(nums[i] > nums[j] and dp[j] + 1 > dp[i]) + { + dp[i] = dp[j] + 1; + hash[i] = j; + } } - + if(dp[i] > ans) + { + ans = dp[i]; + lastIndex = i; + } + } + + vector lis; + lis.push_back(nums[lastIndex]); + while(hash[lastIndex] != lastIndex) + { + lastIndex = hash[lastIndex]; + lis.push_back(nums[lastIndex]); } - return *max_element(dp.begin(),dp.end()); + reverse(lis.begin(),lis.end()); + for(auto itr : lis) + cout< Date: Wed, 14 Dec 2022 11:15:20 +0530 Subject: [PATCH 0164/3167] Attach NOTES - LeetHub From a84b8c5434bc3c7ae9c84b39ef7c44a703561745 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 14:27:08 +0530 Subject: [PATCH 0165/3167] Create README - LeetHub --- 0410-split-array-largest-sum/README.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0410-split-array-largest-sum/README.md diff --git a/0410-split-array-largest-sum/README.md b/0410-split-array-largest-sum/README.md new file mode 100644 index 00000000..3715962f --- /dev/null +++ b/0410-split-array-largest-sum/README.md @@ -0,0 +1,32 @@ +

410. Split Array Largest Sum

Hard


Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized.

+ +

Return the minimized largest sum of the split.

+ +

A subarray is a contiguous part of the array.

+ +

 

+

Example 1:

+ +
Input: nums = [7,2,5,10,8], k = 2
+Output: 18
+Explanation: There are four ways to split nums into two subarrays.
+The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,4,5], k = 2
+Output: 9
+Explanation: There are four ways to split nums into two subarrays.
+The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 0 <= nums[i] <= 106
  • +
  • 1 <= k <= min(50, nums.length)
  • +
+
\ No newline at end of file From 70740abe1cef87c13fb0350c6bbed612989315f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 14:27:09 +0530 Subject: [PATCH 0166/3167] Attach NOTES - LeetHub --- 0410-split-array-largest-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0410-split-array-largest-sum/NOTES.md diff --git a/0410-split-array-largest-sum/NOTES.md b/0410-split-array-largest-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0410-split-array-largest-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2c9869ce73e4dd8ad70d221a9f636aec69e957c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 14:27:12 +0530 Subject: [PATCH 0167/3167] Time: 0 ms (100.00%), Space: 7.4 MB (58.50%) - LeetHub --- .../0410-split-array-largest-sum.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0410-split-array-largest-sum/0410-split-array-largest-sum.cpp diff --git a/0410-split-array-largest-sum/0410-split-array-largest-sum.cpp b/0410-split-array-largest-sum/0410-split-array-largest-sum.cpp new file mode 100644 index 00000000..98f31f83 --- /dev/null +++ b/0410-split-array-largest-sum/0410-split-array-largest-sum.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + + int canSplit(vector& nums, int mid) + { + int count = 1; + int sum = 0; + + for(auto itr : nums) + { + if(sum + itr > mid) + { + sum = itr; + ++count; + } + else + sum += itr; + } + return count; + } + + int splitArray(vector& nums, int k) { + + int low = *max_element(nums.begin(),nums.end()); + int high = accumulate(nums.begin(),nums.end(),0); + + int ans = low; + + while(low <= high) + { + int mid = (low + high) / 2; + + if(canSplit(nums,mid) > k) + { + low = mid + 1; + } + else + { + ans = mid; + high = mid - 1; + } + } + + return ans; + } +}; \ No newline at end of file From 7f19eb8492596c6d4c0fad300b0022aeda749523 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 23:04:41 +0530 Subject: [PATCH 0168/3167] Create README - LeetHub --- 0198-house-robber/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0198-house-robber/README.md diff --git a/0198-house-robber/README.md b/0198-house-robber/README.md new file mode 100644 index 00000000..4784d722 --- /dev/null +++ b/0198-house-robber/README.md @@ -0,0 +1,29 @@ +

198. House Robber

Medium


You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

+ +

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1]
+Output: 4
+Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
+Total amount you can rob = 1 + 3 = 4.
+
+ +

Example 2:

+ +
Input: nums = [2,7,9,3,1]
+Output: 12
+Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
+Total amount you can rob = 2 + 9 + 1 = 12.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 400
  • +
+
\ No newline at end of file From ccf3ef8cd2a5b9e4206c225925302d65194c56fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Dec 2022 23:04:44 +0530 Subject: [PATCH 0169/3167] Time: 5 ms (22.94%), Space: 7.9 MB (6.39%) - LeetHub --- 0198-house-robber/0198-house-robber.cpp | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0198-house-robber/0198-house-robber.cpp diff --git a/0198-house-robber/0198-house-robber.cpp b/0198-house-robber/0198-house-robber.cpp new file mode 100644 index 00000000..80669e31 --- /dev/null +++ b/0198-house-robber/0198-house-robber.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + + int helper(int index, vector& nums, vector& dp) + { + if(index >= nums.size()) + return 0; + + if(dp[index] != -1) + return dp[index]; + + int pick = nums[index] + helper(index+2, nums, dp); + int nopick = helper(index+1, nums, dp); + + return dp[index] = max(pick,nopick); + } + + int rob(vector& nums) { + + int n = nums.size(); + + vector dp(n,-1); + + return helper(0,nums, dp); + } +}; \ No newline at end of file From 84d4f522b53b28a55eef1198e4de903752649db4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Dec 2022 18:23:04 +0530 Subject: [PATCH 0170/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2501-longest-square-streak-in-an-array/README.md diff --git a/2501-longest-square-streak-in-an-array/README.md b/2501-longest-square-streak-in-an-array/README.md new file mode 100644 index 00000000..ea7949f1 --- /dev/null +++ b/2501-longest-square-streak-in-an-array/README.md @@ -0,0 +1,38 @@ +

2501. Longest Square Streak in an Array

Medium


You are given an integer array nums. A subsequence of nums is called a square streak if:

+ +
    +
  • The length of the subsequence is at least 2, and
  • +
  • after sorting the subsequence, each element (except the first element) is the square of the previous number.
  • +
+ +

Return the length of the longest square streak in nums, or return -1 if there is no square streak.

+ +

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: nums = [4,3,6,16,8,2]
+Output: 3
+Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
+- 4 = 2 * 2.
+- 16 = 4 * 4.
+Therefore, [4,16,2] is a square streak.
+It can be shown that every subsequence of length 4 is not a square streak.
+
+ +

Example 2:

+ +
Input: nums = [2,3,5,6,7]
+Output: -1
+Explanation: There is no square streak in nums so return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 2 <= nums[i] <= 105
  • +
+
\ No newline at end of file From 66cdf5628fe146d97f1e8b3d39ec99cd7e327053 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Dec 2022 18:23:08 +0530 Subject: [PATCH 0171/3167] Time: 310 ms (89.71%), Space: 110.5 MB (31.04%) - LeetHub --- ...2501-longest-square-streak-in-an-array.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2501-longest-square-streak-in-an-array/2501-longest-square-streak-in-an-array.cpp diff --git a/2501-longest-square-streak-in-an-array/2501-longest-square-streak-in-an-array.cpp b/2501-longest-square-streak-in-an-array/2501-longest-square-streak-in-an-array.cpp new file mode 100644 index 00000000..902dbddb --- /dev/null +++ b/2501-longest-square-streak-in-an-array/2501-longest-square-streak-in-an-array.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int longestSquareStreak(vector& nums) { + + int n = nums.size(); + unordered_map mp; + + for(auto itr : nums) + ++mp[itr]; + + sort(nums.begin(),nums.end()); + + int ans = 1; + + for(int i = 0; i Date: Fri, 16 Dec 2022 18:33:32 +0530 Subject: [PATCH 0172/3167] Create README - LeetHub --- 0232-implement-queue-using-stacks/README.md | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0232-implement-queue-using-stacks/README.md diff --git a/0232-implement-queue-using-stacks/README.md b/0232-implement-queue-using-stacks/README.md new file mode 100644 index 00000000..044efecc --- /dev/null +++ b/0232-implement-queue-using-stacks/README.md @@ -0,0 +1,48 @@ +

232. Implement Queue using Stacks

Easy


Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

+ +

Implement the MyQueue class:

+ +
    +
  • void push(int x) Pushes element x to the back of the queue.
  • +
  • int pop() Removes the element from the front of the queue and returns it.
  • +
  • int peek() Returns the element at the front of the queue.
  • +
  • boolean empty() Returns true if the queue is empty, false otherwise.
  • +
+ +

Notes:

+ +
    +
  • You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • +
  • Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyQueue", "push", "push", "peek", "pop", "empty"]
+[[], [1], [2], [], [], []]
+Output
+[null, null, null, 1, 1, false]
+
+Explanation
+MyQueue myQueue = new MyQueue();
+myQueue.push(1); // queue is: [1]
+myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
+myQueue.peek(); // return 1
+myQueue.pop(); // return 1, queue is [2]
+myQueue.empty(); // return false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= x <= 9
  • +
  • At most 100 calls will be made to push, pop, peek, and empty.
  • +
  • All the calls to pop and peek are valid.
  • +
+ +

 

+

Follow-up: Can you implement the queue such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.

+
\ No newline at end of file From a957dfee80b6f5ad326eae50d03d7dff4744976d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Dec 2022 18:33:32 +0530 Subject: [PATCH 0173/3167] Attach NOTES - LeetHub --- 0232-implement-queue-using-stacks/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0232-implement-queue-using-stacks/NOTES.md diff --git a/0232-implement-queue-using-stacks/NOTES.md b/0232-implement-queue-using-stacks/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0232-implement-queue-using-stacks/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5e6819d77a228981ba44fb2cfe55fe3719bfd8fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Dec 2022 18:33:35 +0530 Subject: [PATCH 0174/3167] Time: 4 ms (30.47%), Space: 7.1 MB (54.50%) - LeetHub --- .../0232-implement-queue-using-stacks.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0232-implement-queue-using-stacks/0232-implement-queue-using-stacks.cpp diff --git a/0232-implement-queue-using-stacks/0232-implement-queue-using-stacks.cpp b/0232-implement-queue-using-stacks/0232-implement-queue-using-stacks.cpp new file mode 100644 index 00000000..607eed35 --- /dev/null +++ b/0232-implement-queue-using-stacks/0232-implement-queue-using-stacks.cpp @@ -0,0 +1,50 @@ +class MyQueue { +public: + + stack s; + MyQueue() { + + } + + void push(int x) { + pushHelper(x); + } + + void pushHelper(int x) + { + if(s.size() == 0) + { + s.push(x); + return; + } + + int data = s.top(); + s.pop(); + pushHelper(x); + s.push(data); + return; + } + + int pop() { + int a = s.top(); + s.pop(); + return a; + } + + int peek() { + return s.top(); + } + + bool empty() { + return s.empty(); + } +}; + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue* obj = new MyQueue(); + * obj->push(x); + * int param_2 = obj->pop(); + * int param_3 = obj->peek(); + * bool param_4 = obj->empty(); + */ \ No newline at end of file From 49750615e4d6a160f0020df0855d8b1ada3a7243 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Dec 2022 19:57:11 +0530 Subject: [PATCH 0175/3167] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0673-number-of-longest-increasing-subsequence/README.md diff --git a/0673-number-of-longest-increasing-subsequence/README.md b/0673-number-of-longest-increasing-subsequence/README.md new file mode 100644 index 00000000..31ee34a3 --- /dev/null +++ b/0673-number-of-longest-increasing-subsequence/README.md @@ -0,0 +1,27 @@ +

673. Number of Longest Increasing Subsequence

Medium


Given an integer array nums, return the number of longest increasing subsequences.

+ +

Notice that the sequence has to be strictly increasing.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,5,4,7]
+Output: 2
+Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
+
+ +

Example 2:

+ +
Input: nums = [2,2,2,2,2]
+Output: 5
+Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 2000
  • +
  • -106 <= nums[i] <= 106
  • +
+
\ No newline at end of file From fc5aaae0b043826a0b195e89416b2ae0ec88367e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Dec 2022 19:57:14 +0530 Subject: [PATCH 0176/3167] Time: 221 ms (73.66%), Space: 13.3 MB (54.83%) - LeetHub --- ...mber-of-longest-increasing-subsequence.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp diff --git a/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp b/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp new file mode 100644 index 00000000..18f2c851 --- /dev/null +++ b/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int findNumberOfLIS(vector& nums) { + + int n = nums.size(); + vector dp(n,1), cnt(n,1); + int maxi = 1,ans = 0; + + for(int i = 0; i nums[j] and dp[j] + 1 > dp[i]) + { + dp[i] = dp[j] + 1; + cnt[i] = cnt[j]; + } + else if(nums[i] > nums[j] and dp[j] + 1 == dp[i]) + cnt[i] += cnt[j]; + } + maxi = max(dp[i],maxi); + } + + for(int i = 0; i Date: Sat, 17 Dec 2022 08:28:37 +0530 Subject: [PATCH 0177/3167] Attach NOTES - LeetHub --- 0198-house-robber/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0198-house-robber/NOTES.md diff --git a/0198-house-robber/NOTES.md b/0198-house-robber/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0198-house-robber/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bc86c4c0a2d23372a09b6944c0c2037435e3b0fd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Dec 2022 08:28:40 +0530 Subject: [PATCH 0178/3167] Time: 2 ms (52.49%), Space: 7.9 MB (26.46%) - LeetHub --- 0198-house-robber/0198-house-robber.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/0198-house-robber/0198-house-robber.cpp b/0198-house-robber/0198-house-robber.cpp index 80669e31..58673e64 100644 --- a/0198-house-robber/0198-house-robber.cpp +++ b/0198-house-robber/0198-house-robber.cpp @@ -1,26 +1,22 @@ class Solution { public: - int helper(int index, vector& nums, vector& dp) + int helper(int index, int n, vector& nums, vector& dp) { - if(index >= nums.size()) + if(index >= n) return 0; - if(dp[index] != -1) return dp[index]; + int take = nums[index] + helper(index + 2, n,nums,dp); + int notTake = helper(index+1,n, nums , dp); - int pick = nums[index] + helper(index+2, nums, dp); - int nopick = helper(index+1, nums, dp); - - return dp[index] = max(pick,nopick); + return dp[index] = max(take, notTake); } int rob(vector& nums) { int n = nums.size(); - vector dp(n,-1); - - return helper(0,nums, dp); + return helper(0,n,nums,dp); } }; \ No newline at end of file From 900db811f25be5a90fa0ede57c888bfed46ccab8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Dec 2022 08:46:32 +0530 Subject: [PATCH 0179/3167] Attach NOTES - LeetHub From 715026a484a889e3c07ec38a58ef2d5abbed3978 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Dec 2022 08:46:34 +0530 Subject: [PATCH 0180/3167] Time: 0 ms (100.00%), Space: 7.9 MB (26.46%) - LeetHub --- 0198-house-robber/0198-house-robber.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/0198-house-robber/0198-house-robber.cpp b/0198-house-robber/0198-house-robber.cpp index 58673e64..4f8504fb 100644 --- a/0198-house-robber/0198-house-robber.cpp +++ b/0198-house-robber/0198-house-robber.cpp @@ -16,7 +16,19 @@ class Solution { int rob(vector& nums) { int n = nums.size(); - vector dp(n,-1); - return helper(0,n,nums,dp); + vector dp(n+1,0); + + dp[n-1] = nums[n-1]; + for(int i = n-2; i>=0; --i) + { + int take = 0; + if(i+1 < n) + take = nums[i] + dp[i+2]; + int notTake = dp[i+1]; + + dp[i] = max(take, notTake); + } + + return dp[0]; } }; \ No newline at end of file From c04e1b9e41132c09a5bf084bd93666d23ff902c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Dec 2022 10:07:14 +0530 Subject: [PATCH 0181/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0150-evaluate-reverse-polish-notation/README.md diff --git a/0150-evaluate-reverse-polish-notation/README.md b/0150-evaluate-reverse-polish-notation/README.md new file mode 100644 index 00000000..1cde875c --- /dev/null +++ b/0150-evaluate-reverse-polish-notation/README.md @@ -0,0 +1,44 @@ +

150. Evaluate Reverse Polish Notation

Medium


Evaluate the value of an arithmetic expression in Reverse Polish Notation.

+ +

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

+ +

Note that division between two integers should truncate toward zero.

+ +

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

+ +

 

+

Example 1:

+ +
Input: tokens = ["2","1","+","3","*"]
+Output: 9
+Explanation: ((2 + 1) * 3) = 9
+
+ +

Example 2:

+ +
Input: tokens = ["4","13","5","/","+"]
+Output: 6
+Explanation: (4 + (13 / 5)) = 6
+
+ +

Example 3:

+ +
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
+Output: 22
+Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
+= ((10 * (6 / (12 * -11))) + 17) + 5
+= ((10 * (6 / -132)) + 17) + 5
+= ((10 * 0) + 17) + 5
+= (0 + 17) + 5
+= 17 + 5
+= 22
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= tokens.length <= 104
  • +
  • tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].
  • +
+
\ No newline at end of file From 232bafbbb6922d9ac53af76671a50032ba7e0830 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Dec 2022 10:07:18 +0530 Subject: [PATCH 0182/3167] Time: 18 ms (69.24%), Space: 11.9 MB (93.63%) - LeetHub --- .../0150-evaluate-reverse-polish-notation.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0150-evaluate-reverse-polish-notation/0150-evaluate-reverse-polish-notation.cpp diff --git a/0150-evaluate-reverse-polish-notation/0150-evaluate-reverse-polish-notation.cpp b/0150-evaluate-reverse-polish-notation/0150-evaluate-reverse-polish-notation.cpp new file mode 100644 index 00000000..3f8839b1 --- /dev/null +++ b/0150-evaluate-reverse-polish-notation/0150-evaluate-reverse-polish-notation.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int evalRPN(vector& tokens) { + stack st; + for(auto itr : tokens) + { + if(itr == "+" or itr == "-" or itr == "*" or itr == "/") + { + int a = st.top(); + st.pop(); + int b = st.top(); + st.pop(); + if(itr == "+") + a = b + a; + if(itr == "-") + a = b - a; + if(itr == "*") + a = (long long)b * a; + if(itr == "/") + a = b / a; + st.push(a); + } + else + st.push(stoi(itr)); + } + return st.top(); + } +}; + + From 85c74fc63d57f9e442f17f67a5c81d91ced01c4b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 15:19:46 +0530 Subject: [PATCH 0183/3167] Create README - LeetHub --- 0739-daily-temperatures/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0739-daily-temperatures/README.md diff --git a/0739-daily-temperatures/README.md b/0739-daily-temperatures/README.md new file mode 100644 index 00000000..cb144a23 --- /dev/null +++ b/0739-daily-temperatures/README.md @@ -0,0 +1,21 @@ +

739. Daily Temperatures

Medium


Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

+ +

 

+

Example 1:

+
Input: temperatures = [73,74,75,71,69,72,76,73]
+Output: [1,1,4,2,1,1,0,0]
+

Example 2:

+
Input: temperatures = [30,40,50,60]
+Output: [1,1,1,0]
+

Example 3:

+
Input: temperatures = [30,60,90]
+Output: [1,1,0]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= temperatures.length <= 105
  • +
  • 30 <= temperatures[i] <= 100
  • +
+
\ No newline at end of file From 9822747b6ead6badac4cef1f540fb1effbb9ef64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 15:19:46 +0530 Subject: [PATCH 0184/3167] Attach NOTES - LeetHub --- 0739-daily-temperatures/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0739-daily-temperatures/NOTES.md diff --git a/0739-daily-temperatures/NOTES.md b/0739-daily-temperatures/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0739-daily-temperatures/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 90fad07bf56327c489f197074b8702b1242c9cb4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 15:19:49 +0530 Subject: [PATCH 0185/3167] Time: 183 ms (98.37%), Space: 107.6 MB (12.64%) - LeetHub --- .../0739-daily-temperatures.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0739-daily-temperatures/0739-daily-temperatures.cpp diff --git a/0739-daily-temperatures/0739-daily-temperatures.cpp b/0739-daily-temperatures/0739-daily-temperatures.cpp new file mode 100644 index 00000000..d8b2a9c3 --- /dev/null +++ b/0739-daily-temperatures/0739-daily-temperatures.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector dailyTemperatures(vector& temperatures) { + + int n = temperatures.size(); + vector res(n,0); + // next_greater decreasin stack + + stack> st; + + for(int i = 0; i st.top().first) + { + pair here = st.top(); + st.pop(); + res[here.second] = i - here.second; + } + st.push({temperatures[i],i}); + } + + return res; + } +}; \ No newline at end of file From 7207f428e057120cb4f8c55ade7969abfd6bd29c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 15:19:56 +0530 Subject: [PATCH 0186/3167] Attach NOTES - LeetHub From 33c79ff72a172a9f434c562356c3cd2ebe6d3d24 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 15:19:59 +0530 Subject: [PATCH 0187/3167] Time: 183 ms (98.37%), Space: 107.6 MB (12.64%) - LeetHub From 8c7b5e26446b4de91f89d232190ca58a340aa658 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 19:55:34 +0530 Subject: [PATCH 0188/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2508-add-edges-to-make-degrees-of-all-nodes-even/README.md diff --git a/2508-add-edges-to-make-degrees-of-all-nodes-even/README.md b/2508-add-edges-to-make-degrees-of-all-nodes-even/README.md new file mode 100644 index 00000000..55f111f4 --- /dev/null +++ b/2508-add-edges-to-make-degrees-of-all-nodes-even/README.md @@ -0,0 +1,41 @@ +

2508. Add Edges to Make Degrees of All Nodes Even

Hard


There is an undirected graph consisting of n nodes numbered from 1 to n. You are given the integer n and a 2D array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi. The graph can be disconnected.

+ +

You can add at most two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.

+ +

Return true if it is possible to make the degree of each node in the graph even, otherwise return false.

+ +

The degree of a node is the number of edges connected to it.

+ +

 

+

Example 1:

+ +
Input: n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
+Output: true
+Explanation: The above diagram shows a valid way of adding an edge.
+Every node in the resulting graph is connected to an even number of edges.
+
+ +

Example 2:

+ +
Input: n = 4, edges = [[1,2],[3,4]]
+Output: true
+Explanation: The above diagram shows a valid way of adding two edges.
+ +

Example 3:

+ +
Input: n = 4, edges = [[1,2],[1,3],[1,4]]
+Output: false
+Explanation: It is not possible to obtain a valid graph with adding at most 2 edges.
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n <= 105
  • +
  • 2 <= edges.length <= 105
  • +
  • edges[i].length == 2
  • +
  • 1 <= ai, bi <= n
  • +
  • ai != bi
  • +
  • There are no repeated edges.
  • +
+
\ No newline at end of file From abbd9ab5245fadb92207290f57a8885fa4d77077 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 19:55:38 +0530 Subject: [PATCH 0189/3167] Time: 1614 ms (35.00%), Space: 179.3 MB (35.00%) - LeetHub --- ...dges-to-make-degrees-of-all-nodes-even.cpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2508-add-edges-to-make-degrees-of-all-nodes-even/2508-add-edges-to-make-degrees-of-all-nodes-even.cpp diff --git a/2508-add-edges-to-make-degrees-of-all-nodes-even/2508-add-edges-to-make-degrees-of-all-nodes-even.cpp b/2508-add-edges-to-make-degrees-of-all-nodes-even/2508-add-edges-to-make-degrees-of-all-nodes-even.cpp new file mode 100644 index 00000000..8aaa12ca --- /dev/null +++ b/2508-add-edges-to-make-degrees-of-all-nodes-even/2508-add-edges-to-make-degrees-of-all-nodes-even.cpp @@ -0,0 +1,66 @@ +class Solution { +public: + + pair removeTwo(vector& odd, int one , int two) + { + vector res; + for(int i = 0; i<4; ++i) + { + if(i == one or i == two) continue; + res.push_back(odd[i]); + } + return {res[0],res[1]}; + } + + + bool isPossible(int n, vector>& edges) { + + vector degree(n+1,0); + set> allEdge; + vector odd; + + for(auto itr : edges) + { + ++degree[itr[0]]; + ++degree[itr[1]]; + + allEdge.insert({itr[0],itr[1]}); + allEdge.insert({itr[1],itr[0]}); + } + + for(int i = 0 ; i 4) return false; + + if(odd.size() == 2) + { + for(int i = 1; i<=n; ++i) + { + pair edge1 = {odd[0],i}; + pair edge2 = {odd[1],i}; + + if(allEdge.count(edge1) == 0 and allEdge.count(edge2) == 0) return true; + } + return false; + } + + if(odd.size() == 4) + { + for(int i = 1; i<4; ++i){ + pair edge1 = {odd[0],odd[i]}; + pair edge2 = removeTwo(odd,0,i); + + if(allEdge.count(edge1) == 0 and allEdge.count(edge2) == 0) return true; + } + return false; + } + + return false; + } +}; \ No newline at end of file From dc350dbf77455c395424fd9c898ff0a1d7a5c199 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 19:56:36 +0530 Subject: [PATCH 0190/3167] Attach NOTES - LeetHub --- 2508-add-edges-to-make-degrees-of-all-nodes-even/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2508-add-edges-to-make-degrees-of-all-nodes-even/NOTES.md diff --git a/2508-add-edges-to-make-degrees-of-all-nodes-even/NOTES.md b/2508-add-edges-to-make-degrees-of-all-nodes-even/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2508-add-edges-to-make-degrees-of-all-nodes-even/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f6038c65abc3281bdfb115d920f9ada26d47df9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 19:56:40 +0530 Subject: [PATCH 0191/3167] Time: 1614 ms (35.00%), Space: 179.3 MB (35.00%) - LeetHub From 99ebdefacc136ac2263fa9b5f0c67d8546648edb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 19:56:58 +0530 Subject: [PATCH 0192/3167] Attach NOTES - LeetHub From bc01681abf201799eb53562f2c48ea82d42378b4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Dec 2022 19:57:01 +0530 Subject: [PATCH 0193/3167] Time: 1614 ms (35.00%), Space: 179.3 MB (35.00%) - LeetHub From 0b7b35828f2054fff74753c56c0be1fc8c4d6791 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Dec 2022 18:51:57 +0530 Subject: [PATCH 0194/3167] Create README - LeetHub --- 0841-keys-and-rooms/README.md | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0841-keys-and-rooms/README.md diff --git a/0841-keys-and-rooms/README.md b/0841-keys-and-rooms/README.md new file mode 100644 index 00000000..81f7458a --- /dev/null +++ b/0841-keys-and-rooms/README.md @@ -0,0 +1,38 @@ +

841. Keys and Rooms

Medium


There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.

+ +

When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.

+ +

Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: rooms = [[1],[2],[3],[]]
+Output: true
+Explanation: 
+We visit room 0 and pick up key 1.
+We then visit room 1 and pick up key 2.
+We then visit room 2 and pick up key 3.
+We then visit room 3.
+Since we were able to visit every room, we return true.
+
+ +

Example 2:

+ +
Input: rooms = [[1,3],[3,0,1],[2],[0]]
+Output: false
+Explanation: We can not enter room number 2 since the only key that unlocks it is in that room.
+
+ +

 

+

Constraints:

+ +
    +
  • n == rooms.length
  • +
  • 2 <= n <= 1000
  • +
  • 0 <= rooms[i].length <= 1000
  • +
  • 1 <= sum(rooms[i].length) <= 3000
  • +
  • 0 <= rooms[i][j] < n
  • +
  • All the values of rooms[i] are unique.
  • +
+
\ No newline at end of file From d511ab5e06ec86b540acc5f92c0560946dd7f1cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Dec 2022 18:52:00 +0530 Subject: [PATCH 0195/3167] Time: 7 ms (96.41%), Space: 10.5 MB (58.80%) - LeetHub --- 0841-keys-and-rooms/0841-keys-and-rooms.cpp | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0841-keys-and-rooms/0841-keys-and-rooms.cpp diff --git a/0841-keys-and-rooms/0841-keys-and-rooms.cpp b/0841-keys-and-rooms/0841-keys-and-rooms.cpp new file mode 100644 index 00000000..51262f89 --- /dev/null +++ b/0841-keys-and-rooms/0841-keys-and-rooms.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + + void dfs(int sv, vector>& adj, vector& visited) + { + visited[sv] = true; + for(auto itr : adj[sv]) + { + if(!visited[itr]) + dfs(itr,adj,visited); + } + } + + bool canVisitAllRooms(vector>& rooms) { + + int n = rooms.size(); + vector visited(n,false); + + dfs(0,rooms,visited); + + for(int i = 0; i Date: Tue, 20 Dec 2022 18:54:38 +0530 Subject: [PATCH 0196/3167] Attach NOTES - LeetHub --- 0841-keys-and-rooms/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0841-keys-and-rooms/NOTES.md diff --git a/0841-keys-and-rooms/NOTES.md b/0841-keys-and-rooms/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0841-keys-and-rooms/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1351011036d0dfb84c79ddcc68591f9c56459b6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Dec 2022 18:54:41 +0530 Subject: [PATCH 0197/3167] Time: 3 ms (99.61%), Space: 10.5 MB (58.80%) - LeetHub --- 0841-keys-and-rooms/0841-keys-and-rooms.cpp | 29 ++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/0841-keys-and-rooms/0841-keys-and-rooms.cpp b/0841-keys-and-rooms/0841-keys-and-rooms.cpp index 51262f89..cc195490 100644 --- a/0841-keys-and-rooms/0841-keys-and-rooms.cpp +++ b/0841-keys-and-rooms/0841-keys-and-rooms.cpp @@ -1,28 +1,27 @@ class Solution { public: - - void dfs(int sv, vector>& adj, vector& visited) - { - visited[sv] = true; - for(auto itr : adj[sv]) - { - if(!visited[itr]) - dfs(itr,adj,visited); - } - } - bool canVisitAllRooms(vector>& rooms) { int n = rooms.size(); vector visited(n,false); - dfs(0,rooms,visited); + queue q; + q.push(0); - for(int i = 0; i Date: Wed, 21 Dec 2022 19:04:50 +0530 Subject: [PATCH 0198/3167] Create README - LeetHub --- 0886-possible-bipartition/README.md | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0886-possible-bipartition/README.md diff --git a/0886-possible-bipartition/README.md b/0886-possible-bipartition/README.md new file mode 100644 index 00000000..50c984c4 --- /dev/null +++ b/0886-possible-bipartition/README.md @@ -0,0 +1,36 @@ +

886. Possible Bipartition

Medium


We want to split a group of n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group.

+ +

Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way.

+ +

 

+

Example 1:

+ +
Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
+Output: true
+Explanation: group1 [1,4] and group2 [2,3].
+
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2000
  • +
  • 0 <= dislikes.length <= 104
  • +
  • dislikes[i].length == 2
  • +
  • 1 <= dislikes[i][j] <= n
  • +
  • ai < bi
  • +
  • All the pairs of dislikes are unique.
  • +
+
\ No newline at end of file From 25d86ed88ca560e7afbccc3b96bb78fcecbc6ed2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:04:53 +0530 Subject: [PATCH 0199/3167] Time: 450 ms (49.61%), Space: 63.8 MB (93.59%) - LeetHub --- .../0886-possible-bipartition.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0886-possible-bipartition/0886-possible-bipartition.cpp diff --git a/0886-possible-bipartition/0886-possible-bipartition.cpp b/0886-possible-bipartition/0886-possible-bipartition.cpp new file mode 100644 index 00000000..13e2d8fb --- /dev/null +++ b/0886-possible-bipartition/0886-possible-bipartition.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + bool possibleBipartition(int n, vector>& dislikes) { + vector color(n+1,0); + vector adj[n+1]; + for(int i=0;i q; + q.push(i); + while(!q.empty()){ + int node=q.front(); + q.pop(); + for(int child:adj[node]){ + if(color[child]==color[node])return false; + if(!color[child]){ + q.push(child); + color[child]=-1*color[node]; + } + } + } + } + } + + return true; + } +}; \ No newline at end of file From b95f94db7ba3581877c002d03a9e891e8eb7048c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Dec 2022 19:27:10 +0530 Subject: [PATCH 0200/3167] Create README - LeetHub --- 0560-subarray-sum-equals-k/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0560-subarray-sum-equals-k/README.md diff --git a/0560-subarray-sum-equals-k/README.md b/0560-subarray-sum-equals-k/README.md new file mode 100644 index 00000000..ab207d65 --- /dev/null +++ b/0560-subarray-sum-equals-k/README.md @@ -0,0 +1,21 @@ +

560. Subarray Sum Equals K

Medium


Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 2 * 104
  • +
  • -1000 <= nums[i] <= 1000
  • +
  • -107 <= k <= 107
  • +
+
\ No newline at end of file From 17d56475788ff059f872a2b105f2dc3bd8856a45 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Dec 2022 19:27:14 +0530 Subject: [PATCH 0201/3167] Time: 96 ms (90.99%), Space: 41.5 MB (50.98%) - LeetHub --- .../0560-subarray-sum-equals-k.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp diff --git a/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp b/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp new file mode 100644 index 00000000..6d94c12e --- /dev/null +++ b/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int subarraySum(vector& nums, int k) { + + int n = nums.size(); + + int sum = 0; + int count = 0; + + unordered_map mp; + mp.insert({0,1}); + + for(int i = 0; i < n; ++i) + { + sum += nums[i]; + + if(mp.find(sum - k) != mp.end()) + count += mp[sum - k]; + if(mp.find(sum) != mp.end()) + ++mp[sum]; + else + mp[sum] = 1; + } + + return count; + } +}; \ No newline at end of file From d1230c825d3f2702aba0cb9df832c950f56aef01 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Dec 2022 00:04:19 +0530 Subject: [PATCH 0202/3167] Create README - LeetHub --- 0834-sum-of-distances-in-tree/README.md | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0834-sum-of-distances-in-tree/README.md diff --git a/0834-sum-of-distances-in-tree/README.md b/0834-sum-of-distances-in-tree/README.md new file mode 100644 index 00000000..1393c3ed --- /dev/null +++ b/0834-sum-of-distances-in-tree/README.md @@ -0,0 +1,41 @@ +

834. Sum of Distances in Tree

Hard


There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.

+ +

You are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.

+ +

Return an array answer of length n where answer[i] is the sum of the distances between the ith node in the tree and all other nodes.

+ +

 

+

Example 1:

+ +
Input: n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
+Output: [8,12,6,10,10,10]
+Explanation: The tree is shown above.
+We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
+equals 1 + 1 + 2 + 2 + 2 = 8.
+Hence, answer[0] = 8, and so on.
+
+ +

Example 2:

+ +
Input: n = 1, edges = []
+Output: [0]
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 3 * 104
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • The given input represents a valid tree.
  • +
+
\ No newline at end of file From cb31a013b422572ba6658e30c4d8c317a2183cb4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Dec 2022 00:04:22 +0530 Subject: [PATCH 0203/3167] Time: 260 ms (96.18%), Space: 87.4 MB (77.75%) - LeetHub --- .../0834-sum-of-distances-in-tree.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0834-sum-of-distances-in-tree/0834-sum-of-distances-in-tree.cpp diff --git a/0834-sum-of-distances-in-tree/0834-sum-of-distances-in-tree.cpp b/0834-sum-of-distances-in-tree/0834-sum-of-distances-in-tree.cpp new file mode 100644 index 00000000..022e61d1 --- /dev/null +++ b/0834-sum-of-distances-in-tree/0834-sum-of-distances-in-tree.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + vector> v; + vector counter, res; + + void dfs(int i, int p = -1) { + for(auto u : v[i]) { + if(u == p) continue; + dfs(u, i); + counter[i] += counter[u]; + res[i] += res[u] + counter[u]; + } + counter[i] += 1; + } + + void dfs2(int i, int n, int p = -1) { + for(auto u : v[i]) { + if(u == p) continue; + res[u] = res[i] - counter[u] + n - counter[u]; + dfs2(u, n, i); + } + } + + vector sumOfDistancesInTree(int n, vector>& edges) { + v.resize(n); + for(int i = 0; i < n - 1; i++) { + int a = edges[i][0]; + int b = edges[i][1]; + v[a].push_back(b); + v[b].push_back(a); + } + res.resize(n); + counter.resize(n); + dfs(0); + dfs2(0, n); + return res; + } +}; \ No newline at end of file From 354005434b6f81eddab612b46a7a221eba22a72a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Dec 2022 07:42:48 +0530 Subject: [PATCH 0204/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0309-best-time-to-buy-and-sell-stock-with-cooldown/README.md diff --git a/0309-best-time-to-buy-and-sell-stock-with-cooldown/README.md b/0309-best-time-to-buy-and-sell-stock-with-cooldown/README.md new file mode 100644 index 00000000..38466a8b --- /dev/null +++ b/0309-best-time-to-buy-and-sell-stock-with-cooldown/README.md @@ -0,0 +1,32 @@ +

309. Best Time to Buy and Sell Stock with Cooldown

Medium


You are given an array prices where prices[i] is the price of a given stock on the ith day.

+ +

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:

+ +
    +
  • After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
  • +
+ +

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

+ +

 

+

Example 1:

+ +
Input: prices = [1,2,3,0,2]
+Output: 3
+Explanation: transactions = [buy, sell, cooldown, buy, sell]
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= prices.length <= 5000
  • +
  • 0 <= prices[i] <= 1000
  • +
+
\ No newline at end of file From c8d5811487b9ec00d29d850697f5ad5ecfc5a756 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Dec 2022 07:42:48 +0530 Subject: [PATCH 0205/3167] Attach NOTES - LeetHub --- 0309-best-time-to-buy-and-sell-stock-with-cooldown/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0309-best-time-to-buy-and-sell-stock-with-cooldown/NOTES.md diff --git a/0309-best-time-to-buy-and-sell-stock-with-cooldown/NOTES.md b/0309-best-time-to-buy-and-sell-stock-with-cooldown/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0309-best-time-to-buy-and-sell-stock-with-cooldown/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 21c4ab0a671f9485f5dd8db095d11dd5a4515223 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Dec 2022 07:42:51 +0530 Subject: [PATCH 0206/3167] Time: 8 ms (55.55%), Space: 12 MB (25.27%) - LeetHub --- ...me-to-buy-and-sell-stock-with-cooldown.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0309-best-time-to-buy-and-sell-stock-with-cooldown/0309-best-time-to-buy-and-sell-stock-with-cooldown.cpp diff --git a/0309-best-time-to-buy-and-sell-stock-with-cooldown/0309-best-time-to-buy-and-sell-stock-with-cooldown.cpp b/0309-best-time-to-buy-and-sell-stock-with-cooldown/0309-best-time-to-buy-and-sell-stock-with-cooldown.cpp new file mode 100644 index 00000000..3f431264 --- /dev/null +++ b/0309-best-time-to-buy-and-sell-stock-with-cooldown/0309-best-time-to-buy-and-sell-stock-with-cooldown.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + + int helper(int index, int buy, vector& prices, vector>& dp) + { + if(index >= prices.size()) + return 0; + if(dp[index][buy] != -1) + return dp[index][buy]; + if(buy) + return dp[index][buy] = max(-prices[index] + helper(index+1,0,prices ,dp), helper(index+1, 1,prices,dp)); + return dp[index][buy] = max(prices[index] + helper(index+2,1,prices, dp), helper(index+1,0,prices ,dp)); + } + + int maxProfit(vector& prices) { + + int n = prices.size(); + vector> dp(n,vector(2,-1)); + + return helper(0,1,prices,dp); + } +}; \ No newline at end of file From a3f0f25953b2d965d701a82ce49f27a5f668d00e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Dec 2022 21:53:14 +0530 Subject: [PATCH 0207/3167] Create README - LeetHub --- 0790-domino-and-tromino-tiling/README.md | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0790-domino-and-tromino-tiling/README.md diff --git a/0790-domino-and-tromino-tiling/README.md b/0790-domino-and-tromino-tiling/README.md new file mode 100644 index 00000000..0071e07e --- /dev/null +++ b/0790-domino-and-tromino-tiling/README.md @@ -0,0 +1,27 @@ +

790. Domino and Tromino Tiling

Medium


You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes.

+ +

Given an integer n, return the number of ways to tile an 2 x n board. Since the answer may be very large, return it modulo 109 + 7.

+ +

In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 5
+Explanation: The five different ways are show above.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file From 953237d9a962b38c27026fcd91c3cacb0696119d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Dec 2022 21:53:18 +0530 Subject: [PATCH 0208/3167] Time: 0 ms (100.00%), Space: 6 MB (71.49%) - LeetHub --- .../0790-domino-and-tromino-tiling.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0790-domino-and-tromino-tiling/0790-domino-and-tromino-tiling.cpp diff --git a/0790-domino-and-tromino-tiling/0790-domino-and-tromino-tiling.cpp b/0790-domino-and-tromino-tiling/0790-domino-and-tromino-tiling.cpp new file mode 100644 index 00000000..21a87ddb --- /dev/null +++ b/0790-domino-and-tromino-tiling/0790-domino-and-tromino-tiling.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + const int MOD = 1e9+7; + int dp[1001][2]{}; + int numTilings(int n) { + return solve(0, n, false); + } + long solve(int i, int n, bool previousGap) { + if(i > n) return 0; + if(i == n) return !previousGap; + if(dp[i][previousGap]) return dp[i][previousGap]; + if(previousGap) + return dp[i][previousGap] = (solve(i+1, n, false) + solve(i+1, n, true)) % MOD; + return dp[i][previousGap] = (solve(i+1, n, false) + solve(i+2, n, false) + 2*solve(i+2, n, true)) % MOD; + } +}; \ No newline at end of file From 30f023b81dc653186bf31efcbbf1e1969dbb57a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 13:15:12 +0530 Subject: [PATCH 0209/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2513-minimize-the-maximum-of-two-arrays/README.md diff --git a/2513-minimize-the-maximum-of-two-arrays/README.md b/2513-minimize-the-maximum-of-two-arrays/README.md new file mode 100644 index 00000000..d2dbb416 --- /dev/null +++ b/2513-minimize-the-maximum-of-two-arrays/README.md @@ -0,0 +1,48 @@ +

2513. Minimize the Maximum of Two Arrays

Medium


We have two arrays arr1 and arr2 which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:

+ +
    +
  • arr1 contains uniqueCnt1 distinct positive integers, each of which is not divisible by divisor1.
  • +
  • arr2 contains uniqueCnt2 distinct positive integers, each of which is not divisible by divisor2.
  • +
  • No integer is present in both arr1 and arr2.
  • +
+ +

Given divisor1, divisor2, uniqueCnt1, and uniqueCnt2, return the minimum possible maximum integer that can be present in either array.

+ +

 

+

Example 1:

+ +
Input: divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3
+Output: 4
+Explanation: 
+We can distribute the first 4 natural numbers into arr1 and arr2.
+arr1 = [1] and arr2 = [2,3,4].
+We can see that both arrays satisfy all the conditions.
+Since the maximum value is 4, we return it.
+
+ +

Example 2:

+ +
Input: divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
+Output: 3
+Explanation: 
+Here arr1 = [1,2], and arr2 = [3] satisfy all conditions.
+Since the maximum value is 3, we return it.
+ +

Example 3:

+ +
Input: divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2
+Output: 15
+Explanation: 
+Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6].
+It can be shown that it is not possible to obtain a lower maximum satisfying all conditions. 
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= divisor1, divisor2 <= 105
  • +
  • 1 <= uniqueCnt1, uniqueCnt2 < 109
  • +
  • 2 <= uniqueCnt1 + uniqueCnt2 <= 109
  • +
+
\ No newline at end of file From a63def68c9f43dac92ba73f0b98ccb9ff1e00bab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 13:15:15 +0530 Subject: [PATCH 0210/3167] Time: 0 ms (100.00%), Space: 5.9 MB (100.00%) - LeetHub --- ...513-minimize-the-maximum-of-two-arrays.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2513-minimize-the-maximum-of-two-arrays/2513-minimize-the-maximum-of-two-arrays.cpp diff --git a/2513-minimize-the-maximum-of-two-arrays/2513-minimize-the-maximum-of-two-arrays.cpp b/2513-minimize-the-maximum-of-two-arrays/2513-minimize-the-maximum-of-two-arrays.cpp new file mode 100644 index 00000000..d8e412bb --- /dev/null +++ b/2513-minimize-the-maximum-of-two-arrays/2513-minimize-the-maximum-of-two-arrays.cpp @@ -0,0 +1,31 @@ +#define ll long long int +class Solution { +public: + int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) { + + ll start = 1, end = INT_MAX, mid; + ll lcm = divisor1; + lcm*= divisor2; + + lcm /= __gcd(divisor1,divisor2); + + ll ans = end; + while(start <= end) + { + mid = start + (end - start)/2; + + ll first = mid - (mid/divisor1); + ll second = mid - (mid/divisor2); + ll both = mid - (mid/divisor1) - (mid/divisor2) + mid/lcm; + + if(first >= uniqueCnt1 and second >= uniqueCnt2 and first + second - both >= uniqueCnt1 + uniqueCnt2) + { + ans = mid; + end = mid-1; + } + else + start = mid + 1; + } + return (int)ans; + } +}; \ No newline at end of file From f2306bad837823f34899b05aca961feba585e701 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 17:48:40 +0530 Subject: [PATCH 0211/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2515-shortest-distance-to-target-string-in-a-circular-array/README.md diff --git a/2515-shortest-distance-to-target-string-in-a-circular-array/README.md b/2515-shortest-distance-to-target-string-in-a-circular-array/README.md new file mode 100644 index 00000000..baa658f1 --- /dev/null +++ b/2515-shortest-distance-to-target-string-in-a-circular-array/README.md @@ -0,0 +1,49 @@ +

2515. Shortest Distance to Target String in a Circular Array

Easy


You are given a 0-indexed circular string array words and a string target. A circular array means that the array's end connects to the array's beginning.

+ +
    +
  • Formally, the next element of words[i] is words[(i + 1) % n] and the previous element of words[i] is words[(i - 1 + n) % n], where n is the length of words.
  • +
+ +

Starting from startIndex, you can move to either the next word or the previous word with 1 step at a time.

+ +

Return the shortest distance needed to reach the string target. If the string target does not exist in words, return -1.

+ +

 

+

Example 1:

+ +
Input: words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
+Output: 1
+Explanation: We start from index 1 and can reach "hello" by
+- moving 3 units to the right to reach index 4.
+- moving 2 units to the left to reach index 4.
+- moving 4 units to the right to reach index 0.
+- moving 1 unit to the left to reach index 0.
+The shortest distance to reach "hello" is 1.
+
+ +

Example 2:

+ +
Input: words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
+Output: 1
+Explanation: We start from index 0 and can reach "leetcode" by
+- moving 2 units to the right to reach index 3.
+- moving 1 unit to the left to reach index 3.
+The shortest distance to reach "leetcode" is 1.
+ +

Example 3:

+ +
Input: words = ["i","eat","leetcode"], target = "ate", startIndex = 0
+Output: -1
+Explanation: Since "ate" does not exist in words, we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] and target consist of only lowercase English letters.
  • +
  • 0 <= startIndex < words.length
  • +
+
\ No newline at end of file From 1f9851875fdd2c192d8f764531bf9400714bbe3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 17:48:44 +0530 Subject: [PATCH 0212/3167] Time: 38 ms (83.33%), Space: 14.6 MB (33.33%) - LeetHub --- ...ce-to-target-string-in-a-circular-array.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2515-shortest-distance-to-target-string-in-a-circular-array/2515-shortest-distance-to-target-string-in-a-circular-array.cpp diff --git a/2515-shortest-distance-to-target-string-in-a-circular-array/2515-shortest-distance-to-target-string-in-a-circular-array.cpp b/2515-shortest-distance-to-target-string-in-a-circular-array/2515-shortest-distance-to-target-string-in-a-circular-array.cpp new file mode 100644 index 00000000..0b11c2f8 --- /dev/null +++ b/2515-shortest-distance-to-target-string-in-a-circular-array/2515-shortest-distance-to-target-string-in-a-circular-array.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int closetTarget(vector& words, string target, int startIndex) { + + int n = words.size(); + int ans = INT_MAX; + for(int i = 0; i < words.size(); ++i) + { + if(words[i] == target) + { + ans = min(ans,abs(startIndex - i)); + ans = min({ans,n - i + startIndex, n - startIndex + i}); + } + } + + return (ans == INT_MAX) ? -1 : ans; + } +}; \ No newline at end of file From e20a3c5129c8c35403375bad2b91d62a1394b411 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 18:19:59 +0530 Subject: [PATCH 0213/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2516-take-k-of-each-character-from-left-and-right/README.md diff --git a/2516-take-k-of-each-character-from-left-and-right/README.md b/2516-take-k-of-each-character-from-left-and-right/README.md new file mode 100644 index 00000000..5b5c10a4 --- /dev/null +++ b/2516-take-k-of-each-character-from-left-and-right/README.md @@ -0,0 +1,32 @@ +

2516. Take K of Each Character From Left and Right

Medium


You are given a string s consisting of the characters 'a', 'b', and 'c' and a non-negative integer k. Each minute, you may take either the leftmost character of s, or the rightmost character of s.

+ +

Return the minimum number of minutes needed for you to take at least k of each character, or return -1 if it is not possible to take k of each character.

+ +

 

+

Example 1:

+ +
Input: s = "aabaaaacaabc", k = 2
+Output: 8
+Explanation: 
+Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
+Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
+A total of 3 + 5 = 8 minutes is needed.
+It can be proven that 8 is the minimum number of minutes needed.
+
+ +

Example 2:

+ +
Input: s = "a", k = 1
+Output: -1
+Explanation: It is not possible to take one 'b' or 'c' so return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of only the letters 'a', 'b', and 'c'.
  • +
  • 0 <= k <= s.length
  • +
+
\ No newline at end of file From f42f47170434a32dc294e513281bff1550246c75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 18:20:00 +0530 Subject: [PATCH 0214/3167] Attach NOTES - LeetHub --- 2516-take-k-of-each-character-from-left-and-right/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2516-take-k-of-each-character-from-left-and-right/NOTES.md diff --git a/2516-take-k-of-each-character-from-left-and-right/NOTES.md b/2516-take-k-of-each-character-from-left-and-right/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2516-take-k-of-each-character-from-left-and-right/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a9420cae5c4e926bcc122349774d94a37f91f93e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 18:20:04 +0530 Subject: [PATCH 0215/3167] Time: 334 ms (50.00%), Space: 68.9 MB (33.33%) - LeetHub --- ...-of-each-character-from-left-and-right.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2516-take-k-of-each-character-from-left-and-right/2516-take-k-of-each-character-from-left-and-right.cpp diff --git a/2516-take-k-of-each-character-from-left-and-right/2516-take-k-of-each-character-from-left-and-right.cpp b/2516-take-k-of-each-character-from-left-and-right/2516-take-k-of-each-character-from-left-and-right.cpp new file mode 100644 index 00000000..7259f5ed --- /dev/null +++ b/2516-take-k-of-each-character-from-left-and-right/2516-take-k-of-each-character-from-left-and-right.cpp @@ -0,0 +1,57 @@ +class Solution { +public: + int takeCharacters(string s, int k) { + + int ca,cb, cc; + ca = cb = cc = 0; + int n = s.size(); + unordered_map mpa, mpb, mpc; + int ans = -1; + + mpa[0] = mpb[0] = mpc[0] = n; + + for(int i = n-1 ; i >= 0; --i) + { + if(s[i] == 'a') + { + ++ca; + mpa[ca] = i; + } + if(s[i] == 'b') + { + ++cb; + mpb[cb] = i; + } + if(s[i] == 'c') + { + ++cc; + mpc[cc] = i; + } + } + + ca = cb = cc = 0; + + if(k == 0) + return 0; + + if(mpa.find(k) == mpa.end() or mpb.find(k) == mpb.end() or mpc.find(k) == mpc.end()) + return -1; + + ans = n - min({mpa[k],mpb[k] , mpc[k]}); + + for(int i = 0; i Date: Sun, 25 Dec 2022 19:29:12 +0530 Subject: [PATCH 0216/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2389-longest-subsequence-with-limited-sum/README.md diff --git a/2389-longest-subsequence-with-limited-sum/README.md b/2389-longest-subsequence-with-limited-sum/README.md new file mode 100644 index 00000000..9dd2d09c --- /dev/null +++ b/2389-longest-subsequence-with-limited-sum/README.md @@ -0,0 +1,33 @@ +

2389. Longest Subsequence With Limited Sum

Easy


You are given an integer array nums of length n, and an integer array queries of length m.

+ +

Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i].

+ +

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: nums = [4,5,2,1], queries = [3,10,21]
+Output: [2,3,4]
+Explanation: We answer the queries as follows:
+- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
+- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
+- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.
+
+ +

Example 2:

+ +
Input: nums = [2,3,4,5], queries = [1]
+Output: [0]
+Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • m == queries.length
  • +
  • 1 <= n, m <= 1000
  • +
  • 1 <= nums[i], queries[i] <= 106
  • +
+
\ No newline at end of file From 92ad6a9e51974b2b6e5498be9aeff39a879199be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:29:12 +0530 Subject: [PATCH 0217/3167] Attach NOTES - LeetHub --- 2389-longest-subsequence-with-limited-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2389-longest-subsequence-with-limited-sum/NOTES.md diff --git a/2389-longest-subsequence-with-limited-sum/NOTES.md b/2389-longest-subsequence-with-limited-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2389-longest-subsequence-with-limited-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c41ba8579346781084824280a6d4efb1cdb3e931 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:29:16 +0530 Subject: [PATCH 0218/3167] Time: 48 ms (49.90%), Space: 13.8 MB (65.60%) - LeetHub --- ...9-longest-subsequence-with-limited-sum.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp diff --git a/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp b/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp new file mode 100644 index 00000000..fbcec02a --- /dev/null +++ b/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector answerQueries(vector& nums, vector& queries) { + + sort(nums.begin(),nums.end()); + + vector ans; + for(int i =0 ; i Date: Sun, 25 Dec 2022 19:40:40 +0530 Subject: [PATCH 0219/3167] Attach NOTES - LeetHub From 1ba7239640326ea8ed900241e30087b3fe3de527 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:40:44 +0530 Subject: [PATCH 0220/3167] Time: 48 ms (49.90%), Space: 13.8 MB (65.60%) - LeetHub From ef900c67998edd420217eac5ffed8a0c9325d78f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:41:49 +0530 Subject: [PATCH 0221/3167] Attach NOTES - LeetHub From 8e2a3f2ddf95cd4a51cb0117092887ee7d0720e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:41:52 +0530 Subject: [PATCH 0222/3167] Time: 48 ms (49.90%), Space: 13.8 MB (65.60%) - LeetHub From 2652f3f09c4a7d7ded4217acf9b38133127d8ddb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:43:17 +0530 Subject: [PATCH 0223/3167] Attach NOTES - LeetHub From 6ff70583b2609d66a281996217f93c1d7d90e608 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:43:20 +0530 Subject: [PATCH 0224/3167] Time: 48 ms (49.90%), Space: 13.8 MB (65.60%) - LeetHub From 9e0fbcfc8f8a9a6b3dfe96ab09509f8056fb78db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:46:36 +0530 Subject: [PATCH 0225/3167] Attach NOTES - LeetHub From c31f71af6b338d10888cff5554a49ee09ce60591 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 19:46:40 +0530 Subject: [PATCH 0226/3167] Time: 10 ms (99.50%), Space: 13.8 MB (65.60%) - LeetHub --- ...9-longest-subsequence-with-limited-sum.cpp | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp b/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp index fbcec02a..a9377a4a 100644 --- a/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp +++ b/2389-longest-subsequence-with-limited-sum/2389-longest-subsequence-with-limited-sum.cpp @@ -1,27 +1,20 @@ class Solution { public: vector answerQueries(vector& nums, vector& queries) { - + + int n = nums.size(); sort(nums.begin(),nums.end()); vector ans; - for(int i =0 ; i Date: Sun, 25 Dec 2022 20:25:39 +0530 Subject: [PATCH 0227/3167] Attach NOTES - LeetHub From 3dd22e193627cdae94d6a75e0da18ad47bd1bbc8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Dec 2022 20:25:41 +0530 Subject: [PATCH 0228/3167] Time: 10 ms (99.50%), Space: 13.8 MB (65.60%) - LeetHub From ff0000c069f430d4d5132573c6c82b768efb0161 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Dec 2022 11:37:10 +0530 Subject: [PATCH 0229/3167] Create README - LeetHub --- 0055-jump-game/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0055-jump-game/README.md diff --git a/0055-jump-game/README.md b/0055-jump-game/README.md new file mode 100644 index 00000000..bfb8968a --- /dev/null +++ b/0055-jump-game/README.md @@ -0,0 +1,27 @@ +

55. Jump Game

Medium


You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

+ +

Return true if you can reach the last index, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,1,1,4]
+Output: true
+Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
+
+ +

Example 2:

+ +
Input: nums = [3,2,1,0,4]
+Output: false
+Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 104
  • +
  • 0 <= nums[i] <= 105
  • +
+
\ No newline at end of file From 33846eb92a932b19d3835899822bdb8122d9d233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Dec 2022 11:37:10 +0530 Subject: [PATCH 0230/3167] Attach NOTES - LeetHub --- 0055-jump-game/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0055-jump-game/NOTES.md diff --git a/0055-jump-game/NOTES.md b/0055-jump-game/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0055-jump-game/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 738990410cf4b19a7d7536106b5e51a9a0cb6992 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Dec 2022 11:37:14 +0530 Subject: [PATCH 0231/3167] Time: 73 ms (86.56%), Space: 48.4 MB (48.80%) - LeetHub --- 0055-jump-game/0055-jump-game.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0055-jump-game/0055-jump-game.cpp diff --git a/0055-jump-game/0055-jump-game.cpp b/0055-jump-game/0055-jump-game.cpp new file mode 100644 index 00000000..29d96a82 --- /dev/null +++ b/0055-jump-game/0055-jump-game.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + bool canJump(vector& nums) { + + int n = nums.size(); + int index = n-1; + for(int i = n-1 ; i >= 0; --i) + { + if(nums[i] + i >= index) + index = i; + } + + return (index == 0) ? 1 : 0; + } +}; \ No newline at end of file From 81dc469425b51729cd8d202fc00a6f73910ae830 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Dec 2022 11:38:35 +0530 Subject: [PATCH 0232/3167] Attach NOTES - LeetHub --- 0055-jump-game/NOTES.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/0055-jump-game/NOTES.md b/0055-jump-game/NOTES.md index 38c1374a..361c3c56 100644 --- a/0055-jump-game/NOTES.md +++ b/0055-jump-game/NOTES.md @@ -1 +1,13 @@ -​ \ No newline at end of file +class Solution { +public: +bool canJump(vector& nums) { +int n = nums.size(); +int index = n-1; +for(int i = n-1 ; i >= 0; --i) +{ +if(nums[i] + i >= index) +index = i; +} +return (index == 0) ? 1 : 0; +} +}; \ No newline at end of file From df303aa6be4e8ede80586d9de1ba2558aff39d28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Dec 2022 11:38:38 +0530 Subject: [PATCH 0233/3167] Time: 78 ms (81.37%), Space: 48.4 MB (48.80%) - LeetHub --- 0055-jump-game/0055-jump-game.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/0055-jump-game/0055-jump-game.cpp b/0055-jump-game/0055-jump-game.cpp index 29d96a82..38d10fa1 100644 --- a/0055-jump-game/0055-jump-game.cpp +++ b/0055-jump-game/0055-jump-game.cpp @@ -3,13 +3,15 @@ class Solution { bool canJump(vector& nums) { int n = nums.size(); - int index = n-1; - for(int i = n-1 ; i >= 0; --i) + int reachable = 0; + + for(int i =0 ; i= index) - index = i; + if(reachable < i) + return false; + reachable = max(reachable,nums[i] + i); } - return (index == 0) ? 1 : 0; + return true; } }; \ No newline at end of file From 26fc0fcd6a98b0b7463468c2eb82916cb76b468e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:18:48 +0530 Subject: [PATCH 0234/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2279-maximum-bags-with-full-capacity-of-rocks/README.md diff --git a/2279-maximum-bags-with-full-capacity-of-rocks/README.md b/2279-maximum-bags-with-full-capacity-of-rocks/README.md new file mode 100644 index 00000000..4461ac83 --- /dev/null +++ b/2279-maximum-bags-with-full-capacity-of-rocks/README.md @@ -0,0 +1,42 @@ +

2279. Maximum Bags With Full Capacity of Rocks

Medium


You have n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and rocks. The ith bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also given an integer additionalRocks, the number of additional rocks you can place in any of the bags.

+ +

Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags.

+ +

 

+

Example 1:

+ +
Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2
+Output: 3
+Explanation:
+Place 1 rock in bag 0 and 1 rock in bag 1.
+The number of rocks in each bag are now [2,3,4,4].
+Bags 0, 1, and 2 have full capacity.
+There are 3 bags at full capacity, so we return 3.
+It can be shown that it is not possible to have more than 3 bags at full capacity.
+Note that there may be other ways of placing the rocks that result in an answer of 3.
+
+ +

Example 2:

+ +
Input: capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100
+Output: 3
+Explanation:
+Place 8 rocks in bag 0 and 2 rocks in bag 2.
+The number of rocks in each bag are now [10,2,2].
+Bags 0, 1, and 2 have full capacity.
+There are 3 bags at full capacity, so we return 3.
+It can be shown that it is not possible to have more than 3 bags at full capacity.
+Note that we did not use all of the additional rocks.
+
+ +

 

+

Constraints:

+ +
    +
  • n == capacity.length == rocks.length
  • +
  • 1 <= n <= 5 * 104
  • +
  • 1 <= capacity[i] <= 109
  • +
  • 0 <= rocks[i] <= capacity[i]
  • +
  • 1 <= additionalRocks <= 109
  • +
+
\ No newline at end of file From 2ce7921cbca3473ac730fa190036e717a415f9e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:18:51 +0530 Subject: [PATCH 0235/3167] Time: 207 ms (85.27%), Space: 89.4 MB (50.33%) - LeetHub --- ...ximum-bags-with-full-capacity-of-rocks.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp diff --git a/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp b/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp new file mode 100644 index 00000000..3b64cc23 --- /dev/null +++ b/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { + + priority_queue, greater> pq; + + int n = capacity.size(), count = 0; + + for(int i = 0; i Date: Tue, 27 Dec 2022 19:20:04 +0530 Subject: [PATCH 0236/3167] Attach NOTES - LeetHub --- 2279-maximum-bags-with-full-capacity-of-rocks/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2279-maximum-bags-with-full-capacity-of-rocks/NOTES.md diff --git a/2279-maximum-bags-with-full-capacity-of-rocks/NOTES.md b/2279-maximum-bags-with-full-capacity-of-rocks/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2279-maximum-bags-with-full-capacity-of-rocks/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9efd38ef2778fc71617f717b708743834cab8d1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:20:08 +0530 Subject: [PATCH 0237/3167] Time: 207 ms (85.27%), Space: 89.4 MB (50.33%) - LeetHub From 8abf779385e56caef6dc223c60399358201a7568 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:23:10 +0530 Subject: [PATCH 0238/3167] Attach NOTES - LeetHub From a4a790c72ab37e2f8163cf5d0419cf3b673860ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:23:13 +0530 Subject: [PATCH 0239/3167] Time: 185 ms (97.36%), Space: 84.7 MB (91.43%) - LeetHub --- ...ximum-bags-with-full-capacity-of-rocks.cpp | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp b/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp index 3b64cc23..281227ff 100644 --- a/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp +++ b/2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.cpp @@ -2,30 +2,22 @@ class Solution { public: int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { - priority_queue, greater> pq; - int n = capacity.size(), count = 0; for(int i = 0; i Date: Tue, 27 Dec 2022 19:23:38 +0530 Subject: [PATCH 0240/3167] Attach NOTES - LeetHub From 4df7d5fdabd984483401f968f96bbbefac68157b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:23:43 +0530 Subject: [PATCH 0241/3167] Time: 185 ms (97.36%), Space: 84.7 MB (91.43%) - LeetHub From d04ec1510cfa3f337b53a600b5a1f8887d79d386 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:50:52 +0530 Subject: [PATCH 0242/3167] Create README - LeetHub --- 0011-container-with-most-water/README.md | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0011-container-with-most-water/README.md diff --git a/0011-container-with-most-water/README.md b/0011-container-with-most-water/README.md new file mode 100644 index 00000000..8fcd7fcd --- /dev/null +++ b/0011-container-with-most-water/README.md @@ -0,0 +1,31 @@ +

11. Container With Most Water

Medium


You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

+ +

Find two lines that together with the x-axis form a container, such that the container contains the most water.

+ +

Return the maximum amount of water a container can store.

+ +

Notice that you may not slant the container.

+ +

 

+

Example 1:

+ +
Input: height = [1,8,6,2,5,4,8,3,7]
+Output: 49
+Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • n == height.length
  • +
  • 2 <= n <= 105
  • +
  • 0 <= height[i] <= 104
  • +
+
\ No newline at end of file From 529c4ff249e008360a5950e171923a1c843ba671 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:50:56 +0530 Subject: [PATCH 0243/3167] Time: 111 ms (75.63%), Space: 59.1 MB (39.17%) - LeetHub --- .../0011-container-with-most-water.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0011-container-with-most-water/0011-container-with-most-water.cpp diff --git a/0011-container-with-most-water/0011-container-with-most-water.cpp b/0011-container-with-most-water/0011-container-with-most-water.cpp new file mode 100644 index 00000000..e5ce0f2b --- /dev/null +++ b/0011-container-with-most-water/0011-container-with-most-water.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int maxArea(vector& height) { + + int left = 0, right = height.size()-1; + int maxArea = 0; + + while(left < right) + { + int width = right - left; + int h = min(height[left],height[right]); + + maxArea = max(maxArea, width*h); + + if(height[left] < height[right]) + ++left; + else if(height[left] > height[right]) + --right; + else + ++left , --right; + } + return maxArea; + } +}; \ No newline at end of file From 54226af0a54f031bab468271d9cd56ff308161e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:46:51 +0530 Subject: [PATCH 0244/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1962-remove-stones-to-minimize-the-total/README.md diff --git a/1962-remove-stones-to-minimize-the-total/README.md b/1962-remove-stones-to-minimize-the-total/README.md new file mode 100644 index 00000000..9b307b06 --- /dev/null +++ b/1962-remove-stones-to-minimize-the-total/README.md @@ -0,0 +1,43 @@ +

1962. Remove Stones to Minimize the Total

Medium


You are given a 0-indexed integer array piles, where piles[i] represents the number of stones in the ith pile, and an integer k. You should apply the following operation exactly k times:

+ +
    +
  • Choose any piles[i] and remove floor(piles[i] / 2) stones from it.
  • +
+ +

Notice that you can apply the operation on the same pile more than once.

+ +

Return the minimum possible total number of stones remaining after applying the k operations.

+ +

floor(x) is the greatest integer that is smaller than or equal to x (i.e., rounds x down).

+ +

 

+

Example 1:

+ +
Input: piles = [5,4,9], k = 2
+Output: 12
+Explanation: Steps of a possible scenario are:
+- Apply the operation on pile 2. The resulting piles are [5,4,5].
+- Apply the operation on pile 0. The resulting piles are [3,4,5].
+The total number of stones in [3,4,5] is 12.
+
+ +

Example 2:

+ +
Input: piles = [4,3,6,7], k = 3
+Output: 12
+Explanation: Steps of a possible scenario are:
+- Apply the operation on pile 2. The resulting piles are [4,3,3,7].
+- Apply the operation on pile 3. The resulting piles are [4,3,3,4].
+- Apply the operation on pile 0. The resulting piles are [2,3,3,4].
+The total number of stones in [2,3,3,4] is 12.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= piles.length <= 105
  • +
  • 1 <= piles[i] <= 104
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file From 7f9ecaeebf287c9c46cdfabe007f6a265ff0e108 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:46:55 +0530 Subject: [PATCH 0245/3167] Time: 745 ms (70.35%), Space: 104.9 MB (66.55%) - LeetHub --- ...62-remove-stones-to-minimize-the-total.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp diff --git a/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp b/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp new file mode 100644 index 00000000..ed3dbce9 --- /dev/null +++ b/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int minStoneSum(vector& piles, int k) { + + priority_queue pq; + + for(auto itr : piles) + pq.push(itr); + + while(!pq.empty()) + { + --k; + int here = pq.top(); + pq.pop(); + + here -= here/2; + pq.push(here); + if(!k) + break; + } + + int ans = 0; + while(!pq.empty()) + { + ans += pq.top(); + pq.pop(); + } + + return ans; + } +}; \ No newline at end of file From 5cbd537e74301dcb62c53c0d8de6c1ae596aa3ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:47:48 +0530 Subject: [PATCH 0246/3167] Attach NOTES - LeetHub --- 1962-remove-stones-to-minimize-the-total/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1962-remove-stones-to-minimize-the-total/NOTES.md diff --git a/1962-remove-stones-to-minimize-the-total/NOTES.md b/1962-remove-stones-to-minimize-the-total/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1962-remove-stones-to-minimize-the-total/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 94d8e3c9b2bc38875de1da9f330ddaa040e500fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:47:51 +0530 Subject: [PATCH 0247/3167] Time: 745 ms (70.35%), Space: 104.9 MB (66.55%) - LeetHub From 74455f1cefe9d91d4bbdf985b39f7b1c97076347 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:50:47 +0530 Subject: [PATCH 0248/3167] Attach NOTES - LeetHub From 399102a820b16fba95fd5d84b04a52d8be26466c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:50:49 +0530 Subject: [PATCH 0249/3167] Time: 745 ms (70.35%), Space: 104.9 MB (66.55%) - LeetHub From c2605b2d67fa47d987ff604a617ac8f573840894 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:51:47 +0530 Subject: [PATCH 0250/3167] Attach NOTES - LeetHub From cd3ee2f7d0782de90fb4d3757bb4820af5f41cbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:51:50 +0530 Subject: [PATCH 0251/3167] Time: 745 ms (70.35%), Space: 104.9 MB (66.55%) - LeetHub From 014f305416223f8a6b56d0090ac1a1a015d1a651 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:52:09 +0530 Subject: [PATCH 0252/3167] Attach NOTES - LeetHub From 52e13c45d5ae4a3e1e36be597a62e31aefb1b0e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:52:12 +0530 Subject: [PATCH 0253/3167] Time: 533 ms (94.14%), Space: 104.9 MB (66.55%) - LeetHub --- ...62-remove-stones-to-minimize-the-total.cpp | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp b/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp index ed3dbce9..c5729186 100644 --- a/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp +++ b/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp @@ -7,25 +7,16 @@ class Solution { for(auto itr : piles) pq.push(itr); - while(!pq.empty()) - { - --k; - int here = pq.top(); - pq.pop(); - - here -= here/2; - pq.push(here); - if(!k) - break; - } + int res = accumulate(begin(piles),end(piles),0); - int ans = 0; - while(!pq.empty()) + while(k--) { - ans += pq.top(); + int here = pq.top(); pq.pop(); + pq.push(here - here/2); + res -= here/2; } - return ans; + return res; } }; \ No newline at end of file From 81a6a500bc74a9a6743c2ff7fb0d9cb367016bd0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:32:10 +0530 Subject: [PATCH 0254/3167] Create README - LeetHub --- 0508-most-frequent-subtree-sum/README.md | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0508-most-frequent-subtree-sum/README.md diff --git a/0508-most-frequent-subtree-sum/README.md b/0508-most-frequent-subtree-sum/README.md new file mode 100644 index 00000000..a12618c5 --- /dev/null +++ b/0508-most-frequent-subtree-sum/README.md @@ -0,0 +1,25 @@ +

508. Most Frequent Subtree Sum

Medium


Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.

+ +

The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: root = [5,2,-5]
+Output: [2]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -105 <= Node.val <= 105
  • +
+
\ No newline at end of file From e75d3a4d136f786bea1af4c3f4cb5da0be139781 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:32:13 +0530 Subject: [PATCH 0255/3167] Time: 7 ms (100.00%), Space: 24.7 MB (52.77%) - LeetHub --- .../0508-most-frequent-subtree-sum.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp diff --git a/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp b/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp new file mode 100644 index 00000000..7fe357fa --- /dev/null +++ b/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp @@ -0,0 +1,48 @@ +/** + * 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: + + unordered_map mp; + int maxCount = 0; + + int helper(TreeNode * root) + { + if(!root) + return 0; + int leftSum = helper(root->left); + int rightSum = helper(root->right); + + int currSum = root->val + leftSum + rightSum; + + ++mp[currSum]; + maxCount = max(maxCount,mp[currSum]); + + return currSum; + } + + vector findFrequentTreeSum(TreeNode* root) { + + helper(root); + + vector ans; + + for(auto itr : mp) + { + if(itr.second == maxCount) + ans.push_back(itr.first); + } + + return ans; + + } +}; \ No newline at end of file From 1bd3bc1596f27e6167c8d63b1187e05a700cd042 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:32:55 +0530 Subject: [PATCH 0256/3167] Attach NOTES - LeetHub --- 0508-most-frequent-subtree-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0508-most-frequent-subtree-sum/NOTES.md diff --git a/0508-most-frequent-subtree-sum/NOTES.md b/0508-most-frequent-subtree-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0508-most-frequent-subtree-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 51830f73142d872c870d7f9b37a2146e7638bbcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:32:58 +0530 Subject: [PATCH 0257/3167] Time: 7 ms (100.00%), Space: 24.7 MB (52.77%) - LeetHub From 007cbdff8cd7d52c1a017ecc8aad2f5678b08ca2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:34:32 +0530 Subject: [PATCH 0258/3167] Attach NOTES - LeetHub From 52170ca757a41bccdfe8780d62d8ffc14da9efb3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:34:35 +0530 Subject: [PATCH 0259/3167] Time: 27 ms (67.35%), Space: 24.7 MB (52.77%) - LeetHub --- .../0508-most-frequent-subtree-sum.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp b/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp index 7fe357fa..1fbe59eb 100644 --- a/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp +++ b/0508-most-frequent-subtree-sum/0508-most-frequent-subtree-sum.cpp @@ -19,11 +19,7 @@ class Solution { { if(!root) return 0; - int leftSum = helper(root->left); - int rightSum = helper(root->right); - - int currSum = root->val + leftSum + rightSum; - + int currSum = root->val + helper(root->left) + helper(root->right); ++mp[currSum]; maxCount = max(maxCount,mp[currSum]); From ef71bf761dd585febc9d2136c0bce1b8352c1ebe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Dec 2022 20:06:43 +0530 Subject: [PATCH 0260/3167] Create README - LeetHub --- 1834-single-threaded-cpu/README.md | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 1834-single-threaded-cpu/README.md diff --git a/1834-single-threaded-cpu/README.md b/1834-single-threaded-cpu/README.md new file mode 100644 index 00000000..0c648b29 --- /dev/null +++ b/1834-single-threaded-cpu/README.md @@ -0,0 +1,53 @@ +

1834. Single-Threaded CPU

Medium


You are given n​​​​​​ tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the i​​​​​​th​​​​ task will be available to process at enqueueTimei and will take processingTimei to finish processing.

+ +

You have a single-threaded CPU that can process at most one task at a time and will act in the following way:

+ +
    +
  • If the CPU is idle and there are no available tasks to process, the CPU remains idle.
  • +
  • If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
  • +
  • Once a task is started, the CPU will process the entire task without stopping.
  • +
  • The CPU can finish a task then start a new one instantly.
  • +
+ +

Return the order in which the CPU will process the tasks.

+ +

 

+

Example 1:

+ +
Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
+Output: [0,2,3,1]
+Explanation: The events go as follows: 
+- At time = 1, task 0 is available to process. Available tasks = {0}.
+- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
+- At time = 2, task 1 is available to process. Available tasks = {1}.
+- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
+- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
+- At time = 4, task 3 is available to process. Available tasks = {1, 3}.
+- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
+- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
+- At time = 10, the CPU finishes task 1 and becomes idle.
+
+ +

Example 2:

+ +
Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
+Output: [4,3,2,0,1]
+Explanation: The events go as follows:
+- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
+- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
+- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
+- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
+- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
+- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
+- At time = 40, the CPU finishes task 1 and becomes idle.
+
+ +

 

+

Constraints:

+ +
    +
  • tasks.length == n
  • +
  • 1 <= n <= 105
  • +
  • 1 <= enqueueTimei, processingTimei <= 109
  • +
+
\ No newline at end of file From 640cbc89a9504ddc51391ca92e2aa562da22706e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Dec 2022 20:06:47 +0530 Subject: [PATCH 0261/3167] Time: 1099 ms (63.49%), Space: 174.2 MB (11.11%) - LeetHub --- .../1834-single-threaded-cpu.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1834-single-threaded-cpu/1834-single-threaded-cpu.cpp diff --git a/1834-single-threaded-cpu/1834-single-threaded-cpu.cpp b/1834-single-threaded-cpu/1834-single-threaded-cpu.cpp new file mode 100644 index 00000000..63215c81 --- /dev/null +++ b/1834-single-threaded-cpu/1834-single-threaded-cpu.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + vector getOrder(vector>& tasks) { + + int n = tasks.size(); + + vector > vp; + for(int i = 0; i < n; ++i) + vp.push_back({tasks[i][0],tasks[i][1],i}); + + sort(vp.begin(),vp.end()); + + priority_queue, vector>, greater> > pq; + + int endTime = vp[0][0], i = 0; + vector ans; + + while(i < n) + { + if(!pq.empty()) + { + endTime += pq.top()[0]; + ans.push_back(pq.top()[1]); + pq.pop(); + } + while(i < n and vp[i][0] <= endTime) + { + pq.push({vp[i][1],vp[i][2],vp[i][1]}); + ++i; + } + if(i < n and endTime < vp[i][0] and pq.empty()) + endTime = vp[i][0]; + } + + while(!pq.empty()) + { + ans.push_back(pq.top()[1]); + pq.pop(); + } + + return ans; + + } +}; \ No newline at end of file From 343a6912daaf3a8c2705e82cc4808f2fb808cd4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Dec 2022 08:55:52 +0530 Subject: [PATCH 0262/3167] Create README - LeetHub --- 0735-asteroid-collision/README.md | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0735-asteroid-collision/README.md diff --git a/0735-asteroid-collision/README.md b/0735-asteroid-collision/README.md new file mode 100644 index 00000000..2c3aa096 --- /dev/null +++ b/0735-asteroid-collision/README.md @@ -0,0 +1,37 @@ +

735. Asteroid Collision

Medium


We are given an array asteroids of integers representing asteroids in a row.

+ +

For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.

+ +

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

+ +

 

+

Example 1:

+ +
Input: asteroids = [5,10,-5]
+Output: [5,10]
+Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
+
+ +

Example 2:

+ +
Input: asteroids = [8,-8]
+Output: []
+Explanation: The 8 and -8 collide exploding each other.
+
+ +

Example 3:

+ +
Input: asteroids = [10,2,-5]
+Output: [10]
+Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= asteroids.length <= 104
  • +
  • -1000 <= asteroids[i] <= 1000
  • +
  • asteroids[i] != 0
  • +
+
\ No newline at end of file From 23407e6243b77cfab7384cfc13ad30bb68e4304d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Dec 2022 08:55:55 +0530 Subject: [PATCH 0263/3167] Time: 22 ms (64.66%), Space: 17.9 MB (40.96%) - LeetHub --- .../0735-asteroid-collision.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0735-asteroid-collision/0735-asteroid-collision.cpp diff --git a/0735-asteroid-collision/0735-asteroid-collision.cpp b/0735-asteroid-collision/0735-asteroid-collision.cpp new file mode 100644 index 00000000..8cff6a6e --- /dev/null +++ b/0735-asteroid-collision/0735-asteroid-collision.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + vector asteroidCollision(vector& asteroids) { + + int n = asteroids.size(); + stack st; + vector ans; + + for(int i = 0; i 0) + st.push(asteroids[i]); + else + { + while(!st.empty() and st.top() > 0 and abs(asteroids[i]) > st.top()) + st.pop(); + if(!st.empty() and st.top() > 0 and st.top() == abs(asteroids[i])) + { + st.pop(); + continue; + } + if(!st.empty() and st.top() > 0 and asteroids[i] < 0) + continue; + st.push(asteroids[i]); + } + } + } + + while(!st.empty()) + { + ans.push_back(st.top()); + st.pop(); + } + + reverse(ans.begin(),ans.end()); + + return ans; + } +}; \ No newline at end of file From 493cbda9aa000e17ad749549bf446a2fda9fe2d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Dec 2022 09:10:14 +0530 Subject: [PATCH 0264/3167] Attach NOTES - LeetHub --- 0735-asteroid-collision/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0735-asteroid-collision/NOTES.md diff --git a/0735-asteroid-collision/NOTES.md b/0735-asteroid-collision/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0735-asteroid-collision/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2f923a5c67ee437a0f3bca2535ec082f9e841ccf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Dec 2022 09:10:17 +0530 Subject: [PATCH 0265/3167] Time: 20 ms (70.15%), Space: 17.9 MB (13.75%) - LeetHub From d04aa4ad2dbe12c18fb50dac2deeb1fa8681ab38 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Dec 2022 09:51:20 +0530 Subject: [PATCH 0266/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0797-all-paths-from-source-to-target/README.md diff --git a/0797-all-paths-from-source-to-target/README.md b/0797-all-paths-from-source-to-target/README.md new file mode 100644 index 00000000..1adf12c2 --- /dev/null +++ b/0797-all-paths-from-source-to-target/README.md @@ -0,0 +1,30 @@ +

797. All Paths From Source to Target

Medium


Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.

+ +

The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).

+ +

 

+

Example 1:

+ +
Input: graph = [[1,2],[3],[3],[]]
+Output: [[0,1,3],[0,2,3]]
+Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • n == graph.length
  • +
  • 2 <= n <= 15
  • +
  • 0 <= graph[i][j] < n
  • +
  • graph[i][j] != i (i.e., there will be no self-loops).
  • +
  • All the elements of graph[i] are unique.
  • +
  • The input graph is guaranteed to be a DAG.
  • +
+
\ No newline at end of file From fc22a6d1669ecb9529298a9fbad68715f0f97044 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Dec 2022 09:51:23 +0530 Subject: [PATCH 0267/3167] Time: 13 ms (90.04%), Space: 10.7 MB (67.21%) - LeetHub --- .../0797-all-paths-from-source-to-target.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0797-all-paths-from-source-to-target/0797-all-paths-from-source-to-target.cpp diff --git a/0797-all-paths-from-source-to-target/0797-all-paths-from-source-to-target.cpp b/0797-all-paths-from-source-to-target/0797-all-paths-from-source-to-target.cpp new file mode 100644 index 00000000..fea94b39 --- /dev/null +++ b/0797-all-paths-from-source-to-target/0797-all-paths-from-source-to-target.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + + void dfs(int sv, vector>& graph, vector>&res, vector& ds) + { + ds.push_back(sv); + + if(sv == graph.size()-1) + res.push_back(ds); + + for(auto itr : graph[sv]) + { + dfs(itr,graph,res,ds); + } + ds.pop_back(); + } + + vector> allPathsSourceTarget(vector>& graph) { + + int n = graph.size(); + vector ds; + vector> res; + + dfs(0,graph,res,ds); + + return res; + } +}; \ No newline at end of file From 369aeb2a40bf5a2a6d96b879aee385071cafc624 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 31 Dec 2022 08:19:38 +0530 Subject: [PATCH 0268/3167] Create README - LeetHub --- 0980-unique-paths-iii/README.md | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0980-unique-paths-iii/README.md diff --git a/0980-unique-paths-iii/README.md b/0980-unique-paths-iii/README.md new file mode 100644 index 00000000..b89c7c9b --- /dev/null +++ b/0980-unique-paths-iii/README.md @@ -0,0 +1,52 @@ +

980. Unique Paths III

Hard


You are given an m x n integer array grid where grid[i][j] could be:

+ +
    +
  • 1 representing the starting square. There is exactly one starting square.
  • +
  • 2 representing the ending square. There is exactly one ending square.
  • +
  • 0 representing empty squares we can walk over.
  • +
  • -1 representing obstacles that we cannot walk over.
  • +
+ +

Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
+Output: 2
+Explanation: We have the following two paths: 
+1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
+2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
+
+ +

Example 2:

+ +
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
+Output: 4
+Explanation: We have the following four paths: 
+1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
+2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
+3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
+4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
+
+ +

Example 3:

+ +
Input: grid = [[0,1],[2,0]]
+Output: 0
+Explanation: There is no path that walks over every empty square exactly once.
+Note that the starting and ending square can be anywhere in the grid.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 20
  • +
  • 1 <= m * n <= 20
  • +
  • -1 <= grid[i][j] <= 2
  • +
  • There is exactly one starting cell and one ending cell.
  • +
+
\ No newline at end of file From 94e529ec841ece20f258b1a3359b176072de6f3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 31 Dec 2022 08:19:42 +0530 Subject: [PATCH 0269/3167] Time: 5 ms (57.49%), Space: 7.1 MB (64.25%) - LeetHub --- .../0980-unique-paths-iii.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0980-unique-paths-iii/0980-unique-paths-iii.cpp diff --git a/0980-unique-paths-iii/0980-unique-paths-iii.cpp b/0980-unique-paths-iii/0980-unique-paths-iii.cpp new file mode 100644 index 00000000..15919935 --- /dev/null +++ b/0980-unique-paths-iii/0980-unique-paths-iii.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int res = 0, empty = 1; + void dfs(vector>& grid, int x, int y, int count) { + if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == -1) return; + + if (grid[x][y] == 2) { + if(empty == count) res++; + return; + } + + grid[x][y] = -1; + + dfs(grid, x+1, y, count+1); + dfs(grid, x-1, y, count+1); + dfs(grid, x, y+1, count+1); + dfs(grid, x, y-1, count+1); + + grid[x][y] = 0; + + } + + int uniquePathsIII(vector>& grid) { + int start_x, start_y; + for (int i = 0; i < grid.size(); i++) { + for (int j = 0; j < grid[0].size(); j++) { + if (grid[i][j] == 1) start_x = i, start_y = j; + else if (grid[i][j] == 0) empty++; + } + } + + dfs(grid, start_x, start_y, 0); + return res; + } +}; \ No newline at end of file From d9954ff635aed757c2966808d663bc4c83b6b6a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 31 Dec 2022 08:21:08 +0530 Subject: [PATCH 0270/3167] Attach NOTES - LeetHub --- 0980-unique-paths-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0980-unique-paths-iii/NOTES.md diff --git a/0980-unique-paths-iii/NOTES.md b/0980-unique-paths-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0980-unique-paths-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6220bdd5bf85a51c8e710664728a724d60a9a212 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 31 Dec 2022 08:21:11 +0530 Subject: [PATCH 0271/3167] Time: 5 ms (57.49%), Space: 7.1 MB (64.25%) - LeetHub From 2180b6701079716820707ba1ca48b39761f58555 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 31 Dec 2022 09:21:39 +0530 Subject: [PATCH 0272/3167] Update README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 08dbfee6..c84a2351 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # Leetcode This Repository Contains All My Solved Leetcode Problems. + +![image](https://user-images.githubusercontent.com/85362504/210123821-c3c67c36-5321-4b00-88de-faa8d3b0d30f.png) + + + Badges + +![image](https://user-images.githubusercontent.com/85362504/210123832-cff189d5-dca8-4b87-a5f3-fc6a961c5138.png) + + Community Stats + +![image](https://user-images.githubusercontent.com/85362504/210123852-f4f351a3-d5c3-4296-9690-b15fbd916d04.png) + + Complete Green + +![image](https://user-images.githubusercontent.com/85362504/210123902-9619044c-c362-4aeb-8280-54dcc0df3798.png) + + 300 + days, Problems Solved Stats + +![image](https://user-images.githubusercontent.com/85362504/210123921-9172d936-7bf7-488d-8088-d3235cb25015.png) + From e40940eef2b97752a114eba9ebb8476982e3dfe8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Jan 2023 08:35:56 +0530 Subject: [PATCH 0273/3167] Create README - LeetHub --- 0290-word-pattern/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0290-word-pattern/README.md diff --git a/0290-word-pattern/README.md b/0290-word-pattern/README.md new file mode 100644 index 00000000..1f095e4c --- /dev/null +++ b/0290-word-pattern/README.md @@ -0,0 +1,35 @@ +

290. Word Pattern

Easy


Given a pattern and a string s, find if s follows the same pattern.

+ +

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

+ +

 

+

Example 1:

+ +
Input: pattern = "abba", s = "dog cat cat dog"
+Output: true
+
+ +

Example 2:

+ +
Input: pattern = "abba", s = "dog cat cat fish"
+Output: false
+
+ +

Example 3:

+ +
Input: pattern = "aaaa", s = "dog cat cat dog"
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= pattern.length <= 300
  • +
  • pattern contains only lower-case English letters.
  • +
  • 1 <= s.length <= 3000
  • +
  • s contains only lowercase English letters and spaces ' '.
  • +
  • s does not contain any leading or trailing spaces.
  • +
  • All the words in s are separated by a single space.
  • +
+
\ No newline at end of file From 3de4e29b199cff0a2c5b24c73274d023b3596ecf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Jan 2023 08:35:56 +0530 Subject: [PATCH 0274/3167] Attach NOTES - LeetHub --- 0290-word-pattern/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0290-word-pattern/NOTES.md diff --git a/0290-word-pattern/NOTES.md b/0290-word-pattern/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0290-word-pattern/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 97c16f7c87534a310565f25dabd87f21917ddfe9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Jan 2023 08:35:59 +0530 Subject: [PATCH 0275/3167] Time: 4 ms (32.33%), Space: 6.5 MB (19.87%) - LeetHub --- 0290-word-pattern/0290-word-pattern.cpp | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0290-word-pattern/0290-word-pattern.cpp diff --git a/0290-word-pattern/0290-word-pattern.cpp b/0290-word-pattern/0290-word-pattern.cpp new file mode 100644 index 00000000..1c1b3f23 --- /dev/null +++ b/0290-word-pattern/0290-word-pattern.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + bool wordPattern(string pattern, string s) { + + int count = 0; + stringstream ss(s); + vector words; + string word; + + int n = pattern.size(); + + while (ss >> word) + words.push_back(word); + + if(n != words.size()) + return false; + + unordered_map mp1; + unordered_map mp2; + + for(int i = 0; i Date: Mon, 2 Jan 2023 00:12:56 +0530 Subject: [PATCH 0276/3167] Attach NOTES - LeetHub From cbebb4da8a172697d445a5bdf0f30da964b2da7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 00:12:58 +0530 Subject: [PATCH 0277/3167] Time: 0 ms (100.00%), Space: 6.5 MB (53.35%) - LeetHub From 6f4facc0647efef81a4b9e39a2691eaf02105ba5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 01:02:02 +0530 Subject: [PATCH 0278/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1277-count-square-submatrices-with-all-ones/README.md diff --git a/1277-count-square-submatrices-with-all-ones/README.md b/1277-count-square-submatrices-with-all-ones/README.md new file mode 100644 index 00000000..9bd542fa --- /dev/null +++ b/1277-count-square-submatrices-with-all-ones/README.md @@ -0,0 +1,43 @@ +

1277. Count Square Submatrices with All Ones

Medium


Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.

+ +

 

+

Example 1:

+ +
Input: matrix =
+[
+  [0,1,1,1],
+  [1,1,1,1],
+  [0,1,1,1]
+]
+Output: 15
+Explanation: 
+There are 10 squares of side 1.
+There are 4 squares of side 2.
+There is  1 square of side 3.
+Total number of squares = 10 + 4 + 1 = 15.
+
+ +

Example 2:

+ +
Input: matrix = 
+[
+  [1,0,1],
+  [1,1,0],
+  [1,1,0]
+]
+Output: 7
+Explanation: 
+There are 6 squares of side 1.  
+There is 1 square of side 2. 
+Total number of squares = 6 + 1 = 7.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 300
  • +
  • 1 <= arr[0].length <= 300
  • +
  • 0 <= arr[i][j] <= 1
  • +
+
\ No newline at end of file From 0088a7aa48a5d45d9363e91d12466f35dad78692 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 01:02:02 +0530 Subject: [PATCH 0279/3167] Attach NOTES - LeetHub --- 1277-count-square-submatrices-with-all-ones/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1277-count-square-submatrices-with-all-ones/NOTES.md diff --git a/1277-count-square-submatrices-with-all-ones/NOTES.md b/1277-count-square-submatrices-with-all-ones/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1277-count-square-submatrices-with-all-ones/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bd72a1141ee434f0edb63c07f7b20aa3adf01b74 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 01:02:05 +0530 Subject: [PATCH 0280/3167] Time: 71 ms (93.43%), Space: 23.7 MB (78.08%) - LeetHub --- ...count-square-submatrices-with-all-ones.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp diff --git a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp new file mode 100644 index 00000000..dcc6504a --- /dev/null +++ b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int countSquares(vector>& matrix) { + + int count = 0; + int n = matrix.size(), m = matrix[0].size(); + + for(int i = 0; i 0 and j > 0 and matrix[i][j] > 0) + { + matrix[i][j] = min({matrix[i-1][j-1],matrix[i-1][j], matrix[i][j-1]}) + 1; + } + count += matrix[i][j]; + } + } + return count; + } +}; + From fce8fa7b3296945e744c89a64c2165917f7c07c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:02:45 +0530 Subject: [PATCH 0281/3167] Create README - LeetHub --- 0520-detect-capital/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0520-detect-capital/README.md diff --git a/0520-detect-capital/README.md b/0520-detect-capital/README.md new file mode 100644 index 00000000..7ab5b8cc --- /dev/null +++ b/0520-detect-capital/README.md @@ -0,0 +1,26 @@ +

520. Detect Capital

Easy


We define the usage of capitals in a word to be right when one of the following cases holds:

+ +
    +
  • All letters in this word are capitals, like "USA".
  • +
  • All letters in this word are not capitals, like "leetcode".
  • +
  • Only the first letter in this word is capital, like "Google".
  • +
+ +

Given a string word, return true if the usage of capitals in it is right.

+ +

 

+

Example 1:

+
Input: word = "USA"
+Output: true
+

Example 2:

+
Input: word = "FlaG"
+Output: false
+
+

 

+

Constraints:

+ +
    +
  • 1 <= word.length <= 100
  • +
  • word consists of lowercase and uppercase English letters.
  • +
+
\ No newline at end of file From 1faa2c08a1c39902edf46cb82bcd7c41fdf329c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:02:45 +0530 Subject: [PATCH 0282/3167] Attach NOTES - LeetHub --- 0520-detect-capital/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0520-detect-capital/NOTES.md diff --git a/0520-detect-capital/NOTES.md b/0520-detect-capital/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0520-detect-capital/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cd99329c6a444f869f5c271fc37c8a622734a357 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:02:49 +0530 Subject: [PATCH 0283/3167] Time: 0 ms (100.00%), Space: 6.1 MB (70.23%) - LeetHub --- 0520-detect-capital/0520-detect-capital.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0520-detect-capital/0520-detect-capital.cpp diff --git a/0520-detect-capital/0520-detect-capital.cpp b/0520-detect-capital/0520-detect-capital.cpp new file mode 100644 index 00000000..ade5c914 --- /dev/null +++ b/0520-detect-capital/0520-detect-capital.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + bool detectCapitalUse(string word) { + + vector ans; + + stringstream ss(word); + string ok; + + while(ss >> ok) + ans.push_back(ok); + + for(auto itr : ans) + { + int caps = 0, small = 0, firstCaps = 0; + + if(itr[0] >= 'A' and itr[0] <= 'Z') + firstCaps = 1; + for(auto x : itr) + { + if(x >= 'a' and x <= 'z') + ++small; + else + ++caps; + } + if(small == itr.size() or caps == itr.size() or (firstCaps == 1 and small == itr.size()-1)) + { + + } + else + return false; + } + return true; + } +}; \ No newline at end of file From b3c05898caab85e402749c94c9cb47ab0479c568 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:07:00 +0530 Subject: [PATCH 0284/3167] Attach NOTES - LeetHub From 3b1884a9b5d6e9d185b2ea638f2656f5474c09cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:07:03 +0530 Subject: [PATCH 0285/3167] Time: 0 ms (100.00%), Space: 6.1 MB (70.23%) - LeetHub From 21acc03a1f6dbd8fda0c1a96b14bf27bad459655 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Jan 2023 19:33:17 +0530 Subject: [PATCH 0286/3167] Create README - LeetHub --- 0944-delete-columns-to-make-sorted/README.md | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0944-delete-columns-to-make-sorted/README.md diff --git a/0944-delete-columns-to-make-sorted/README.md b/0944-delete-columns-to-make-sorted/README.md new file mode 100644 index 00000000..8fb9e242 --- /dev/null +++ b/0944-delete-columns-to-make-sorted/README.md @@ -0,0 +1,56 @@ +

944. Delete Columns to Make Sorted

Easy


You are given an array of n strings strs, all of the same length.

+ +

The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] can be arranged as:

+ +
abc
+bce
+cae
+
+ +

You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted while column 1 ('b', 'c', 'a') is not, so you would delete column 1.

+ +

Return the number of columns that you will delete.

+ +

 

+

Example 1:

+ +
Input: strs = ["cba","daf","ghi"]
+Output: 1
+Explanation: The grid looks as follows:
+  cba
+  daf
+  ghi
+Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
+
+ +

Example 2:

+ +
Input: strs = ["a","b"]
+Output: 0
+Explanation: The grid looks as follows:
+  a
+  b
+Column 0 is the only column and is sorted, so you will not delete any columns.
+
+ +

Example 3:

+ +
Input: strs = ["zyx","wvu","tsr"]
+Output: 3
+Explanation: The grid looks as follows:
+  zyx
+  wvu
+  tsr
+All 3 columns are not sorted, so you will delete all 3.
+
+ +

 

+

Constraints:

+ +
    +
  • n == strs.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= strs[i].length <= 1000
  • +
  • strs[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file From a8a1d6f9633c70143a414ec0bb3557f84d0ff3b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Jan 2023 19:33:21 +0530 Subject: [PATCH 0287/3167] Time: 98 ms (47.41%), Space: 20.2 MB (7.66%) - LeetHub --- .../0944-delete-columns-to-make-sorted.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp diff --git a/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp b/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp new file mode 100644 index 00000000..d594b8bc --- /dev/null +++ b/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minDeletionSize(vector& strs) { + + int n = strs.size(); + int m = strs[0].size(); + int ans = 0; + + for(int i = 0;i Date: Tue, 3 Jan 2023 19:42:48 +0530 Subject: [PATCH 0288/3167] Attach NOTES - LeetHub --- 0944-delete-columns-to-make-sorted/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0944-delete-columns-to-make-sorted/NOTES.md diff --git a/0944-delete-columns-to-make-sorted/NOTES.md b/0944-delete-columns-to-make-sorted/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0944-delete-columns-to-make-sorted/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1329bd742f7c8e03431ae835092c6fa5c5de9f39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Jan 2023 19:42:51 +0530 Subject: [PATCH 0289/3167] Time: 98 ms (47.41%), Space: 12.2 MB (30.23%) - LeetHub --- .../0944-delete-columns-to-make-sorted.cpp | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp b/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp index d594b8bc..b1a44086 100644 --- a/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp +++ b/0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp @@ -1,26 +1,19 @@ class Solution { public: int minDeletionSize(vector& strs) { - - int n = strs.size(); - int m = strs[0].size(); - int ans = 0; - for(int i = 0;i strs[row][col]) + { + ++ans; + break; + } } - - string sortedStr = str; - sort(sortedStr.begin(),sortedStr.end()); - - if(str != sortedStr) - ++ans; } - return ans; } }; \ No newline at end of file From cc0d6d9da87a3d59cd36a5c3542532b38ea2f75b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Jan 2023 13:27:51 +0530 Subject: [PATCH 0290/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2244-minimum-rounds-to-complete-all-tasks/README.md diff --git a/2244-minimum-rounds-to-complete-all-tasks/README.md b/2244-minimum-rounds-to-complete-all-tasks/README.md new file mode 100644 index 00000000..b1d05378 --- /dev/null +++ b/2244-minimum-rounds-to-complete-all-tasks/README.md @@ -0,0 +1,32 @@ +

2244. Minimum Rounds to Complete All Tasks

Medium


You are given a 0-indexed integer array tasks, where tasks[i] represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level.

+ +

Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks.

+ +

 

+

Example 1:

+ +
Input: tasks = [2,2,3,3,2,4,4,4,4,4]
+Output: 4
+Explanation: To complete all the tasks, a possible plan is:
+- In the first round, you complete 3 tasks of difficulty level 2. 
+- In the second round, you complete 2 tasks of difficulty level 3. 
+- In the third round, you complete 3 tasks of difficulty level 4. 
+- In the fourth round, you complete 2 tasks of difficulty level 4.  
+It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
+
+ +

Example 2:

+ +
Input: tasks = [2,3,3]
+Output: -1
+Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= tasks.length <= 105
  • +
  • 1 <= tasks[i] <= 109
  • +
+
\ No newline at end of file From dc6e4e57c9dadf358893c4ad72e3d4eeb7b488c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Jan 2023 13:27:55 +0530 Subject: [PATCH 0291/3167] Time: 521 ms (39.98%), Space: 103.6 MB (72.00%) - LeetHub --- ...4-minimum-rounds-to-complete-all-tasks.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2244-minimum-rounds-to-complete-all-tasks/2244-minimum-rounds-to-complete-all-tasks.cpp diff --git a/2244-minimum-rounds-to-complete-all-tasks/2244-minimum-rounds-to-complete-all-tasks.cpp b/2244-minimum-rounds-to-complete-all-tasks/2244-minimum-rounds-to-complete-all-tasks.cpp new file mode 100644 index 00000000..8dee4506 --- /dev/null +++ b/2244-minimum-rounds-to-complete-all-tasks/2244-minimum-rounds-to-complete-all-tasks.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int minimumRounds(vector& tasks) { + + unordered_map mp; + + int ans = 0; + + for(auto itr : tasks) + ++mp[itr]; + + for(auto itr : mp) + { + if(itr.second == 1) + return -1; + else if(itr.second % 3 == 0) + ans += itr.second/3; + else + ans += itr.second/3 + 1; + } + + return ans; + } +}; \ No newline at end of file From 362dbe2c0e62a5cf9906fa79681aad3e4a4aceea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Jan 2023 18:58:26 +0530 Subject: [PATCH 0292/3167] Attach NOTES - LeetHub From 1e5913698b2d800ff644f85d86cce7d0f684b545 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Jan 2023 18:58:29 +0530 Subject: [PATCH 0293/3167] Time: 11 ms (25.87%), Space: 6.1 MB (74.64%) - LeetHub --- 0520-detect-capital/0520-detect-capital.cpp | 34 +++++---------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/0520-detect-capital/0520-detect-capital.cpp b/0520-detect-capital/0520-detect-capital.cpp index ade5c914..ae35aa50 100644 --- a/0520-detect-capital/0520-detect-capital.cpp +++ b/0520-detect-capital/0520-detect-capital.cpp @@ -2,34 +2,16 @@ class Solution { public: bool detectCapitalUse(string word) { - vector ans; + int caps = 0; + int n = word.size(); - stringstream ss(word); - string ok; - - while(ss >> ok) - ans.push_back(ok); - - for(auto itr : ans) + for(auto itr : word) { - int caps = 0, small = 0, firstCaps = 0; - - if(itr[0] >= 'A' and itr[0] <= 'Z') - firstCaps = 1; - for(auto x : itr) - { - if(x >= 'a' and x <= 'z') - ++small; - else - ++caps; - } - if(small == itr.size() or caps == itr.size() or (firstCaps == 1 and small == itr.size()-1)) - { - - } - else - return false; + if(isupper(itr)) + ++caps; } - return true; + + return caps == 0 or caps == n or (caps == 1 and isupper(word[0])); + } }; \ No newline at end of file From 527b605376f13979543d9488d00cd0a227e59710 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Jan 2023 21:42:18 +0530 Subject: [PATCH 0294/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0452-minimum-number-of-arrows-to-burst-balloons/README.md diff --git a/0452-minimum-number-of-arrows-to-burst-balloons/README.md b/0452-minimum-number-of-arrows-to-burst-balloons/README.md new file mode 100644 index 00000000..2223a5e2 --- /dev/null +++ b/0452-minimum-number-of-arrows-to-burst-balloons/README.md @@ -0,0 +1,41 @@ +

452. Minimum Number of Arrows to Burst Balloons

Medium


There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

+ +

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

+ +

Given the array points, return the minimum number of arrows that must be shot to burst all balloons.

+ +

 

+

Example 1:

+ +
Input: points = [[10,16],[2,8],[1,6],[7,12]]
+Output: 2
+Explanation: The balloons can be burst by 2 arrows:
+- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
+- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
+
+ +

Example 2:

+ +
Input: points = [[1,2],[3,4],[5,6],[7,8]]
+Output: 4
+Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
+
+ +

Example 3:

+ +
Input: points = [[1,2],[2,3],[3,4],[4,5]]
+Output: 2
+Explanation: The balloons can be burst by 2 arrows:
+- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
+- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= points.length <= 105
  • +
  • points[i].length == 2
  • +
  • -231 <= xstart < xend <= 231 - 1
  • +
+
\ No newline at end of file From 644bf31888ba8acc87c41d09467bed1fffe5dad1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Jan 2023 21:42:24 +0530 Subject: [PATCH 0295/3167] Time: 1193 ms (12.24%), Space: 102.3 MB (8.96%) - LeetHub --- ...imum-number-of-arrows-to-burst-balloons.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 0452-minimum-number-of-arrows-to-burst-balloons/0452-minimum-number-of-arrows-to-burst-balloons.cpp diff --git a/0452-minimum-number-of-arrows-to-burst-balloons/0452-minimum-number-of-arrows-to-burst-balloons.cpp b/0452-minimum-number-of-arrows-to-burst-balloons/0452-minimum-number-of-arrows-to-burst-balloons.cpp new file mode 100644 index 00000000..fd04b700 --- /dev/null +++ b/0452-minimum-number-of-arrows-to-burst-balloons/0452-minimum-number-of-arrows-to-burst-balloons.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int findMinArrowShots(vector>& p) { + + sort(p.begin(), p.end()); + int lastpoint = p[0][1]; + int ans = 1; + for(auto point : p) { + if(point[0] > lastpoint) { + ans++; + lastpoint = point[1]; + } + lastpoint = min(point[1],lastpoint); + } + return ans; + + } +}; \ No newline at end of file From 2080e87c6dcfce2a8a16b18401081c27866f3f2d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Jan 2023 21:43:22 +0530 Subject: [PATCH 0296/3167] Attach NOTES - LeetHub --- 0452-minimum-number-of-arrows-to-burst-balloons/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0452-minimum-number-of-arrows-to-burst-balloons/NOTES.md diff --git a/0452-minimum-number-of-arrows-to-burst-balloons/NOTES.md b/0452-minimum-number-of-arrows-to-burst-balloons/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0452-minimum-number-of-arrows-to-burst-balloons/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5a7555a901d9fe14efc6a189cd07746679696a6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Jan 2023 21:44:12 +0530 Subject: [PATCH 0297/3167] Attach NOTES - LeetHub From b1d0443f242718162b9a3bd0f285ebf97e5a5304 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Jan 2023 21:44:17 +0530 Subject: [PATCH 0298/3167] Time: 1193 ms (12.24%), Space: 102.3 MB (8.96%) - LeetHub From efe32f7c442d7029845ac5d6d2289cf62ce58d11 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Jan 2023 08:45:19 +0530 Subject: [PATCH 0299/3167] Create README - LeetHub --- 1833-maximum-ice-cream-bars/README.md | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1833-maximum-ice-cream-bars/README.md diff --git a/1833-maximum-ice-cream-bars/README.md b/1833-maximum-ice-cream-bars/README.md new file mode 100644 index 00000000..c1a4b84f --- /dev/null +++ b/1833-maximum-ice-cream-bars/README.md @@ -0,0 +1,39 @@ +

1833. Maximum Ice Cream Bars

Medium


It is a sweltering summer day, and a boy wants to buy some ice cream bars.

+ +

At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the ith ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible. 

+ +

Return the maximum number of ice cream bars the boy can buy with coins coins.

+ +

Note: The boy can buy the ice cream bars in any order.

+ +

 

+

Example 1:

+ +
Input: costs = [1,3,2,4,1], coins = 7
+Output: 4
+Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
+
+ +

Example 2:

+ +
Input: costs = [10,6,8,7,7,8], coins = 5
+Output: 0
+Explanation: The boy cannot afford any of the ice cream bars.
+
+ +

Example 3:

+ +
Input: costs = [1,6,3,1,2,5], coins = 20
+Output: 6
+Explanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.
+
+ +

 

+

Constraints:

+ +
    +
  • costs.length == n
  • +
  • 1 <= n <= 105
  • +
  • 1 <= costs[i] <= 105
  • +
  • 1 <= coins <= 108
  • +
\ No newline at end of file From 779aa1bf990864df7401aac0534615056f9cce3e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Jan 2023 08:45:23 +0530 Subject: [PATCH 0300/3167] Time: 371 ms (40.93%), Space: 76.6 MB (32.82%) - LeetHub --- .../1833-maximum-ice-cream-bars.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1833-maximum-ice-cream-bars/1833-maximum-ice-cream-bars.cpp diff --git a/1833-maximum-ice-cream-bars/1833-maximum-ice-cream-bars.cpp b/1833-maximum-ice-cream-bars/1833-maximum-ice-cream-bars.cpp new file mode 100644 index 00000000..f12c3ff0 --- /dev/null +++ b/1833-maximum-ice-cream-bars/1833-maximum-ice-cream-bars.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maxIceCream(vector& costs, int coins) { + + int ans = 0; + sort(costs.begin(),costs.end()); + + for(auto itr : costs) + { + if(itr <= coins) + { + ++ans; + coins -= itr; + } + else + { + break; + } + } + + return ans; + } +}; \ No newline at end of file From 9ebf80f08feb080f93ffcc89103110c40abf4790 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Jan 2023 08:47:14 +0530 Subject: [PATCH 0301/3167] Attach NOTES - LeetHub --- 1833-maximum-ice-cream-bars/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1833-maximum-ice-cream-bars/NOTES.md diff --git a/1833-maximum-ice-cream-bars/NOTES.md b/1833-maximum-ice-cream-bars/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1833-maximum-ice-cream-bars/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9042d46868a7e3b4a2c953b40d7a22dc59839924 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Jan 2023 08:47:17 +0530 Subject: [PATCH 0302/3167] Time: 371 ms (40.93%), Space: 76.6 MB (32.82%) - LeetHub From c8233809f4b8eaccf7cec2bdc9ec288da5e34eb4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Jan 2023 23:42:45 +0530 Subject: [PATCH 0303/3167] Create README - LeetHub --- 0134-gas-station/README.md | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0134-gas-station/README.md diff --git a/0134-gas-station/README.md b/0134-gas-station/README.md new file mode 100644 index 00000000..24e2f2e5 --- /dev/null +++ b/0134-gas-station/README.md @@ -0,0 +1,43 @@ +

134. Gas Station

Medium


There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].

+ +

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.

+ +

Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique

+ +

 

+

Example 1:

+ +
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
+Output: 3
+Explanation:
+Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
+Travel to station 4. Your tank = 4 - 1 + 5 = 8
+Travel to station 0. Your tank = 8 - 2 + 1 = 7
+Travel to station 1. Your tank = 7 - 3 + 2 = 6
+Travel to station 2. Your tank = 6 - 4 + 3 = 5
+Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
+Therefore, return 3 as the starting index.
+
+ +

Example 2:

+ +
Input: gas = [2,3,4], cost = [3,4,3]
+Output: -1
+Explanation:
+You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
+Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
+Travel to station 0. Your tank = 4 - 3 + 2 = 3
+Travel to station 1. Your tank = 3 - 3 + 3 = 3
+You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
+Therefore, you can't travel around the circuit once no matter where you start.
+
+ +

 

+

Constraints:

+ +
    +
  • n == gas.length == cost.length
  • +
  • 1 <= n <= 105
  • +
  • 0 <= gas[i], cost[i] <= 104
  • +
+
\ No newline at end of file From 95383db1310125984ea9e4d142733d98c71263c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Jan 2023 23:42:50 +0530 Subject: [PATCH 0304/3167] Time: 101 ms (65.28%), Space: 69.4 MB (55.78%) - LeetHub --- 0134-gas-station/0134-gas-station.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0134-gas-station/0134-gas-station.cpp diff --git a/0134-gas-station/0134-gas-station.cpp b/0134-gas-station/0134-gas-station.cpp new file mode 100644 index 00000000..7c9cd4f4 --- /dev/null +++ b/0134-gas-station/0134-gas-station.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int canCompleteCircuit(vector& gas, vector& cost) { + + int n = gas.size(); + int total_surplus = 0; + int surplus = 0; + int start = 0; + + for(int i = 0; i < n; i++){ + total_surplus += gas[i] - cost[i]; + surplus += gas[i] - cost[i]; + if(surplus < 0){ + surplus = 0; + start = i + 1; + } + } + return (total_surplus < 0) ? -1 : start; + + } +}; \ No newline at end of file From 04d5b181c131fc33b631e26f93bf6afc01a13669 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Jan 2023 19:21:03 +0530 Subject: [PATCH 0305/3167] Create README - LeetHub --- 0149-max-points-on-a-line/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0149-max-points-on-a-line/README.md diff --git a/0149-max-points-on-a-line/README.md b/0149-max-points-on-a-line/README.md new file mode 100644 index 00000000..123643b3 --- /dev/null +++ b/0149-max-points-on-a-line/README.md @@ -0,0 +1,25 @@ +

149. Max Points on a Line

Hard


Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= points.length <= 300
  • +
  • points[i].length == 2
  • +
  • -104 <= xi, yi <= 104
  • +
  • All the points are unique.
  • +
+
\ No newline at end of file From 6af68db528822e5dd2afa3ec18a507e625536a8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Jan 2023 19:21:06 +0530 Subject: [PATCH 0306/3167] Time: 26 ms (96.61%), Space: 13.3 MB (58.55%) - LeetHub --- .../0149-max-points-on-a-line.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0149-max-points-on-a-line/0149-max-points-on-a-line.cpp diff --git a/0149-max-points-on-a-line/0149-max-points-on-a-line.cpp b/0149-max-points-on-a-line/0149-max-points-on-a-line.cpp new file mode 100644 index 00000000..e7eb68dc --- /dev/null +++ b/0149-max-points-on-a-line/0149-max-points-on-a-line.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int maxPoints(vector>& points) { + + int n = points.size(); + + int ans = 1; + + for(int i = 0; i mp; + for(int j = i + 1; j Date: Mon, 9 Jan 2023 18:17:34 +0530 Subject: [PATCH 0307/3167] Attach NOTES - LeetHub From 4ef0d1c7dc5bdb421227f84d638f5ad2b9b80e45 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Jan 2023 18:17:37 +0530 Subject: [PATCH 0308/3167] Time: 0 ms (100.00%), Space: 8.5 MB (14.75%) - LeetHub --- .../0144-binary-tree-preorder-traversal.cpp | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp b/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp index bcf2a442..1ecc542d 100644 --- a/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp +++ b/0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp @@ -13,35 +13,27 @@ class Solution { public: vector preorderTraversal(TreeNode* root) { - vector preorder; - TreeNode* curr = root; + vector ans; + stack st; - while(curr) + while(root or !st.empty()) { - if(!curr->left) + if(root) { - preorder.push_back(curr->val); - curr = curr->right; + ans.push_back(root->val); + if(root->right) + { + st.push(root->right); + } + + root = root->left; } else { - TreeNode* prev = curr->left; - while(prev->right and prev->right != curr) - prev = prev->right; - - if(!prev->right) - { - prev->right = curr; - preorder.push_back(curr->val); - curr = curr->left; - } - else - { - prev->right = nullptr; - curr = curr->right; - } + root = st.top(); + st.pop(); } } - return preorder; + return ans; } -}; \ No newline at end of file +}; From e97261c3ab081c43be0a2ba0e3b282920707dcfb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 10 Jan 2023 18:56:05 +0530 Subject: [PATCH 0309/3167] Create README - LeetHub --- 0100-same-tree/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0100-same-tree/README.md diff --git a/0100-same-tree/README.md b/0100-same-tree/README.md new file mode 100644 index 00000000..ac214aa8 --- /dev/null +++ b/0100-same-tree/README.md @@ -0,0 +1,31 @@ +

100. Same Tree

Easy


Given the roots of two binary trees p and q, write a function to check if they are the same or not.

+ +

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: p = [1,2], q = [1,null,2]
+Output: false
+
+ +

Example 3:

+ +
Input: p = [1,2,1], q = [1,1,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in both trees is in the range [0, 100].
  • +
  • -104 <= Node.val <= 104
  • +
+
\ No newline at end of file From e047cdaab044dcecd857bddee32f57bbf8b85a5d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 10 Jan 2023 18:56:09 +0530 Subject: [PATCH 0310/3167] Time: 8 ms (10.63%), Space: 10.1 MB (8.85%) - LeetHub --- 0100-same-tree/0100-same-tree.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0100-same-tree/0100-same-tree.cpp diff --git a/0100-same-tree/0100-same-tree.cpp b/0100-same-tree/0100-same-tree.cpp new file mode 100644 index 00000000..fd41f371 --- /dev/null +++ b/0100-same-tree/0100-same-tree.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: + + bool helper(TreeNode* p, TreeNode* q) + { + if(!p and !q) + return true; + if(!p and q or p and !q or p->val != q->val) + return false; + return helper(p->left,q->left) and helper(p->right,q->right); + } + + bool isSameTree(TreeNode* p, TreeNode* q) { + return helper(p,q); + } +}; \ No newline at end of file From b61eeae93c87b49422258f6d58888321cb56e55f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 10 Jan 2023 18:57:20 +0530 Subject: [PATCH 0311/3167] Attach NOTES - LeetHub --- 0100-same-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0100-same-tree/NOTES.md diff --git a/0100-same-tree/NOTES.md b/0100-same-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0100-same-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3767be4dfe4403b15aa7aab5a15dcea86f29014c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 10 Jan 2023 18:57:23 +0530 Subject: [PATCH 0312/3167] Time: 8 ms (10.63%), Space: 10.1 MB (8.85%) - LeetHub From 05671597b7bc779a6e0dd27c39a7e9bcae0826b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Jan 2023 21:19:53 +0530 Subject: [PATCH 0313/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1443-minimum-time-to-collect-all-apples-in-a-tree/README.md diff --git a/1443-minimum-time-to-collect-all-apples-in-a-tree/README.md b/1443-minimum-time-to-collect-all-apples-in-a-tree/README.md new file mode 100644 index 00000000..a9756d97 --- /dev/null +++ b/1443-minimum-time-to-collect-all-apples-in-a-tree/README.md @@ -0,0 +1,37 @@ +

1443. Minimum Time to Collect All Apples in a Tree

Medium


Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at vertex 0 and coming back to this vertex.

+ +

The edges of the undirected tree are given in the array edges, where edges[i] = [ai, bi] means that exists an edge connecting the vertices ai and bi. Additionally, there is a boolean array hasApple, where hasApple[i] = true means that vertex i has an apple; otherwise, it does not have any apple.

+ +

 

+

Example 1:

+ +
Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
+Output: 8 
+Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  
+
+ +

Example 2:

+ +
Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
+Output: 6
+Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai < bi <= n - 1
  • +
  • fromi < toi
  • +
  • hasApple.length == n
  • +
+
\ No newline at end of file From c353b4274140ad1854a92df9d9a6039c6ae94c06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Jan 2023 21:19:56 +0530 Subject: [PATCH 0314/3167] Time: 183 ms (90.60%), Space: 60.5 MB (64.77%) - LeetHub --- ...m-time-to-collect-all-apples-in-a-tree.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1443-minimum-time-to-collect-all-apples-in-a-tree/1443-minimum-time-to-collect-all-apples-in-a-tree.cpp diff --git a/1443-minimum-time-to-collect-all-apples-in-a-tree/1443-minimum-time-to-collect-all-apples-in-a-tree.cpp b/1443-minimum-time-to-collect-all-apples-in-a-tree/1443-minimum-time-to-collect-all-apples-in-a-tree.cpp new file mode 100644 index 00000000..8e6550bd --- /dev/null +++ b/1443-minimum-time-to-collect-all-apples-in-a-tree/1443-minimum-time-to-collect-all-apples-in-a-tree.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector> adjList; + int dfs(vector& hasApple,int node,int d,int prev) + { + int result=0,temp; + for(int &i:adjList[node]) + if(i!=prev) + { + temp=dfs(hasApple,i,d+1,node); + if(temp) //If child has apples it'll return a non zero result which is the distance traveled upto that node. + result+=temp-d; + } + return result||hasApple[node]?result+d:0; //If nothing is added to result and current node doesnt have apple return 0 else return distances of children + current distance from root. + + } + int minTime(int n, vector>& edges, vector& hasApple) + { + adjList.resize(n); + for(vector &e:edges) + adjList[e[0]].push_back(e[1]),adjList[e[1]].push_back(e[0]); + return dfs(hasApple,0,0,-1)*2; //Result is doubled the distance travelled as per our observation. + } +}; \ No newline at end of file From 3952e46f8ca43d1378337473d8127a9ddf5e78f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:08:16 +0530 Subject: [PATCH 0315/3167] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1519-number-of-nodes-in-the-sub-tree-with-the-same-label/README.md diff --git a/1519-number-of-nodes-in-the-sub-tree-with-the-same-label/README.md b/1519-number-of-nodes-in-the-sub-tree-with-the-same-label/README.md new file mode 100644 index 00000000..c20a15bc --- /dev/null +++ b/1519-number-of-nodes-in-the-sub-tree-with-the-same-label/README.md @@ -0,0 +1,46 @@ +

1519. Number of Nodes in the Sub-Tree With the Same Label

Medium


You are given a tree (i.e. a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. The root of the tree is the node 0, and each node of the tree has a label which is a lower-case character given in the string labels (i.e. The node with the number i has the label labels[i]).

+ +

The edges array is given on the form edges[i] = [ai, bi], which means there is an edge between nodes ai and bi in the tree.

+ +

Return an array of size n where ans[i] is the number of nodes in the subtree of the ith node which have the same label as node i.

+ +

A subtree of a tree T is the tree consisting of a node in T and all of its descendant nodes.

+ +

 

+

Example 1:

+ +
Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd"
+Output: [2,1,1,1,1,1,1]
+Explanation: Node 0 has label 'a' and its sub-tree has node 2 with label 'a' as well, thus the answer is 2. Notice that any node is part of its sub-tree.
+Node 1 has a label 'b'. The sub-tree of node 1 contains nodes 1,4 and 5, as nodes 4 and 5 have different labels than node 1, the answer is just 1 (the node itself).
+
+ +

Example 2:

+ +
Input: n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb"
+Output: [4,2,1,1]
+Explanation: The sub-tree of node 2 contains only node 2, so the answer is 1.
+The sub-tree of node 3 contains only node 3, so the answer is 1.
+The sub-tree of node 1 contains nodes 1 and 2, both have label 'b', thus the answer is 2.
+The sub-tree of node 0 contains nodes 0, 1, 2 and 3, all with label 'b', thus the answer is 4.
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • labels.length == n
  • +
  • labels is consisting of only of lowercase English letters.
  • +
+
\ No newline at end of file From 199cde1d4d4a0988a4be21e708c575d2fd58a0e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:08:20 +0530 Subject: [PATCH 0316/3167] Time: 631 ms (79.26%), Space: 205.7 MB (74.07%) - LeetHub --- ...es-in-the-sub-tree-with-the-same-label.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1519-number-of-nodes-in-the-sub-tree-with-the-same-label/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.cpp diff --git a/1519-number-of-nodes-in-the-sub-tree-with-the-same-label/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.cpp b/1519-number-of-nodes-in-the-sub-tree-with-the-same-label/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.cpp new file mode 100644 index 00000000..c60434ce --- /dev/null +++ b/1519-number-of-nodes-in-the-sub-tree-with-the-same-label/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector fun(vector> &adj, string &labels, int i,vector&result){ + vector ans(26, 0); + result[i] = 1; + ans[labels[i] - 'a'] = 1; + + for(int j = 0; j != adj[i].size(); j++) + if(!result[adj[i][j]]){ + vector tmp = fun(adj, labels,adj[i][j],result); + for(int k = 0; k != 26; k++) ans[k] += tmp[k]; + } + + result[i] = ans[labels[i] - 'a']; + + return ans; + } + + vector countSubTrees(int n, vector>& edges, string labels) { + vector> adj(n); + vector result(n,0); + for(int i = 0; i != edges.size(); i++) + {adj[edges[i][0]].push_back(edges[i][1]); + adj[edges[i][1]].push_back(edges[i][0]); + } + + fun(adj, labels, 0,result); + return result; + } +}; \ No newline at end of file From d518a2c6ab2293f246ad849fb83a73599742f26d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Jan 2023 23:03:13 +0530 Subject: [PATCH 0317/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2246-longest-path-with-different-adjacent-characters/README.md diff --git a/2246-longest-path-with-different-adjacent-characters/README.md b/2246-longest-path-with-different-adjacent-characters/README.md new file mode 100644 index 00000000..78cb587b --- /dev/null +++ b/2246-longest-path-with-different-adjacent-characters/README.md @@ -0,0 +1,34 @@ +

2246. Longest Path With Different Adjacent Characters

Hard


You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.

+ +

You are also given a string s of length n, where s[i] is the character assigned to node i.

+ +

Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.

+ +

 

+

Example 1:

+ +
Input: parent = [-1,0,0,1,1,2], s = "abacbe"
+Output: 3
+Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned.
+It can be proven that there is no longer path that satisfies the conditions. 
+
+ +

Example 2:

+ +
Input: parent = [-1,0,0,0], s = "aabc"
+Output: 3
+Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • n == parent.length == s.length
  • +
  • 1 <= n <= 105
  • +
  • 0 <= parent[i] <= n - 1 for all i >= 1
  • +
  • parent[0] == -1
  • +
  • parent represents a valid tree.
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file From cb6a64ceeed490bd1db220b283d6e75807bc68bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Jan 2023 23:03:17 +0530 Subject: [PATCH 0318/3167] Time: 523 ms (76.77%), Space: 158.6 MB (91.44%) - LeetHub --- ...ath-with-different-adjacent-characters.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2246-longest-path-with-different-adjacent-characters/2246-longest-path-with-different-adjacent-characters.cpp diff --git a/2246-longest-path-with-different-adjacent-characters/2246-longest-path-with-different-adjacent-characters.cpp b/2246-longest-path-with-different-adjacent-characters/2246-longest-path-with-different-adjacent-characters.cpp new file mode 100644 index 00000000..bb3fc6f6 --- /dev/null +++ b/2246-longest-path-with-different-adjacent-characters/2246-longest-path-with-different-adjacent-characters.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + vector child[100001]; + int ans; + int dfs(string &s, int curr_node) + { + if(child[curr_node].empty())return 1; + int mx1 = 0, mx2 =0; + // traversing over all the child nodes of the curr_node + for(auto &child_node : child[curr_node]) + { + // recursively trying for child nodes + int len = dfs(s, child_node); + ans = max(ans , len); + // rejecting the current node if it's of same character + if(s[curr_node] == s[child_node])continue; + // updating the mx1 and mx2 paths that we can take from all the children of the given node + if(len > mx1) + { + mx2 = mx1; + mx1 = len; + } + //seecond max will be updated + else mx2 = max(mx2 , len); + } + // Update the result. + //Again, max1+mx2+1 means the length of the longest valid path + //going through this node in the sub-tree rooted at this node + ans = max(ans, 1 + mx1 + mx2); + //Adding 1 for the current node + return 1 + mx1; + } + int longestPath(vector& parent, string s){ + int n = parent.size(); + for(int i=1;i Date: Sat, 14 Jan 2023 19:49:39 +0530 Subject: [PATCH 0319/3167] Create README - LeetHub --- .../README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 1061-lexicographically-smallest-equivalent-string/README.md diff --git a/1061-lexicographically-smallest-equivalent-string/README.md b/1061-lexicographically-smallest-equivalent-string/README.md new file mode 100644 index 00000000..0cdfde4a --- /dev/null +++ b/1061-lexicographically-smallest-equivalent-string/README.md @@ -0,0 +1,54 @@ +

1061. Lexicographically Smallest Equivalent String

Medium


You are given two strings of the same length s1 and s2 and a string baseStr.

+ +

We say s1[i] and s2[i] are equivalent characters.

+ +
    +
  • For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'.
  • +
+ +

Equivalent characters follow the usual rules of any equivalence relation:

+ +
    +
  • Reflexivity: 'a' == 'a'.
  • +
  • Symmetry: 'a' == 'b' implies 'b' == 'a'.
  • +
  • Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'.
  • +
+ +

For example, given the equivalency information from s1 = "abc" and s2 = "cde", "acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.

+ +

Return the lexicographically smallest equivalent string of baseStr by using the equivalency information from s1 and s2.

+ +

 

+

Example 1:

+ +
Input: s1 = "parker", s2 = "morris", baseStr = "parser"
+Output: "makkek"
+Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
+The characters in each group are equivalent and sorted in lexicographical order.
+So the answer is "makkek".
+
+ +

Example 2:

+ +
Input: s1 = "hello", s2 = "world", baseStr = "hold"
+Output: "hdld"
+Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
+So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
+
+ +

Example 3:

+ +
Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
+Output: "aauaaaaada"
+Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s1.length, s2.length, baseStr <= 1000
  • +
  • s1.length == s2.length
  • +
  • s1, s2, and baseStr consist of lowercase English letters.
  • +
+
\ No newline at end of file From a7c7ed86f982cf5c85b82649f52bee0d1daccba1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Jan 2023 19:49:43 +0530 Subject: [PATCH 0320/3167] Time: 3 ms (79.82%), Space: 6.5 MB (78.90%) - LeetHub --- ...graphically-smallest-equivalent-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1061-lexicographically-smallest-equivalent-string/1061-lexicographically-smallest-equivalent-string.cpp diff --git a/1061-lexicographically-smallest-equivalent-string/1061-lexicographically-smallest-equivalent-string.cpp b/1061-lexicographically-smallest-equivalent-string/1061-lexicographically-smallest-equivalent-string.cpp new file mode 100644 index 00000000..30071c25 --- /dev/null +++ b/1061-lexicographically-smallest-equivalent-string/1061-lexicographically-smallest-equivalent-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int par[26]; + + int find(int x){ + if(par[x]==-1) return x; + return par[x]=find(par[x]); + } + + void Union(int x, int y) { + x = find(x); + y = find(y); + + if (x != y) + par[max(x, y)] = min(x, y); + } + + string smallestEquivalentString(string s1, string s2, string baseStr) { + + memset(par, -1, sizeof(par)); + + for (auto i = 0; i < s1.size(); ++i) + Union(s1[i] - 'a', s2[i] - 'a'); + + for(auto i=0;i Date: Sun, 15 Jan 2023 22:13:57 +0530 Subject: [PATCH 0321/3167] Create README - LeetHub --- 2421-number-of-good-paths/README.md | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2421-number-of-good-paths/README.md diff --git a/2421-number-of-good-paths/README.md b/2421-number-of-good-paths/README.md new file mode 100644 index 00000000..2c710218 --- /dev/null +++ b/2421-number-of-good-paths/README.md @@ -0,0 +1,55 @@ +

2421. Number of Good Paths

Hard


There is a tree (i.e. a connected, undirected graph with no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges.

+ +

You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node. You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.

+ +

A good path is a simple path that satisfies the following conditions:

+ +
    +
  1. The starting node and the ending node have the same value.
  2. +
  3. All nodes between the starting node and the ending node have values less than or equal to the starting node (i.e. the starting node's value should be the maximum value along the path).
  4. +
+ +

Return the number of distinct good paths.

+ +

Note that a path and its reverse are counted as the same path. For example, 0 -> 1 is considered to be the same as 1 -> 0. A single node is also considered as a valid path.

+ +

 

+

Example 1:

+ +
Input: vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
+Output: 6
+Explanation: There are 5 good paths consisting of a single node.
+There is 1 additional good path: 1 -> 0 -> 2 -> 4.
+(The reverse path 4 -> 2 -> 0 -> 1 is treated as the same as 1 -> 0 -> 2 -> 4.)
+Note that 0 -> 2 -> 3 is not a good path because vals[2] > vals[0].
+
+ +

Example 2:

+ +
Input: vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
+Output: 7
+Explanation: There are 5 good paths consisting of a single node.
+There are 2 additional good paths: 0 -> 1 and 2 -> 3.
+
+ +

Example 3:

+ +
Input: vals = [1], edges = []
+Output: 1
+Explanation: The tree consists of only one node, so there is one good path.
+
+ +

 

+

Constraints:

+ +
    +
  • n == vals.length
  • +
  • 1 <= n <= 3 * 104
  • +
  • 0 <= vals[i] <= 105
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • edges represents a valid tree.
  • +
+
\ No newline at end of file From 5834d9d06f7dc328ba30678e5a96bbade8b93298 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Jan 2023 22:14:01 +0530 Subject: [PATCH 0322/3167] Time: 680 ms (95.05%), Space: 148.2 MB (96.29%) - LeetHub --- .../2421-number-of-good-paths.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2421-number-of-good-paths/2421-number-of-good-paths.cpp diff --git a/2421-number-of-good-paths/2421-number-of-good-paths.cpp b/2421-number-of-good-paths/2421-number-of-good-paths.cpp new file mode 100644 index 00000000..b7ea45ef --- /dev/null +++ b/2421-number-of-good-paths/2421-number-of-good-paths.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int find(vector& y,int i) { + if(i==y[i]) return i; + y[i]=find(y,y[i]); + return y[i]; + } + int numberOfGoodPaths(vector& vals, vector>& edges) { + int n = vals.size(),m=edges.size(),ans=0; + vector> x(n); + vector y(n); + for(int i=0;i& a,vector& b){ + return max(vals[a[0]],vals[a[1]])x[b][0]) y[b]=a; + else y[a]=b; + } + else{ + y[a]=b; + ans+=x[a][1]*x[b][1]; + x[b][1]+=x[a][1]; + } + } + return ans+n; + } +}; \ No newline at end of file From 3f28825f78c961b53d30eafc8e5e99377e7f0d8c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 16 Jan 2023 10:28:09 +0530 Subject: [PATCH 0323/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0698-partition-to-k-equal-sum-subsets/README.md diff --git a/0698-partition-to-k-equal-sum-subsets/README.md b/0698-partition-to-k-equal-sum-subsets/README.md new file mode 100644 index 00000000..45cec40d --- /dev/null +++ b/0698-partition-to-k-equal-sum-subsets/README.md @@ -0,0 +1,25 @@ +

698. Partition to K Equal Sum Subsets

Medium


Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.

+ +

 

+

Example 1:

+ +
Input: nums = [4,3,2,3,5,2,1], k = 4
+Output: true
+Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= k <= nums.length <= 16
  • +
  • 1 <= nums[i] <= 104
  • +
  • The frequency of each element is in the range [1, 4].
  • +
+
\ No newline at end of file From ef7937c4189b965831803881e0b25dbd6c14ebdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 16 Jan 2023 10:28:13 +0530 Subject: [PATCH 0324/3167] Time: 1323 ms (26.80%), Space: 9.4 MB (86.83%) - LeetHub --- .../0698-partition-to-k-equal-sum-subsets.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0698-partition-to-k-equal-sum-subsets/0698-partition-to-k-equal-sum-subsets.cpp diff --git a/0698-partition-to-k-equal-sum-subsets/0698-partition-to-k-equal-sum-subsets.cpp b/0698-partition-to-k-equal-sum-subsets/0698-partition-to-k-equal-sum-subsets.cpp new file mode 100644 index 00000000..77c96166 --- /dev/null +++ b/0698-partition-to-k-equal-sum-subsets/0698-partition-to-k-equal-sum-subsets.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + bool helper(int idx, int currSum, int targetSum, vector& nums, vector& visited, int k) + { + if(k == 0) return true; + if(currSum > targetSum) return false; + if(currSum == targetSum) return helper(nums.size()-1, 0, targetSum, nums, visited, k-1); + + for(int i = idx; i>= 0; --i) + { + if(!visited[i]) + { + visited[i] = true; + if(helper(i - 1, currSum + nums[i], targetSum, nums, visited, k)) + return true; + visited[i] = false; + } + } + + return false; + } + + bool canPartitionKSubsets(vector& nums, int k) { + + int n = nums.size(); + int sum = accumulate(nums.begin(),nums.end(),0); + int maxi = *max_element(nums.begin(),nums.end()); + + if(sum % k != 0 or maxi > sum/k) + return false; + + vector visited(n,false); + + return helper(n-1, 0, sum/k, nums,visited,k); + } +}; \ No newline at end of file From 0092faaebfb90d4910291f4e33106c36e187a658 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:53:51 +0530 Subject: [PATCH 0325/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2327-number-of-people-aware-of-a-secret/README.md diff --git a/2327-number-of-people-aware-of-a-secret/README.md b/2327-number-of-people-aware-of-a-secret/README.md new file mode 100644 index 00000000..a608eb8d --- /dev/null +++ b/2327-number-of-people-aware-of-a-secret/README.md @@ -0,0 +1,39 @@ +

2327. Number of People Aware of a Secret

Medium


On day 1, one person discovers a secret.

+ +

You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer forget, which means that each person will forget the secret forget days after discovering it. A person cannot share the secret on the same day they forgot it, or on any day afterwards.

+ +

Given an integer n, return the number of people who know the secret at the end of day n. Since the answer may be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 6, delay = 2, forget = 4
+Output: 5
+Explanation:
+Day 1: Suppose the first person is named A. (1 person)
+Day 2: A is the only person who knows the secret. (1 person)
+Day 3: A shares the secret with a new person, B. (2 people)
+Day 4: A shares the secret with a new person, C. (3 people)
+Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
+Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
+
+ +

Example 2:

+ +
Input: n = 4, delay = 1, forget = 3
+Output: 6
+Explanation:
+Day 1: The first person is named A. (1 person)
+Day 2: A shares the secret with B. (2 people)
+Day 3: A and B share the secret with 2 new people, C and D. (4 people)
+Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 1000
  • +
  • 1 <= delay < forget <= n
  • +
+
\ No newline at end of file From 04eccd0ecf1ea9c10e221b3f19da2a83f6633ed5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:53:56 +0530 Subject: [PATCH 0326/3167] Time: 5 ms (72.45%), Space: 6.8 MB (42.00%) - LeetHub --- ...327-number-of-people-aware-of-a-secret.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2327-number-of-people-aware-of-a-secret/2327-number-of-people-aware-of-a-secret.cpp diff --git a/2327-number-of-people-aware-of-a-secret/2327-number-of-people-aware-of-a-secret.cpp b/2327-number-of-people-aware-of-a-secret/2327-number-of-people-aware-of-a-secret.cpp new file mode 100644 index 00000000..49cc5598 --- /dev/null +++ b/2327-number-of-people-aware-of-a-secret/2327-number-of-people-aware-of-a-secret.cpp @@ -0,0 +1,27 @@ +#define ll long long int +int mod = 1e9+7; + +class Solution { +public: + int peopleAwareOfSecret(int n, int delay, int forget) { + + vector dp(n+1,0); + + dp[1] = 1; + ll ans = 0, spreading = 0; + + for(int i = 2; i<= n; ++i) + { + ll sharing = dp[max(i-delay,0)]; + ll forgetting = dp[max(i-forget,0)]; + spreading += (sharing - forgetting + mod) % mod; + + dp[i] = spreading; + } + + for(int i = n - forget + 1; i <= n; ++i) + ans = (ans + dp[i]) % mod; + + return (int)ans; + } +}; \ No newline at end of file From 523a59d9338cc0c1b1cf2eb7ce1e94b15f57b133 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:22:31 +0530 Subject: [PATCH 0327/3167] Create README - LeetHub --- 0057-insert-interval/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0057-insert-interval/README.md diff --git a/0057-insert-interval/README.md b/0057-insert-interval/README.md new file mode 100644 index 00000000..3805102b --- /dev/null +++ b/0057-insert-interval/README.md @@ -0,0 +1,32 @@ +

57. Insert Interval

Medium


You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.

+ +

Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).

+ +

Return intervals after the insertion.

+ +

 

+

Example 1:

+ +
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
+Output: [[1,5],[6,9]]
+
+ +

Example 2:

+ +
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
+Output: [[1,2],[3,10],[12,16]]
+Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= intervals.length <= 104
  • +
  • intervals[i].length == 2
  • +
  • 0 <= starti <= endi <= 105
  • +
  • intervals is sorted by starti in ascending order.
  • +
  • newInterval.length == 2
  • +
  • 0 <= start <= end <= 105
  • +
+
\ No newline at end of file From a0101c8b86912d3110fb3469ac7b17cf05161af0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:22:35 +0530 Subject: [PATCH 0328/3167] Time: 25 ms (54.92%), Space: 17.5 MB (23.91%) - LeetHub --- 0057-insert-interval/0057-insert-interval.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0057-insert-interval/0057-insert-interval.cpp diff --git a/0057-insert-interval/0057-insert-interval.cpp b/0057-insert-interval/0057-insert-interval.cpp new file mode 100644 index 00000000..ea38198c --- /dev/null +++ b/0057-insert-interval/0057-insert-interval.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + vector> insert(vector>& intervals, vector& newInterval) { + + vector>ans; + for(auto itr:intervals) + { + if(itr[1] Date: Tue, 17 Jan 2023 22:24:36 +0530 Subject: [PATCH 0329/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0926-flip-string-to-monotone-increasing/README.md diff --git a/0926-flip-string-to-monotone-increasing/README.md b/0926-flip-string-to-monotone-increasing/README.md new file mode 100644 index 00000000..191b2d8f --- /dev/null +++ b/0926-flip-string-to-monotone-increasing/README.md @@ -0,0 +1,36 @@ +

926. Flip String to Monotone Increasing

Medium


A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none).

+ +

You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.

+ +

Return the minimum number of flips to make s monotone increasing.

+ +

 

+

Example 1:

+ +
Input: s = "00110"
+Output: 1
+Explanation: We flip the last digit to get 00111.
+
+ +

Example 2:

+ +
Input: s = "010110"
+Output: 2
+Explanation: We flip to get 011111, or alternatively 000111.
+
+ +

Example 3:

+ +
Input: s = "00011000"
+Output: 2
+Explanation: We flip to get 00000000.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s[i] is either '0' or '1'.
  • +
+
\ No newline at end of file From 1e8e1da09e6064da60cdd30bbd9f60d6cf4e2836 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Jan 2023 22:24:40 +0530 Subject: [PATCH 0330/3167] Time: 28 ms (73.92%), Space: 11.1 MB (82.03%) - LeetHub --- ...0926-flip-string-to-monotone-increasing.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 0926-flip-string-to-monotone-increasing/0926-flip-string-to-monotone-increasing.cpp diff --git a/0926-flip-string-to-monotone-increasing/0926-flip-string-to-monotone-increasing.cpp b/0926-flip-string-to-monotone-increasing/0926-flip-string-to-monotone-increasing.cpp new file mode 100644 index 00000000..6eafea45 --- /dev/null +++ b/0926-flip-string-to-monotone-increasing/0926-flip-string-to-monotone-increasing.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int minFlipsMonoIncr(string s) { + { + int count_flip = 0, count_one = 0; + for (auto i : s) + { + if (i == '1') + count_one++; + else{ + count_flip++; + count_flip = min(count_flip, count_one); + } + } + return count_flip; + } + } +}; \ No newline at end of file From 587b15835526b2b9c43d6ee3363a2cbf5c160670 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Jan 2023 21:56:47 +0530 Subject: [PATCH 0331/3167] Create README - LeetHub --- 0053-maximum-subarray/README.md | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0053-maximum-subarray/README.md diff --git a/0053-maximum-subarray/README.md b/0053-maximum-subarray/README.md new file mode 100644 index 00000000..8c856c7c --- /dev/null +++ b/0053-maximum-subarray/README.md @@ -0,0 +1,35 @@ +

53. Maximum Subarray

Medium


Given an integer array nums, find the subarray with the largest sum, and return its sum.

+ +

 

+

Example 1:

+ +
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
+Output: 6
+Explanation: The subarray [4,-1,2,1] has the largest sum 6.
+
+ +

Example 2:

+ +
Input: nums = [1]
+Output: 1
+Explanation: The subarray [1] has the largest sum 1.
+
+ +

Example 3:

+ +
Input: nums = [5,4,-1,7,8]
+Output: 23
+Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+ +

 

+

Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

+
\ No newline at end of file From 787678ae40b9b7cda9b182aa38405dab07116341 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Jan 2023 22:59:08 +0530 Subject: [PATCH 0332/3167] Attach NOTES - LeetHub --- 0918-maximum-sum-circular-subarray/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0918-maximum-sum-circular-subarray/NOTES.md diff --git a/0918-maximum-sum-circular-subarray/NOTES.md b/0918-maximum-sum-circular-subarray/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0918-maximum-sum-circular-subarray/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 75a08fd1918f4ec4b8fc92a59628777181f98c95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Jan 2023 22:59:13 +0530 Subject: [PATCH 0333/3167] Time: 73 ms (72.20%), Space: 40 MB (38.25%) - LeetHub --- .../0918-maximum-sum-circular-subarray.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0918-maximum-sum-circular-subarray/0918-maximum-sum-circular-subarray.cpp diff --git a/0918-maximum-sum-circular-subarray/0918-maximum-sum-circular-subarray.cpp b/0918-maximum-sum-circular-subarray/0918-maximum-sum-circular-subarray.cpp new file mode 100644 index 00000000..4b5928cf --- /dev/null +++ b/0918-maximum-sum-circular-subarray/0918-maximum-sum-circular-subarray.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int maxSubarraySumCircular(vector& nums) { + + int currSum = 0, maxSum = INT_MIN, currSum2 = 0, minSum = INT_MAX; + int totSum =0 ; + int n = nums.size(); + + for(int i = 0; i 0) + currSum2 = 0; + } + + return (maxSum > 0 ? max(maxSum, totSum - minSum) : maxSum); + + } +}; \ No newline at end of file From cc36884e9710b796ed0131f9253332b94ba0ec28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 19 Jan 2023 07:47:43 +0530 Subject: [PATCH 0334/3167] Create README - LeetHub --- 0974-subarray-sums-divisible-by-k/README.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0974-subarray-sums-divisible-by-k/README.md diff --git a/0974-subarray-sums-divisible-by-k/README.md b/0974-subarray-sums-divisible-by-k/README.md new file mode 100644 index 00000000..af732be7 --- /dev/null +++ b/0974-subarray-sums-divisible-by-k/README.md @@ -0,0 +1,28 @@ +

974. Subarray Sums Divisible by K

Medium


Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k.

+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [4,5,0,-2,-3,1], k = 5
+Output: 7
+Explanation: There are 7 subarrays with a sum divisible by k = 5:
+[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
+
+ +

Example 2:

+ +
Input: nums = [5], k = 9
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • 2 <= k <= 104
  • +
+
\ No newline at end of file From 590016a5c159dce89351dbbb2fb0b168c4c3d345 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 19 Jan 2023 07:47:43 +0530 Subject: [PATCH 0335/3167] Attach NOTES - LeetHub --- 0974-subarray-sums-divisible-by-k/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0974-subarray-sums-divisible-by-k/NOTES.md diff --git a/0974-subarray-sums-divisible-by-k/NOTES.md b/0974-subarray-sums-divisible-by-k/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0974-subarray-sums-divisible-by-k/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e69b7e3ff804ef4f60de8873f03784f43384e806 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 19 Jan 2023 07:47:47 +0530 Subject: [PATCH 0336/3167] Time: 50 ms (88.94%), Space: 30.1 MB (94.47%) - LeetHub --- .../0974-subarray-sums-divisible-by-k.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp diff --git a/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp b/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp new file mode 100644 index 00000000..a18694dc --- /dev/null +++ b/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int subarraysDivByK(vector& nums, int k) { + + int sum = 0, n = nums.size(); + vector cnt(k,0); + + for(auto itr : nums) + { + sum += (itr % k + k) % k; + ++cnt[sum%k]; + } + + int res = cnt[0]; + for(auto itr : cnt) + { + res += (itr * (itr - 1))/2; + } + return res; + } +}; \ No newline at end of file From f9a0bc29cb852940d13b9b6f2c89f61636802a4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 08:58:15 +0530 Subject: [PATCH 0337/3167] Create README - LeetHub --- 0713-subarray-product-less-than-k/README.md | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0713-subarray-product-less-than-k/README.md diff --git a/0713-subarray-product-less-than-k/README.md b/0713-subarray-product-less-than-k/README.md new file mode 100644 index 00000000..92b81e11 --- /dev/null +++ b/0713-subarray-product-less-than-k/README.md @@ -0,0 +1,27 @@ +

713. Subarray Product Less Than K

Medium


Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

+ +

 

+

Example 1:

+ +
Input: nums = [10,5,2,6], k = 100
+Output: 8
+Explanation: The 8 subarrays that have product less than 100 are:
+[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
+Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • 1 <= nums[i] <= 1000
  • +
  • 0 <= k <= 106
  • +
+
\ No newline at end of file From 8f0ab831fa4167ad651bb94bdb09d1125a475019 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 08:58:18 +0530 Subject: [PATCH 0338/3167] Time: 99 ms (55.31%), Space: 61.3 MB (26.57%) - LeetHub --- .../0713-subarray-product-less-than-k.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp diff --git a/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp b/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp new file mode 100644 index 00000000..e904f316 --- /dev/null +++ b/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp @@ -0,0 +1,23 @@ +#define ll long long int +class Solution { +public: + int numSubarrayProductLessThanK(vector& nums, int k) { + + ll product = 1, count = 0; + int n = nums.size(), i = 0; + + if(k == 0 or k == 1) return 0; + + for(int j = 0; j < n; ++j) + { + product *= nums[j]; + + while(product >= k) + product /= nums[i++]; + + count += (j-i+1); + } + + return count; + } +}; \ No newline at end of file From e6f3c2ba245c714907ae19692ea07f2bd8f4a3eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:11:55 +0530 Subject: [PATCH 0339/3167] Create README - LeetHub --- 0491-non-decreasing-subsequences/README.md | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0491-non-decreasing-subsequences/README.md diff --git a/0491-non-decreasing-subsequences/README.md b/0491-non-decreasing-subsequences/README.md new file mode 100644 index 00000000..d50f1d96 --- /dev/null +++ b/0491-non-decreasing-subsequences/README.md @@ -0,0 +1,23 @@ +

491. Non-decreasing Subsequences

Medium


Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

+ +

 

+

Example 1:

+ +
Input: nums = [4,6,7,7]
+Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 15
  • +
  • -100 <= nums[i] <= 100
  • +
+
\ No newline at end of file From 4e05837c2ac416ab4edb1456f3a29bc39fcd3ea7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:11:55 +0530 Subject: [PATCH 0340/3167] Attach NOTES - LeetHub --- 0491-non-decreasing-subsequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0491-non-decreasing-subsequences/NOTES.md diff --git a/0491-non-decreasing-subsequences/NOTES.md b/0491-non-decreasing-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0491-non-decreasing-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From dedc17c1b14eabb45032be2e260f00f1a02f5b32 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:11:58 +0530 Subject: [PATCH 0341/3167] Time: 92 ms (56.49%), Space: 31.1 MB (33.17%) - LeetHub --- .../0491-non-decreasing-subsequences.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.cpp diff --git a/0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.cpp b/0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.cpp new file mode 100644 index 00000000..332cbc5d --- /dev/null +++ b/0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + void helper(int idx, vector& ds, vector>& res,vector& nums, int prev) + { + if(idx >= nums.size()) + { + if(ds.size() >= 2) + res.push_back(ds); + return; + } + + if(prev == -1 or nums[prev] <= nums[idx]) + { + ds.push_back(nums[idx]); + helper(idx+1,ds,res,nums, idx); + ds.pop_back(); + } + helper(idx+1, ds, res, nums, prev); + } + + + vector> findSubsequences(vector& nums) { + + vector ds; + vector> res; + + helper(0,ds,res,nums,-1); + + set> s(res.begin(),res.end()); + + vector> ans(s.begin(),s.end()); + + return ans; + + } +}; \ No newline at end of file From eca355815a22f26088f33880e249711d7472579b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:15:27 +0530 Subject: [PATCH 0342/3167] Attach NOTES - LeetHub From eec4a33d0b243025d05fac0169028b921a6ad1bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:15:30 +0530 Subject: [PATCH 0343/3167] Time: 92 ms (56.49%), Space: 31.1 MB (33.17%) - LeetHub From 8b3592c22435c129a14f3e0c3ea128aed4aafdbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:15:46 +0530 Subject: [PATCH 0344/3167] Attach NOTES - LeetHub From b60d01c4fbac3d2f9f18556261e91158dbe068be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:15:49 +0530 Subject: [PATCH 0345/3167] Time: 92 ms (56.49%), Space: 31.1 MB (33.17%) - LeetHub From d80cb72bf6157dc49d5e33535452adaa615b67fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:18:46 +0530 Subject: [PATCH 0346/3167] Create README - LeetHub --- 0093-restore-ip-addresses/README.md | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0093-restore-ip-addresses/README.md diff --git a/0093-restore-ip-addresses/README.md b/0093-restore-ip-addresses/README.md new file mode 100644 index 00000000..6456cfc0 --- /dev/null +++ b/0093-restore-ip-addresses/README.md @@ -0,0 +1,35 @@ +

93. Restore IP Addresses

Medium


A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.

+ +
    +
  • For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.
  • +
+ +

Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.

+ +

 

+

Example 1:

+ +
Input: s = "25525511135"
+Output: ["255.255.11.135","255.255.111.35"]
+
+ +

Example 2:

+ +
Input: s = "0000"
+Output: ["0.0.0.0"]
+
+ +

Example 3:

+ +
Input: s = "101023"
+Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 20
  • +
  • s consists of digits only.
  • +
+
\ No newline at end of file From f42a117260033fd54d1f47ea4b670b110a31b783 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:18:49 +0530 Subject: [PATCH 0347/3167] Time: 4 ms (50.52%), Space: 6.4 MB (89.32%) - LeetHub --- .../0093-restore-ip-addresses.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0093-restore-ip-addresses/0093-restore-ip-addresses.cpp diff --git a/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp b/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp new file mode 100644 index 00000000..c7114088 --- /dev/null +++ b/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector restoreIpAddresses(string s) { + vector result; + string ip; + dfs(s,0,0,ip,result); + return result; + } + void dfs(string s,int start,int step,string ip,vector& result){ + if(start==s.size()&&step==4){ + ip.erase(ip.end()-1); + result.push_back(ip); + return; + } + if(s.size()-start>(4-step)*3) return; + if(s.size()-start<(4-step)) return; + int num=0; + for(int i=start;i Date: Sun, 22 Jan 2023 11:35:44 +0530 Subject: [PATCH 0348/3167] Attach NOTES - LeetHub --- 0093-restore-ip-addresses/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0093-restore-ip-addresses/NOTES.md diff --git a/0093-restore-ip-addresses/NOTES.md b/0093-restore-ip-addresses/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0093-restore-ip-addresses/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0ce97bfcd4594bcc3687a8bb4ddb331c048dc0b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Jan 2023 11:35:47 +0530 Subject: [PATCH 0349/3167] Time: 6 ms (29.80%), Space: 7 MB (23.13%) - LeetHub --- .../0093-restore-ip-addresses.cpp | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp b/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp index c7114088..023ba55b 100644 --- a/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp +++ b/0093-restore-ip-addresses/0093-restore-ip-addresses.cpp @@ -1,27 +1,41 @@ class Solution { public: - vector restoreIpAddresses(string s) { - vector result; - string ip; - dfs(s,0,0,ip,result); - return result; + + bool isValid(string str) + { + if(str[0] == '0') + return false; + + int num = stoi(str); + + return num <= 255; } - void dfs(string s,int start,int step,string ip,vector& result){ - if(start==s.size()&&step==4){ - ip.erase(ip.end()-1); - result.push_back(ip); - return; - } - if(s.size()-start>(4-step)*3) return; - if(s.size()-start<(4-step)) return; - int num=0; - for(int i=start;i& res) + { + if(idx == s.size() or part == 4) + { + if(idx == s.size() and part == 4) + { + res.push_back(ans.substr(0,ans.size()-1)); } - if(num==0) break; + return; } + + helper(idx + 1, s, part + 1, ans + s[idx] + "." , res); + if(idx + 2 <= s.size() and isValid(s.substr(idx,2))) + helper(idx + 2, s, part + 1, ans + s.substr(idx,2) + "." , res); + if(idx + 3 <= s.size() and isValid(s.substr(idx,3))) + helper(idx + 3, s, part + 1, ans + s.substr(idx,3) +"." , res); + } + + vector restoreIpAddresses(string s) { + + vector res; + + helper(0, s, 0, "" , res); + + return res; + } }; \ No newline at end of file From abe2c961190070c97e7272aa4fe0cbd30705e117 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:06:11 +0530 Subject: [PATCH 0350/3167] Create README - LeetHub --- 0131-palindrome-partitioning/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 0131-palindrome-partitioning/README.md diff --git a/0131-palindrome-partitioning/README.md b/0131-palindrome-partitioning/README.md new file mode 100644 index 00000000..93474224 --- /dev/null +++ b/0131-palindrome-partitioning/README.md @@ -0,0 +1,18 @@ +

131. Palindrome Partitioning

Medium


Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

+ +

 

+

Example 1:

+
Input: s = "aab"
+Output: [["a","a","b"],["aa","b"]]
+

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 16
  • +
  • s contains only lowercase English letters.
  • +
+
\ No newline at end of file From ae36ade0ebe681ef9ab7a8abef2d3824532c22c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:06:11 +0530 Subject: [PATCH 0351/3167] Attach NOTES - LeetHub --- 0131-palindrome-partitioning/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0131-palindrome-partitioning/NOTES.md diff --git a/0131-palindrome-partitioning/NOTES.md b/0131-palindrome-partitioning/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0131-palindrome-partitioning/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a357225ada87797dbb47752fa2d261b6e813a55f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:06:15 +0530 Subject: [PATCH 0352/3167] Time: 195 ms (46.94%), Space: 140.6 MB (19.34%) - LeetHub --- .../0131-palindrome-partitioning.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0131-palindrome-partitioning/0131-palindrome-partitioning.cpp diff --git a/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp b/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp new file mode 100644 index 00000000..bc97d9ad --- /dev/null +++ b/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + + bool isPalindrome(string str , int start, int end) + { + while(start <= end) + { + if(str[start++] == str[end--]) + { + continue; + } + return false; + } + return true; + } + + void helper(int idx, string& s, vector ds, vector>& res) + { + if(idx == s.size()) + { + res.push_back(ds); + return ; + } + + for(int i = idx; i> partition(string s) { + + vector ds; + vector > res; + + helper(0,s, ds, res); + + return res; + + } +}; \ No newline at end of file From 67b6cc9a976b6a50a0911341cd41f2542d47ebbd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Jan 2023 15:23:36 +0530 Subject: [PATCH 0353/3167] Create README - LeetHub --- 0997-find-the-town-judge/README.md | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0997-find-the-town-judge/README.md diff --git a/0997-find-the-town-judge/README.md b/0997-find-the-town-judge/README.md new file mode 100644 index 00000000..2927b118 --- /dev/null +++ b/0997-find-the-town-judge/README.md @@ -0,0 +1,45 @@ +

997. Find the Town Judge

Easy


In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.

+ +

If the town judge exists, then:

+ +
    +
  1. The town judge trusts nobody.
  2. +
  3. Everybody (except for the town judge) trusts the town judge.
  4. +
  5. There is exactly one person that satisfies properties 1 and 2.
  6. +
+ +

You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi.

+ +

Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: n = 3, trust = [[1,3],[2,3]]
+Output: 3
+
+ +

Example 3:

+ +
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
+Output: -1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
  • 0 <= trust.length <= 104
  • +
  • trust[i].length == 2
  • +
  • All the pairs of trust are unique.
  • +
  • ai != bi
  • +
  • 1 <= ai, bi <= n
  • +
+
\ No newline at end of file From effa5a82026d6649fb7ae207443c53b289359b95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Jan 2023 15:23:40 +0530 Subject: [PATCH 0354/3167] Time: 220 ms (58.40%), Space: 69 MB (29.65%) - LeetHub --- .../0997-find-the-town-judge.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0997-find-the-town-judge/0997-find-the-town-judge.cpp diff --git a/0997-find-the-town-judge/0997-find-the-town-judge.cpp b/0997-find-the-town-judge/0997-find-the-town-judge.cpp new file mode 100644 index 00000000..6b312854 --- /dev/null +++ b/0997-find-the-town-judge/0997-find-the-town-judge.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int findJudge(int n, vector>& trust) { + + if(n == 1 and trust.size() == 0) + return 1; + + vector outDegree(n+1,0), inDegree(n+1,0); + + for(auto itr : trust) + { + ++outDegree[itr[0]]; + ++inDegree[itr[1]]; + } + + for(int i = 1; i<= n; ++i) + { + if(outDegree[i] == 0 and inDegree[i] == n-1) + return i; + } + + return -1; + + } +}; \ No newline at end of file From d5c585cd2aece377353dee4f00bd547773aed06e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Jan 2023 15:24:12 +0530 Subject: [PATCH 0355/3167] Attach NOTES - LeetHub --- 0997-find-the-town-judge/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0997-find-the-town-judge/NOTES.md diff --git a/0997-find-the-town-judge/NOTES.md b/0997-find-the-town-judge/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0997-find-the-town-judge/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 39fa4e45b6682b167c7d960ccc5fe6a9f99ca520 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Jan 2023 15:24:15 +0530 Subject: [PATCH 0356/3167] Time: 220 ms (58.40%), Space: 69 MB (29.65%) - LeetHub From 6dff67a21cc7dc86713b4338982bec617e874d12 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Jan 2023 22:49:38 +0530 Subject: [PATCH 0357/3167] Attach NOTES - LeetHub --- 0909-snakes-and-ladders/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0909-snakes-and-ladders/NOTES.md diff --git a/0909-snakes-and-ladders/NOTES.md b/0909-snakes-and-ladders/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0909-snakes-and-ladders/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6d44df6b2c999eeecace78235186254c2589918f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Jan 2023 22:49:42 +0530 Subject: [PATCH 0358/3167] Time: 89 ms (17.17%), Space: 24.7 MB (8.37%) - LeetHub --- .../0909-snakes-and-ladders.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0909-snakes-and-ladders/0909-snakes-and-ladders.cpp diff --git a/0909-snakes-and-ladders/0909-snakes-and-ladders.cpp b/0909-snakes-and-ladders/0909-snakes-and-ladders.cpp new file mode 100644 index 00000000..787089f9 --- /dev/null +++ b/0909-snakes-and-ladders/0909-snakes-and-ladders.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + vector findcordinates(int pos,int n){ + vectortemp; + int r=n-(pos-1)/n-1; + int c=(pos-1)%n; + if(r%2==n%2){ + return temp={r,n-1-c}; + }else{ + return temp={r,c}; + } + } + + + int snakesAndLadders(vector>& board) { + int n= board.size(); + queueq; + vector>visited( n , vector(n, false)); + int ans=0; + q.push(1); + visited[n-1][0]=true; + while(!q.empty()){ + int size=q.size(); + + for(int i=0;in*n) break; + vectorpos=findcordinates(front+k,n); + int r=pos[0]; + int c=pos[1]; + if(visited[r][c]==true) continue; + visited[r][c]=true; + if(board[r][c]!=-1){ + q.push(board[r][c]); + }else{ + q.push(front+k); + } + + } + } + ans++; + } + return -1; + } +}; From 5cebd77b03c95049a8c64497fd828ae17f680e7e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 09:50:03 +0530 Subject: [PATCH 0359/3167] Create README - LeetHub --- 2063-vowels-of-all-substrings/README.md | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2063-vowels-of-all-substrings/README.md diff --git a/2063-vowels-of-all-substrings/README.md b/2063-vowels-of-all-substrings/README.md new file mode 100644 index 00000000..f4081a44 --- /dev/null +++ b/2063-vowels-of-all-substrings/README.md @@ -0,0 +1,45 @@ +

2063. Vowels of All Substrings

Medium


Given a string word, return the sum of the number of vowels ('a', 'e', 'i', 'o', and 'u') in every substring of word.

+ +

A substring is a contiguous (non-empty) sequence of characters within a string.

+ +

Note: Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations.

+ +

 

+

Example 1:

+ +
Input: word = "aba"
+Output: 6
+Explanation: 
+All possible substrings are: "a", "ab", "aba", "b", "ba", and "a".
+- "b" has 0 vowels in it
+- "a", "ab", "ba", and "a" have 1 vowel each
+- "aba" has 2 vowels in it
+Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6. 
+
+ +

Example 2:

+ +
Input: word = "abc"
+Output: 3
+Explanation: 
+All possible substrings are: "a", "ab", "abc", "b", "bc", and "c".
+- "a", "ab", and "abc" have 1 vowel each
+- "b", "bc", and "c" have 0 vowels each
+Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3.
+
+ +

Example 3:

+ +
Input: word = "ltcd"
+Output: 0
+Explanation: There are no vowels in any substring of "ltcd".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word.length <= 105
  • +
  • word consists of lowercase English letters.
  • +
+
\ No newline at end of file From 390130686d1c8f05e5560b096f42d772330bed37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 09:50:07 +0530 Subject: [PATCH 0360/3167] Time: 28 ms (62.17%), Space: 11.1 MB (60.20%) - LeetHub --- .../2063-vowels-of-all-substrings.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2063-vowels-of-all-substrings/2063-vowels-of-all-substrings.cpp diff --git a/2063-vowels-of-all-substrings/2063-vowels-of-all-substrings.cpp b/2063-vowels-of-all-substrings/2063-vowels-of-all-substrings.cpp new file mode 100644 index 00000000..4d878b50 --- /dev/null +++ b/2063-vowels-of-all-substrings/2063-vowels-of-all-substrings.cpp @@ -0,0 +1,19 @@ +#define ll long long int +class Solution { +public: + long long countVowels(string word) { + + // char at i will appear in (i+1) * (size - i) substrings + + ll n = word.size(), ans = 0; + + vector count = {1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0}; + + for(int i = 0 ; i < n; ++i){ + ans += count[word[i] - 'a'] * (i+1) * (n-i); + } + + return ans; + + } +}; \ No newline at end of file From a5ffdec6a5d8fce19aac89f1306b75acb4cfd4bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:06:47 +0530 Subject: [PATCH 0361/3167] Create README - LeetHub --- 2262-total-appeal-of-a-string/README.md | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2262-total-appeal-of-a-string/README.md diff --git a/2262-total-appeal-of-a-string/README.md b/2262-total-appeal-of-a-string/README.md new file mode 100644 index 00000000..36d55121 --- /dev/null +++ b/2262-total-appeal-of-a-string/README.md @@ -0,0 +1,44 @@ +

2262. Total Appeal of A String

Hard


The appeal of a string is the number of distinct characters found in the string.

+ +
    +
  • For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.
  • +
+ +

Given a string s, return the total appeal of all of its substrings.

+ +

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

+ +

 

+

Example 1:

+ +
Input: s = "abbca"
+Output: 28
+Explanation: The following are the substrings of "abbca":
+- Substrings of length 1: "a", "b", "b", "c", "a" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.
+- Substrings of length 2: "ab", "bb", "bc", "ca" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.
+- Substrings of length 3: "abb", "bbc", "bca" have an appeal of 2, 2, and 3 respectively. The sum is 7.
+- Substrings of length 4: "abbc", "bbca" have an appeal of 3 and 3 respectively. The sum is 6.
+- Substrings of length 5: "abbca" has an appeal of 3. The sum is 3.
+The total sum is 5 + 7 + 7 + 6 + 3 = 28.
+
+ +

Example 2:

+ +
Input: s = "code"
+Output: 20
+Explanation: The following are the substrings of "code":
+- Substrings of length 1: "c", "o", "d", "e" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.
+- Substrings of length 2: "co", "od", "de" have an appeal of 2, 2, and 2 respectively. The sum is 6.
+- Substrings of length 3: "cod", "ode" have an appeal of 3 and 3 respectively. The sum is 6.
+- Substrings of length 4: "code" has an appeal of 4. The sum is 4.
+The total sum is 4 + 6 + 6 + 4 = 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From 8beae572cd67c98172324095a667cef8683cd604 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:06:47 +0530 Subject: [PATCH 0362/3167] Attach NOTES - LeetHub --- 2262-total-appeal-of-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2262-total-appeal-of-a-string/NOTES.md diff --git a/2262-total-appeal-of-a-string/NOTES.md b/2262-total-appeal-of-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2262-total-appeal-of-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 793c629823e0c02a5cd05edfb6c1e3e42cf29f62 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:06:51 +0530 Subject: [PATCH 0363/3167] Time: 59 ms (51.86%), Space: 10.5 MB (80.75%) - LeetHub --- .../2262-total-appeal-of-a-string.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2262-total-appeal-of-a-string/2262-total-appeal-of-a-string.cpp diff --git a/2262-total-appeal-of-a-string/2262-total-appeal-of-a-string.cpp b/2262-total-appeal-of-a-string/2262-total-appeal-of-a-string.cpp new file mode 100644 index 00000000..5f5d6b36 --- /dev/null +++ b/2262-total-appeal-of-a-string/2262-total-appeal-of-a-string.cpp @@ -0,0 +1,21 @@ +#define ll long long int +class Solution { +public: + long long appealSum(string s) { + + ll ans = 0, n = s.size(); + vector prev(26,0); + + for(int i = 0; i < n; ++i) + { + // removig prev + ans += (i+1 - prev[s[i] - 'a']) * (n - i); + prev[s[i] - 'a'] = i+1; + } + + return ans; + + + } +}; + From 1eae6aae23330078990482e2a05f0a32b02945dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:20:09 +0530 Subject: [PATCH 0364/3167] Attach NOTES - LeetHub From 687c81821dd4c10251ca2457aa06702a85530abd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:20:12 +0530 Subject: [PATCH 0365/3167] Time: 59 ms (51.86%), Space: 10.5 MB (80.75%) - LeetHub From b8f94b3bd81b11e39007b995014c0eed37d92f6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:20:13 +0530 Subject: [PATCH 0366/3167] Attach NOTES - LeetHub --- 2359-find-closest-node-to-given-two-nodes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2359-find-closest-node-to-given-two-nodes/NOTES.md diff --git a/2359-find-closest-node-to-given-two-nodes/NOTES.md b/2359-find-closest-node-to-given-two-nodes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2359-find-closest-node-to-given-two-nodes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b4f401b86d575a29ff60272083ada436aeb2183c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:20:17 +0530 Subject: [PATCH 0367/3167] Time: 215 ms (66.76%), Space: 105 MB (51.80%) - LeetHub --- ...9-find-closest-node-to-given-two-nodes.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2359-find-closest-node-to-given-two-nodes/2359-find-closest-node-to-given-two-nodes.cpp diff --git a/2359-find-closest-node-to-given-two-nodes/2359-find-closest-node-to-given-two-nodes.cpp b/2359-find-closest-node-to-given-two-nodes/2359-find-closest-node-to-given-two-nodes.cpp new file mode 100644 index 00000000..c96350af --- /dev/null +++ b/2359-find-closest-node-to-given-two-nodes/2359-find-closest-node-to-given-two-nodes.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + + void dfs(int node, vector& edges, vector& dist , vector& visited, int distance) + { + if(node != -1 and !visited[node]) { + visited[node] = 1; + dist[node] = distance; + dfs(edges[node], edges, dist, visited, distance + 1); + } + } + + int closestMeetingNode(vector& edges, int node1, int node2) { + + int n = edges.size(); + + vector visited1(n,0), visited2(n,0), dist1(n,-1), dist2(n,-1); + + dfs(node1, edges, dist1, visited1, 0); + dfs(node2 , edges, dist2, visited2, 0); + + int ans = edges.size(); + int res = -1; + for(int i = 0; i Date: Thu, 26 Jan 2023 23:21:08 +0530 Subject: [PATCH 0368/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0787-cheapest-flights-within-k-stops/README.md diff --git a/0787-cheapest-flights-within-k-stops/README.md b/0787-cheapest-flights-within-k-stops/README.md new file mode 100644 index 00000000..9bd2c685 --- /dev/null +++ b/0787-cheapest-flights-within-k-stops/README.md @@ -0,0 +1,48 @@ +

787. Cheapest Flights Within K Stops

Medium


There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from city fromi to city toi with cost pricei.

+ +

You are also given three integers src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.

+ +

 

+

Example 1:

+ +
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
+Output: 700
+Explanation:
+The graph is shown above.
+The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
+Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
+
+ +

Example 2:

+ +
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
+Output: 200
+Explanation:
+The graph is shown above.
+The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
+
+ +

Example 3:

+ +
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
+Output: 500
+Explanation:
+The graph is shown above.
+The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= flights.length <= (n * (n - 1) / 2)
  • +
  • flights[i].length == 3
  • +
  • 0 <= fromi, toi < n
  • +
  • fromi != toi
  • +
  • 1 <= pricei <= 104
  • +
  • There will not be any multiple flights between two cities.
  • +
  • 0 <= src, dst, k < n
  • +
  • src != dst
  • +
+
\ No newline at end of file From 72d05c5d17d000432b69457d4508b346a0a9e99d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Jan 2023 23:21:12 +0530 Subject: [PATCH 0369/3167] Time: 36 ms (53.58%), Space: 13.5 MB (60.38%) - LeetHub --- .../0787-cheapest-flights-within-k-stops.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0787-cheapest-flights-within-k-stops/0787-cheapest-flights-within-k-stops.cpp diff --git a/0787-cheapest-flights-within-k-stops/0787-cheapest-flights-within-k-stops.cpp b/0787-cheapest-flights-within-k-stops/0787-cheapest-flights-within-k-stops.cpp new file mode 100644 index 00000000..2a806abc --- /dev/null +++ b/0787-cheapest-flights-within-k-stops/0787-cheapest-flights-within-k-stops.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { + + vector> adj[n]; + + for(auto itr : flights){ + adj[itr[0]].push_back({itr[1],itr[2]}); + } + + vector dist(n,1e9); + + queue> > q; + + q.push({0,{src,0}}); + dist[src] = 0; + + // stops node cost + + while(!q.empty()){ + + auto curr = q.front(); + q.pop(); + + int stops = curr.first; + int node = curr.second.first; + int cost = curr.second.second; + + for(auto itr : adj[node]) + { + // itr is pair of adjNode and cost + if(stops > k) + continue; + + if(stops <= k and cost + itr.second <= dist[itr.first]) + { + dist[itr.first] = cost + itr.second; + q.push({stops+1,{itr.first,cost + itr.second}}); + } + } + } + + + if(dist[dst] == 1e9) + return -1; + + return dist[dst]; + + } +}; \ No newline at end of file From ae74bc9e7fee3037c6699d08559d2965754404f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Jan 2023 23:32:31 +0530 Subject: [PATCH 0370/3167] Create README - LeetHub --- 0472-concatenated-words/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0472-concatenated-words/README.md diff --git a/0472-concatenated-words/README.md b/0472-concatenated-words/README.md new file mode 100644 index 00000000..543d6429 --- /dev/null +++ b/0472-concatenated-words/README.md @@ -0,0 +1,30 @@ +

472. Concatenated Words

Hard


Given an array of strings words (without duplicates), return all the concatenated words in the given list of words.

+ +

A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

+ +

 

+

Example 1:

+ +
Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
+Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
+Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; 
+"dogcatsdog" can be concatenated by "dog", "cats" and "dog"; 
+"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
+ +

Example 2:

+ +
Input: words = ["cat","dog","catdog"]
+Output: ["catdog"]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 104
  • +
  • 1 <= words[i].length <= 30
  • +
  • words[i] consists of only lowercase English letters.
  • +
  • All the strings of words are unique.
  • +
  • 1 <= sum(words[i].length) <= 105
  • +
+
\ No newline at end of file From 6aa09a478c0e6169659b643eca0c17c500d12814 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Jan 2023 23:32:34 +0530 Subject: [PATCH 0371/3167] Time: 410 ms (44.88%), Space: 110.3 MB (40.04%) - LeetHub --- .../0472-concatenated-words.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0472-concatenated-words/0472-concatenated-words.cpp diff --git a/0472-concatenated-words/0472-concatenated-words.cpp b/0472-concatenated-words/0472-concatenated-words.cpp new file mode 100644 index 00000000..701e4c6d --- /dev/null +++ b/0472-concatenated-words/0472-concatenated-words.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + bool helper(string str, unordered_map& mp, vector& words) + { + int n = str.size(); + + for(int i = 0; i < n; ++i) + { + string left = str.substr(0,i); + string right = str.substr(i); + + if(mp[left] and (mp[right] or helper(right,mp,words))) + return true; + } + return false; + } + + vector findAllConcatenatedWordsInADict(vector& words) { + + unordered_map mp; + + for(auto itr : words) + ++mp[itr]; + + vector ans; + + for(auto itr : words) + { + if(helper(itr,mp, words)) + ans.push_back(itr); + } + + return ans; + + } +}; \ No newline at end of file From f527516fc318ad06844c939a0ce48800458b00ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 00:41:17 +0530 Subject: [PATCH 0372/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0352-data-stream-as-disjoint-intervals/README.md diff --git a/0352-data-stream-as-disjoint-intervals/README.md b/0352-data-stream-as-disjoint-intervals/README.md new file mode 100644 index 00000000..40fda598 --- /dev/null +++ b/0352-data-stream-as-disjoint-intervals/README.md @@ -0,0 +1,44 @@ +

352. Data Stream as Disjoint Intervals

Hard


Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.

+ +

Implement the SummaryRanges class:

+ +
    +
  • SummaryRanges() Initializes the object with an empty stream.
  • +
  • void addNum(int value) Adds the integer value to the stream.
  • +
  • int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [starti, endi]. The answer should be sorted by starti.
  • +
+ +

 

+

Example 1:

+ +
Input
+["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
+[[], [1], [], [3], [], [7], [], [2], [], [6], []]
+Output
+[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
+
+Explanation
+SummaryRanges summaryRanges = new SummaryRanges();
+summaryRanges.addNum(1);      // arr = [1]
+summaryRanges.getIntervals(); // return [[1, 1]]
+summaryRanges.addNum(3);      // arr = [1, 3]
+summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
+summaryRanges.addNum(7);      // arr = [1, 3, 7]
+summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
+summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
+summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
+summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
+summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= value <= 104
  • +
  • At most 3 * 104 calls will be made to addNum and getIntervals.
  • +
+ +

 

+

Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?

+
\ No newline at end of file From 1bd3ed95147f3bcb3cf9e39cbd49e37e5c42a415 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 00:41:20 +0530 Subject: [PATCH 0373/3167] Time: 296 ms (6.27%), Space: 33.5 MB (54.90%) - LeetHub --- ...0352-data-stream-as-disjoint-intervals.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0352-data-stream-as-disjoint-intervals/0352-data-stream-as-disjoint-intervals.cpp diff --git a/0352-data-stream-as-disjoint-intervals/0352-data-stream-as-disjoint-intervals.cpp b/0352-data-stream-as-disjoint-intervals/0352-data-stream-as-disjoint-intervals.cpp new file mode 100644 index 00000000..594a7e0d --- /dev/null +++ b/0352-data-stream-as-disjoint-intervals/0352-data-stream-as-disjoint-intervals.cpp @@ -0,0 +1,47 @@ +class SummaryRanges { +public: + + set s; + + SummaryRanges() { + + } + + void addNum(int value) { + s.insert(value); + } + + vector> getIntervals() { + + vector> res; + + int start = -1, end = -1; + for(auto itr : s) + { + if(start == -1 and end == -1) + { + start = itr, end = itr; + } + end = itr; + if(s.find(itr + 1) == s.end()) + { + res.push_back({start,end}); + start = -1, end = -1; + } + else + { + continue; + } + } + + return res; + + } +}; + +/** + * Your SummaryRanges object will be instantiated and called as such: + * SummaryRanges* obj = new SummaryRanges(); + * obj->addNum(value); + * vector> param_2 = obj->getIntervals(); + */ \ No newline at end of file From a9e2c8426172734b449a60ee43d65139e71d607c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:38:14 +0530 Subject: [PATCH 0374/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1039-minimum-score-triangulation-of-polygon/README.md diff --git a/1039-minimum-score-triangulation-of-polygon/README.md b/1039-minimum-score-triangulation-of-polygon/README.md new file mode 100644 index 00000000..40a7e4a5 --- /dev/null +++ b/1039-minimum-score-triangulation-of-polygon/README.md @@ -0,0 +1,38 @@ +

1039. Minimum Score Triangulation of Polygon

Medium


You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex (i.e., clockwise order).

+ +

You will triangulate the polygon into n - 2 triangles. For each triangle, the value of that triangle is the product of the values of its vertices, and the total score of the triangulation is the sum of these values over all n - 2 triangles in the triangulation.

+ +

Return the smallest possible total score that you can achieve with some triangulation of the polygon.

+ +

 

+

Example 1:

+ +
Input: values = [1,2,3]
+Output: 6
+Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
+
+ +

Example 2:

+ +
Input: values = [3,7,4,5]
+Output: 144
+Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
+The minimum score is 144.
+
+ +

Example 3:

+ +
Input: values = [1,3,1,4,1,5]
+Output: 13
+Explanation: The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
+
+ +

 

+

Constraints:

+ +
    +
  • n == values.length
  • +
  • 3 <= n <= 50
  • +
  • 1 <= values[i] <= 100
  • +
+
\ No newline at end of file From ad80e4ffcb57564f5f7743e58e1522608fcaf98a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:38:14 +0530 Subject: [PATCH 0375/3167] Attach NOTES - LeetHub --- 1039-minimum-score-triangulation-of-polygon/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1039-minimum-score-triangulation-of-polygon/NOTES.md diff --git a/1039-minimum-score-triangulation-of-polygon/NOTES.md b/1039-minimum-score-triangulation-of-polygon/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1039-minimum-score-triangulation-of-polygon/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 079d21c8ad6c1d419828846888e68821e900f51f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:38:18 +0530 Subject: [PATCH 0376/3167] Time: 16 ms (25.40%), Space: 8.6 MB (17.58%) - LeetHub --- ...minimum-score-triangulation-of-polygon.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp diff --git a/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp b/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp new file mode 100644 index 00000000..9b06379f --- /dev/null +++ b/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + int helper(int i, int j, vector& values, vector>& dp) + { + if(i == j) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int ans = INT_MAX; + for(int k = i; k < j; ++k) + { + int steps = values[i-1] * values[k] * values[j] + helper(i,k, values, dp) + helper(k+1,j,values, dp); + + ans = min(ans, steps); + } + + return dp[i][j] = ans; + } + + int minScoreTriangulation(vector& values) { + + int n = values.size(); + vector> dp(n,vector(n,-1)); + + return helper(1,n-1,values, dp); + + } +}; \ No newline at end of file From c8c0807516a3fadb0f68b0937a1efd6e7475a95f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:54:52 +0530 Subject: [PATCH 0377/3167] Attach NOTES - LeetHub From b9dea54d6c4a69daa4631841a4fb7343a9430973 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:54:55 +0530 Subject: [PATCH 0378/3167] Time: 16 ms (25.40%), Space: 8.6 MB (17.58%) - LeetHub From 33418633cf8cbb068716f944238891a69ad6a2e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:59:37 +0530 Subject: [PATCH 0379/3167] Attach NOTES - LeetHub From 9b405a7338a9e728332a803b2646876149542df7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:59:39 +0530 Subject: [PATCH 0380/3167] Time: 12 ms (47.05%), Space: 8.5 MB (83.92%) - LeetHub --- ...minimum-score-triangulation-of-polygon.cpp | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp b/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp index 9b06379f..25f1e439 100644 --- a/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp +++ b/1039-minimum-score-triangulation-of-polygon/1039-minimum-score-triangulation-of-polygon.cpp @@ -23,9 +23,26 @@ class Solution { int minScoreTriangulation(vector& values) { int n = values.size(); - vector> dp(n,vector(n,-1)); + vector> dp(n+1,vector(n+1,INT_MAX)); - return helper(1,n-1,values, dp); + for(int i = 0; i=1; --i) + { + for(int j = i+1; j Date: Sun, 29 Jan 2023 17:12:24 +0530 Subject: [PATCH 0381/3167] Create README - LeetHub --- 0050-powx-n/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0050-powx-n/README.md diff --git a/0050-powx-n/README.md b/0050-powx-n/README.md new file mode 100644 index 00000000..7c41e658 --- /dev/null +++ b/0050-powx-n/README.md @@ -0,0 +1,32 @@ +

50. Pow(x, n)

Medium


Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

+ +

 

+

Example 1:

+ +
Input: x = 2.00000, n = 10
+Output: 1024.00000
+
+ +

Example 2:

+ +
Input: x = 2.10000, n = 3
+Output: 9.26100
+
+ +

Example 3:

+ +
Input: x = 2.00000, n = -2
+Output: 0.25000
+Explanation: 2-2 = 1/22 = 1/4 = 0.25
+
+ +

 

+

Constraints:

+ +
    +
  • -100.0 < x < 100.0
  • +
  • -231 <= n <= 231-1
  • +
  • n is an integer.
  • +
  • -104 <= xn <= 104
  • +
+
\ No newline at end of file From 6cfc87ce9dc6f4355b3fc793a7fa4b195d3f02e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Jan 2023 17:12:28 +0530 Subject: [PATCH 0382/3167] Time: 0 ms (100.00%), Space: 6 MB (33.57%) - LeetHub --- 0050-powx-n/0050-powx-n.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0050-powx-n/0050-powx-n.cpp diff --git a/0050-powx-n/0050-powx-n.cpp b/0050-powx-n/0050-powx-n.cpp new file mode 100644 index 00000000..c5487890 --- /dev/null +++ b/0050-powx-n/0050-powx-n.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + double myPow(double x, int n) { + + // fast power + + long long num = n; + + if(num < 0) num *= -1; + + double result = 1.0; + + while(num > 0) + { + if(num & 1) + result = result * x; + + x = (x * x); + num >>= 1; + + } + + if(n < 0) + result = (double)1.0/(double)result; + + return result; + } +}; \ No newline at end of file From b6ab778003c2314b273359da387f96fab1ec622b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 00:06:03 +0530 Subject: [PATCH 0383/3167] Create README - LeetHub --- 0460-lfu-cache/README.md | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 0460-lfu-cache/README.md diff --git a/0460-lfu-cache/README.md b/0460-lfu-cache/README.md new file mode 100644 index 00000000..a75deca0 --- /dev/null +++ b/0460-lfu-cache/README.md @@ -0,0 +1,59 @@ +

460. LFU Cache

Hard


Design and implement a data structure for a Least Frequently Used (LFU) cache.

+ +

Implement the LFUCache class:

+ +
    +
  • LFUCache(int capacity) Initializes the object with the capacity of the data structure.
  • +
  • int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
  • +
  • void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.
  • +
+ +

To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.

+ +

When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.

+ +

The functions get and put must each run in O(1) average time complexity.

+ +

 

+

Example 1:

+ +
Input
+["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
+[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
+Output
+[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
+
+Explanation
+// cnt(x) = the use counter for key x
+// cache=[] will show the last used order for tiebreakers (leftmost element is  most recent)
+LFUCache lfu = new LFUCache(2);
+lfu.put(1, 1);   // cache=[1,_], cnt(1)=1
+lfu.put(2, 2);   // cache=[2,1], cnt(2)=1, cnt(1)=1
+lfu.get(1);      // return 1
+                 // cache=[1,2], cnt(2)=1, cnt(1)=2
+lfu.put(3, 3);   // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
+                 // cache=[3,1], cnt(3)=1, cnt(1)=2
+lfu.get(2);      // return -1 (not found)
+lfu.get(3);      // return 3
+                 // cache=[3,1], cnt(3)=2, cnt(1)=2
+lfu.put(4, 4);   // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
+                 // cache=[4,3], cnt(4)=1, cnt(3)=2
+lfu.get(1);      // return -1 (not found)
+lfu.get(3);      // return 3
+                 // cache=[3,4], cnt(4)=1, cnt(3)=3
+lfu.get(4);      // return 4
+                 // cache=[4,3], cnt(4)=2, cnt(3)=3
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= capacity <= 104
  • +
  • 0 <= key <= 105
  • +
  • 0 <= value <= 109
  • +
  • At most 2 * 105 calls will be made to get and put.
  • +
+ +

 

+ 
\ No newline at end of file From a0d5f563ce376eaba2fde10be889ffaa64139567 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 00:06:06 +0530 Subject: [PATCH 0384/3167] Time: 514 ms (86.31%), Space: 186.8 MB (41.29%) - LeetHub --- 0460-lfu-cache/0460-lfu-cache.cpp | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0460-lfu-cache/0460-lfu-cache.cpp diff --git a/0460-lfu-cache/0460-lfu-cache.cpp b/0460-lfu-cache/0460-lfu-cache.cpp new file mode 100644 index 00000000..b9412581 --- /dev/null +++ b/0460-lfu-cache/0460-lfu-cache.cpp @@ -0,0 +1,49 @@ +class LFUCache { + int capacity; + int minFreq; + unordered_map> keyVal; + unordered_map> freqList; + unordered_map::iterator> pos; +public: + LFUCache(int capacity) { + this->capacity = capacity; + minFreq = 0; + } + + int get(int key) { + if(keyVal.find(key) == keyVal.end()) + return -1; + freqList[keyVal[key].second].erase(pos[key]); + keyVal[key].second++; + freqList[keyVal[key].second].push_back(key); + pos[key] = --freqList[keyVal[key].second].end(); + if(freqList[minFreq].empty()) + minFreq++; + return keyVal[key].first; + } + + void put(int key, int value) { + if(!capacity) + return; + if(keyVal.find(key) != keyVal.end()) { + keyVal[key].first = value; + freqList[keyVal[key].second].erase(pos[key]); + keyVal[key].second++; + freqList[keyVal[key].second].push_back(key); + pos[key] = --freqList[keyVal[key].second].end(); + if(freqList[minFreq].empty()) + minFreq++; + return; + } + if(keyVal.size() == capacity) { + int delKey = freqList[minFreq].front(); + keyVal.erase(delKey); + pos.erase(delKey); + freqList[minFreq].pop_front(); + } + keyVal[key] = {value,1}; + freqList[1].push_back(key); + pos[key] = --freqList[1].end(); + minFreq = 1; + } +}; \ No newline at end of file From 3538da6155d3598803678ce4f06604e24eff47b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:33:49 +0530 Subject: [PATCH 0385/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2231-largest-number-after-digit-swaps-by-parity/README.md diff --git a/2231-largest-number-after-digit-swaps-by-parity/README.md b/2231-largest-number-after-digit-swaps-by-parity/README.md new file mode 100644 index 00000000..92c56a00 --- /dev/null +++ b/2231-largest-number-after-digit-swaps-by-parity/README.md @@ -0,0 +1,31 @@ +

2231. Largest Number After Digit Swaps by Parity

Easy


You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits).

+ +

Return the largest possible value of num after any number of swaps.

+ +

 

+

Example 1:

+ +
Input: num = 1234
+Output: 3412
+Explanation: Swap the digit 3 with the digit 1, this results in the number 3214.
+Swap the digit 2 with the digit 4, this results in the number 3412.
+Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
+Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
+
+ +

Example 2:

+ +
Input: num = 65875
+Output: 87655
+Explanation: Swap the digit 8 with the digit 6, this results in the number 85675.
+Swap the first digit 5 with the digit 7, this results in the number 87655.
+Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num <= 109
  • +
+
\ No newline at end of file From 7e16bda571a6edcc9ed13f29c91c2438912ac687 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:33:52 +0530 Subject: [PATCH 0386/3167] Time: 5 ms (10.06%), Space: 6.2 MB (9.50%) - LeetHub --- ...est-number-after-digit-swaps-by-parity.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2231-largest-number-after-digit-swaps-by-parity/2231-largest-number-after-digit-swaps-by-parity.cpp diff --git a/2231-largest-number-after-digit-swaps-by-parity/2231-largest-number-after-digit-swaps-by-parity.cpp b/2231-largest-number-after-digit-swaps-by-parity/2231-largest-number-after-digit-swaps-by-parity.cpp new file mode 100644 index 00000000..299d4d95 --- /dev/null +++ b/2231-largest-number-after-digit-swaps-by-parity/2231-largest-number-after-digit-swaps-by-parity.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + + // atmost 2 + int largestInteger(int num) { + + int cnt = 0; + vector v, even, odd; + + while(num > 0) + { + v.push_back(num%10); + num /= 10; + } + + for(auto itr : v) + { + if(itr & 1) + odd.push_back(itr); + else + even.push_back(itr); + } + + reverse(v.begin(),v.end()); + sort(even.begin(),even.end(),greater()); + sort(odd.begin(),odd.end(),greater()); + + int k = 0, l = 0; + + string ans; + + for(auto itr : v) + { + if(itr & 1) + ans += to_string(odd[k++]); + else + ans += to_string(even[l++]); + } + + // cout< Date: Mon, 30 Jan 2023 12:49:28 +0530 Subject: [PATCH 0387/3167] Create README - LeetHub --- 1137-n-th-tribonacci-number/README.md | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1137-n-th-tribonacci-number/README.md diff --git a/1137-n-th-tribonacci-number/README.md b/1137-n-th-tribonacci-number/README.md new file mode 100644 index 00000000..198667d5 --- /dev/null +++ b/1137-n-th-tribonacci-number/README.md @@ -0,0 +1,29 @@ +

1137. N-th Tribonacci Number

Easy


The Tribonacci sequence Tn is defined as follows: 

+ +

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

+ +

Given n, return the value of Tn.

+ +

 

+

Example 1:

+ +
Input: n = 4
+Output: 4
+Explanation:
+T_3 = 0 + 1 + 1 = 2
+T_4 = 1 + 1 + 2 = 4
+
+ +

Example 2:

+ +
Input: n = 25
+Output: 1389537
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 37
  • +
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.
  • +
\ No newline at end of file From 9c7e083517a0e939925b6cb09aad96690c0bab47 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:49:29 +0530 Subject: [PATCH 0388/3167] Attach NOTES - LeetHub --- 1137-n-th-tribonacci-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1137-n-th-tribonacci-number/NOTES.md diff --git a/1137-n-th-tribonacci-number/NOTES.md b/1137-n-th-tribonacci-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1137-n-th-tribonacci-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 83fe33c0320326871f9d42bfbe53ea6123143f9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:49:33 +0530 Subject: [PATCH 0389/3167] Time: 0 ms (100.00%), Space: 6 MB (73.21%) - LeetHub --- .../1137-n-th-tribonacci-number.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp diff --git a/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp b/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp new file mode 100644 index 00000000..77766916 --- /dev/null +++ b/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + + int tribonacci(int n) { + + if(n == 0) return 0; + if(n == 1 or n == 2) return 1; + + int dp[n+1]; + + dp[0] = 0; + dp[1] = 1; + dp[2] = 1; + + for(int i = 3; i<=n; ++i) + { + dp[i] = dp[i-1] + dp[i-2] + dp[i-3]; + } + + return dp[n]; + } +}; \ No newline at end of file From 1536edd0417d1491050bf672a2500bb43941c8b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:54:28 +0530 Subject: [PATCH 0390/3167] Attach NOTES - LeetHub From 7903a47951864c9b13c10962b3d9a8fee0ab5196 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:54:32 +0530 Subject: [PATCH 0391/3167] Time: 0 ms (100.00%), Space: 6 MB (73.21%) - LeetHub From 61b265d7db884521b5bfc57b2dce0d75d4f0f011 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 13:13:13 +0530 Subject: [PATCH 0392/3167] Attach NOTES - LeetHub From bc1dba1392de2325dec41000b73918b7539f9648 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 13:13:16 +0530 Subject: [PATCH 0393/3167] Time: 3 ms (38.83%), Space: 6 MB (37.24%) - LeetHub --- .../1137-n-th-tribonacci-number.cpp | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp b/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp index 77766916..55514f8d 100644 --- a/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp +++ b/1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.cpp @@ -1,22 +1,21 @@ class Solution { public: - int tribonacci(int n) { - if(n == 0) return 0; - if(n == 1 or n == 2) return 1; + if(n == 0) + return 0; + if(n <= 2) + return 1; - int dp[n+1]; + int prev3 = 0, prev2 = 1, prev1 = 1, next; - dp[0] = 0; - dp[1] = 1; - dp[2] = 1; - - for(int i = 3; i<=n; ++i) + for(int i = 3; i <= n; ++i) { - dp[i] = dp[i-1] + dp[i-2] + dp[i-3]; + next = prev1 + prev2 + prev3; + prev3 = prev2; + prev2 = prev1; + prev1 = next; } - - return dp[n]; + return next; } }; \ No newline at end of file From 556cf606e239cee17680321daa83f8de2db68317 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 21:53:50 +0530 Subject: [PATCH 0394/3167] Create README - LeetHub --- 0146-lru-cache/README.md | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0146-lru-cache/README.md diff --git a/0146-lru-cache/README.md b/0146-lru-cache/README.md new file mode 100644 index 00000000..ae892bc0 --- /dev/null +++ b/0146-lru-cache/README.md @@ -0,0 +1,44 @@ +

146. LRU Cache

Medium


Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

+ +

Implement the LRUCache class:

+ +
    +
  • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  • +
  • int get(int key) Return the value of the key if the key exists, otherwise return -1.
  • +
  • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
  • +
+ +

The functions get and put must each run in O(1) average time complexity.

+ +

 

+

Example 1:

+ +
Input
+["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
+[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
+Output
+[null, null, null, 1, null, -1, null, -1, 3, 4]
+
+Explanation
+LRUCache lRUCache = new LRUCache(2);
+lRUCache.put(1, 1); // cache is {1=1}
+lRUCache.put(2, 2); // cache is {1=1, 2=2}
+lRUCache.get(1);    // return 1
+lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
+lRUCache.get(2);    // returns -1 (not found)
+lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
+lRUCache.get(1);    // return -1 (not found)
+lRUCache.get(3);    // return 3
+lRUCache.get(4);    // return 4
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= capacity <= 3000
  • +
  • 0 <= key <= 104
  • +
  • 0 <= value <= 105
  • +
  • At most 2 * 105 calls will be made to get and put.
  • +
+
\ No newline at end of file From 8e1e8b4552e3edacb9edd92ea4839bd6c4ecd207 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 21:53:50 +0530 Subject: [PATCH 0395/3167] Attach NOTES - LeetHub --- 0146-lru-cache/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0146-lru-cache/NOTES.md diff --git a/0146-lru-cache/NOTES.md b/0146-lru-cache/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0146-lru-cache/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 063471bd044d8662b0467e1a818432b28f6a8119 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Jan 2023 21:53:54 +0530 Subject: [PATCH 0396/3167] Time: 470 ms (75.24%), Space: 178.6 MB (18.27%) - LeetHub --- 0146-lru-cache/0146-lru-cache.cpp | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 0146-lru-cache/0146-lru-cache.cpp diff --git a/0146-lru-cache/0146-lru-cache.cpp b/0146-lru-cache/0146-lru-cache.cpp new file mode 100644 index 00000000..b04c62f3 --- /dev/null +++ b/0146-lru-cache/0146-lru-cache.cpp @@ -0,0 +1,82 @@ +class LRUCache { +public: + + class Node{ + public: + int key, val; + Node* prev, *next; + Node(int k, int v) + { + key = k; + val = v; + } + }; + + int cap; + Node* head = new Node(-1,-1); + Node* tail = new Node(-1,-1); + + unordered_map mp; + + void addNode(Node* ptr) + { + Node* temp = head->next; + head->next = ptr; + ptr->prev = head; + ptr->next = temp; + temp->prev = ptr; + } + + void deleteNode(Node *ptr) + { + Node* ptrPrev = ptr->prev; + Node* ptrNext = ptr->next; + ptrPrev->next = ptrNext; + ptrNext->prev = ptrPrev; + } + + LRUCache(int capacity) { + cap = capacity; + head->next = tail; + tail->prev = head; + } + + int get(int key) { + if(mp.find(key) != mp.end()) + { + Node* resultNode = mp[key]; + int res = resultNode->val; + mp.erase(key); + deleteNode(resultNode); + addNode(resultNode); + mp[key] = head->next; + return res; + } + return -1; + } + + void put(int key, int value) { + if(mp.find(key) != mp.end()) + { + Node* existingNode = mp[key]; + mp.erase(key); + deleteNode(existingNode); + } + if(mp.size() == cap) + { + Node* recentNode = tail->prev; + mp.erase(recentNode->key); + deleteNode(recentNode); + } + + addNode(new Node(key, value)); + mp[key] = head->next; + } +}; + +/** + * Your LRUCache object will be instantiated and called as such: + * LRUCache* obj = new LRUCache(capacity); + * int param_1 = obj->get(key); + * obj->put(key,value); + */ \ No newline at end of file From 0b24faef5d929f431c41b550c99f4b546aea983e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Jan 2023 23:30:48 +0530 Subject: [PATCH 0397/3167] Create README - LeetHub --- 1626-best-team-with-no-conflicts/README.md | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1626-best-team-with-no-conflicts/README.md diff --git a/1626-best-team-with-no-conflicts/README.md b/1626-best-team-with-no-conflicts/README.md new file mode 100644 index 00000000..ec832c60 --- /dev/null +++ b/1626-best-team-with-no-conflicts/README.md @@ -0,0 +1,38 @@ +

1626. Best Team With No Conflicts

Medium


You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.

+ +

However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.

+ +

Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and age of the ith player, respectively, return the highest overall score of all possible basketball teams.

+ +

 

+

Example 1:

+ +
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
+Output: 34
+Explanation: You can choose all the players.
+
+ +

Example 2:

+ +
Input: scores = [4,5,6,5], ages = [2,1,2,1]
+Output: 16
+Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.
+
+ +

Example 3:

+ +
Input: scores = [1,2,3,5], ages = [8,9,10,1]
+Output: 6
+Explanation: It is best to choose the first 3 players. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= scores.length, ages.length <= 1000
  • +
  • scores.length == ages.length
  • +
  • 1 <= scores[i] <= 106
  • +
  • 1 <= ages[i] <= 1000
  • +
+
\ No newline at end of file From dbe87bcd018d5a237f38dd761b6728f46ba081c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Jan 2023 23:30:52 +0530 Subject: [PATCH 0398/3167] Time: 162 ms (86.36%), Space: 19.5 MB (79.87%) - LeetHub --- .../1626-best-team-with-no-conflicts.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1626-best-team-with-no-conflicts/1626-best-team-with-no-conflicts.cpp diff --git a/1626-best-team-with-no-conflicts/1626-best-team-with-no-conflicts.cpp b/1626-best-team-with-no-conflicts/1626-best-team-with-no-conflicts.cpp new file mode 100644 index 00000000..a0f445e3 --- /dev/null +++ b/1626-best-team-with-no-conflicts/1626-best-team-with-no-conflicts.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int bestTeamScore(vector& scores, vector& ages) { + int n = scores.size(); + int dp[n], ans = 0; + vector> players; + for(int i = 0; i < n; i++) + players.push_back({ages[i], scores[i]}); + sort(players.begin(), players.end()); + for(int i = 0; i < n; i++) { + dp[i] = players[i].second; + for(int j = 0; j < i; j++) { + if(players[j].second <= players[i].second) + dp[i] = max(dp[i], dp[j] + players[i].second); + } + ans = max(ans, dp[i]); + } + return ans; + } +}; \ No newline at end of file From ccea6bd4c53f61fe0b8bce77678cf0104e3e33c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Feb 2023 09:26:55 +0530 Subject: [PATCH 0399/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1071-greatest-common-divisor-of-strings/README.md diff --git a/1071-greatest-common-divisor-of-strings/README.md b/1071-greatest-common-divisor-of-strings/README.md new file mode 100644 index 00000000..67a8a3af --- /dev/null +++ b/1071-greatest-common-divisor-of-strings/README.md @@ -0,0 +1,31 @@ +

1071. Greatest Common Divisor of Strings

Easy


For two strings s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times).

+ +

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

+ +

 

+

Example 1:

+ +
Input: str1 = "ABCABC", str2 = "ABC"
+Output: "ABC"
+
+ +

Example 2:

+ +
Input: str1 = "ABABAB", str2 = "ABAB"
+Output: "AB"
+
+ +

Example 3:

+ +
Input: str1 = "LEET", str2 = "CODE"
+Output: ""
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= str1.length, str2.length <= 1000
  • +
  • str1 and str2 consist of English uppercase letters.
  • +
+
\ No newline at end of file From 489ed394caf804260770a4851e1764c4eb18de22 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Feb 2023 09:26:59 +0530 Subject: [PATCH 0400/3167] Time: 3 ms (83.39%), Space: 7.2 MB (43.04%) - LeetHub --- .../1071-greatest-common-divisor-of-strings.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1071-greatest-common-divisor-of-strings/1071-greatest-common-divisor-of-strings.cpp diff --git a/1071-greatest-common-divisor-of-strings/1071-greatest-common-divisor-of-strings.cpp b/1071-greatest-common-divisor-of-strings/1071-greatest-common-divisor-of-strings.cpp new file mode 100644 index 00000000..e8bee77c --- /dev/null +++ b/1071-greatest-common-divisor-of-strings/1071-greatest-common-divisor-of-strings.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + string gcdOfStrings(string str1, string str2) { + + if(str1 + str2 != str2 + str1) + return ""; + + return str1.substr(0, __gcd(size(str1), size(str2))); + + } +}; \ No newline at end of file From c3ba1b527883a64ae37f3361fcba0e6e2401eab1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Feb 2023 09:27:41 +0530 Subject: [PATCH 0401/3167] Attach NOTES - LeetHub --- 1071-greatest-common-divisor-of-strings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1071-greatest-common-divisor-of-strings/NOTES.md diff --git a/1071-greatest-common-divisor-of-strings/NOTES.md b/1071-greatest-common-divisor-of-strings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1071-greatest-common-divisor-of-strings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7ceb05a26eb51a58d5ffa0b62aceab5cb684d0fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Feb 2023 09:27:44 +0530 Subject: [PATCH 0402/3167] Time: 3 ms (83.39%), Space: 7.2 MB (43.04%) - LeetHub From 10921474378af5ee6f8f149f0aadd0b5dd8c2df7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Feb 2023 19:19:31 +0530 Subject: [PATCH 0403/3167] Create README - LeetHub --- 0953-verifying-an-alien-dictionary/README.md | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0953-verifying-an-alien-dictionary/README.md diff --git a/0953-verifying-an-alien-dictionary/README.md b/0953-verifying-an-alien-dictionary/README.md new file mode 100644 index 00000000..c8bab49a --- /dev/null +++ b/0953-verifying-an-alien-dictionary/README.md @@ -0,0 +1,36 @@ +

953. Verifying an Alien Dictionary

Easy


In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

+ +

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.

+ +

 

+

Example 1:

+ +
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
+Output: true
+Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
+
+ +

Example 2:

+ +
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
+Output: false
+Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
+
+ +

Example 3:

+ +
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
+Output: false
+Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 20
  • +
  • order.length == 26
  • +
  • All characters in words[i] and order are English lowercase letters.
  • +
+
\ No newline at end of file From 77b493637e4cbd9a98b61a81e88ecbf97e9b8d6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Feb 2023 19:19:31 +0530 Subject: [PATCH 0404/3167] Attach NOTES - LeetHub --- 0953-verifying-an-alien-dictionary/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0953-verifying-an-alien-dictionary/NOTES.md diff --git a/0953-verifying-an-alien-dictionary/NOTES.md b/0953-verifying-an-alien-dictionary/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0953-verifying-an-alien-dictionary/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c31e02b3c85381abe9178889bbe60e496e248e6c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Feb 2023 19:19:34 +0530 Subject: [PATCH 0405/3167] Time: 3 ms (93.87%), Space: 9.8 MB (25.79%) - LeetHub --- .../0953-verifying-an-alien-dictionary.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0953-verifying-an-alien-dictionary/0953-verifying-an-alien-dictionary.cpp diff --git a/0953-verifying-an-alien-dictionary/0953-verifying-an-alien-dictionary.cpp b/0953-verifying-an-alien-dictionary/0953-verifying-an-alien-dictionary.cpp new file mode 100644 index 00000000..d102c35c --- /dev/null +++ b/0953-verifying-an-alien-dictionary/0953-verifying-an-alien-dictionary.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + bool helper(string& a, string& b, map& mp) + { + int i = 0, j = 0; + + while(i < a.size() and j < b.size()) + { + if(a[i] != b[i]) + { + if(mp[a[i]] > mp[b[i]]) + return false; + else + return true; + } + ++i, ++j; + } + if(a.size() > b.size()) + return false; + return true; + } + + bool isAlienSorted(vector& words, string order) { + + map mp; + for(int i = 0; i < order.size(); ++i) + mp.insert({order[i],i+1}); + + for(int i = 0; i < words.size()-1; ++i) + { + if(!helper(words[i], words[i+1], mp)) + return false; + } + return true; + } +}; \ No newline at end of file From a5fe79c04d29309bc266dd6553f3de86ea9a124c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Feb 2023 01:17:54 +0530 Subject: [PATCH 0406/3167] Create README - LeetHub --- 0006-zigzag-conversion/README.md | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0006-zigzag-conversion/README.md diff --git a/0006-zigzag-conversion/README.md b/0006-zigzag-conversion/README.md new file mode 100644 index 00000000..b4396e03 --- /dev/null +++ b/0006-zigzag-conversion/README.md @@ -0,0 +1,47 @@ +

6. Zigzag Conversion

Medium


The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

+ +
P   A   H   N
+A P L S I I G
+Y   I   R
+
+ +

And then read line by line: "PAHNAPLSIIGYIR"

+ +

Write the code that will take a string and make this conversion given a number of rows:

+ +
string convert(string s, int numRows);
+
+ +

 

+

Example 1:

+ +
Input: s = "PAYPALISHIRING", numRows = 3
+Output: "PAHNAPLSIIGYIR"
+
+ +

Example 2:

+ +
Input: s = "PAYPALISHIRING", numRows = 4
+Output: "PINALSIGYAHRPI"
+Explanation:
+P     I    N
+A   L S  I G
+Y A   H R
+P     I
+
+ +

Example 3:

+ +
Input: s = "A", numRows = 1
+Output: "A"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • +
  • 1 <= numRows <= 1000
  • +
+
\ No newline at end of file From ed25e78e6073de647ea703eda63ca7f93d755f56 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Feb 2023 01:17:58 +0530 Subject: [PATCH 0407/3167] Time: 3 ms (98.89%), Space: 10.6 MB (48.65%) - LeetHub --- .../0006-zigzag-conversion.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0006-zigzag-conversion/0006-zigzag-conversion.cpp diff --git a/0006-zigzag-conversion/0006-zigzag-conversion.cpp b/0006-zigzag-conversion/0006-zigzag-conversion.cpp new file mode 100644 index 00000000..9f22a4ea --- /dev/null +++ b/0006-zigzag-conversion/0006-zigzag-conversion.cpp @@ -0,0 +1,51 @@ +class Solution { +public: + string convert(string s, int numRows) { + + int n = s.size(); + + vector v(numRows); + string ans; + + if(numRows == 1) + return s; + + int k = 0; + bool ok = true; + for(int i = 0; i Date: Sat, 4 Feb 2023 22:38:46 +0530 Subject: [PATCH 0408/3167] Create README - LeetHub --- 0567-permutation-in-string/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0567-permutation-in-string/README.md diff --git a/0567-permutation-in-string/README.md b/0567-permutation-in-string/README.md new file mode 100644 index 00000000..095f6526 --- /dev/null +++ b/0567-permutation-in-string/README.md @@ -0,0 +1,26 @@ +

567. Permutation in String

Medium


Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.

+ +

In other words, return true if one of s1's permutations is the substring of s2.

+ +

 

+

Example 1:

+ +
Input: s1 = "ab", s2 = "eidbaooo"
+Output: true
+Explanation: s2 contains one permutation of s1 ("ba").
+
+ +

Example 2:

+ +
Input: s1 = "ab", s2 = "eidboaoo"
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s1.length, s2.length <= 104
  • +
  • s1 and s2 consist of lowercase English letters.
  • +
+
\ No newline at end of file From 4efe74caa008b603f36e6fea6c0d4fc13c8a7d1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Feb 2023 22:38:49 +0530 Subject: [PATCH 0409/3167] Time: 31 ms (28.27%), Space: 29.9 MB (11.76%) - LeetHub --- .../0567-permutation-in-string.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0567-permutation-in-string/0567-permutation-in-string.cpp diff --git a/0567-permutation-in-string/0567-permutation-in-string.cpp b/0567-permutation-in-string/0567-permutation-in-string.cpp new file mode 100644 index 00000000..a8f32418 --- /dev/null +++ b/0567-permutation-in-string/0567-permutation-in-string.cpp @@ -0,0 +1,33 @@ +class Solution { + bool areVectorsEqual(vector a, vector b){ + for(int i=0; i<26; i++){ + if(a[i]!=b[i]) return false; + } + return true; + } +public: + bool checkInclusion(string s1, string s2) { + if(s2.size() freqS1(26, 0); + for(char c: s1) freqS1[c-'a']++; + + vector freqS2(26, 0); + int i=0, j=0; + + while(j Date: Mon, 6 Feb 2023 23:12:13 +0530 Subject: [PATCH 0410/3167] Create BST Downward Traversal.cpp --- BST Downward Traversal.cpp | 171 +++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 BST Downward Traversal.cpp diff --git a/BST Downward Traversal.cpp b/BST Downward Traversal.cpp new file mode 100644 index 00000000..350d82c8 --- /dev/null +++ b/BST Downward Traversal.cpp @@ -0,0 +1,171 @@ +//{ Driver Code Starts +//Initial Template for C++ +#include +using namespace std; +#define MAX_HEIGHT 100000 + +// Tree Node +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + + + + +// } Driver Code Ends +//User function Template for C++ +/* +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +*/ + +class Solution{ +public: + + int ans = 0; + int sum(Node *root, int level) + { + if(!root) + return 0; + if(level == 0) + ans += root->data; + sum(root->left,level - 1); + sum (root->right,level + 1); + + return ans; + } + + Node* helper(Node* root, int target){ + if(!root) + return nullptr; + if(root->data == target) + return root; + if(root->data < target) + helper(root->right,target); + else if(root->data > target) + helper(root->left,target); + } + + long long int verticallyDownBST(Node *root,int target){ + // Code here + + Node* ptr = helper(root,target); + if(!ptr) + return -1; + + return sum(ptr,0) - ptr->data; + } +}; + +//{ Driver Code Starts. + +// 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; +} + +void inorder(Node *root, vector &v) +{ + if(root==NULL) + return; + + inorder(root->left, v); + v.push_back(root->data); + inorder(root->right, v); +} + +int main() { + + int t; + string tc; + getline(cin, tc); + t=stoi(tc); + while(t--) + { + int target; + cin>>target; + string newline; + getline(cin,newline); + string s; + getline(cin, s); + Node* root = buildTree(s); + Solution ob; + cout< Date: Mon, 6 Feb 2023 23:25:48 +0530 Subject: [PATCH 0411/3167] Delete BST Downward Traversal.cpp --- BST Downward Traversal.cpp | 171 ------------------------------------- 1 file changed, 171 deletions(-) delete mode 100644 BST Downward Traversal.cpp diff --git a/BST Downward Traversal.cpp b/BST Downward Traversal.cpp deleted file mode 100644 index 350d82c8..00000000 --- a/BST Downward Traversal.cpp +++ /dev/null @@ -1,171 +0,0 @@ -//{ Driver Code Starts -//Initial Template for C++ -#include -using namespace std; -#define MAX_HEIGHT 100000 - -// Tree Node -struct Node { - int data; - Node *left; - Node *right; - - Node(int val) { - data = val; - left = right = NULL; - } -}; - - - - -// } Driver Code Ends -//User function Template for C++ -/* -struct Node { - int data; - Node *left; - Node *right; - - Node(int val) { - data = val; - left = right = NULL; - } -}; -*/ - -class Solution{ -public: - - int ans = 0; - int sum(Node *root, int level) - { - if(!root) - return 0; - if(level == 0) - ans += root->data; - sum(root->left,level - 1); - sum (root->right,level + 1); - - return ans; - } - - Node* helper(Node* root, int target){ - if(!root) - return nullptr; - if(root->data == target) - return root; - if(root->data < target) - helper(root->right,target); - else if(root->data > target) - helper(root->left,target); - } - - long long int verticallyDownBST(Node *root,int target){ - // Code here - - Node* ptr = helper(root,target); - if(!ptr) - return -1; - - return sum(ptr,0) - ptr->data; - } -}; - -//{ Driver Code Starts. - -// 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; -} - -void inorder(Node *root, vector &v) -{ - if(root==NULL) - return; - - inorder(root->left, v); - v.push_back(root->data); - inorder(root->right, v); -} - -int main() { - - int t; - string tc; - getline(cin, tc); - t=stoi(tc); - while(t--) - { - int target; - cin>>target; - string newline; - getline(cin,newline); - string s; - getline(cin, s); - Node* root = buildTree(s); - Solution ob; - cout< Date: Tue, 7 Feb 2023 07:48:24 +0530 Subject: [PATCH 0412/3167] Create README - LeetHub --- 1470-shuffle-the-array/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1470-shuffle-the-array/README.md diff --git a/1470-shuffle-the-array/README.md b/1470-shuffle-the-array/README.md new file mode 100644 index 00000000..49c3eae9 --- /dev/null +++ b/1470-shuffle-the-array/README.md @@ -0,0 +1,32 @@ +

1470. Shuffle the Array

Easy


Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

+ +

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

+ +

 

+

Example 1:

+ +
Input: nums = [2,5,1,3,4,7], n = 3
+Output: [2,3,5,4,1,7] 
+Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
+
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 500
  • +
  • nums.length == 2n
  • +
  • 1 <= nums[i] <= 10^3
  • +
\ No newline at end of file From 365320a298fc7479a1e5f66470fd48cd994fb2ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 07:48:25 +0530 Subject: [PATCH 0413/3167] Attach NOTES - LeetHub --- 1470-shuffle-the-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1470-shuffle-the-array/NOTES.md diff --git a/1470-shuffle-the-array/NOTES.md b/1470-shuffle-the-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1470-shuffle-the-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e63ca4042833c27a149f1f514f14b45f8607d904 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 07:48:28 +0530 Subject: [PATCH 0414/3167] Time: 18 ms (5.56%), Space: 9.7 MB (87.16%) - LeetHub --- .../1470-shuffle-the-array.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1470-shuffle-the-array/1470-shuffle-the-array.cpp diff --git a/1470-shuffle-the-array/1470-shuffle-the-array.cpp b/1470-shuffle-the-array/1470-shuffle-the-array.cpp new file mode 100644 index 00000000..ba652b0b --- /dev/null +++ b/1470-shuffle-the-array/1470-shuffle-the-array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector shuffle(vector& nums, int n) { + + int i = 1, j = n; + while(i < nums.size() and j < nums.size()) + { + int k = i; + while(i < j) + swap(nums[i++],nums[j]); + i = k + 2; + ++j; + } + return nums; + } +}; \ No newline at end of file From d3ba6ac6ad57677088bb28e02443843fec6dcdc6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 08:45:23 +0530 Subject: [PATCH 0415/3167] Attach NOTES - LeetHub From 8546d4e35ec7f6e16d8e2fd94187b62d1447e646 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 08:45:26 +0530 Subject: [PATCH 0416/3167] Time: 7 ms (61.84%), Space: 9.7 MB (61.99%) - LeetHub --- .../1470-shuffle-the-array.cpp | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/1470-shuffle-the-array/1470-shuffle-the-array.cpp b/1470-shuffle-the-array/1470-shuffle-the-array.cpp index ba652b0b..db044029 100644 --- a/1470-shuffle-the-array/1470-shuffle-the-array.cpp +++ b/1470-shuffle-the-array/1470-shuffle-the-array.cpp @@ -2,14 +2,20 @@ class Solution { public: vector shuffle(vector& nums, int n) { - int i = 1, j = n; - while(i < nums.size() and j < nums.size()) + // storing secNum in next 10 bits and or with first to combine second and first + for(int i = n; i < 2*n; ++i) { - int k = i; - while(i < j) - swap(nums[i++],nums[j]); - i = k + 2; - ++j; + int secNum = nums[i] << 10; + nums[i-n] |= secNum; + } + + for(int i = n-1; i>= 0; --i) + { + int secNum = nums[i] >> 10; + int firstNum = nums[i] & 1023; + + nums[2*i+1] = secNum; + nums[2*i] = firstNum; } return nums; } From dd174507ad9c43ef30b710d925423dc224fdbf15 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 09:04:49 +0530 Subject: [PATCH 0417/3167] Attach NOTES - LeetHub --- 0384-shuffle-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0384-shuffle-an-array/NOTES.md diff --git a/0384-shuffle-an-array/NOTES.md b/0384-shuffle-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0384-shuffle-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7d6ae69e74024b2fd12371b6e8929c76b0ff7b80 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 09:04:53 +0530 Subject: [PATCH 0418/3167] Time: 63 ms (89.83%), Space: 57.1 MB (14.15%) - LeetHub --- .../0384-shuffle-an-array.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0384-shuffle-an-array/0384-shuffle-an-array.cpp diff --git a/0384-shuffle-an-array/0384-shuffle-an-array.cpp b/0384-shuffle-an-array/0384-shuffle-an-array.cpp new file mode 100644 index 00000000..8bd8e359 --- /dev/null +++ b/0384-shuffle-an-array/0384-shuffle-an-array.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + + vector org; + int n; + + Solution(vector& nums) { + org = nums; + n = nums.size(); + } + + vector reset() { + return org; + } + + vector shuffle() { + vector temp = org; + int size = org.size(); + + for(int i = n-1; i>=0; --i) + { + int j = rand() % size; + swap(temp[i],temp[j]); + --size; + } + + return temp; + } +}; + +/** + * Your Solution object will be instantiated and called as such: + * Solution* obj = new Solution(nums); + * vector param_1 = obj->reset(); + * vector param_2 = obj->shuffle(); + */ \ No newline at end of file From 603483192faf87184f53672f4d1e633440c3b9e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 09:05:03 +0530 Subject: [PATCH 0419/3167] Attach NOTES - LeetHub From e58c2a6c8d116f6b8b287f47336e752bb257057b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 09:05:06 +0530 Subject: [PATCH 0420/3167] Time: 63 ms (89.83%), Space: 57.1 MB (14.15%) - LeetHub From 83f7ce32582822fa6a3fbddf5f4082befb377975 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 10:20:03 +0530 Subject: [PATCH 0421/3167] Create README - LeetHub From 27f57abc9c06f65076cc0833a1061fd70d85a835 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Feb 2023 10:20:03 +0530 Subject: [PATCH 0422/3167] Attach NOTES - LeetHub --- 0053-maximum-subarray/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0053-maximum-subarray/NOTES.md diff --git a/0053-maximum-subarray/NOTES.md b/0053-maximum-subarray/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0053-maximum-subarray/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 383d0659bbb3811aaf7d97a35ae80d628af6b33c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Feb 2023 01:42:03 +0530 Subject: [PATCH 0423/3167] Create README - LeetHub --- 0904-fruit-into-baskets/README.md | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0904-fruit-into-baskets/README.md diff --git a/0904-fruit-into-baskets/README.md b/0904-fruit-into-baskets/README.md new file mode 100644 index 00000000..d8e708d4 --- /dev/null +++ b/0904-fruit-into-baskets/README.md @@ -0,0 +1,44 @@ +

904. Fruit Into Baskets

Medium


You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces.

+ +

You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:

+ +
    +
  • You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
  • +
  • Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
  • +
  • Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
  • +
+ +

Given the integer array fruits, return the maximum number of fruits you can pick.

+ +

 

+

Example 1:

+ +
Input: fruits = [1,2,1]
+Output: 3
+Explanation: We can pick from all 3 trees.
+
+ +

Example 2:

+ +
Input: fruits = [0,1,2,2]
+Output: 3
+Explanation: We can pick from trees [1,2,2].
+If we had started at the first tree, we would only pick from trees [0,1].
+
+ +

Example 3:

+ +
Input: fruits = [1,2,3,2,2]
+Output: 4
+Explanation: We can pick from trees [2,3,2,2].
+If we had started at the first tree, we would only pick from trees [1,2].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= fruits.length <= 105
  • +
  • 0 <= fruits[i] < fruits.length
  • +
+
\ No newline at end of file From bb6a9d066467d3314f3d7c65f10a255e589fd7ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Feb 2023 01:42:06 +0530 Subject: [PATCH 0424/3167] Time: 207 ms (36.20%), Space: 75.3 MB (12.99%) - LeetHub --- .../0904-fruit-into-baskets.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0904-fruit-into-baskets/0904-fruit-into-baskets.cpp diff --git a/0904-fruit-into-baskets/0904-fruit-into-baskets.cpp b/0904-fruit-into-baskets/0904-fruit-into-baskets.cpp new file mode 100644 index 00000000..8139d7d2 --- /dev/null +++ b/0904-fruit-into-baskets/0904-fruit-into-baskets.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int totalFruit(vector& fruits) { + + map mp; + int i = 0, j = 0, n = fruits.size(), k = 2; + int ans = 0; + + while(j < n) + { + ++mp[fruits[j]]; + if(mp.size() <= k) + { + ans = max(ans, j - i + 1); + } + else + { + while(mp.size() > k) + { + --mp[fruits[i]]; + if(mp[fruits[i]] == 0) + mp.erase(fruits[i]); + ++i; + } + } + ++j; + + } + + return ans; + + } +}; \ No newline at end of file From 293e8caf13de109f8e823c1dd29134456aae672a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Feb 2023 01:43:30 +0530 Subject: [PATCH 0425/3167] Attach NOTES - LeetHub --- 0904-fruit-into-baskets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0904-fruit-into-baskets/NOTES.md diff --git a/0904-fruit-into-baskets/NOTES.md b/0904-fruit-into-baskets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0904-fruit-into-baskets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 65259736a5ac9d467c27ad34a00bf38456dd67c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Feb 2023 01:43:33 +0530 Subject: [PATCH 0426/3167] Time: 207 ms (36.20%), Space: 75.3 MB (12.99%) - LeetHub From 754684f623b711f4370b1858237ce4cd161604af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 08:56:02 +0530 Subject: [PATCH 0427/3167] Create README - LeetHub --- 0312-burst-balloons/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0312-burst-balloons/README.md diff --git a/0312-burst-balloons/README.md b/0312-burst-balloons/README.md new file mode 100644 index 00000000..22efd541 --- /dev/null +++ b/0312-burst-balloons/README.md @@ -0,0 +1,30 @@ +

312. Burst Balloons

Hard


You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

+ +

If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

+ +

Return the maximum coins you can collect by bursting the balloons wisely.

+ +

 

+

Example 1:

+ +
Input: nums = [3,1,5,8]
+Output: 167
+Explanation:
+nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
+coins =  3*1*5    +   3*5*8   +  1*3*8  + 1*8*1 = 167
+ +

Example 2:

+ +
Input: nums = [1,5]
+Output: 10
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 300
  • +
  • 0 <= nums[i] <= 100
  • +
+
\ No newline at end of file From d4b0fddb5a8d289ba2556276aac21ada61b3f229 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 08:56:03 +0530 Subject: [PATCH 0428/3167] Attach NOTES - LeetHub --- 0312-burst-balloons/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0312-burst-balloons/NOTES.md diff --git a/0312-burst-balloons/NOTES.md b/0312-burst-balloons/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0312-burst-balloons/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b213a26a8787a4a6fee190da07d6b14671a2b8b4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 08:56:06 +0530 Subject: [PATCH 0429/3167] Time: 565 ms (48.91%), Space: 10.4 MB (63.30%) - LeetHub --- 0312-burst-balloons/0312-burst-balloons.cpp | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0312-burst-balloons/0312-burst-balloons.cpp diff --git a/0312-burst-balloons/0312-burst-balloons.cpp b/0312-burst-balloons/0312-burst-balloons.cpp new file mode 100644 index 00000000..d53ec5af --- /dev/null +++ b/0312-burst-balloons/0312-burst-balloons.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + int helper(int i , int j, vector& nums, vector>& dp) + { + if(i > j) + return 0; + if(dp[i][j] != -1) + return dp[i][j]; + int ans = INT_MIN; + for(int idx = i; idx <= j; ++idx) + { + int coins = nums[i-1] * nums[idx] * nums[j+1] + helper(i, idx-1, nums, dp) + helper(idx+1, j,nums, dp); + ans = max(ans,coins); + } + return dp[i][j] = ans; + } + + int maxCoins(vector& nums) { + + int n = nums.size(); + + nums.insert(nums.begin(),1); + nums.push_back(1); + + vector> dp(n+2,vector(n+2,-1)); + + return helper(1,n, nums, dp); + + } +}; \ No newline at end of file From a24dcef6fd27d61d05ef9b3bdc1c76ff51805006 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 09:07:58 +0530 Subject: [PATCH 0430/3167] Attach NOTES - LeetHub From ef55e2b504fea99f8ff05e6469185ae153ae8bc9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 09:08:00 +0530 Subject: [PATCH 0431/3167] Time: 340 ms (89.43%), Space: 10.4 MB (24.40%) - LeetHub --- 0312-burst-balloons/0312-burst-balloons.cpp | 23 +++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/0312-burst-balloons/0312-burst-balloons.cpp b/0312-burst-balloons/0312-burst-balloons.cpp index d53ec5af..6ef419c4 100644 --- a/0312-burst-balloons/0312-burst-balloons.cpp +++ b/0312-burst-balloons/0312-burst-balloons.cpp @@ -1,3 +1,4 @@ +#define ll long long int class Solution { public: @@ -23,9 +24,27 @@ class Solution { nums.insert(nums.begin(),1); nums.push_back(1); - vector> dp(n+2,vector(n+2,-1)); + // vector> dp(n+2,vector(n+2,-1)); + vector>dp(n+2,vector(n+2,0)); - return helper(1,n, nums, dp); + // return helper(1,n, nums, dp); + for(int i = n; i >= 1; --i) + { + for(int j = 1; j<=n; ++j) + { + if(i > j) + continue; + ll ans = INT_MIN; + + for(int idx = i; idx <= j; ++idx) + { + ll cost = (ll)nums[i-1] * nums[idx] * nums[j+1] + dp[i][idx-1] + dp[idx+1][j]; + ans = max(ans, cost); + } + dp[i][j] = ans; + } + } + return dp[1][n]; } }; \ No newline at end of file From 4ecd23a0995168809946c62786373030b7b1127f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 19:20:40 +0530 Subject: [PATCH 0432/3167] Create README - LeetHub --- 2306-naming-a-company/README.md | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2306-naming-a-company/README.md diff --git a/2306-naming-a-company/README.md b/2306-naming-a-company/README.md new file mode 100644 index 00000000..a2d1dbeb --- /dev/null +++ b/2306-naming-a-company/README.md @@ -0,0 +1,48 @@ +

2306. Naming a Company

Hard


You are given an array of strings ideas that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:

+ +
    +
  1. Choose 2 distinct names from ideas, call them ideaA and ideaB.
  2. +
  3. Swap the first letters of ideaA and ideaB with each other.
  4. +
  5. If both of the new names are not found in the original ideas, then the name ideaA ideaB (the concatenation of ideaA and ideaB, separated by a space) is a valid company name.
  6. +
  7. Otherwise, it is not a valid name.
  8. +
+ +

Return the number of distinct valid names for the company.

+ +

 

+

Example 1:

+ +
Input: ideas = ["coffee","donuts","time","toffee"]
+Output: 6
+Explanation: The following selections are valid:
+- ("coffee", "donuts"): The company name created is "doffee conuts".
+- ("donuts", "coffee"): The company name created is "conuts doffee".
+- ("donuts", "time"): The company name created is "tonuts dime".
+- ("donuts", "toffee"): The company name created is "tonuts doffee".
+- ("time", "donuts"): The company name created is "dime tonuts".
+- ("toffee", "donuts"): The company name created is "doffee tonuts".
+Therefore, there are a total of 6 distinct company names.
+
+The following are some examples of invalid selections:
+- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
+- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
+- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
+
+ +

Example 2:

+ +
Input: ideas = ["lack","back"]
+Output: 0
+Explanation: There are no valid selections. Therefore, 0 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= ideas.length <= 5 * 104
  • +
  • 1 <= ideas[i].length <= 10
  • +
  • ideas[i] consists of lowercase English letters.
  • +
  • All the strings in ideas are unique.
  • +
+
\ No newline at end of file From a15d49e76805436e21e7732a3c7053eb50ee24d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 19:20:41 +0530 Subject: [PATCH 0433/3167] Attach NOTES - LeetHub --- 2306-naming-a-company/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2306-naming-a-company/NOTES.md diff --git a/2306-naming-a-company/NOTES.md b/2306-naming-a-company/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2306-naming-a-company/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e6224e7a6f85c6084411176c235335a0548e1c8c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Feb 2023 19:20:44 +0530 Subject: [PATCH 0434/3167] Time: 671 ms (72.36%), Space: 83.3 MB (56.91%) - LeetHub --- .../2306-naming-a-company.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2306-naming-a-company/2306-naming-a-company.cpp diff --git a/2306-naming-a-company/2306-naming-a-company.cpp b/2306-naming-a-company/2306-naming-a-company.cpp new file mode 100644 index 00000000..058588d5 --- /dev/null +++ b/2306-naming-a-company/2306-naming-a-company.cpp @@ -0,0 +1,26 @@ +#define ll long long int +class Solution { +public: + long long distinctNames(vector& ideas) { + unordered_set count[26]; + + for(auto& itr : ideas) + count[itr[0] - 'a'].insert(itr.substr(1)); + + ll res = 0; + for(int i = 0; i < 26; ++i) + { + for(int j = i+1; j < 26; ++j) + { + ll c1 = 0, c2 = 0; + for(auto& c : count[i]) + if(!count[j].count(c)) ++c1; + for(auto& c : count[j]) + if(!count[i].count(c)) ++c2; + + res += c1 * c2; + } + } + return res * 2; + } +}; From 2ac99a37618aac6e545441a8b554432f2faeb532 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:28:00 +0530 Subject: [PATCH 0435/3167] Create README - LeetHub --- 1162-as-far-from-land-as-possible/README.md | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1162-as-far-from-land-as-possible/README.md diff --git a/1162-as-far-from-land-as-possible/README.md b/1162-as-far-from-land-as-possible/README.md new file mode 100644 index 00000000..54ce9084 --- /dev/null +++ b/1162-as-far-from-land-as-possible/README.md @@ -0,0 +1,29 @@ +

1162. As Far from Land as Possible

Medium


Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1.

+ +

The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
+Output: 2
+Explanation: The cell (1, 1) is as far as possible from all the land with distance 2.
+
+ +

Example 2:

+ +
Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
+Output: 4
+Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= n <= 100
  • +
  • grid[i][j] is 0 or 1
  • +
+
\ No newline at end of file From fd7193f86287944817619efbf9b48e1547855fa8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:28:03 +0530 Subject: [PATCH 0436/3167] Time: 289 ms (11.00%), Space: 65.8 MB (5.03%) - LeetHub --- .../1162-as-far-from-land-as-possible.cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 1162-as-far-from-land-as-possible/1162-as-far-from-land-as-possible.cpp diff --git a/1162-as-far-from-land-as-possible/1162-as-far-from-land-as-possible.cpp b/1162-as-far-from-land-as-possible/1162-as-far-from-land-as-possible.cpp new file mode 100644 index 00000000..8fa408c0 --- /dev/null +++ b/1162-as-far-from-land-as-possible/1162-as-far-from-land-as-possible.cpp @@ -0,0 +1,59 @@ +class Solution { +public: + int maxDistance(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + vector> visited(n,vector(m,false)); + vector> dir ={{0,1},{-1,0},{1,0}, {0,-1}}; + queue> q; + int ans = -1; + + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + { + visited[i][j] = true; + q.push({i,j}); + } + } + } + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0 ; i here = q.front(); + q.pop(); + + int currx = here.first; + int curry = here.second; + + for(auto itr : dir) + { + int newx = currx + itr[0]; + int newy = curry + itr[1]; + + + if(newx < 0 or newx >= n or newy < 0 or newy >= m or visited[newx][newy]) + continue; + + + visited[newx][newy] = true; + + grid[newx][newy] = grid[currx][curry] + 1; + + ans = max(ans,grid[newx][newy]); + + q.push({newx,newy}); + } + } + } + + return (ans == -1 ? -1 : ans - 1); + } +}; \ No newline at end of file From de2f9b523a16d4d4d1ad6ba848ede7f2f6c7f35f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:07:04 +0530 Subject: [PATCH 0437/3167] Create README - LeetHub --- 0021-merge-two-sorted-lists/README.md | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0021-merge-two-sorted-lists/README.md diff --git a/0021-merge-two-sorted-lists/README.md b/0021-merge-two-sorted-lists/README.md new file mode 100644 index 00000000..d9a9154d --- /dev/null +++ b/0021-merge-two-sorted-lists/README.md @@ -0,0 +1,34 @@ +

21. Merge Two Sorted Lists

Easy


You are given the heads of two sorted linked lists list1 and list2.

+ +

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

+ +

Return the head of the merged linked list.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: list1 = [], list2 = []
+Output: []
+
+ +

Example 3:

+ +
Input: list1 = [], list2 = [0]
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in both lists is in the range [0, 50].
  • +
  • -100 <= Node.val <= 100
  • +
  • Both list1 and list2 are sorted in non-decreasing order.
  • +
+
\ No newline at end of file From fa0fb5208452fb6bd6073a32035fd71b240c9be1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:07:04 +0530 Subject: [PATCH 0438/3167] Attach NOTES - LeetHub --- 0021-merge-two-sorted-lists/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0021-merge-two-sorted-lists/NOTES.md diff --git a/0021-merge-two-sorted-lists/NOTES.md b/0021-merge-two-sorted-lists/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0021-merge-two-sorted-lists/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 019510b4c7e6f755f99587b395fa85c03757484c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:07:07 +0530 Subject: [PATCH 0439/3167] Time: 12 ms (38.95%), Space: 14.9 MB (48.57%) - LeetHub --- .../0021-merge-two-sorted-lists.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp diff --git a/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp b/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp new file mode 100644 index 00000000..f0073f10 --- /dev/null +++ b/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp @@ -0,0 +1,43 @@ +/** + * 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* mergeTwoLists(ListNode* node1, ListNode* node2) { + + ListNode* dummy = new ListNode(); + ListNode* temp = dummy; + + while(node1 and node2) + { + if(node1->val <= node2->val) + { + temp->next = node1; + node1 = node1->next; + } + else + { + temp->next = node2; + node2 = node2->next; + } + temp = temp->next; + } + + if(node1) + temp->next = node1; + if(node2) + temp->next = node2; + + + return dummy->next; + + + } +}; \ No newline at end of file From 2e256f43f2fabdedf50ca1d240f707b143a65edb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:09:47 +0530 Subject: [PATCH 0440/3167] Attach NOTES - LeetHub From 06505326ab177d72f7c580bb7106dce3b75973c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:09:50 +0530 Subject: [PATCH 0441/3167] Time: 12 ms (38.95%), Space: 14.9 MB (48.57%) - LeetHub From ce69d06a791807c72b611c90ebafde22f3451ec0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:12:57 +0530 Subject: [PATCH 0442/3167] Attach NOTES - LeetHub From 17f392248df6e7127d90e961c81c64b73e96bd7e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 08:13:00 +0530 Subject: [PATCH 0443/3167] Time: 12 ms (38.95%), Space: 14.9 MB (48.57%) - LeetHub From 477fa8817271c083a4eb0a36f95b17f609a9f7ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:21:31 +0530 Subject: [PATCH 0444/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1129-shortest-path-with-alternating-colors/README.md diff --git a/1129-shortest-path-with-alternating-colors/README.md b/1129-shortest-path-with-alternating-colors/README.md new file mode 100644 index 00000000..1409cff8 --- /dev/null +++ b/1129-shortest-path-with-alternating-colors/README.md @@ -0,0 +1,34 @@ +

1129. Shortest Path with Alternating Colors

Medium


You are given an integer n, the number of nodes in a directed graph where the nodes are labeled from 0 to n - 1. Each edge is red or blue in this graph, and there could be self-edges and parallel edges.

+ +

You are given two arrays redEdges and blueEdges where:

+ +
    +
  • redEdges[i] = [ai, bi] indicates that there is a directed red edge from node ai to node bi in the graph, and
  • +
  • blueEdges[j] = [uj, vj] indicates that there is a directed blue edge from node uj to node vj in the graph.
  • +
+ +

Return an array answer of length n, where each answer[x] is the length of the shortest path from node 0 to node x such that the edge colors alternate along the path, or -1 if such a path does not exist.

+ +

 

+

Example 1:

+ +
Input: n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
+Output: [0,1,-1]
+
+ +

Example 2:

+ +
Input: n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
+Output: [0,1,-1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= redEdges.length, blueEdges.length <= 400
  • +
  • redEdges[i].length == blueEdges[j].length == 2
  • +
  • 0 <= ai, bi, uj, vj < n
  • +
+
\ No newline at end of file From cae580fdb30668c6f6613fb176107b307998bcba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:21:31 +0530 Subject: [PATCH 0445/3167] Attach NOTES - LeetHub --- 1129-shortest-path-with-alternating-colors/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1129-shortest-path-with-alternating-colors/NOTES.md diff --git a/1129-shortest-path-with-alternating-colors/NOTES.md b/1129-shortest-path-with-alternating-colors/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1129-shortest-path-with-alternating-colors/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8735db39ff091f9e3d7e8578be5b7aa82549c032 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:21:34 +0530 Subject: [PATCH 0446/3167] Time: 24 ms (58.44%), Space: 16.1 MB (24.17%) - LeetHub --- ...-shortest-path-with-alternating-colors.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1129-shortest-path-with-alternating-colors/1129-shortest-path-with-alternating-colors.cpp diff --git a/1129-shortest-path-with-alternating-colors/1129-shortest-path-with-alternating-colors.cpp b/1129-shortest-path-with-alternating-colors/1129-shortest-path-with-alternating-colors.cpp new file mode 100644 index 00000000..b7086e8f --- /dev/null +++ b/1129-shortest-path-with-alternating-colors/1129-shortest-path-with-alternating-colors.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + vector shortestAlternatingPaths(int n, vector>& redEdges, vector>& blueEdges) { + + vector>> graph(n); + for(auto edge: redEdges) + graph[edge[0]].emplace_back(edge[1],0); + for(auto edge: blueEdges) + graph[edge[0]].emplace_back(edge[1],1); + vector dist(n,-1); + + queue> q; + q.emplace(vector{0,0,-1}); + + while(!q.empty()) { + auto front = q.front(); + q.pop(); + dist[front[0]] = dist[front[0]] != -1 ? dist[front[0]] : front[1]; + + for(auto &adj : graph[front[0]]) { + + if(front[2] != adj.second && adj.first!= -1) { + q.emplace(vector{adj.first, front[1] + 1, adj.second}); + + adj.first = -1; + } + } + } + return dist; + } +}; \ No newline at end of file From ed7d3bdd2c2278b85ec0c18fd1b63ecf73e5fa13 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Feb 2023 21:15:50 +0530 Subject: [PATCH 0447/3167] Attach NOTES - LeetHub --- 2477-minimum-fuel-cost-to-report-to-the-capital/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2477-minimum-fuel-cost-to-report-to-the-capital/NOTES.md diff --git a/2477-minimum-fuel-cost-to-report-to-the-capital/NOTES.md b/2477-minimum-fuel-cost-to-report-to-the-capital/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2477-minimum-fuel-cost-to-report-to-the-capital/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cd95fc0de7882655c084a6d73aae4fd7b1861672 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Feb 2023 21:15:54 +0530 Subject: [PATCH 0448/3167] Time: 721 ms (44.93%), Space: 200.5 MB (52.89%) - LeetHub --- ...mum-fuel-cost-to-report-to-the-capital.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2477-minimum-fuel-cost-to-report-to-the-capital/2477-minimum-fuel-cost-to-report-to-the-capital.cpp diff --git a/2477-minimum-fuel-cost-to-report-to-the-capital/2477-minimum-fuel-cost-to-report-to-the-capital.cpp b/2477-minimum-fuel-cost-to-report-to-the-capital/2477-minimum-fuel-cost-to-report-to-the-capital.cpp new file mode 100644 index 00000000..12c28b39 --- /dev/null +++ b/2477-minimum-fuel-cost-to-report-to-the-capital/2477-minimum-fuel-cost-to-report-to-the-capital.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + long long ans = 0; + + int dfs(int curr, int prev, vector adj[], int seats) + { + int people = 1; + + for(auto itr : adj[curr]) + { + if(itr == prev) + continue; + people += dfs(itr, curr, adj, seats); + } + + if(curr != 0) + ans += (long)(ceil((double)people/seats)); + + return people; + } + + long long minimumFuelCost(vector>& roads, int seats) { + + int n = roads.size(); + vector adj[n+1]; + + for(auto itr : roads) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + } + + dfs(0,-1,adj,seats); + + return ans; + + } +}; \ No newline at end of file From 870ca383c27f6508eba1b190bef8580a7e91f516 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Feb 2023 21:16:57 +0530 Subject: [PATCH 0449/3167] Attach NOTES - LeetHub From e9e3f5b14bbec5f2589898e4e29cd779b8234a88 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Feb 2023 21:17:09 +0530 Subject: [PATCH 0450/3167] Attach NOTES - LeetHub From 66ba65a071076abe5c8d9122d3cc45efc47511f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Feb 2023 18:33:23 +0530 Subject: [PATCH 0451/3167] Attach NOTES - LeetHub --- 1523-count-odd-numbers-in-an-interval-range/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1523-count-odd-numbers-in-an-interval-range/NOTES.md diff --git a/1523-count-odd-numbers-in-an-interval-range/NOTES.md b/1523-count-odd-numbers-in-an-interval-range/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1523-count-odd-numbers-in-an-interval-range/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 444aeb39121413322d16e515532dba117122d3d1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Feb 2023 18:33:26 +0530 Subject: [PATCH 0452/3167] Time: 0 ms (100.00%), Space: 6 MB (7.66%) - LeetHub --- ...count-odd-numbers-in-an-interval-range.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1523-count-odd-numbers-in-an-interval-range/1523-count-odd-numbers-in-an-interval-range.cpp diff --git a/1523-count-odd-numbers-in-an-interval-range/1523-count-odd-numbers-in-an-interval-range.cpp b/1523-count-odd-numbers-in-an-interval-range/1523-count-odd-numbers-in-an-interval-range.cpp new file mode 100644 index 00000000..4bd1e11b --- /dev/null +++ b/1523-count-odd-numbers-in-an-interval-range/1523-count-odd-numbers-in-an-interval-range.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int countOdds(int low, int high) { + + int ans = 0; + bool one = false, two = false; + + if(low & 1) + ++ans, one = true; + if(high & 1) + ++ans, two = true; + ans += (high-low)/2; + + if(one and two) + ans -= 1; + + return ans; + + } +}; \ No newline at end of file From 2be5a0bab00b4ae739fd57739b31acc9e9fee534 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:04:50 +0530 Subject: [PATCH 0453/3167] Create README - LeetHub --- 0067-add-binary/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0067-add-binary/README.md diff --git a/0067-add-binary/README.md b/0067-add-binary/README.md new file mode 100644 index 00000000..ee3f0c76 --- /dev/null +++ b/0067-add-binary/README.md @@ -0,0 +1,19 @@ +

67. Add Binary

Easy


Given two binary strings a and b, return their sum as a binary string.

+ +

 

+

Example 1:

+
Input: a = "11", b = "1"
+Output: "100"
+

Example 2:

+
Input: a = "1010", b = "1011"
+Output: "10101"
+
+

 

+

Constraints:

+ +
    +
  • 1 <= a.length, b.length <= 104
  • +
  • a and b consist only of '0' or '1' characters.
  • +
  • Each string does not contain leading zeros except for the zero itself.
  • +
+
\ No newline at end of file From b57f90125be2c5d2fd8ba7496f2df385b90b498c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:04:53 +0530 Subject: [PATCH 0454/3167] Time: 0 ms (100.00%), Space: 6.3 MB (86.82%) - LeetHub --- 0067-add-binary/0067-add-binary.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0067-add-binary/0067-add-binary.cpp diff --git a/0067-add-binary/0067-add-binary.cpp b/0067-add-binary/0067-add-binary.cpp new file mode 100644 index 00000000..e3aa2944 --- /dev/null +++ b/0067-add-binary/0067-add-binary.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + string addBinary(string a, string b) { + + int i = a.size()-1, j = b.size()-1; + + int carry = 0; + string ans; + while(i >= 0 or j >= 0) + { + int sum = carry; + if(i >= 0) + sum += a[i--] - '0'; + if(j >= 0) + sum += b[j--] - '0'; + + carry = (sum > 1) ? 1 : 0; + ans += to_string(sum%2); + } + + if(carry) + ans += to_string(carry); + reverse(ans.begin(),ans.end()); + + return ans; + } +}; \ No newline at end of file From f91247f8d02f65b04c33096ff367b45b20032791 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:14:21 +0530 Subject: [PATCH 0455/3167] Create README - LeetHub --- 0056-merge-intervals/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0056-merge-intervals/README.md diff --git a/0056-merge-intervals/README.md b/0056-merge-intervals/README.md new file mode 100644 index 00000000..78870292 --- /dev/null +++ b/0056-merge-intervals/README.md @@ -0,0 +1,26 @@ +

56. Merge Intervals

Medium


Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

+ +

 

+

Example 1:

+ +
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
+Output: [[1,6],[8,10],[15,18]]
+Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
+
+ +

Example 2:

+ +
Input: intervals = [[1,4],[4,5]]
+Output: [[1,5]]
+Explanation: Intervals [1,4] and [4,5] are considered overlapping.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= intervals.length <= 104
  • +
  • intervals[i].length == 2
  • +
  • 0 <= starti <= endi <= 104
  • +
+
\ No newline at end of file From 8d42485a0b595dcfc463d696d29f05edf3680b0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:14:25 +0530 Subject: [PATCH 0456/3167] Time: 43 ms (61.73%), Space: 19.9 MB (36.58%) - LeetHub --- 0056-merge-intervals/0056-merge-intervals.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0056-merge-intervals/0056-merge-intervals.cpp diff --git a/0056-merge-intervals/0056-merge-intervals.cpp b/0056-merge-intervals/0056-merge-intervals.cpp new file mode 100644 index 00000000..b4b25962 --- /dev/null +++ b/0056-merge-intervals/0056-merge-intervals.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector> merge(vector>& intervals) { + + vector> ans; + + sort(intervals.begin(),intervals.end()); + + for(auto itr : intervals) + { + if(ans.empty() or ans.back()[1] < itr[0]) + ans.push_back({itr[0],itr[1]}); + else + { + ans.back()[1] = max(ans.back()[1],itr[1]); + } + } + + return ans; + } +}; \ No newline at end of file From 1b7fe4cdb5adc8eed17c5131de710dc8be72954d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:05:00 +0530 Subject: [PATCH 0457/3167] Attach NOTES - LeetHub --- 0049-group-anagrams/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0049-group-anagrams/NOTES.md diff --git a/0049-group-anagrams/NOTES.md b/0049-group-anagrams/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0049-group-anagrams/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0079b02c8e2215ee5f4d0fccb1061d0d5f8dccb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:05:02 +0530 Subject: [PATCH 0458/3167] Time: 28 ms (96.14%), Space: 18.2 MB (96.73%) - LeetHub --- 0049-group-anagrams/0049-group-anagrams.cpp | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0049-group-anagrams/0049-group-anagrams.cpp diff --git a/0049-group-anagrams/0049-group-anagrams.cpp b/0049-group-anagrams/0049-group-anagrams.cpp new file mode 100644 index 00000000..aceab9f2 --- /dev/null +++ b/0049-group-anagrams/0049-group-anagrams.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + vector> groupAnagrams(vector& strs) { + + vector > vp; + int n = strs.size(); + vector > ans; + + for(int i = 0; i < n; ++i) + { + string s = strs[i]; + sort(s.begin(),s.end()); + vp.push_back(make_pair(s,i)); + } + + sort(vp.begin(),vp.end()); + + vector v; + + for(int i = 0; i < vp.size(); ++i) + { + if(v.empty() or vp[i-1].first == vp[i].first) + { + v.push_back(strs[vp[i].second]); + } + else + { + ans.push_back(v); + v.clear(); + v.push_back(strs[vp[i].second]); + } + } + + if(!v.empty()) + ans.push_back(v); + + + return ans; + } +}; \ No newline at end of file From 112bfecd76097533f768708a6201fd88fd9e4b9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:06:28 +0530 Subject: [PATCH 0459/3167] Attach NOTES - LeetHub From e0e0f1b4879d865ce9f078a258314b13411ae4a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:06:31 +0530 Subject: [PATCH 0460/3167] Time: 28 ms (96.14%), Space: 18.2 MB (96.73%) - LeetHub From 394d822626c70c1f392f2efcb724ccf9d7e730b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Feb 2023 19:28:41 +0530 Subject: [PATCH 0461/3167] Create README - LeetHub --- 0989-add-to-array-form-of-integer/README.md | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0989-add-to-array-form-of-integer/README.md diff --git a/0989-add-to-array-form-of-integer/README.md b/0989-add-to-array-form-of-integer/README.md new file mode 100644 index 00000000..348e12a9 --- /dev/null +++ b/0989-add-to-array-form-of-integer/README.md @@ -0,0 +1,40 @@ +

989. Add to Array-Form of Integer

Easy


The array-form of an integer num is an array representing its digits in left to right order.

+ +
    +
  • For example, for num = 1321, the array form is [1,3,2,1].
  • +
+ +

Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.

+ +

 

+

Example 1:

+ +
Input: num = [1,2,0,0], k = 34
+Output: [1,2,3,4]
+Explanation: 1200 + 34 = 1234
+
+ +

Example 2:

+ +
Input: num = [2,7,4], k = 181
+Output: [4,5,5]
+Explanation: 274 + 181 = 455
+
+ +

Example 3:

+ +
Input: num = [2,1,5], k = 806
+Output: [1,0,2,1]
+Explanation: 215 + 806 = 1021
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num.length <= 104
  • +
  • 0 <= num[i] <= 9
  • +
  • num does not contain any leading zeros except for the zero itself.
  • +
  • 1 <= k <= 104
  • +
+
\ No newline at end of file From 3e6bbe13d13648e6369edcbced9575d636e654e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Feb 2023 19:28:41 +0530 Subject: [PATCH 0462/3167] Attach NOTES - LeetHub --- 0989-add-to-array-form-of-integer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0989-add-to-array-form-of-integer/NOTES.md diff --git a/0989-add-to-array-form-of-integer/NOTES.md b/0989-add-to-array-form-of-integer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0989-add-to-array-form-of-integer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8aa81b21717a428de42025292e79cc1589a0b6aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Feb 2023 19:28:44 +0530 Subject: [PATCH 0463/3167] Time: 33 ms (57.94%), Space: 27.4 MB (66.17%) - LeetHub --- .../0989-add-to-array-form-of-integer.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0989-add-to-array-form-of-integer/0989-add-to-array-form-of-integer.cpp diff --git a/0989-add-to-array-form-of-integer/0989-add-to-array-form-of-integer.cpp b/0989-add-to-array-form-of-integer/0989-add-to-array-form-of-integer.cpp new file mode 100644 index 00000000..85978ecf --- /dev/null +++ b/0989-add-to-array-form-of-integer/0989-add-to-array-form-of-integer.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector addToArrayForm(vector& num, int k) { + + int n = num.size(); + + for(int i = n-1; i >= 0; --i) + { + num[i] += k; + k = num[i] / 10; + num[i] = num[i] % 10; + } + + while(k > 0) + { + num.insert(num.begin(),k%10); + k /= 10; + } + + return num; + } +}; \ No newline at end of file From ebbc3ccbb3dabb3f2a74553bd3ee131bacf23346 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 18:51:36 +0530 Subject: [PATCH 0464/3167] Create README - LeetHub --- 0104-maximum-depth-of-binary-tree/README.md | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0104-maximum-depth-of-binary-tree/README.md diff --git a/0104-maximum-depth-of-binary-tree/README.md b/0104-maximum-depth-of-binary-tree/README.md new file mode 100644 index 00000000..60a62249 --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/README.md @@ -0,0 +1,25 @@ +

104. Maximum Depth of Binary Tree

Easy


Given the root of a binary tree, return its maximum depth.

+ +

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: 3
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 104].
  • +
  • -100 <= Node.val <= 100
  • +
+
\ No newline at end of file From 4365e511529e1066385ba7ceffea0dc4668f27b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 18:51:36 +0530 Subject: [PATCH 0465/3167] Attach NOTES - LeetHub --- 0104-maximum-depth-of-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0104-maximum-depth-of-binary-tree/NOTES.md diff --git a/0104-maximum-depth-of-binary-tree/NOTES.md b/0104-maximum-depth-of-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c90d88165d382b1459a31e98a81d16aa7621f0ae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 18:51:39 +0530 Subject: [PATCH 0466/3167] Time: 8 ms (76.88%), Space: 18.8 MB (92.80%) - LeetHub --- .../0104-maximum-depth-of-binary-tree.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp diff --git a/0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp b/0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp new file mode 100644 index 00000000..a5e75f91 --- /dev/null +++ b/0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp @@ -0,0 +1,28 @@ +/** + * 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 height(TreeNode* root) + { + if(!root) + return 0; + int left = 1 + height(root->left); + int right = 1 + height(root->right); + + return max(left,right); + } + + int maxDepth(TreeNode* root) { + return height(root); + } +}; \ No newline at end of file From 05dacc81bd1b954cebc02b37a9c5072cfaac3434 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:16:59 +0530 Subject: [PATCH 0467/3167] Create README - LeetHub --- 0826-most-profit-assigning-work/README.md | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0826-most-profit-assigning-work/README.md diff --git a/0826-most-profit-assigning-work/README.md b/0826-most-profit-assigning-work/README.md new file mode 100644 index 00000000..150af98d --- /dev/null +++ b/0826-most-profit-assigning-work/README.md @@ -0,0 +1,40 @@ +

826. Most Profit Assigning Work

Medium


You have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where:

+ +
    +
  • difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and
  • +
  • worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]).
  • +
+ +

Every worker can be assigned at most one job, but one job can be completed multiple times.

+ +
    +
  • For example, if three workers attempt the same job that pays $1, then the total profit will be $3. If a worker cannot complete any job, their profit is $0.
  • +
+ +

Return the maximum profit we can achieve after assigning the workers to the jobs.

+ +

 

+

Example 1:

+ +
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
+Output: 100
+Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
+
+ +

Example 2:

+ +
Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • n == difficulty.length
  • +
  • n == profit.length
  • +
  • m == worker.length
  • +
  • 1 <= n, m <= 104
  • +
  • 1 <= difficulty[i], profit[i], worker[i] <= 105
  • +
+
\ No newline at end of file From 56c15c710348fe9de6146a22aeebe2f30d03884e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:17:02 +0530 Subject: [PATCH 0468/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub --- .../0826-most-profit-assigning-work.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0826-most-profit-assigning-work/0826-most-profit-assigning-work.cpp diff --git a/0826-most-profit-assigning-work/0826-most-profit-assigning-work.cpp b/0826-most-profit-assigning-work/0826-most-profit-assigning-work.cpp new file mode 100644 index 00000000..700d6e14 --- /dev/null +++ b/0826-most-profit-assigning-work/0826-most-profit-assigning-work.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int maxProfitAssignment(vector& difficulty, vector& profit, vector& worker) { + + int n = profit.size(); + vector> vp; + + for(int i = 0; i < n; ++i) + vp.push_back({difficulty[i],profit[i]}); + + sort(vp.begin(),vp.end()); + long long ans = 0; + + for(auto itr : worker) + { + int maxi = 0; + for(auto x : vp) + { + if(itr >= x.first) + maxi = max(maxi,x.second); + else + break; + } + ans += maxi; + } + + return (int)ans; + + } +}; \ No newline at end of file From 3547fcbbbf3d9c2c3254b84389ebe35bf18ff380 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:21:57 +0530 Subject: [PATCH 0469/3167] Attach NOTES - LeetHub --- 0826-most-profit-assigning-work/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0826-most-profit-assigning-work/NOTES.md diff --git a/0826-most-profit-assigning-work/NOTES.md b/0826-most-profit-assigning-work/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0826-most-profit-assigning-work/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8e1f4ef348c9d89a1e3181dbd9779b3aa88ae966 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:22:00 +0530 Subject: [PATCH 0470/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From fae423e4ff2c8e1e58cb7402573f8dabbac7e2de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:27:52 +0530 Subject: [PATCH 0471/3167] Attach NOTES - LeetHub From 96c3bb1f7f66a8d557eb38eaa46dd7de41b75afe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:27:55 +0530 Subject: [PATCH 0472/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 58ebcf5f2556c8376f52ecb203f161fb16e33d8e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:27:57 +0530 Subject: [PATCH 0473/3167] Attach NOTES - LeetHub From 72c12c6bd600b28300706d37e317cee77f836b54 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:28:10 +0530 Subject: [PATCH 0474/3167] Attach NOTES - LeetHub From 27586147609fba2d146a4eedc30f59a45c405e93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:28:13 +0530 Subject: [PATCH 0475/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From e4e8d2975e3407902f821e6b47b8f41df3c436ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:28:20 +0530 Subject: [PATCH 0476/3167] Attach NOTES - LeetHub From 3de66e80486cbd3ed247025f4f10aff596c18a8e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:28:23 +0530 Subject: [PATCH 0477/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 30f34aeb25c8218bd884f1d6cfd79aaf3e414bfd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:28:45 +0530 Subject: [PATCH 0478/3167] Attach NOTES - LeetHub From 9db8a16b7d6599330840134b40803889f02601df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:28:48 +0530 Subject: [PATCH 0479/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 1080d5637e2ac93103fd7bdbc1d3ad2a4c981b56 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:31:56 +0530 Subject: [PATCH 0480/3167] Attach NOTES - LeetHub From 4732e9d59f0e12629285b1b2ff1008796964ee94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:31:59 +0530 Subject: [PATCH 0481/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 7dc1360c0bcfbd4169e2215e0685ac3f07955584 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:34:13 +0530 Subject: [PATCH 0482/3167] Attach NOTES - LeetHub From 5c4c540d37ce8c8900d573d07b21930344a055db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:34:15 +0530 Subject: [PATCH 0483/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 4f4735292bb8cf7f05e26610ed943147eb3b2b8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:35:55 +0530 Subject: [PATCH 0484/3167] Attach NOTES - LeetHub From 9305ddb6d7c18cff39673d71bf3be316626dd0e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:35:58 +0530 Subject: [PATCH 0485/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 94c6e28b0ff19ae087d5f9d37f40866761cfa5f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:42:34 +0530 Subject: [PATCH 0486/3167] Attach NOTES - LeetHub From 0f124f470a825838965c9cd59f27e4d4f9c2560b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:42:35 +0530 Subject: [PATCH 0487/3167] Attach NOTES - LeetHub From c21974466945d0d39c988e39e6f5e3dbc37af495 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:42:37 +0530 Subject: [PATCH 0488/3167] Time: 1536 ms (7.05%), Space: 37.7 MB (68.52%) - LeetHub From 537d7952891eb32b34dbe619f9427b45ed5f8099 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:42:40 +0530 Subject: [PATCH 0489/3167] Attach NOTES - LeetHub From c9f57c1ca9d7993c40ac6b33a88fcd10513f889d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:42:44 +0530 Subject: [PATCH 0490/3167] Attach NOTES - LeetHub From fb3a8f02e6ed4e344a0fe63c7340ebb962e3d137 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:42:46 +0530 Subject: [PATCH 0491/3167] Attach NOTES - LeetHub From 0b4b572cfcd6c2ca2edbf3dab4157066c9d292ee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 22:52:11 +0530 Subject: [PATCH 0492/3167] Attach NOTES - LeetHub --- 1985-find-the-kth-largest-integer-in-the-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1985-find-the-kth-largest-integer-in-the-array/NOTES.md diff --git a/1985-find-the-kth-largest-integer-in-the-array/NOTES.md b/1985-find-the-kth-largest-integer-in-the-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1985-find-the-kth-largest-integer-in-the-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a7704b2165ce73b7e7165dad20d94e5cd41b71c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 22:52:14 +0530 Subject: [PATCH 0493/3167] Time: 216 ms (94.93%), Space: 55.2 MB (79.25%) - LeetHub --- ...nd-the-kth-largest-integer-in-the-array.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1985-find-the-kth-largest-integer-in-the-array/1985-find-the-kth-largest-integer-in-the-array.cpp diff --git a/1985-find-the-kth-largest-integer-in-the-array/1985-find-the-kth-largest-integer-in-the-array.cpp b/1985-find-the-kth-largest-integer-in-the-array/1985-find-the-kth-largest-integer-in-the-array.cpp new file mode 100644 index 00000000..f551e82a --- /dev/null +++ b/1985-find-the-kth-largest-integer-in-the-array/1985-find-the-kth-largest-integer-in-the-array.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + string kthLargestNumber(vector& nums, int k) { + + int n = nums.size(); + + sort(nums.begin(),nums.end(),[&](const auto &a, const auto &b){ + if(a.size() < b.size()) + return 1; + else if(a.size() == b.size()) + return a < b ? 1 : 0; + return 0; + }); + + return nums[n-k]; + + } +}; From 08a49f07ef14299caece83c5d21c09700f6d8194 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Feb 2023 20:21:00 +0530 Subject: [PATCH 0494/3167] Attach NOTES - LeetHub --- 0205-isomorphic-strings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0205-isomorphic-strings/NOTES.md diff --git a/0205-isomorphic-strings/NOTES.md b/0205-isomorphic-strings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0205-isomorphic-strings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8d1145bd4e79315bc0030c29188316d923d09742 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Feb 2023 20:21:03 +0530 Subject: [PATCH 0495/3167] Time: 8 ms (60.58%), Space: 7.3 MB (9.44%) - LeetHub --- .../0205-isomorphic-strings.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0205-isomorphic-strings/0205-isomorphic-strings.cpp diff --git a/0205-isomorphic-strings/0205-isomorphic-strings.cpp b/0205-isomorphic-strings/0205-isomorphic-strings.cpp new file mode 100644 index 00000000..cea37444 --- /dev/null +++ b/0205-isomorphic-strings/0205-isomorphic-strings.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + + bool helper(string s, string t) + { + map mp; + int n = s.size(); + + for(int i =0 ; i < n; ++i) + { + if(mp.find(s[i]) != mp.end()) + { + if(mp[s[i]] != t[i]) + return false; + } + else + mp[s[i]] = t[i]; + } + + return true; + } + + bool isIsomorphic(string s, string t) { + + return helper(s,t) and helper(t,s); + + } +}; \ No newline at end of file From d03b27475518d7c964701d5c31de958e2a959f43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Feb 2023 21:08:11 +0530 Subject: [PATCH 0496/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0783-minimum-distance-between-bst-nodes/README.md diff --git a/0783-minimum-distance-between-bst-nodes/README.md b/0783-minimum-distance-between-bst-nodes/README.md new file mode 100644 index 00000000..ad1e472e --- /dev/null +++ b/0783-minimum-distance-between-bst-nodes/README.md @@ -0,0 +1,26 @@ +

783. Minimum Distance Between BST Nodes

Easy


Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: root = [1,0,48,null,null,12,49]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [2, 100].
  • +
  • 0 <= Node.val <= 105
  • +
+ +

 

+

Note: This question is the same as 530: https://leetcode.com/problems/minimum-absolute-difference-in-bst/

+
\ No newline at end of file From 6db157e36f98f97df096eb18a2d57da5c42cb094 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Feb 2023 21:08:15 +0530 Subject: [PATCH 0497/3167] Time: 4 ms (56.01%), Space: 9.8 MB (69.86%) - LeetHub --- ...783-minimum-distance-between-bst-nodes.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0783-minimum-distance-between-bst-nodes/0783-minimum-distance-between-bst-nodes.cpp diff --git a/0783-minimum-distance-between-bst-nodes/0783-minimum-distance-between-bst-nodes.cpp b/0783-minimum-distance-between-bst-nodes/0783-minimum-distance-between-bst-nodes.cpp new file mode 100644 index 00000000..aad64551 --- /dev/null +++ b/0783-minimum-distance-between-bst-nodes/0783-minimum-distance-between-bst-nodes.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: + + void helper(TreeNode* root, int& diff, int& pre) + { + if(root) + { + helper(root->left,diff,pre); + if(pre != -1) + { + diff = min(diff, abs(root->val - pre)); + } + + pre = root->val; + + helper(root->right,diff,pre); + } + } + + int minDiffInBST(TreeNode* root) { + + int diff = INT_MAX, pre = -1; + helper(root,diff,pre); + return diff; + + } +}; + From a6cbe1a38f9899db236962335a39c262c59c574f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Feb 2023 21:08:53 +0530 Subject: [PATCH 0498/3167] Attach NOTES - LeetHub --- 0783-minimum-distance-between-bst-nodes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0783-minimum-distance-between-bst-nodes/NOTES.md diff --git a/0783-minimum-distance-between-bst-nodes/NOTES.md b/0783-minimum-distance-between-bst-nodes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0783-minimum-distance-between-bst-nodes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7a3084b1583e563b61c6c7f9aa1429e6b3a54cfc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Feb 2023 21:08:56 +0530 Subject: [PATCH 0499/3167] Time: 4 ms (56.01%), Space: 9.8 MB (69.86%) - LeetHub From 8b463c76ea5c1fa90e7551e7014c34ad79d12cb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 08:56:23 +0530 Subject: [PATCH 0500/3167] Create README - LeetHub --- 0226-invert-binary-tree/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0226-invert-binary-tree/README.md diff --git a/0226-invert-binary-tree/README.md b/0226-invert-binary-tree/README.md new file mode 100644 index 00000000..253f28a2 --- /dev/null +++ b/0226-invert-binary-tree/README.md @@ -0,0 +1,29 @@ +

226. Invert Binary Tree

Easy


Given the root of a binary tree, invert the tree, and return its root.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 100].
  • +
  • -100 <= Node.val <= 100
  • +
+
\ No newline at end of file From 8ab4852bccb4142b2e60ec426424bbd63cae9354 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 08:56:27 +0530 Subject: [PATCH 0501/3167] Time: 3 ms (62.83%), Space: 9.6 MB (85.72%) - LeetHub --- .../0226-invert-binary-tree.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0226-invert-binary-tree/0226-invert-binary-tree.cpp diff --git a/0226-invert-binary-tree/0226-invert-binary-tree.cpp b/0226-invert-binary-tree/0226-invert-binary-tree.cpp new file mode 100644 index 00000000..de9099a8 --- /dev/null +++ b/0226-invert-binary-tree/0226-invert-binary-tree.cpp @@ -0,0 +1,29 @@ +/** + * 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* invertTree(TreeNode* root) { + + if(!root) + return nullptr; + + TreeNode* temp = root->left; + root->left = root->right; + root->right = temp; + + invertTree(root->left); + invertTree(root->right); + + return root; + } +}; \ No newline at end of file From e7cad35c889f01da449d4a7ac7bfa0899d76df5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 09:20:01 +0530 Subject: [PATCH 0502/3167] Attach NOTES - LeetHub --- 0226-invert-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0226-invert-binary-tree/NOTES.md diff --git a/0226-invert-binary-tree/NOTES.md b/0226-invert-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0226-invert-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0b84a9cee926cb17e56f58d863f7b460eef10e2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 09:20:04 +0530 Subject: [PATCH 0503/3167] Time: 3 ms (62.83%), Space: 9.6 MB (85.72%) - LeetHub From 098cf95b272f19fa52db2f3e2458b8927a514494 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 11:41:27 +0530 Subject: [PATCH 0504/3167] Create README - LeetHub --- 0542-01-matrix/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0542-01-matrix/README.md diff --git a/0542-01-matrix/README.md b/0542-01-matrix/README.md new file mode 100644 index 00000000..931bf9a2 --- /dev/null +++ b/0542-01-matrix/README.md @@ -0,0 +1,29 @@ +

542. 01 Matrix

Medium


Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.

+ +

The distance between two adjacent cells is 1.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
+Output: [[0,0,0],[0,1,0],[1,2,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 1 <= m, n <= 104
  • +
  • 1 <= m * n <= 104
  • +
  • mat[i][j] is either 0 or 1.
  • +
  • There is at least one 0 in mat.
  • +
+
\ No newline at end of file From 0f5562f0b9959eb06e9b906f65085e1ba4861bd6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 11:41:27 +0530 Subject: [PATCH 0505/3167] Attach NOTES - LeetHub --- 0542-01-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0542-01-matrix/NOTES.md diff --git a/0542-01-matrix/NOTES.md b/0542-01-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0542-01-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9773b7152dd64f1962e39652b7be70508a3c8c92 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Feb 2023 11:41:31 +0530 Subject: [PATCH 0506/3167] Time: 218 ms (9.72%), Space: 53.8 MB (7.84%) - LeetHub --- 0542-01-matrix/0542-01-matrix.cpp | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 0542-01-matrix/0542-01-matrix.cpp diff --git a/0542-01-matrix/0542-01-matrix.cpp b/0542-01-matrix/0542-01-matrix.cpp new file mode 100644 index 00000000..a5b241f6 --- /dev/null +++ b/0542-01-matrix/0542-01-matrix.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + vector> updateMatrix(vector>& mat) { + + int n = mat.size(); + int m = mat[0].size(); + + vector> visited(n,vector(m,false)); + vector> dist(n,vector(m,0)); + + queue, int> > q; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(mat[i][j] == 0) + { + q.push({{i,j}, 0}); + visited[i][j] = true; + } + } + } + + while(!q.empty()) + { + int currRow = q.front().first.first; + int currCol = q.front().first.second; + int steps = q.front().second; + + q.pop(); + + dist[currRow][currCol] = steps; + + vector rows = {-1, 0, 0, +1}; + vector cols = {0, -1, +1, 0}; + + for(int i = 0; i < 4; ++i) + { + int newRow = currRow + rows[i]; + int newCol = currCol + cols[i]; + + if(newRow >= 0 and newRow < n and newCol >= 0 and newCol < m and !visited[newRow][newCol]) + { + q.push({{newRow,newCol}, steps + 1}); + visited[newRow][newCol] = true; + } + } + } + + return dist; + } +}; \ No newline at end of file From 7e036b600cb695c4d40b92148a4551ba256e9690 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Feb 2023 07:57:50 +0530 Subject: [PATCH 0507/3167] Create README - LeetHub --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0103-binary-tree-zigzag-level-order-traversal/README.md diff --git a/0103-binary-tree-zigzag-level-order-traversal/README.md b/0103-binary-tree-zigzag-level-order-traversal/README.md new file mode 100644 index 00000000..001e9e36 --- /dev/null +++ b/0103-binary-tree-zigzag-level-order-traversal/README.md @@ -0,0 +1,29 @@ +

103. Binary Tree Zigzag Level Order Traversal

Medium


Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: [[3],[20,9],[15,7]]
+
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 2000].
  • +
  • -100 <= Node.val <= 100
  • +
+
\ No newline at end of file From f3a565b98b5e3bc38518ddf3e177a1d69c652c86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Feb 2023 07:57:51 +0530 Subject: [PATCH 0508/3167] Time: 3 ms (73.66%), Space: 12.2 MB (17.21%) - LeetHub --- ...nary-tree-zigzag-level-order-traversal.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0103-binary-tree-zigzag-level-order-traversal/0103-binary-tree-zigzag-level-order-traversal.cpp diff --git a/0103-binary-tree-zigzag-level-order-traversal/0103-binary-tree-zigzag-level-order-traversal.cpp b/0103-binary-tree-zigzag-level-order-traversal/0103-binary-tree-zigzag-level-order-traversal.cpp new file mode 100644 index 00000000..8a1f20bd --- /dev/null +++ b/0103-binary-tree-zigzag-level-order-traversal/0103-binary-tree-zigzag-level-order-traversal.cpp @@ -0,0 +1,55 @@ +/** + * 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> zigzagLevelOrder(TreeNode* root) { + + vector> ans; + + if(!root) + return ans; + + queue q; + q.push(root); + + bool flag = 0; + while(!q.empty()) + { + int size = q.size(); + + vector v; + for(int i = 0; ileft) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + + v.push_back(curr->val); + } + + + if(flag & 1) + reverse(v.begin(),v.end()); + + ans.push_back(v); + + flag ^= 1; + } + + return ans; + + } +}; \ No newline at end of file From 77a34e9e2586c4d757a5eabb51c2c6749f982642 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:34:28 +0530 Subject: [PATCH 0509/3167] Create README - LeetHub --- 0743-network-delay-time/README.md | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0743-network-delay-time/README.md diff --git a/0743-network-delay-time/README.md b/0743-network-delay-time/README.md new file mode 100644 index 00000000..b8bd8fc8 --- /dev/null +++ b/0743-network-delay-time/README.md @@ -0,0 +1,36 @@ +

743. Network Delay Time

Medium


You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from source to target.

+ +

We will send a signal from a given node k. Return the minimum time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1.

+ +

 

+

Example 1:

+ +
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
+Output: 2
+
+ +

Example 2:

+ +
Input: times = [[1,2,1]], n = 2, k = 1
+Output: 1
+
+ +

Example 3:

+ +
Input: times = [[1,2,1]], n = 2, k = 2
+Output: -1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= n <= 100
  • +
  • 1 <= times.length <= 6000
  • +
  • times[i].length == 3
  • +
  • 1 <= ui, vi <= n
  • +
  • ui != vi
  • +
  • 0 <= wi <= 100
  • +
  • All the pairs (ui, vi) are unique. (i.e., no multiple edges.)
  • +
+
\ No newline at end of file From 976c7376bba94e712a0d46f81a60794fcfb7061f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:34:31 +0530 Subject: [PATCH 0510/3167] Time: 147 ms (48.17%), Space: 43.6 MB (32.04%) - LeetHub --- .../0743-network-delay-time.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 0743-network-delay-time/0743-network-delay-time.cpp diff --git a/0743-network-delay-time/0743-network-delay-time.cpp b/0743-network-delay-time/0743-network-delay-time.cpp new file mode 100644 index 00000000..0a8f688c --- /dev/null +++ b/0743-network-delay-time/0743-network-delay-time.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + + void dijkstra(vector > adj[], int& ans, int n, int k ) + { + vector visited(n+1,false); + vector dist(n+1,1e9); + + priority_queue , vector > , greater > > pq; + + pq.push({0,k}); + dist[k] = 0; + + while(!pq.empty()) + { + int curr = pq.top().second; + pq.pop(); + + if(visited[curr]) continue; + + visited[curr] = true; + + for(auto child : adj[curr]) + { + int child_v = child.first; + int wt = child.second; + + if(dist[curr] + wt < dist[child_v]) + { + dist[child_v] = dist[curr] + wt; + pq.push({dist[child_v], child_v}); + } + } + } + + ans = *max_element(dist.begin()+1,dist.end()); + + } + + + int networkDelayTime(vector>& times, int n, int k) { + + vector> adj[n+1]; + + for(auto itr : times) + adj[itr[0]].push_back({itr[1],itr[2]}); + + int ans = 1e9; + dijkstra(adj,ans, n, k); + + return ans == 1e9 ? -1 : ans; + } +}; \ No newline at end of file From cef28bfecd5de7b3ec5fac4a423b1bb49afa4ffb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Feb 2023 18:36:13 +0530 Subject: [PATCH 0511/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1011-capacity-to-ship-packages-within-d-days/README.md diff --git a/1011-capacity-to-ship-packages-within-d-days/README.md b/1011-capacity-to-ship-packages-within-d-days/README.md new file mode 100644 index 00000000..fc96f7e9 --- /dev/null +++ b/1011-capacity-to-ship-packages-within-d-days/README.md @@ -0,0 +1,49 @@ +

1011. Capacity To Ship Packages Within D Days

Medium


A conveyor belt has packages that must be shipped from one port to another within days days.

+ +

The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

+ +

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.

+ +

 

+

Example 1:

+ +
Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5
+Output: 15
+Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
+1st day: 1, 2, 3, 4, 5
+2nd day: 6, 7
+3rd day: 8
+4th day: 9
+5th day: 10
+
+Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
+
+ +

Example 2:

+ +
Input: weights = [3,2,2,4,1,4], days = 3
+Output: 6
+Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
+1st day: 3, 2
+2nd day: 2, 4
+3rd day: 1, 4
+
+ +

Example 3:

+ +
Input: weights = [1,2,3,1,1], days = 4
+Output: 3
+Explanation:
+1st day: 1
+2nd day: 2
+3rd day: 3
+4th day: 1, 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= days <= weights.length <= 5 * 104
  • +
  • 1 <= weights[i] <= 500
  • +
\ No newline at end of file From eb623149c6c599ed67f6bb4826572cdb4190bd83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Feb 2023 18:36:13 +0530 Subject: [PATCH 0512/3167] Attach NOTES - LeetHub --- 1011-capacity-to-ship-packages-within-d-days/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1011-capacity-to-ship-packages-within-d-days/NOTES.md diff --git a/1011-capacity-to-ship-packages-within-d-days/NOTES.md b/1011-capacity-to-ship-packages-within-d-days/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1011-capacity-to-ship-packages-within-d-days/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 124188c7012eba2db4fb8feb3b714dd5df72ed8c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Feb 2023 18:36:16 +0530 Subject: [PATCH 0513/3167] Time: 51 ms (83.18%), Space: 25.9 MB (96.08%) - LeetHub --- ...apacity-to-ship-packages-within-d-days.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp diff --git a/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp b/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp new file mode 100644 index 00000000..4c30f638 --- /dev/null +++ b/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + + + int binarySearch(vector& weights, int days, int mid) + { + int currDays = 1; + int sum = 0; + for(auto &itr : weights) + { + if(sum + itr > mid) + { + sum = itr; + ++currDays; + } + else + { + sum += itr; + } + } + + return (currDays <= days); + } + + + int shipWithinDays(vector& weights, int days) { + + int high = accumulate(weights.begin(),weights.end(),0); + int low = *max_element(weights.begin(),weights.end()); + + int ans = low; + + while(low <= high) + { + int mid = low + (high - low)/2; + + if(binarySearch(weights,days, mid)) + { + ans = mid; + high = mid-1; + } + else + low = mid + 1; + } + + return ans; + + + } +}; \ No newline at end of file From f681cdd4b835ee569dfa8801964b9fc2dc83969a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Feb 2023 19:26:40 +0530 Subject: [PATCH 0514/3167] Attach NOTES - LeetHub --- 2567-minimum-score-by-changing-two-elements/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2567-minimum-score-by-changing-two-elements/NOTES.md diff --git a/2567-minimum-score-by-changing-two-elements/NOTES.md b/2567-minimum-score-by-changing-two-elements/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2567-minimum-score-by-changing-two-elements/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cdf9af2e677c4e80369f44cd0b00c72137bcd6b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Feb 2023 19:26:43 +0530 Subject: [PATCH 0515/3167] Time: 107 ms (14.25%), Space: 34.2 MB (60.26%) - LeetHub --- ...minimum-score-by-changing-two-elements.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2567-minimum-score-by-changing-two-elements/2567-minimum-score-by-changing-two-elements.cpp diff --git a/2567-minimum-score-by-changing-two-elements/2567-minimum-score-by-changing-two-elements.cpp b/2567-minimum-score-by-changing-two-elements/2567-minimum-score-by-changing-two-elements.cpp new file mode 100644 index 00000000..feb15c9c --- /dev/null +++ b/2567-minimum-score-by-changing-two-elements/2567-minimum-score-by-changing-two-elements.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minimizeSum(vector& nums) { + + int n = nums.size(); + sort(nums.begin(),nums.end()); + + // both last and secondLast same to 3rd last which is now max + int a = nums[n-3] - nums[0]; + + // both first and second to 3rd which is now min + int b = nums[n-1] - nums[2]; + + // last equals second last and first equals second , now min is second and max is secondLast + int c = nums[n-2] - nums[1]; + + return min({a,b,c}); + + } +}; \ No newline at end of file From 33bdf1af6b46905bd3750a0511a6bd023e16cfe6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Feb 2023 18:46:26 +0530 Subject: [PATCH 0516/3167] Attach NOTES - LeetHub --- 0713-subarray-product-less-than-k/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0713-subarray-product-less-than-k/NOTES.md diff --git a/0713-subarray-product-less-than-k/NOTES.md b/0713-subarray-product-less-than-k/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0713-subarray-product-less-than-k/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e7e70d31243a75663afc91a4887534510767a6d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Feb 2023 18:46:28 +0530 Subject: [PATCH 0517/3167] Time: 83 ms (86.07%), Space: 61.4 MB (31.41%) - LeetHub --- .../0713-subarray-product-less-than-k.cpp | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp b/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp index e904f316..fd352156 100644 --- a/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp +++ b/0713-subarray-product-less-than-k/0713-subarray-product-less-than-k.cpp @@ -1,23 +1,32 @@ -#define ll long long int class Solution { public: int numSubarrayProductLessThanK(vector& nums, int k) { + + int n = nums.size(); - ll product = 1, count = 0; - int n = nums.size(), i = 0; + int i = 0, j = 0; - if(k == 0 or k == 1) return 0; + long long prod = 1, cnt = 0; - for(int j = 0; j < n; ++j) + if(k == 0 or k == 1) + return 0; + + while(j < n) { - product *= nums[j]; + prod *= nums[j]; + + while(prod >= k) + { + prod /= nums[i++]; + } - while(product >= k) - product /= nums[i++]; + if(prod < k) + cnt += (j - i + 1); - count += (j-i+1); + ++j; } - return count; + return cnt; + } }; \ No newline at end of file From 86b76c090e8a35695c89ca4365b8874cd542d83d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Feb 2023 23:34:55 +0530 Subject: [PATCH 0518/3167] Create README - LeetHub --- 0502-ipo/README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0502-ipo/README.md diff --git a/0502-ipo/README.md b/0502-ipo/README.md new file mode 100644 index 00000000..80db493d --- /dev/null +++ b/0502-ipo/README.md @@ -0,0 +1,41 @@ +

502. IPO

Hard


Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.

+ +

You are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.

+ +

Initially, you have w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.

+ +

Pick a list of at most k distinct projects from given projects to maximize your final capital, and return the final maximized capital.

+ +

The answer is guaranteed to fit in a 32-bit signed integer.

+ +

 

+

Example 1:

+ +
Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
+Output: 4
+Explanation: Since your initial capital is 0, you can only start the project indexed 0.
+After finishing it you will obtain profit 1 and your capital becomes 1.
+With capital 1, you can either start the project indexed 1 or the project indexed 2.
+Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
+Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
+
+ +

Example 2:

+ +
Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
+Output: 6
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= 105
  • +
  • 0 <= w <= 109
  • +
  • n == profits.length
  • +
  • n == capital.length
  • +
  • 1 <= n <= 105
  • +
  • 0 <= profits[i] <= 104
  • +
  • 0 <= capital[i] <= 109
  • +
+
\ No newline at end of file From ac6c3b7a58629f087e68f309c84c53c65ed3180a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Feb 2023 23:34:59 +0530 Subject: [PATCH 0519/3167] Time: 249 ms (48.35%), Space: 82.1 MB (23.74%) - LeetHub --- 0502-ipo/0502-ipo.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0502-ipo/0502-ipo.cpp diff --git a/0502-ipo/0502-ipo.cpp b/0502-ipo/0502-ipo.cpp new file mode 100644 index 00000000..23ee8b76 --- /dev/null +++ b/0502-ipo/0502-ipo.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int findMaximizedCapital(int k, int w, vector& profits, vector& capital) { + + int n = capital.size(); + vector > vp; + for(int i = 0; i < n; ++i) + vp.push_back({capital[i],profits[i]}); + + sort(vp.begin(),vp.end()); + + priority_queue pq; + + int j = 0; + + for(int i = 0; i < k; ++i) + { + while(j < n and vp[j].first <= w) + pq.push(vp[j++].second); + + if(pq.empty()) + break; + + w += pq.top(); + pq.pop(); + } + + return w; + } +}; + + \ No newline at end of file From 6afcc5d359299b620f1281cb31fea0911bc5093f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:03:54 +0530 Subject: [PATCH 0520/3167] Create README - LeetHub --- 1675-minimize-deviation-in-array/README.md | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 1675-minimize-deviation-in-array/README.md diff --git a/1675-minimize-deviation-in-array/README.md b/1675-minimize-deviation-in-array/README.md new file mode 100644 index 00000000..5d41d8fe --- /dev/null +++ b/1675-minimize-deviation-in-array/README.md @@ -0,0 +1,52 @@ +

1675. Minimize Deviation in Array

Hard


You are given an array nums of n positive integers.

+ +

You can perform two types of operations on any element of the array any number of times:

+ +
    +
  • If the element is even, divide it by 2. + +
      +
    • For example, if the array is [1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2].
    • +
    +
  • +
  • If the element is odd, multiply it by 2. +
      +
    • For example, if the array is [1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4].
    • +
    +
  • +
+ +

The deviation of the array is the maximum difference between any two elements in the array.

+ +

Return the minimum deviation the array can have after performing some number of operations.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: 1
+Explanation: You can transform the array to [1,2,3,2], then to [2,2,3,2], then the deviation will be 3 - 2 = 1.
+
+ +

Example 2:

+ +
Input: nums = [4,1,5,20,3]
+Output: 3
+Explanation: You can transform the array after two operations to [4,2,5,5,3], then the deviation will be 5 - 2 = 3.
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 2 <= n <= 5 * 104
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From aa93f3cd07a6e19f7c73401c1bd904f5f3c97364 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:03:58 +0530 Subject: [PATCH 0521/3167] Time: 333 ms (88.98%), Space: 89.1 MB (37.29%) - LeetHub --- .../1675-minimize-deviation-in-array.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1675-minimize-deviation-in-array/1675-minimize-deviation-in-array.cpp diff --git a/1675-minimize-deviation-in-array/1675-minimize-deviation-in-array.cpp b/1675-minimize-deviation-in-array/1675-minimize-deviation-in-array.cpp new file mode 100644 index 00000000..27dc6415 --- /dev/null +++ b/1675-minimize-deviation-in-array/1675-minimize-deviation-in-array.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int minimumDeviation(vector& nums) { + + set s; + + for(auto itr : nums) + { + if(itr & 1) + s.insert(itr*2); + else + s.insert(itr); + } + + int diff = *s.rbegin() - *s.begin(); + + while(*s.rbegin() % 2 == 0) + { + int top = *s.rbegin(); + s.erase(top); + + s.insert(top/2); + + diff = min(diff, *s.rbegin() - *s.begin()); + } + + + return diff; + + } +}; \ No newline at end of file From 7a122edb977d1f86acf3d161363874d5861cf9c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:05:17 +0530 Subject: [PATCH 0522/3167] Attach NOTES - LeetHub --- 1675-minimize-deviation-in-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1675-minimize-deviation-in-array/NOTES.md diff --git a/1675-minimize-deviation-in-array/NOTES.md b/1675-minimize-deviation-in-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1675-minimize-deviation-in-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8343ba8ca49b750f7e6274f70df12315a695b8b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:05:20 +0530 Subject: [PATCH 0523/3167] Time: 333 ms (88.98%), Space: 89.1 MB (37.29%) - LeetHub From 20b2c7e560324441fc8c8f2678e68022dcfe1668 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 10:37:01 +0530 Subject: [PATCH 0524/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0121-best-time-to-buy-and-sell-stock/README.md diff --git a/0121-best-time-to-buy-and-sell-stock/README.md b/0121-best-time-to-buy-and-sell-stock/README.md new file mode 100644 index 00000000..9453c7e0 --- /dev/null +++ b/0121-best-time-to-buy-and-sell-stock/README.md @@ -0,0 +1,30 @@ +

121. Best Time to Buy and Sell Stock

Easy


You are given an array prices where prices[i] is the price of a given stock on the ith day.

+ +

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

+ +

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

+ +

 

+

Example 1:

+ +
Input: prices = [7,1,5,3,6,4]
+Output: 5
+Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
+Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
+
+ +

Example 2:

+ +
Input: prices = [7,6,4,3,1]
+Output: 0
+Explanation: In this case, no transactions are done and the max profit = 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= prices.length <= 105
  • +
  • 0 <= prices[i] <= 104
  • +
+
\ No newline at end of file From 7e56b8bd8ce741876e36402ea89c98dcbdd81405 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 10:37:05 +0530 Subject: [PATCH 0525/3167] Time: 143 ms (56.87%), Space: 93.4 MB (56.82%) - LeetHub --- .../0121-best-time-to-buy-and-sell-stock.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp diff --git a/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp b/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp new file mode 100644 index 00000000..412cf328 --- /dev/null +++ b/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxProfit(vector& prices) { + + int n = prices.size(); + int mini = prices[0]; + int ans = 0; + + for(int i = 0; i < n; ++i) + { + mini = min(mini, prices[i]); + ans = max(ans, prices[i] - mini); + } + + return ans; + } +}; \ No newline at end of file From 85249336a2e1aa679ed1648f0b2cf1f594116772 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 12:48:12 +0530 Subject: [PATCH 0526/3167] Create README - LeetHub --- 0692-top-k-frequent-words/README.md | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0692-top-k-frequent-words/README.md diff --git a/0692-top-k-frequent-words/README.md b/0692-top-k-frequent-words/README.md new file mode 100644 index 00000000..b5f320de --- /dev/null +++ b/0692-top-k-frequent-words/README.md @@ -0,0 +1,33 @@ +

692. Top K Frequent Words

Medium


Given an array of strings words and an integer k, return the k most frequent strings.

+ +

Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.

+ +

 

+

Example 1:

+ +
Input: words = ["i","love","leetcode","i","love","coding"], k = 2
+Output: ["i","love"]
+Explanation: "i" and "love" are the two most frequent words.
+Note that "i" comes before "love" due to a lower alphabetical order.
+
+ +

Example 2:

+ +
Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
+Output: ["the","is","sunny","day"]
+Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 500
  • +
  • 1 <= words[i].length <= 10
  • +
  • words[i] consists of lowercase English letters.
  • +
  • k is in the range [1, The number of unique words[i]]
  • +
+ +

 

+

Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?

+
\ No newline at end of file From a93e6d5811002fee179ce38deb3d107019454264 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 12:48:16 +0530 Subject: [PATCH 0527/3167] Time: 10 ms (88.09%), Space: 12.7 MB (44.87%) - LeetHub --- .../0692-top-k-frequent-words.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0692-top-k-frequent-words/0692-top-k-frequent-words.cpp diff --git a/0692-top-k-frequent-words/0692-top-k-frequent-words.cpp b/0692-top-k-frequent-words/0692-top-k-frequent-words.cpp new file mode 100644 index 00000000..b96d979b --- /dev/null +++ b/0692-top-k-frequent-words/0692-top-k-frequent-words.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector topKFrequent(vector& words, int k) { + + map mp; + vector ans; + for(auto itr : words) + ++mp[itr]; + + vector> vp; + for(auto itr : mp) + { + vp.push_back({itr.second,itr.first}); + } + + sort(vp.begin(),vp.end(),[&](const auto &a , auto& b){ + if(a.first == b.first) + return a.second < b.second; + return a.first > b.first; + }); + + for(int i = 0; i Date: Sat, 25 Feb 2023 13:40:22 +0530 Subject: [PATCH 0528/3167] Attach NOTES - LeetHub From 867df1319614c2cc590581e4af5df53a09ef034c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 13:40:25 +0530 Subject: [PATCH 0529/3167] Time: 73 ms (39.23%), Space: 26.2 MB (32.10%) - LeetHub --- ...apacity-to-ship-packages-within-d-days.cpp | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp b/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp index 4c30f638..8f6d933e 100644 --- a/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp +++ b/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp @@ -1,50 +1,45 @@ class Solution { public: - int binarySearch(vector& weights, int days, int mid) { - int currDays = 1; + int cnt = 1; + int n = weights.size(); int sum = 0; - for(auto &itr : weights) + for(int i = 0; i mid) + if(sum + weights[i] > mid) { - sum = itr; - ++currDays; + ++cnt; + sum = weights[i]; } else - { - sum += itr; - } + sum += weights[i]; } - return (currDays <= days); + return (cnt <= days); } int shipWithinDays(vector& weights, int days) { - int high = accumulate(weights.begin(),weights.end(),0); - int low = *max_element(weights.begin(),weights.end()); + int right = accumulate(weights.begin(),weights.end(),0); + int left = *max_element(weights.begin(),weights.end()); - int ans = low; - - while(low <= high) + int ans = left; + while(left <= right) { - int mid = low + (high - low)/2; - - if(binarySearch(weights,days, mid)) + int mid = (right + left)/2; + if(binarySearch(weights,days, mid)) { - ans = mid; - high = mid-1; + ans = mid; + right = mid - 1; } else - low = mid + 1; + left = mid + 1; } return ans; - } }; \ No newline at end of file From 463c8cfa20215965db68e76d8bc9c5a262f92a1d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:45:50 +0530 Subject: [PATCH 0530/3167] Create README - LeetHub --- 2512-reward-top-k-students/README.md | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2512-reward-top-k-students/README.md diff --git a/2512-reward-top-k-students/README.md b/2512-reward-top-k-students/README.md new file mode 100644 index 00000000..8e8bb1b8 --- /dev/null +++ b/2512-reward-top-k-students/README.md @@ -0,0 +1,45 @@ +

2512. Reward Top K Students

Medium


You are given two string arrays positive_feedback and negative_feedback, containing the words denoting positive and negative feedback, respectively. Note that no word is both positive and negative.

+ +

Initially every student has 0 points. Each positive word in a feedback report increases the points of a student by 3, whereas each negative word decreases the points by 1.

+ +

You are given n feedback reports, represented by a 0-indexed string array report and a 0-indexed integer array student_id, where student_id[i] represents the ID of the student who has received the feedback report report[i]. The ID of each student is unique.

+ +

Given an integer k, return the top k students after ranking them in non-increasing order by their points. In case more than one student has the same points, the one with the lower ID ranks higher.

+ +

 

+

Example 1:

+ +
Input: positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2
+Output: [1,2]
+Explanation: 
+Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher.
+
+ +

Example 2:

+ +
Input: positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2
+Output: [2,1]
+Explanation: 
+- The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points. 
+- The student with ID 2 has 1 positive feedback, so he has 3 points. 
+Since student 2 has more points, [2,1] is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= positive_feedback.length, negative_feedback.length <= 104
  • +
  • 1 <= positive_feedback[i].length, negative_feedback[j].length <= 100
  • +
  • Both positive_feedback[i] and negative_feedback[j] consists of lowercase English letters.
  • +
  • No word is present in both positive_feedback and negative_feedback.
  • +
  • n == report.length == student_id.length
  • +
  • 1 <= n <= 104
  • +
  • report[i] consists of lowercase English letters and spaces ' '.
  • +
  • There is a single space between consecutive words of report[i].
  • +
  • 1 <= report[i].length <= 100
  • +
  • 1 <= student_id[i] <= 109
  • +
  • All the values of student_id[i] are unique.
  • +
  • 1 <= k <= n
  • +
+
\ No newline at end of file From a8b6cd9f5e4bd1ad281e8dc814c2d2af09e664e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:45:50 +0530 Subject: [PATCH 0531/3167] Attach NOTES - LeetHub --- 2512-reward-top-k-students/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2512-reward-top-k-students/NOTES.md diff --git a/2512-reward-top-k-students/NOTES.md b/2512-reward-top-k-students/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2512-reward-top-k-students/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 08d969bd95c93515852e29eb68adc810ed5202d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:45:54 +0530 Subject: [PATCH 0532/3167] Time: 890 ms (6.14%), Space: 154 MB (5.06%) - LeetHub --- .../2512-reward-top-k-students.cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2512-reward-top-k-students/2512-reward-top-k-students.cpp diff --git a/2512-reward-top-k-students/2512-reward-top-k-students.cpp b/2512-reward-top-k-students/2512-reward-top-k-students.cpp new file mode 100644 index 00000000..5bd540fb --- /dev/null +++ b/2512-reward-top-k-students/2512-reward-top-k-students.cpp @@ -0,0 +1,62 @@ +class Solution { +public: + vector topStudents(vector& positive_feedback, vector& negative_feedback, vector& report, vector& student_id, int k) { + + map pos, neg; + int n = student_id.size(); + + for(auto itr : positive_feedback) + ++pos[itr]; + for(auto itr : negative_feedback) + ++neg[itr]; + + vector topK; + vector> vp; + + for(int i = 0; i current; + int score = 0; + int j = 0; + while(str[j] != '\0') + { + if(str[j] == ' ') + { + current.push_back(curr); + curr.clear(); + ++j; + } + else + { + curr += str[j]; + ++j; + } + } + if(!curr.empty()) + current.push_back(curr); + + for(auto itr : current) + { + if(pos[itr]) + score += 3; + if(neg[itr]) + score -= 1; + } + + vp.push_back({score,student_id[i]}); + } + + sort(vp.begin(),vp.end(),[&](const auto &a, const auto &b){ + if(a.first == b.first) + return a.second < b.second; + return a.first > b.first; + }); + + for(int i =0 ; i Date: Sun, 26 Feb 2023 07:40:27 +0530 Subject: [PATCH 0533/3167] Attach NOTES - LeetHub --- 1192-critical-connections-in-a-network/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1192-critical-connections-in-a-network/NOTES.md diff --git a/1192-critical-connections-in-a-network/NOTES.md b/1192-critical-connections-in-a-network/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1192-critical-connections-in-a-network/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1100fb2c355380041639a83992dc1b442e484710 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Feb 2023 07:40:30 +0530 Subject: [PATCH 0534/3167] Time: 673 ms (53.38%), Space: 174.9 MB (45.52%) - LeetHub --- ...1192-critical-connections-in-a-network.cpp | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp b/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp index ca191c63..f438b177 100644 --- a/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp +++ b/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp @@ -1,52 +1,56 @@ class Solution { -public: - private: - int timer = 0; - - void dfs(int sv, int parent, vector& visited, vector& dis, vector& low, int timer, vector adj[],vector>& vp){ - visited[sv] = true; - dis[sv] = low[sv] = timer++; +private: + int timer = 0; +public: + void dfs(int sv, int par, vector adj[], vector& visited, vector& disc, vector& low, vector>& bridges) + { + low[sv] = disc[sv] = ++timer; + visited[sv] = 1; - for(auto itr : adj[sv]) + for(auto child : adj[sv]) { - if(itr == parent) + if(child == par) continue; - if(!visited[itr]) + if(!visited[child]) { - dfs(itr,sv,visited,dis,low,timer,adj,vp); - low[sv] = min(low[sv],low[itr]); + dfs(child, sv, adj, visited, disc, low, bridges); + low[sv] = min(low[sv],low[child]); - if(low[itr] > dis[sv]) + if(low[child] > disc[sv]) { - vp.push_back({itr,sv}); + bridges.push_back({sv, child}); } } - else{ - low[sv] = min(low[sv],low[itr]); + else + { + low[sv] = min(low[sv],low[child]); } } } - public: - vector> criticalConnections(int n, vector>& connections) - { - vector adj[n]; + + vector> criticalConnections(int n, vector>& connections) { + + // no of bridges in a graph + + vector adj[n+1]; + vector visited(n,false); + vector disc(n,-1), low(n,-1); + vector > bridges; + for(auto itr : connections) { adj[itr[0]].push_back(itr[1]); adj[itr[1]].push_back(itr[0]); } - vector visited(n,false); - vector dis(n,-1), low(n,-1); - vector> vp; - int timer = 0; - for(int i = 0; i Date: Sun, 26 Feb 2023 13:38:47 +0530 Subject: [PATCH 0535/3167] Attach NOTES - LeetHub From 3442950bfa3b4381a9dfef3ba31068710c886122 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Feb 2023 13:38:50 +0530 Subject: [PATCH 0536/3167] Time: 18 ms (48.15%), Space: 9.4 MB (19.83%) - LeetHub --- 0072-edit-distance/0072-edit-distance.cpp | 44 +++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/0072-edit-distance/0072-edit-distance.cpp b/0072-edit-distance/0072-edit-distance.cpp index fa77a66d..acbdf7db 100644 --- a/0072-edit-distance/0072-edit-distance.cpp +++ b/0072-edit-distance/0072-edit-distance.cpp @@ -1,27 +1,35 @@ +#define ll long long class Solution { public: - - int minDistance(string word1, string word2) { + long long helper(int i, int j,int n, int m, string& word1, string& word2, vector>& dp ) + { + if(j >= m) + return n - i; + if(i >= n) + return m - j; + if(i >= n or j >= m) + return INT_MAX; - int n = word1.size(), m = word2.size(); - vector prev(m+1,0), curr(m+1,0); + if(dp[i][j] != -1) + return dp[i][j]; - for(int j = 0; j<=m; ++j) - prev[j] = j; - - for(int i = 1; i<=n; ++i) + if(word1[i] == word2[j]) + return dp[i][j] = 0 + helper(i+1, j+1, n, m, word1, word2, dp); + else { - curr[0] = i; - for(int j = 1; j<=m; ++j) - { - if(word1[i-1] == word2[j-1]) - curr[j] = prev[j-1]; - else - curr[j] = 1 + min({prev[j-1], prev[j], curr[j-1]}); - } - prev = curr; + ll ins = 1 + helper(i,j+1,n,m, word1, word2, dp); + ll del = 1 + helper(i+1, j,n,m, word1, word2, dp); + ll rep = 1 + helper(i+1, j+1,n,m, word1, word2, dp); + + return dp[i][j] = min({ins, del, rep}); } - return prev[m]; + } + + int minDistance(string word1, string word2) { + + int n = word1.size(), m = word2.size(); + vector> dp(n+1,vector(m+1,-1)); + return helper(0,0,n,m,word1,word2,dp); } }; \ No newline at end of file From fcc2cae7f380d7d6b57c88a6669387200910bed4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Feb 2023 23:31:28 +0530 Subject: [PATCH 0537/3167] Create README - LeetHub --- 0427-construct-quad-tree/README.md | 71 ++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 0427-construct-quad-tree/README.md diff --git a/0427-construct-quad-tree/README.md b/0427-construct-quad-tree/README.md new file mode 100644 index 00000000..c056a027 --- /dev/null +++ b/0427-construct-quad-tree/README.md @@ -0,0 +1,71 @@ +

427. Construct Quad Tree

Medium


Given a n * n matrix grid of 0's and 1's only. We want to represent the grid with a Quad-Tree.

+ +

Return the root of the Quad-Tree representing the grid.

+ +

Notice that you can assign the value of a node to True or False when isLeaf is False, and both are accepted in the answer.

+ +

A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:

+ +
    +
  • val: True if the node represents a grid of 1's or False if the node represents a grid of 0's.
  • +
  • isLeaf: True if the node is leaf node on the tree or False if the node has the four children.
  • +
+ +
class Node {
+    public boolean val;
+    public boolean isLeaf;
+    public Node topLeft;
+    public Node topRight;
+    public Node bottomLeft;
+    public Node bottomRight;
+}
+ +

We can construct a Quad-Tree from a two-dimensional area using the following steps:

+ +
    +
  1. If the current grid has the same value (i.e all 1's or all 0's) set isLeaf True and set val to the value of the grid and set the four children to Null and stop.
  2. +
  3. If the current grid has different values, set isLeaf to False and set val to any value and divide the current grid into four sub-grids as shown in the photo.
  4. +
  5. Recurse for each of the children with the proper sub-grid.
  6. +
+ +

If you want to know more about the Quad-Tree, you can refer to the wiki.

+ +

Quad-Tree format:

+ +

The output represents the serialized format of a Quad-Tree using level order traversal, where null signifies a path terminator where no node exists below.

+ +

It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list [isLeaf, val].

+ +

If the value of isLeaf or val is True we represent it as 1 in the list [isLeaf, val] and if the value of isLeaf or val is False we represent it as 0.

+ +

 

+

Example 1:

+ +
Input: grid = [[0,1],[1,0]]
+Output: [[0,1],[1,0],[1,1],[1,1],[1,0]]
+Explanation: The explanation of this example is shown below:
+Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree.
+
+
+ +

Example 2:

+ +

+ +
Input: grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
+Output: [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
+Explanation: All values in the grid are not the same. We divide the grid into four sub-grids.
+The topLeft, bottomLeft and bottomRight each has the same value.
+The topRight have different values so we divide it into 4 sub-grids where each has the same value.
+Explanation is shown in the photo below:
+
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • n == 2x where 0 <= x <= 6
  • +
+
\ No newline at end of file From 361b07dc4e1ed350ee3997b920fb1c5dd67a7374 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Feb 2023 23:31:32 +0530 Subject: [PATCH 0538/3167] Time: 10 ms (95.65%), Space: 15.6 MB (77.90%) - LeetHub --- .../0427-construct-quad-tree.cpp | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 0427-construct-quad-tree/0427-construct-quad-tree.cpp diff --git a/0427-construct-quad-tree/0427-construct-quad-tree.cpp b/0427-construct-quad-tree/0427-construct-quad-tree.cpp new file mode 100644 index 00000000..f315dbcb --- /dev/null +++ b/0427-construct-quad-tree/0427-construct-quad-tree.cpp @@ -0,0 +1,67 @@ +/* +// Definition for a QuadTree node. +class Node { +public: + bool val; + bool isLeaf; + Node* topLeft; + Node* topRight; + Node* bottomLeft; + Node* bottomRight; + + Node() { + val = false; + isLeaf = false; + topLeft = NULL; + topRight = NULL; + bottomLeft = NULL; + bottomRight = NULL; + } + + Node(bool _val, bool _isLeaf) { + val = _val; + isLeaf = _isLeaf; + topLeft = NULL; + topRight = NULL; + bottomLeft = NULL; + bottomRight = NULL; + } + + Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) { + val = _val; + isLeaf = _isLeaf; + topLeft = _topLeft; + topRight = _topRight; + bottomLeft = _bottomLeft; + bottomRight = _bottomRight; + } +}; +*/ + +class Solution { + public: + Node* construct(vector>& grid) { + return helper(grid, 0, 0, grid.size()); + } + + private: + Node* helper(const vector>& grid, int i, int j, int w) { + if (allSame(grid, i, j, w)) + return new Node(grid[i][j], true); + + Node* node = new Node(true, false); + node->topLeft = helper(grid, i, j, w / 2); + node->topRight = helper(grid, i, j + w / 2, w / 2); + node->bottomLeft = helper(grid, i + w / 2, j, w / 2); + node->bottomRight = helper(grid, i + w / 2, j + w / 2, w / 2); + return node; + } + + bool allSame(const vector>& grid, int i, int j, int w) { + return all_of(begin(grid) + i, begin(grid) + i + w, + [&](const vector& row) { + return all_of(begin(row) + j, begin(row) + j + w, + [&](int num) { return num == grid[i][j]; }); + }); + } +}; \ No newline at end of file From c9e4428f89c600f933cbfe4e52516f0015831d10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Feb 2023 21:15:29 +0530 Subject: [PATCH 0539/3167] Create README - LeetHub --- 0652-find-duplicate-subtrees/README.md | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0652-find-duplicate-subtrees/README.md diff --git a/0652-find-duplicate-subtrees/README.md b/0652-find-duplicate-subtrees/README.md new file mode 100644 index 00000000..b0404998 --- /dev/null +++ b/0652-find-duplicate-subtrees/README.md @@ -0,0 +1,33 @@ +

652. Find Duplicate Subtrees

Medium


Given the root of a binary tree, return all duplicate subtrees.

+ +

For each kind of duplicate subtrees, you only need to return the root node of any one of them.

+ +

Two trees are duplicate if they have the same structure with the same node values.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of the nodes in the tree will be in the range [1, 5000]
  • +
  • -200 <= Node.val <= 200
  • +
+
\ No newline at end of file From ab98b2dac21ff7f081bbcf8e419824da5312d6e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Feb 2023 21:15:34 +0530 Subject: [PATCH 0540/3167] Time: 24 ms (86.10%), Space: 43.6 MB (38.15%) - LeetHub --- .../0652-find-duplicate-subtrees.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0652-find-duplicate-subtrees/0652-find-duplicate-subtrees.cpp diff --git a/0652-find-duplicate-subtrees/0652-find-duplicate-subtrees.cpp b/0652-find-duplicate-subtrees/0652-find-duplicate-subtrees.cpp new file mode 100644 index 00000000..18d632b6 --- /dev/null +++ b/0652-find-duplicate-subtrees/0652-find-duplicate-subtrees.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: + vector v; + unordered_map > m; + + string help(TreeNode *root) + { + if(root == NULL) + return "#"; + string ans = to_string(root->val) + "-" + help(root->left) + "-" + help(root->right); + m[ans].push_back(root); + return ans; + } + + vector findDuplicateSubtrees(TreeNode* root) { + if(root == NULL) + return v; + + help(root); + + for(auto i = m.begin(); i != m.end(); i++) + { + if(i->second.size() > 1) + v.push_back(i->second[0]); + } + return v; + } +}; \ No newline at end of file From cf38f5bc023ef5756f78ced18ff4ae8a213fbb47 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Mar 2023 23:20:35 +0530 Subject: [PATCH 0541/3167] Attach NOTES - LeetHub --- 0912-sort-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0912-sort-an-array/NOTES.md diff --git a/0912-sort-an-array/NOTES.md b/0912-sort-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0912-sort-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cff636144d846222b35d2d5b63eef9376fd116d3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Mar 2023 23:20:38 +0530 Subject: [PATCH 0542/3167] Time: 183 ms (70.38%), Space: 61.5 MB (54.36%) - LeetHub --- 0912-sort-an-array/0912-sort-an-array.cpp | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 0912-sort-an-array/0912-sort-an-array.cpp diff --git a/0912-sort-an-array/0912-sort-an-array.cpp b/0912-sort-an-array/0912-sort-an-array.cpp new file mode 100644 index 00000000..d01e3134 --- /dev/null +++ b/0912-sort-an-array/0912-sort-an-array.cpp @@ -0,0 +1,71 @@ +class Solution { +public: + + void merge(int beg, int end, int mid, vector&a) + { + int i, j, k; + int n1 = mid - beg + 1; + int n2 = end - mid; + + int LeftArray[n1], RightArray[n2]; + + + for (int i = 0; i < n1; i++) + LeftArray[i] = a[beg + i]; + for (int j = 0; j < n2; j++) + RightArray[j] = a[mid + 1 + j]; + + i = 0; + j = 0; + k = beg; + + while (i < n1 && j < n2) + { + if(LeftArray[i] <= RightArray[j]) + { + a[k] = LeftArray[i]; + i++; + } + else + { + a[k] = RightArray[j]; + j++; + } + k++; + } + while (i& nums) + { + if(i < j) + { + int mid = i + (j-i)/2; + mergeSort(i,mid,nums); + mergeSort(mid+1,j, nums); + merge(i,j,mid,nums); + } + } + + vector sortArray(vector& nums) { + + int n = nums.size(); + + mergeSort(0,n-1,nums); + + return nums; + + } +}; \ No newline at end of file From 7db93401500cda63a85bfd8932fd91390c5716ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Mar 2023 19:33:19 +0530 Subject: [PATCH 0543/3167] Create README - LeetHub --- 0443-string-compression/README.md | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0443-string-compression/README.md diff --git a/0443-string-compression/README.md b/0443-string-compression/README.md new file mode 100644 index 00000000..5392816b --- /dev/null +++ b/0443-string-compression/README.md @@ -0,0 +1,44 @@ +

443. String Compression

Medium


Given an array of characters chars, compress it using the following algorithm:

+ +

Begin with an empty string s. For each group of consecutive repeating characters in chars:

+ +
    +
  • If the group's length is 1, append the character to s.
  • +
  • Otherwise, append the character followed by the group's length.
  • +
+ +

The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.

+ +

After you are done modifying the input array, return the new length of the array.

+ +

You must write an algorithm that uses only constant extra space.

+ +

 

+

Example 1:

+ +
Input: chars = ["a","a","b","b","c","c","c"]
+Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
+Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
+
+ +

Example 2:

+ +
Input: chars = ["a"]
+Output: Return 1, and the first character of the input array should be: ["a"]
+Explanation: The only group is "a", which remains uncompressed since it's a single character.
+
+ +

Example 3:

+ +
Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
+Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
+Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
+ +

 

+

Constraints:

+ +
    +
  • 1 <= chars.length <= 2000
  • +
  • chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.
  • +
+
\ No newline at end of file From 2cd0607931f0a6496543b8827000978a41abdf30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Mar 2023 19:33:23 +0530 Subject: [PATCH 0544/3167] Time: 7 ms (43.14%), Space: 9 MB (83.02%) - LeetHub --- .../0443-string-compression.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0443-string-compression/0443-string-compression.cpp diff --git a/0443-string-compression/0443-string-compression.cpp b/0443-string-compression/0443-string-compression.cpp new file mode 100644 index 00000000..c5efcad9 --- /dev/null +++ b/0443-string-compression/0443-string-compression.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + int compress(vector& chars) { + + int running = 0,n = chars.size(); + int j = 0; + for(int i = 0; i 0 and chars[i] == chars[i-1]) + { + ++running; + } + else + { + if(running > 1) + { + string here = to_string(running); + for(auto itr : here) + chars[j++] = itr; + } + running = 0; + chars[j++] = chars[i]; + running = 1; + } + } + + if(running > 1) + { + string here = to_string(running); + for(auto itr : here) + chars[j++] = itr; + } + + return j; + } +}; \ No newline at end of file From 86b916c88889fa3a697ad89c6aa0646c7426e5a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:05:14 +0530 Subject: [PATCH 0545/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0028-find-the-index-of-the-first-occurrence-in-a-string/README.md diff --git a/0028-find-the-index-of-the-first-occurrence-in-a-string/README.md b/0028-find-the-index-of-the-first-occurrence-in-a-string/README.md new file mode 100644 index 00000000..d5c5e6e4 --- /dev/null +++ b/0028-find-the-index-of-the-first-occurrence-in-a-string/README.md @@ -0,0 +1,26 @@ +

28. Find the Index of the First Occurrence in a String

Medium


Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

+ +

 

+

Example 1:

+ +
Input: haystack = "sadbutsad", needle = "sad"
+Output: 0
+Explanation: "sad" occurs at index 0 and 6.
+The first occurrence is at index 0, so we return 0.
+
+ +

Example 2:

+ +
Input: haystack = "leetcode", needle = "leeto"
+Output: -1
+Explanation: "leeto" did not occur in "leetcode", so we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= haystack.length, needle.length <= 104
  • +
  • haystack and needle consist of only lowercase English characters.
  • +
+
\ No newline at end of file From 808b895bee0689e2fdfa7c67df3734b764325dc2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:05:15 +0530 Subject: [PATCH 0546/3167] Attach NOTES - LeetHub --- 0028-find-the-index-of-the-first-occurrence-in-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0028-find-the-index-of-the-first-occurrence-in-a-string/NOTES.md diff --git a/0028-find-the-index-of-the-first-occurrence-in-a-string/NOTES.md b/0028-find-the-index-of-the-first-occurrence-in-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0028-find-the-index-of-the-first-occurrence-in-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0aefc410be5be1e169b2d1ca7df953fba5000639 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:05:18 +0530 Subject: [PATCH 0547/3167] Time: 4 ms (38.84%), Space: 6.4 MB (11.62%) - LeetHub --- ...ex-of-the-first-occurrence-in-a-string.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp diff --git a/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp b/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp new file mode 100644 index 00000000..9039662c --- /dev/null +++ b/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + + vector prefix_function(string s) + { + int n = s.size(); + vector pi(n,0); + + for(int i = 1; i0 and s[i]!=s[j]) + j = pi[j-1]; + + if(s[i] == s[j]) + j++; + pi[i] = j; + } + //Time Complexity = O(n) + return pi; + } + + int strStr(string haystack, string needle) + { + if(needle.length() == 0) + return 0; + + vector prefix = prefix_function(needle); + + int pos = -1; + int i(0), j(0); + while(i Date: Sat, 4 Mar 2023 01:17:00 +0530 Subject: [PATCH 0548/3167] Attach NOTES - LeetHub From 49b5b67f1cd622da821fbbc356ff405150f7a5f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:17:03 +0530 Subject: [PATCH 0549/3167] Time: 0 ms (100.00%), Space: 6.4 MB (11.56%) - LeetHub --- ...ex-of-the-first-occurrence-in-a-string.cpp | 66 +++++++------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp b/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp index 9039662c..2c944c3b 100644 --- a/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp +++ b/0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp @@ -1,58 +1,42 @@ class Solution { public: - - vector prefix_function(string s) - { - int n = s.size(); - vector pi(n,0); + int strStr(string haystack, string needle) { + + int n = needle.size(); + vector lps(n,0); - for(int i = 1; i0 and s[i]!=s[j]) - j = pi[j-1]; - - if(s[i] == s[j]) - j++; - pi[i] = j; + int j = lps[i-1] ; + + while(j > 0 and needle[i] != needle[j]) + j = lps[j-1]; + + if(needle[i] == needle[j]) + ++j; + + lps[i] = j; } - //Time Complexity = O(n) - return pi; - } - - int strStr(string haystack, string needle) - { - if(needle.length() == 0) - return 0; - vector prefix = prefix_function(needle); - int pos = -1; - int i(0), j(0); - while(i Date: Sat, 4 Mar 2023 23:27:39 +0530 Subject: [PATCH 0550/3167] Attach NOTES - LeetHub --- 2444-count-subarrays-with-fixed-bounds/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2444-count-subarrays-with-fixed-bounds/NOTES.md diff --git a/2444-count-subarrays-with-fixed-bounds/NOTES.md b/2444-count-subarrays-with-fixed-bounds/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2444-count-subarrays-with-fixed-bounds/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c8618df18d4969c7ac23f247239b34d471914735 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Mar 2023 23:27:43 +0530 Subject: [PATCH 0551/3167] Time: 109 ms (90.61%), Space: 70.4 MB (90.20%) - LeetHub --- ...2444-count-subarrays-with-fixed-bounds.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2444-count-subarrays-with-fixed-bounds/2444-count-subarrays-with-fixed-bounds.cpp diff --git a/2444-count-subarrays-with-fixed-bounds/2444-count-subarrays-with-fixed-bounds.cpp b/2444-count-subarrays-with-fixed-bounds/2444-count-subarrays-with-fixed-bounds.cpp new file mode 100644 index 00000000..8b7a11da --- /dev/null +++ b/2444-count-subarrays-with-fixed-bounds/2444-count-subarrays-with-fixed-bounds.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + long long countSubarrays(vector& nums, int minK, int maxK) { + + int n = nums.size(); + long long res = 0; + + long long maxiIdx = -1, miniIdx = -1, badIdx = -1; + for(int i = 0; i maxK or nums[i] < minK) + badIdx = i; + if(nums[i] == maxK) maxiIdx = i; + if(nums[i] == minK) miniIdx = i; + res += max(0LL, (min(maxiIdx,miniIdx) - badIdx)); + } + + return res; + + } +}; \ No newline at end of file From e407ec675ca47be55362c6759b69afb958d45841 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 Mar 2023 22:09:02 +0530 Subject: [PATCH 0552/3167] Create README - LeetHub --- 1345-jump-game-iv/README.md | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1345-jump-game-iv/README.md diff --git a/1345-jump-game-iv/README.md b/1345-jump-game-iv/README.md new file mode 100644 index 00000000..0dd40e5a --- /dev/null +++ b/1345-jump-game-iv/README.md @@ -0,0 +1,44 @@ +

1345. Jump Game IV

Hard


Given an array of integers arr, you are initially positioned at the first index of the array.

+ +

In one step you can jump from index i to index:

+ +
    +
  • i + 1 where: i + 1 < arr.length.
  • +
  • i - 1 where: i - 1 >= 0.
  • +
  • j where: arr[i] == arr[j] and i != j.
  • +
+ +

Return the minimum number of steps to reach the last index of the array.

+ +

Notice that you can not jump outside of the array at any time.

+ +

 

+

Example 1:

+ +
Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
+Output: 3
+Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
+
+ +

Example 2:

+ +
Input: arr = [7]
+Output: 0
+Explanation: Start index is the last index. You do not need to jump.
+
+ +

Example 3:

+ +
Input: arr = [7,6,9,6,9,6,9,7]
+Output: 1
+Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 5 * 104
  • +
  • -108 <= arr[i] <= 108
  • +
+
\ No newline at end of file From 71a8c36159fbd2c6788487e51207b3665aacd0f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 Mar 2023 22:09:06 +0530 Subject: [PATCH 0553/3167] Time: 216 ms (76.33%), Space: 76.1 MB (59.43%) - LeetHub --- 1345-jump-game-iv/1345-jump-game-iv.cpp | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1345-jump-game-iv/1345-jump-game-iv.cpp diff --git a/1345-jump-game-iv/1345-jump-game-iv.cpp b/1345-jump-game-iv/1345-jump-game-iv.cpp new file mode 100644 index 00000000..38d0c179 --- /dev/null +++ b/1345-jump-game-iv/1345-jump-game-iv.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int minJumps(vector& arr) { + int n = arr.size(); + unordered_map> indicesOfValue; + for (int i = 0; i < n; i++) + indicesOfValue[arr[i]].push_back(i); + vector visited(n); visited[0] = true; + queue q; q.push(0); + int step = 0; + while (!q.empty()) { + for (int size = q.size(); size > 0; --size) { + int i = q.front(); q.pop(); + if (i == n - 1) return step; // Reached to last index + vector& next = indicesOfValue[arr[i]]; + next.push_back(i - 1); next.push_back(i + 1); + for (int j : next) { + if (j >= 0 && j < n && !visited[j]) { + visited[j] = true; + q.push(j); + } + } + next.clear(); // avoid later lookup indicesOfValue arr[i] + } + step++; + } + return 0; + } +}; \ No newline at end of file From d52f45f6aeb528f12be19baf5367a0178991cce4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Mar 2023 12:48:28 +0530 Subject: [PATCH 0554/3167] Create README - LeetHub --- 0547-number-of-provinces/README.md | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0547-number-of-provinces/README.md diff --git a/0547-number-of-provinces/README.md b/0547-number-of-provinces/README.md new file mode 100644 index 00000000..a3193d12 --- /dev/null +++ b/0547-number-of-provinces/README.md @@ -0,0 +1,33 @@ +

547. Number of Provinces

Medium


There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.

+ +

A province is a group of directly or indirectly connected cities and no other cities outside of the group.

+ +

You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.

+ +

Return the total number of provinces.

+ +

 

+

Example 1:

+ +
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
+Output: 2
+
+ +

Example 2:

+ +
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 200
  • +
  • n == isConnected.length
  • +
  • n == isConnected[i].length
  • +
  • isConnected[i][j] is 1 or 0.
  • +
  • isConnected[i][i] == 1
  • +
  • isConnected[i][j] == isConnected[j][i]
  • +
+
\ No newline at end of file From c34929da3c408315b295d000a11d6e2dfa8856f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Mar 2023 12:48:28 +0530 Subject: [PATCH 0555/3167] Attach NOTES - LeetHub --- 0547-number-of-provinces/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0547-number-of-provinces/NOTES.md diff --git a/0547-number-of-provinces/NOTES.md b/0547-number-of-provinces/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0547-number-of-provinces/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From da26146443fa99b9c316d4689a8ad731777f1f52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Mar 2023 12:48:31 +0530 Subject: [PATCH 0556/3167] Time: 20 ms (91.69%), Space: 13.8 MB (62.29%) - LeetHub --- .../0547-number-of-provinces.cpp | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 0547-number-of-provinces/0547-number-of-provinces.cpp diff --git a/0547-number-of-provinces/0547-number-of-provinces.cpp b/0547-number-of-provinces/0547-number-of-provinces.cpp new file mode 100644 index 00000000..af2f16de --- /dev/null +++ b/0547-number-of-provinces/0547-number-of-provinces.cpp @@ -0,0 +1,113 @@ +template +class DSU +{ +public: + int N; + vector rank, parent, size; + DSU(int n) + { + N = n; + rank.resize(n + 1, 0); + size.resize(n + 1, 1); + parent.resize(n + 1); + + for (T i = 0; i <= n; ++i) + { + parent[i] = i; + } + } + + template + X findParent(X u) + { + if (u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } + + // Not be using unionByRank + // but yes for future reference + template + void unionByRank(X u, X v) + { + int nodeX = findParent(u); + int nodeY = findParent(v); + + if (nodeX == nodeY) + return; + + if (rank[nodeX] < rank[nodeY]) + { + parent[nodeX] = nodeY; + } + else if (rank[nodeY] < rank[nodeX]) + { + parent[nodeY] = nodeX; + } + else + { + parent[nodeY] = nodeX; + ++rank[nodeX]; + } + } + + void unionBySize(int u, int v) + { + int nodeX = findParent(u); + int nodeY = findParent(v); + + if (nodeX == nodeY) + return; + + if (size[nodeX] < size[nodeY]) + { + parent[nodeX] = nodeY; + size[nodeY] += size[nodeX]; + } + else + { + parent[nodeY] = nodeX; + size[nodeX] += size[nodeY]; + } + } + + template + bool isSame(X u, X v) + { + return findParent(u) == findParent(v); + } +}; +// DSU dsu(n); + +// dsu.unionByRank(u, v); +// dsu.unionBySize(u,v); +// dsu.isSame(u, v) bool + + +class Solution { +public: + int findCircleNum(vector>& isConnected) { + + int n = isConnected.size(); + int m = isConnected[0].size(); + int provinces = 0; + + DSU dsu(n); + for(int i = 0; i< n; ++i) + { + for(int j = 0; j Date: Mon, 6 Mar 2023 21:43:59 +0530 Subject: [PATCH 0557/3167] Create README - LeetHub --- 1539-kth-missing-positive-number/README.md | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1539-kth-missing-positive-number/README.md diff --git a/1539-kth-missing-positive-number/README.md b/1539-kth-missing-positive-number/README.md new file mode 100644 index 00000000..295a5838 --- /dev/null +++ b/1539-kth-missing-positive-number/README.md @@ -0,0 +1,34 @@ +

1539. Kth Missing Positive Number

Easy


Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.

+ +

Return the kth positive integer that is missing from this array.

+ +

 

+

Example 1:

+ +
Input: arr = [2,3,4,7,11], k = 5
+Output: 9
+Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
+
+ +

Example 2:

+ +
Input: arr = [1,2,3,4], k = 2
+Output: 6
+Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 1 <= arr[i] <= 1000
  • +
  • 1 <= k <= 1000
  • +
  • arr[i] < arr[j] for 1 <= i < j <= arr.length
  • +
+ +

 

+

Follow up:

+ +

Could you solve this problem in less than O(n) complexity?

+
\ No newline at end of file From 89dc7698a5fc97c8cf7aa7040f5b2b4354831880 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Mar 2023 21:44:03 +0530 Subject: [PATCH 0558/3167] Time: 0 ms (100.00%), Space: 9.5 MB (94.95%) - LeetHub --- .../1539-kth-missing-positive-number.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp diff --git a/1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp b/1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp new file mode 100644 index 00000000..5fb8b1aa --- /dev/null +++ b/1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int findKthPositive(vector& arr, int k) { + int start = 0, end = arr.size(); + while(start < end) + { + int mid = start + (end - start) /2; + if(arr[mid]- mid - 1 < k) + start = mid + 1; + else + end = mid; + } + return start + k; + } +}; \ No newline at end of file From 3b6a3af755fa685117918bbc209bcab524354688 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:05:47 +0530 Subject: [PATCH 0559/3167] Attach NOTES - LeetHub --- 2187-minimum-time-to-complete-trips/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2187-minimum-time-to-complete-trips/NOTES.md diff --git a/2187-minimum-time-to-complete-trips/NOTES.md b/2187-minimum-time-to-complete-trips/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2187-minimum-time-to-complete-trips/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fdea2fe4b1f8bef888ed6fe2122e7d6472a8540d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:05:56 +0530 Subject: [PATCH 0560/3167] Time: 318 ms (78.24%), Space: 94.5 MB (76.55%) - LeetHub --- .../2187-minimum-time-to-complete-trips.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2187-minimum-time-to-complete-trips/2187-minimum-time-to-complete-trips.cpp diff --git a/2187-minimum-time-to-complete-trips/2187-minimum-time-to-complete-trips.cpp b/2187-minimum-time-to-complete-trips/2187-minimum-time-to-complete-trips.cpp new file mode 100644 index 00000000..9f3978f2 --- /dev/null +++ b/2187-minimum-time-to-complete-trips/2187-minimum-time-to-complete-trips.cpp @@ -0,0 +1,38 @@ +#define ll long long int +class Solution { +public: + + ll check(vector& time, int totalTrips, ll mid) + { + ll ans = 0, n = time.size(); + for(int i =0 ; i& time, int totalTrips) { + + ll mini = *min_element(time.begin(),time.end()); + ll start = 0, end = (ll)(mini * totalTrips); // maxTime; + ll ans = end; + + + while(start <= end) + { + ll mid = start + (end - start)/2; + if(check(time,totalTrips,mid) >= totalTrips) + { + ans = mid; + end = mid-1; + } + else + start = mid + 1; + } + + return ans; + + } +}; \ No newline at end of file From f97bb4de7e3ee7f3d81b329d8dc32305f774f383 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:53:10 +0530 Subject: [PATCH 0561/3167] Attach NOTES - LeetHub --- 0307-range-sum-query-mutable/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0307-range-sum-query-mutable/NOTES.md diff --git a/0307-range-sum-query-mutable/NOTES.md b/0307-range-sum-query-mutable/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0307-range-sum-query-mutable/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2a474374791390653dc9acebbc4c22ab9e3b2dca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:53:14 +0530 Subject: [PATCH 0562/3167] Time: 500 ms (81.45%), Space: 175.5 MB (20.97%) - LeetHub --- .../0307-range-sum-query-mutable.cpp | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp diff --git a/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp b/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp new file mode 100644 index 00000000..f4341f92 --- /dev/null +++ b/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp @@ -0,0 +1,83 @@ +class NumArray { +public: + + void buildTree(int idx, int low, int high) + { + if (low == high) + { + seg[idx] = v[low]; + return; + } + + int mid = (low + high) / 2; + buildTree(2 * idx + 1, low, mid); + buildTree(2 * idx + 2, mid + 1, high); + + // update + seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; + } + + int query(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 seg[idx]; + + // partial overlap + int mid = (low + high) / 2; + int left = query(2 * idx + 1, low, mid, l, r); + int right = query(2 * idx + 2, mid + 1, high, l, r); + + // update + return left + right; + } + + void update(int idx, int low, int high, int ind, int val) + { + if (low == high) + { + seg[idx] = val; + return; + } + + int mid = (low + high) / 2; + if (ind <= mid) + update(2 * idx + 1, low, mid, ind, val); + else + update(2 * idx + 2, mid + 1, high, ind, val); + + // update + seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; + } + + vector seg; + vector v; + + NumArray(vector& nums) { + int n = nums.size(); + seg.resize(n * 4 + 5); + v = nums; + buildTree(0,0,n-1); + } + + void update(int index, int val) { + update(0,0,v.size()-1,index,val); + } + + int sumRange(int left, int right) { + return query(0,0,v.size()-1,left,right); + } +}; + +/** + * Your NumArray object will be instantiated and called as such: + * NumArray* obj = new NumArray(nums); + * obj->update(index,val); + * int param_2 = obj->sumRange(left,right); + */ \ No newline at end of file From b21e78873a61dcc39b9de276cae0ad87a0126864 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Mar 2023 23:29:10 +0530 Subject: [PATCH 0563/3167] Create README - LeetHub --- 0875-koko-eating-bananas/README.md | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0875-koko-eating-bananas/README.md diff --git a/0875-koko-eating-bananas/README.md b/0875-koko-eating-bananas/README.md new file mode 100644 index 00000000..c6eea5f5 --- /dev/null +++ b/0875-koko-eating-bananas/README.md @@ -0,0 +1,36 @@ +

875. Koko Eating Bananas

Medium


Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.

+ +

Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.

+ +

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

+ +

Return the minimum integer k such that she can eat all the bananas within h hours.

+ +

 

+

Example 1:

+ +
Input: piles = [3,6,7,11], h = 8
+Output: 4
+
+ +

Example 2:

+ +
Input: piles = [30,11,23,4,20], h = 5
+Output: 30
+
+ +

Example 3:

+ +
Input: piles = [30,11,23,4,20], h = 6
+Output: 23
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= piles.length <= 104
  • +
  • piles.length <= h <= 109
  • +
  • 1 <= piles[i] <= 109
  • +
+
\ No newline at end of file From ca6fa76c4d0c72ffcde80f108b27e865cf1f2b82 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Mar 2023 23:29:13 +0530 Subject: [PATCH 0564/3167] Time: 82 ms (11.71%), Space: 18.9 MB (82.50%) - LeetHub --- .../0875-koko-eating-bananas.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0875-koko-eating-bananas/0875-koko-eating-bananas.cpp diff --git a/0875-koko-eating-bananas/0875-koko-eating-bananas.cpp b/0875-koko-eating-bananas/0875-koko-eating-bananas.cpp new file mode 100644 index 00000000..79c84b65 --- /dev/null +++ b/0875-koko-eating-bananas/0875-koko-eating-bananas.cpp @@ -0,0 +1,39 @@ +#define ll long long int +class Solution { +public: + + ll check(vector& piles, int mid, int h) + { + int take = 0; + for(int i = 0; i& piles, int h) { + + ll low = 1; + ll high = accumulate(piles.begin(),piles.end(),0LL); + int ans = 0; + + while(low <= high) + { + ll mid = low + (high - low)/2; + if(check(piles,mid,h)) + { + high = mid-1; + ans = mid; + } + else + low = mid + 1; + } + + return ans; + } +}; \ No newline at end of file From 9c25692d66fb288aa377516b93a6873f5db5e9b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Mar 2023 23:29:52 +0530 Subject: [PATCH 0565/3167] Attach NOTES - LeetHub --- 0875-koko-eating-bananas/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0875-koko-eating-bananas/NOTES.md diff --git a/0875-koko-eating-bananas/NOTES.md b/0875-koko-eating-bananas/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0875-koko-eating-bananas/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bf224efe8404daf9ecdc863a6542a8ae17b5d7ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Mar 2023 23:29:56 +0530 Subject: [PATCH 0566/3167] Time: 82 ms (11.71%), Space: 18.9 MB (82.50%) - LeetHub From 165f2df8ed6cd26e7367c0edf7d1e15f67f23871 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Mar 2023 12:10:38 +0530 Subject: [PATCH 0567/3167] Attach NOTES - LeetHub From 9ca34684adaad028dbd77016f1a5d751d76334c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Mar 2023 12:10:42 +0530 Subject: [PATCH 0568/3167] Time: 520 ms (63.68%), Space: 175.5 MB (20.91%) - LeetHub --- .../0307-range-sum-query-mutable.cpp | 90 +++++++++++++------ 1 file changed, 64 insertions(+), 26 deletions(-) diff --git a/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp b/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp index f4341f92..0f5e7e2a 100644 --- a/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp +++ b/0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp @@ -1,77 +1,115 @@ class NumArray { public: + public: void buildTree(int idx, int low, int high) { if (low == high) { - seg[idx] = v[low]; + tree[idx] = arr[low]; return; } - int mid = (low + high) / 2; + int mid = (low + high) >> 1; buildTree(2 * idx + 1, low, mid); buildTree(2 * idx + 2, mid + 1, high); - // update - seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; - } + // updation part + + // sum + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; - int query(int idx, int low, int high, int l, int r) + // min + // tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]); + return; + } + + public: + 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; + // return INT_MAX; // min // complete overlap // [l low high r] if (low >= l and high <= r) - return seg[idx]; + return tree[idx]; // partial overlap int mid = (low + high) / 2; - int left = query(2 * idx + 1, low, mid, l, r); - int right = query(2 * idx + 2, mid + 1, high, l, r); + int left = queryTree(2 * idx + 1, low, mid, l, r); + int right = queryTree(2 * idx + 2, mid + 1, high, l, r); + + // updation part + // sum - // update return left + right; + + // min + // return min(left, right); } - void update(int idx, int low, int high, int ind, int val) +public: + void updateTree(int idx, int low, int high, int ind, int val) { if (low == high) { - seg[idx] = val; + // sum ranges increment tree[idx] by val case + // tree[idx] += val; + + // min and update single value case + tree[idx] = val; return; } - - int mid = (low + high) / 2; + int mid = (low + high) >> 1; if (ind <= mid) - update(2 * idx + 1, low, mid, ind, val); + updateTree(2 * idx + 1, low, mid, ind, val); else - update(2 * idx + 2, mid + 1, high, ind, val); + updateTree(2 * idx + 2, mid + 1, high, ind, val); - // update - seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; + // updation Part + // sum + + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; + + // min + + // tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]); + } + + void build() + { + buildTree(0, 0, N - 1); } - vector seg; - vector v; + 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); + } + vector tree, arr; + int N; NumArray(vector& nums) { - int n = nums.size(); - seg.resize(n * 4 + 5); - v = nums; - buildTree(0,0,n-1); + N = nums.size(); + arr = nums; + tree.resize(N*4 + 5); + build(); } void update(int index, int val) { - update(0,0,v.size()-1,index,val); + Update(index,val); } int sumRange(int left, int right) { - return query(0,0,v.size()-1,left,right); + return query(left, right); } }; From 8b70c3eb4c4156eba5a151f8ad4266097b6e74cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Mar 2023 23:48:48 +0530 Subject: [PATCH 0569/3167] Create README - LeetHub --- 0142-linked-list-cycle-ii/README.md | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0142-linked-list-cycle-ii/README.md diff --git a/0142-linked-list-cycle-ii/README.md b/0142-linked-list-cycle-ii/README.md new file mode 100644 index 00000000..50b0fd7c --- /dev/null +++ b/0142-linked-list-cycle-ii/README.md @@ -0,0 +1,40 @@ +

142. Linked List Cycle II

Medium


Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.

+ +

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.

+ +

Do not modify the linked list.

+ +

 

+

Example 1:

+ +
Input: head = [3,2,0,-4], pos = 1
+Output: tail connects to node index 1
+Explanation: There is a cycle in the linked list, where tail connects to the second node.
+
+ +

Example 2:

+ +
Input: head = [1,2], pos = 0
+Output: tail connects to node index 0
+Explanation: There is a cycle in the linked list, where tail connects to the first node.
+
+ +

Example 3:

+ +
Input: head = [1], pos = -1
+Output: no cycle
+Explanation: There is no cycle in the linked list.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the list is in the range [0, 104].
  • +
  • -105 <= Node.val <= 105
  • +
  • pos is -1 or a valid index in the linked-list.
  • +
+ +

 

+

Follow up: Can you solve it using O(1) (i.e. constant) memory?

+
\ No newline at end of file From 6bcd2cb3d096c7a069e1a991fbcc4ddd9a089343 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Mar 2023 23:48:48 +0530 Subject: [PATCH 0570/3167] Attach NOTES - LeetHub --- 0142-linked-list-cycle-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0142-linked-list-cycle-ii/NOTES.md diff --git a/0142-linked-list-cycle-ii/NOTES.md b/0142-linked-list-cycle-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0142-linked-list-cycle-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a61cedade9956aedc25b7b5c5a0280b774b91790 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Mar 2023 23:48:51 +0530 Subject: [PATCH 0571/3167] Time: 6 ms (90.60%), Space: 7.4 MB (98.05%) - LeetHub --- .../0142-linked-list-cycle-ii.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.cpp diff --git a/0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.cpp b/0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.cpp new file mode 100644 index 00000000..bedac9a0 --- /dev/null +++ b/0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.cpp @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + if(head == NULL || head -> next == NULL) + return NULL; + ListNode *slow = head,*fast = head, *temp = head; + + while(fast != NULL && fast -> next != NULL) + { + slow = slow -> next; + fast = fast -> next -> next; + + if(fast == slow) //cycle || loop + { + while(slow != temp) + { + slow = slow -> next; + temp = temp -> next; + } + return temp ; + } + + } + + + return NULL; + } +}; \ No newline at end of file From a175a7065dcf43c1ec4f7a9c4f2af343cb6edfd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:13:51 +0530 Subject: [PATCH 0572/3167] Attach NOTES - LeetHub --- 0208-implement-trie-prefix-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0208-implement-trie-prefix-tree/NOTES.md diff --git a/0208-implement-trie-prefix-tree/NOTES.md b/0208-implement-trie-prefix-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0208-implement-trie-prefix-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 09bdc030d22e137fadf791918fa082448c8f6abb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:13:53 +0530 Subject: [PATCH 0573/3167] Time: 52 ms (93.35%), Space: 45 MB (58.33%) - LeetHub --- .../0208-implement-trie-prefix-tree.cpp | 66 +++++++++++++------ 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp b/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp index 4be1fc25..f80af203 100644 --- a/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp +++ b/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp @@ -1,46 +1,72 @@ -class Trie { -public: +class Node{ - struct Node{ - Node *Child[26] = {nullptr}; - bool isWord = false; - }; + public: - Node* root = new Node(); + Node* child[26] = {nullptr}; + bool isWord = false; - Trie() { + 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; + } + + bool isEnd(){ + return isWord; + } +}; + +class Trie { +public: + private: + Node* root; + public: + Trie() { + root = new Node(); } void insert(string word) { - Node * temp = root; - for(int i= 0; iChild[word[i]-'a']) - temp->Child[word[i]-'a'] = new Node(); - temp = temp->Child[word[i]-'a']; + if(!temp->containsKey(word[i])) + temp->put(word[i],new Node()); + temp = temp->get(word[i]); } - temp->isWord = true; + temp->setEnd(); } bool search(string word) { Node *temp = root; - for(int i = 0; i < word.size(); ++i) + for(int i = 0; iChild[word[i]-'a']) + if(!temp->containsKey(word[i])) return false; - temp = temp->Child[word[i]-'a']; + temp = temp->get(word[i]); } - return temp->isWord; + return temp->isEnd(); } bool startsWith(string prefix) { Node *temp = root; for(int i = 0; iChild[prefix[i] - 'a']) + if(!temp->containsKey(prefix[i])) return false; - temp = temp->Child[prefix[i] - 'a']; + temp = temp->get(prefix[i]); } return true; } From 90ee98e3b85d9a97e6a0e87fef0d45e3e93bc15b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:48:27 +0530 Subject: [PATCH 0574/3167] Attach NOTES - LeetHub From 0cce69f56b6c9bcf741d312791d5898a903dacf7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:48:30 +0530 Subject: [PATCH 0575/3167] Time: 68 ms (58.98%), Space: 45.1 MB (58.33%) - LeetHub --- .../0208-implement-trie-prefix-tree.cpp | 77 ++++++++++++++----- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp b/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp index f80af203..474e9385 100644 --- a/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp +++ b/0208-implement-trie-prefix-tree/0208-implement-trie-prefix-tree.cpp @@ -1,34 +1,69 @@ -class Node{ - - public: - - Node* child[26] = {nullptr}; +class Node +{ + +public: + Node *child[26] = {nullptr}; bool isWord = false; - + int cntEndWith = 0; + int cntPrefix = 0; + bool containsKey(char ch) { - return (child[ch-'a'] != nullptr); + return (child[ch - 'a'] != nullptr); } - - void put(char ch, Node* node) + + void put(char ch, Node *node) { - child[ch-'a'] = node; + child[ch - 'a'] = node; } - - Node* get(char ch) + + Node *get(char ch) { return child[ch - 'a']; } - - void setEnd(){ + + void setEnd() + { isWord = true; } - - bool isEnd(){ + + bool isEnd() + { return isWord; } + + void increaseEnd() + { + ++cntEndWith; + } + + void increasePrefix() + { + ++cntPrefix; + } + + void deleteEnd() + { + --cntEndWith; + } + + void reducePrefix() + { + --cntPrefix; + } + + int getEnd() + { + return cntEndWith; + } + + int getPrefix() + { + return cntPrefix; + } }; + class Trie { public: private: @@ -40,12 +75,14 @@ class Trie { void insert(string word) { Node *temp = root; - for(int i = 0; i < word.size(); ++i) + for (int i = 0; i < word.size(); ++i) { - if(!temp->containsKey(word[i])) - temp->put(word[i],new Node()); - temp = temp->get(word[i]); + if (!temp->containsKey(word[i])) + temp->put(word[i], new Node()); + temp = temp->get(word[i]); + temp->increasePrefix(); } + temp->increaseEnd(); temp->setEnd(); } From 5623ac73ac8a378db1178ab1b0d7c37486c41b87 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Mar 2023 22:30:28 +0530 Subject: [PATCH 0576/3167] Create README - LeetHub --- 0382-linked-list-random-node/README.md | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0382-linked-list-random-node/README.md diff --git a/0382-linked-list-random-node/README.md b/0382-linked-list-random-node/README.md new file mode 100644 index 00000000..21f56322 --- /dev/null +++ b/0382-linked-list-random-node/README.md @@ -0,0 +1,45 @@ +

382. Linked List Random Node

Medium


Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.

+ +

Implement the Solution class:

+ +
    +
  • Solution(ListNode head) Initializes the object with the head of the singly-linked list head.
  • +
  • int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.
  • +
+ +

 

+

Example 1:

+ +
Input
+["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
+[[[1, 2, 3]], [], [], [], [], []]
+Output
+[null, 1, 3, 2, 2, 3]
+
+Explanation
+Solution solution = new Solution([1, 2, 3]);
+solution.getRandom(); // return 1
+solution.getRandom(); // return 3
+solution.getRandom(); // return 2
+solution.getRandom(); // return 2
+solution.getRandom(); // return 3
+// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the linked list will be in the range [1, 104].
  • +
  • -104 <= Node.val <= 104
  • +
  • At most 104 calls will be made to getRandom.
  • +
+ +

 

+

Follow up:

+ +
    +
  • What if the linked list is extremely large and its length is unknown to you?
  • +
  • Could you solve this efficiently without using extra space?
  • +
+
\ No newline at end of file From ce990941db079c074d2f474276bf61bba714b155 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Mar 2023 22:30:31 +0530 Subject: [PATCH 0577/3167] Time: 20 ms (85.48%), Space: 16.8 MB (8.14%) - LeetHub --- .../0382-linked-list-random-node.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0382-linked-list-random-node/0382-linked-list-random-node.cpp diff --git a/0382-linked-list-random-node/0382-linked-list-random-node.cpp b/0382-linked-list-random-node/0382-linked-list-random-node.cpp new file mode 100644 index 00000000..7d22969b --- /dev/null +++ b/0382-linked-list-random-node/0382-linked-list-random-node.cpp @@ -0,0 +1,31 @@ +/** + * 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 v; + Solution(ListNode* head) { + while(head) + { + v.push_back(head->val); + head = head->next; + } + } + + int getRandom() { + return v[rand()%v.size()]; + } +}; + +/** + * Your Solution object will be instantiated and called as such: + * Solution* obj = new Solution(head); + * int param_1 = obj->getRandom(); + */ \ No newline at end of file From feec0cbb1510ef50bb0da6dcc5e01526025ac8ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 00:28:41 +0530 Subject: [PATCH 0578/3167] Attach NOTES - LeetHub --- 0079-word-search/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0079-word-search/NOTES.md diff --git a/0079-word-search/NOTES.md b/0079-word-search/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0079-word-search/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cc06b07d9a6f86858fbe0354e09ff70155fd896d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 00:28:44 +0530 Subject: [PATCH 0579/3167] Time: 432 ms (78.21%), Space: 8.1 MB (61.80%) - LeetHub --- 0079-word-search/0079-word-search.cpp | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0079-word-search/0079-word-search.cpp diff --git a/0079-word-search/0079-word-search.cpp b/0079-word-search/0079-word-search.cpp new file mode 100644 index 00000000..158257fc --- /dev/null +++ b/0079-word-search/0079-word-search.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + + bool helper(int i, int j, int n, int m , int idx, vector >& board, string& word) + { + + if(idx == word.size()) + return true; + if(i <0 or j < 0 or i >= n or j >= m or board[i][j] == '!' or board[i][j] != word[idx]) + return false; + char ch = board[i][j]; + board[i][j] = '!'; + + int top = helper(i-1,j,n,m,idx+1,board,word); + int bottom = helper(i+1,j,n,m,idx+1, board, word); + int left = helper(i,j-1,n,m,idx+1,board,word); + int right = helper(i,j+1,n,m,idx+1,board,word); + + board[i][j] = ch; + + return top or bottom or left or right; + + + } + + bool exist(vector>& board, string word) { + + int n = board.size(); + int m = board[0].size(); + + for(int i = 0; i Date: Sat, 11 Mar 2023 10:22:28 +0530 Subject: [PATCH 0580/3167] Attach NOTES - LeetHub --- 0720-longest-word-in-dictionary/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0720-longest-word-in-dictionary/NOTES.md diff --git a/0720-longest-word-in-dictionary/NOTES.md b/0720-longest-word-in-dictionary/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0720-longest-word-in-dictionary/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ad3b1c4f27d5974736daabf89eeef0e6119162cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 10:22:32 +0530 Subject: [PATCH 0581/3167] Time: 58 ms (60.92%), Space: 30.8 MB (45.74%) - LeetHub --- .../0720-longest-word-in-dictionary.cpp | 224 ++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 0720-longest-word-in-dictionary/0720-longest-word-in-dictionary.cpp diff --git a/0720-longest-word-in-dictionary/0720-longest-word-in-dictionary.cpp b/0720-longest-word-in-dictionary/0720-longest-word-in-dictionary.cpp new file mode 100644 index 00000000..c2e2fdd0 --- /dev/null +++ b/0720-longest-word-in-dictionary/0720-longest-word-in-dictionary.cpp @@ -0,0 +1,224 @@ +class Node +{ + +public: + Node *child[26] = {nullptr}; + bool isWord = false; + int cntEndWith = 0; + int cntPrefix = 0; + + 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 remEnd() + { + isWord = false; + } + + bool isEnd() + { + return isWord; + } + + void increaseEnd() + { + ++cntEndWith; + } + + void increasePrefix() + { + ++cntPrefix; + } + + void deleteEnd() + { + --cntEndWith; + } + + void reducePrefix() + { + --cntPrefix; + } + + int getEnd() + { + return cntEndWith; + } + + int getPrefix() + { + return cntPrefix; + } +}; + +class Trie +{ + +private: + Node *root; + +public: + Trie() + { + root = new Node(); + } + + // insert string into trie data structure + void insert(string &word) + { + Node *temp = root; + for (int i = 0; i < word.size(); ++i) + { + if (!temp->containsKey(word[i])) + temp->put(word[i], new Node()); + temp = temp->get(word[i]); + temp->increasePrefix(); + } + temp->increaseEnd(); + temp->setEnd(); + } + + // if given word exist + bool search(string &word) + { + Node *temp = root; + for (int i = 0; i < word.size(); ++i) + { + if (!temp->containsKey(word[i])) + return false; + temp = temp->get(word[i]); + } + return temp->isEnd(); + } + + // if word with given prefix exist + bool startsWith(string &prefix) + { + Node *temp = root; + for (int i = 0; i < prefix.size(); ++i) + { + if (!temp->containsKey(prefix[i])) + return false; + temp = temp->get(prefix[i]); + } + return true; + } + + // check if all prefixes exist + bool checkIfAllPrefixesExists(string &word) + { + Node *temp = root; + bool ok = true; + for (int i = 0; i < word.size(); ++i) + { + if (temp->containsKey(word[i])) + { + temp = temp->get(word[i]); + ok = ok & temp->isEnd(); + } + else + return false; + } + return ok; + } + + // count frequency of given word + int countWordsEqualTo(string &word) + { + Node *temp = root; + for (int i = 0; i < word.size(); ++i) + { + if (temp->containsKey(word[i])) + temp = temp->get(word[i]); + else + return 0; + } + return temp->getEnd(); + } + + // count words contains same given Prefix + int countWordsStartingWith(string &word) + { + Node *temp = root; + for (int i = 0; i < word.size(); ++i) + { + if (temp->containsKey(word[i])) + temp = temp->get(word[i]); + else + return 0; + } + return temp->getPrefix(); + } + + // erase word + // assuming that word exists + void erase(string &word) + { + Node *temp = root; + for (int i = 0; i < word.size(); ++i) + { + if (temp->containsKey(word[i])) + { + temp = temp->get(word[i]); + temp->reducePrefix(); + } + else + return; + } + temp->deleteEnd(); + + // if no more count of word exist then set isWord to false word not exist + if (temp->getEnd() == 0) + temp->remEnd(); + } +}; + +// Trie *obj = new Trie(); +// obj->insert(word); +// obj->search(word); // is exist +// obj->startsWith(word); // prefix exist +// obj->checkIfAllPrefixesExists(word) // all prefixes of string +// obj->countWordsEqualTo(word); // freq of word +// obj->countWordsStartingWith(str) // freq of prefix +// obj->erase(word); + + +class Solution { +public: + string longestWord(vector& words) { + Trie *obj = new Trie(); + + for(auto word : words) + obj->insert(word); + + string ans = ""; + for(auto word : words) + { + if(obj->checkIfAllPrefixesExists(word)) + { + if(ans.size() < word.size()) + ans = word; + else if(ans.size() == word.size() and word < ans) + ans = word; + } + } + return ans; + } +}; \ No newline at end of file From 77350191fa77ee22b5f09e6e5a6801c55e57d280 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 13:22:33 +0530 Subject: [PATCH 0582/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1707-maximum-xor-with-an-element-from-array/README.md diff --git a/1707-maximum-xor-with-an-element-from-array/README.md b/1707-maximum-xor-with-an-element-from-array/README.md new file mode 100644 index 00000000..5ba8d119 --- /dev/null +++ b/1707-maximum-xor-with-an-element-from-array/README.md @@ -0,0 +1,32 @@ +

1707. Maximum XOR With an Element From Array

Hard


You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi].

+ +

The answer to the ith query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1.

+ +

Return an integer array answer where answer.length == queries.length and answer[i] is the answer to the ith query.

+ +

 

+

Example 1:

+ +
Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
+Output: [3,3,7]
+Explanation:
+1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
+2) 1 XOR 2 = 3.
+3) 5 XOR 2 = 7.
+
+ +

Example 2:

+ +
Input: nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
+Output: [15,-1,5]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length, queries.length <= 105
  • +
  • queries[i].length == 2
  • +
  • 0 <= nums[j], xi, mi <= 109
  • +
+
\ No newline at end of file From 0da54bc371a51e26ca5c74ab78a93c6f63fd9007 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 13:22:37 +0530 Subject: [PATCH 0583/3167] Time: 833 ms (58.05%), Space: 216.7 MB (58.39%) - LeetHub --- ...maximum-xor-with-an-element-from-array.cpp | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 1707-maximum-xor-with-an-element-from-array/1707-maximum-xor-with-an-element-from-array.cpp diff --git a/1707-maximum-xor-with-an-element-from-array/1707-maximum-xor-with-an-element-from-array.cpp b/1707-maximum-xor-with-an-element-from-array/1707-maximum-xor-with-an-element-from-array.cpp new file mode 100644 index 00000000..e7465886 --- /dev/null +++ b/1707-maximum-xor-with-an-element-from-array/1707-maximum-xor-with-an-element-from-array.cpp @@ -0,0 +1,110 @@ +class Node{ + public: + Node *child[2] = {nullptr}; + + bool containsKey(int bit) + { + return (child[bit] != nullptr); + } + + Node *get(int bit) + { + return child[bit]; + } + + void put(int bit , Node* node) + { + child[bit] = node; + } +}; + +class Trie{ + + private: + Node *root; + public: + Trie(){ + root = new Node(); + } + public: + void insert(int num) + { + Node*temp = root; + + for(int i = 31; i>=0; --i) + { + int bit = (num >> i) & 1; + if(!temp->containsKey(bit)) + { + temp->put(bit,new Node()); + } + temp = temp->get(bit); + } + } + public: + int getMax(int num){ + Node* temp = root; + int maxNum = 0; + for(int i = 31; i>=0; --i) + { + int bit = (num >> i) & 1; + if(temp->containsKey(1-bit)) // opposite + { + maxNum = maxNum | (1 << i); + temp = temp->get(1-bit); + } + else{ + temp = temp->get(bit); + } + } + + return maxNum; + } +}; + + +class Solution { +public: + vector maximizeXor(vector& nums, vector>& queries) { + + vector>> vec; + int n = queries.size(); + + for(int i = 0; i ans(n); + + for(int i = 0; i < n; ++i) + { + int mi = vec[i].first; + int xi = vec[i].second.first; + int idx = vec[i].second.second; + + for(int i = k; i < m; ++i) + { + if(nums[k] <= mi) + obj->insert(nums[k++]); + else + break; + } + + if(k == 0) + ans[idx] = -1; + else + ans[idx] = obj->getMax(xi); + } + + return ans; + + } +}; \ No newline at end of file From 45da58edd0cfca6108702e928b99b7ede42ef60a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 23:38:39 +0530 Subject: [PATCH 0584/3167] Create README - LeetHub --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0109-convert-sorted-list-to-binary-search-tree/README.md diff --git a/0109-convert-sorted-list-to-binary-search-tree/README.md b/0109-convert-sorted-list-to-binary-search-tree/README.md new file mode 100644 index 00000000..1e21899b --- /dev/null +++ b/0109-convert-sorted-list-to-binary-search-tree/README.md @@ -0,0 +1,24 @@ +

109. Convert Sorted List to Binary Search Tree

Medium


Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.

+ +

 

+

Example 1:

+ +
Input: head = [-10,-3,0,5,9]
+Output: [0,-3,9,-10,null,5]
+Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in head is in the range [0, 2 * 104].
  • +
  • -105 <= Node.val <= 105
  • +
+
\ No newline at end of file From 0ce01c46d63165fac4820e430c8a087262828a9d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Mar 2023 23:38:43 +0530 Subject: [PATCH 0585/3167] Time: 29 ms (61.10%), Space: 28.3 MB (92.35%) - LeetHub --- ...vert-sorted-list-to-binary-search-tree.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0109-convert-sorted-list-to-binary-search-tree/0109-convert-sorted-list-to-binary-search-tree.cpp diff --git a/0109-convert-sorted-list-to-binary-search-tree/0109-convert-sorted-list-to-binary-search-tree.cpp b/0109-convert-sorted-list-to-binary-search-tree/0109-convert-sorted-list-to-binary-search-tree.cpp new file mode 100644 index 00000000..63279058 --- /dev/null +++ b/0109-convert-sorted-list-to-binary-search-tree/0109-convert-sorted-list-to-binary-search-tree.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) {} + * }; + */ +/** + * 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* sortedListToBST(ListNode* head) { + if(head == NULL) + return NULL; + if(head -> next == NULL) + { + TreeNode *root = new TreeNode(head->val); + return root; + } + + ListNode *slow = head, *fast = head->next; + + while(fast -> next != NULL && fast->next->next != NULL) + { + slow = slow->next; + fast = fast->next->next; + } + + ListNode *mid = slow->next; + slow ->next = NULL; + + TreeNode *root = new TreeNode(mid->val); + root->left = sortedListToBST(head); + root->right = sortedListToBST(mid->next); + + return root; + } +}; \ No newline at end of file From 3f56bb26c918aab9fd9a166c0e3c0dad99370cfd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Mar 2023 19:32:21 +0530 Subject: [PATCH 0586/3167] Attach NOTES - LeetHub From 564a911962e50c382e73375ae710ec9a13f3b35b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Mar 2023 19:32:23 +0530 Subject: [PATCH 0587/3167] Time: 14 ms (11.87%), Space: 14.8 MB (85.89%) - LeetHub --- .../0021-merge-two-sorted-lists.cpp | 43 +++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp b/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp index f0073f10..c1682516 100644 --- a/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp +++ b/0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp @@ -10,34 +10,23 @@ */ class Solution { public: - ListNode* mergeTwoLists(ListNode* node1, ListNode* node2) { + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { - ListNode* dummy = new ListNode(); - ListNode* temp = dummy; - - while(node1 and node2) - { - if(node1->val <= node2->val) - { - temp->next = node1; - node1 = node1->next; - } - else - { - temp->next = node2; - node2 = node2->next; - } - temp = temp->next; - } - - if(node1) - temp->next = node1; - if(node2) - temp->next = node2; - - - return dummy->next; - + if(!list1) + return list2; + if(!list2) + return list1; + if(list1->val < list2->val) + { + list1->next = mergeTwoLists(list1->next,list2); + return list1; + } + else + { + list2 ->next = mergeTwoLists(list1,list2->next); + return list2; + } + } }; \ No newline at end of file From 64ff80c5e70f97e0cc635bca89fde2ef565c0911 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Mar 2023 19:42:51 +0530 Subject: [PATCH 0588/3167] Create README - LeetHub --- 0023-merge-k-sorted-lists/README.md | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0023-merge-k-sorted-lists/README.md diff --git a/0023-merge-k-sorted-lists/README.md b/0023-merge-k-sorted-lists/README.md new file mode 100644 index 00000000..1f0df1fb --- /dev/null +++ b/0023-merge-k-sorted-lists/README.md @@ -0,0 +1,43 @@ +

23. Merge k Sorted Lists

Hard


You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.

+ +

Merge all the linked-lists into one sorted linked-list and return it.

+ +

 

+

Example 1:

+ +
Input: lists = [[1,4,5],[1,3,4],[2,6]]
+Output: [1,1,2,3,4,4,5,6]
+Explanation: The linked-lists are:
+[
+  1->4->5,
+  1->3->4,
+  2->6
+]
+merging them into one sorted list:
+1->1->2->3->4->4->5->6
+
+ +

Example 2:

+ +
Input: lists = []
+Output: []
+
+ +

Example 3:

+ +
Input: lists = [[]]
+Output: []
+
+ +

 

+

Constraints:

+ +
    +
  • k == lists.length
  • +
  • 0 <= k <= 104
  • +
  • 0 <= lists[i].length <= 500
  • +
  • -104 <= lists[i][j] <= 104
  • +
  • lists[i] is sorted in ascending order.
  • +
  • The sum of lists[i].length will not exceed 104.
  • +
+
\ No newline at end of file From 3f9e92cd0fc413ea793c9a0e580a36a174313531 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Mar 2023 19:42:54 +0530 Subject: [PATCH 0589/3167] Time: 25 ms (68.77%), Space: 13.8 MB (29.59%) - LeetHub --- .../0023-merge-k-sorted-lists.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp diff --git a/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp b/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp new file mode 100644 index 00000000..cb3677bc --- /dev/null +++ b/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp @@ -0,0 +1,40 @@ +/** + * 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* mergeKLists(vector& lists) { + + priority_queue,greater> pq; + + for(auto itr : lists) + { + while(itr) + { + pq.push(itr->val); + itr = itr->next; + } + } + + ListNode *ptr = new ListNode(); + ListNode *ans = ptr; + while(!pq.empty()) + { + ListNode* temp = new ListNode(pq.top()); + ans->next = temp; + ans = ans->next; + pq.pop(); + } + + return ptr->next; + + + } +}; \ No newline at end of file From f952d21efc64168e27e1cf4e18e484e828ad8091 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Mar 2023 19:52:11 +0530 Subject: [PATCH 0590/3167] Attach NOTES - LeetHub --- 0023-merge-k-sorted-lists/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0023-merge-k-sorted-lists/NOTES.md diff --git a/0023-merge-k-sorted-lists/NOTES.md b/0023-merge-k-sorted-lists/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0023-merge-k-sorted-lists/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5d5dbdccb589a3bd7406802d14f0a8c355acc265 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 Mar 2023 19:52:13 +0530 Subject: [PATCH 0591/3167] Time: 256 ms (16.77%), Space: 13.4 MB (46.15%) - LeetHub --- .../0023-merge-k-sorted-lists.cpp | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp b/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp index cb3677bc..a4abcfe9 100644 --- a/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp +++ b/0023-merge-k-sorted-lists/0023-merge-k-sorted-lists.cpp @@ -10,31 +10,37 @@ */ class Solution { public: - ListNode* mergeKLists(vector& lists) { - - priority_queue,greater> pq; + + ListNode* merge(ListNode* l1, ListNode* l2) + { + if(!l1) + return l2; + if(!l2) + return l1; - for(auto itr : lists) + if(l1->val < l2->val) { - while(itr) - { - pq.push(itr->val); - itr = itr->next; - } + l1->next = merge(l1->next,l2); + return l1; } - - ListNode *ptr = new ListNode(); - ListNode *ans = ptr; - while(!pq.empty()) + else { - ListNode* temp = new ListNode(pq.top()); - ans->next = temp; - ans = ans->next; - pq.pop(); + l2->next = merge(l1,l2->next); + return l2; } + } + + ListNode* mergeKLists(vector& lists) { - return ptr->next; + if(!lists.size()) + return nullptr; + ListNode* head = lists[0]; + for(int i = 1; i Date: Mon, 13 Mar 2023 07:46:24 +0530 Subject: [PATCH 0592/3167] Create README - LeetHub --- 0101-symmetric-tree/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0101-symmetric-tree/README.md diff --git a/0101-symmetric-tree/README.md b/0101-symmetric-tree/README.md new file mode 100644 index 00000000..a4b74c2d --- /dev/null +++ b/0101-symmetric-tree/README.md @@ -0,0 +1,25 @@ +

101. Symmetric Tree

Easy


Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

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

 

+Follow up: Could you solve it both recursively and iteratively?
\ No newline at end of file From d712a6943928e6267a5f48d7eab567fa37e0b3d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 07:46:26 +0530 Subject: [PATCH 0593/3167] Time: 7 ms (53.98%), Space: 16.2 MB (96.56%) - LeetHub --- 0101-symmetric-tree/0101-symmetric-tree.cpp | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0101-symmetric-tree/0101-symmetric-tree.cpp diff --git a/0101-symmetric-tree/0101-symmetric-tree.cpp b/0101-symmetric-tree/0101-symmetric-tree.cpp new file mode 100644 index 00000000..035387d7 --- /dev/null +++ b/0101-symmetric-tree/0101-symmetric-tree.cpp @@ -0,0 +1,40 @@ +/** + * 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* root1, TreeNode* root2) + { + if(root1 and root2) + { + bool one = helper(root1->left, root2->right); + bool two = helper(root1->right,root2->left); + + if(root1->val == root2->val and one and two) + return true; + else + return false; + } + else if(!root1 and !root2) + return true; + else + return false; + } + + + bool isSymmetric(TreeNode* root) { + + if(root == nullptr) + return true; + return helper(root->left, root->right); + } +}; \ No newline at end of file From 51f3c75ec034c3e2f83ed3c2a63a528cccac2003 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 07:51:38 +0530 Subject: [PATCH 0594/3167] Attach NOTES - LeetHub --- 0101-symmetric-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0101-symmetric-tree/NOTES.md diff --git a/0101-symmetric-tree/NOTES.md b/0101-symmetric-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0101-symmetric-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1b5818f8a2466ba88fd38dc2e0cca0ff5b09ebf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 07:51:40 +0530 Subject: [PATCH 0595/3167] Time: 7 ms (53.98%), Space: 16.2 MB (96.56%) - LeetHub From bf9df0bf25bb161fffcdb76a478082cca8f8fde6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 07:59:16 +0530 Subject: [PATCH 0596/3167] Attach NOTES - LeetHub From ad6acb5112eec96ae38b0aeeb0ef48557ce0c0e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 07:59:18 +0530 Subject: [PATCH 0597/3167] Time: 8 ms (37.04%), Space: 16.8 MB (8.51%) - LeetHub --- 0101-symmetric-tree/0101-symmetric-tree.cpp | 44 +++++++++++---------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/0101-symmetric-tree/0101-symmetric-tree.cpp b/0101-symmetric-tree/0101-symmetric-tree.cpp index 035387d7..a623ddc8 100644 --- a/0101-symmetric-tree/0101-symmetric-tree.cpp +++ b/0101-symmetric-tree/0101-symmetric-tree.cpp @@ -11,30 +11,32 @@ */ class Solution { public: - - bool helper(TreeNode* root1, TreeNode* root2) - { - if(root1 and root2) + bool isSymmetric(TreeNode* root) { + if(root == nullptr) + return true; + queue q1, q2; + q1.push(root->left); + q2.push(root->right); + + while(!q1.empty() and !q2.empty()) { - bool one = helper(root1->left, root2->right); - bool two = helper(root1->right,root2->left); + TreeNode*curr1 = q1.front(); + TreeNode*curr2 = q2.front(); + q1.pop(); + q2.pop(); - if(root1->val == root2->val and one and two) - return true; - else + if(!curr1 and !curr2) + continue; + if(!curr1 or !curr2) + return false; + if(curr1->val != curr2->val) return false; + + q1.push(curr1->left); + q1.push(curr1->right); + q2.push(curr2->right); + q2.push(curr2->left); } - else if(!root1 and !root2) - return true; - else - return false; - } - - - bool isSymmetric(TreeNode* root) { - - if(root == nullptr) - return true; - return helper(root->left, root->right); + return true; } }; \ No newline at end of file From 03d2f2d6ba43b75b8f97ad7a7ae538ccb9d0a009 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:19:39 +0530 Subject: [PATCH 0598/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2342-max-sum-of-a-pair-with-equal-sum-of-digits/README.md diff --git a/2342-max-sum-of-a-pair-with-equal-sum-of-digits/README.md b/2342-max-sum-of-a-pair-with-equal-sum-of-digits/README.md new file mode 100644 index 00000000..8c107197 --- /dev/null +++ b/2342-max-sum-of-a-pair-with-equal-sum-of-digits/README.md @@ -0,0 +1,30 @@ +

2342. Max Sum of a Pair With Equal Sum of Digits

Medium


You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].

+ +

Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy the conditions.

+ +

 

+

Example 1:

+ +
Input: nums = [18,43,36,13,7]
+Output: 54
+Explanation: The pairs (i, j) that satisfy the conditions are:
+- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
+- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
+So the maximum sum that we can obtain is 54.
+
+ +

Example 2:

+ +
Input: nums = [10,12,19,14]
+Output: -1
+Explanation: There are no two numbers that satisfy the conditions, so we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 67c5be4fc6f039355e904d37d45dd9ab6d7d45d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:19:39 +0530 Subject: [PATCH 0599/3167] Attach NOTES - LeetHub --- 2342-max-sum-of-a-pair-with-equal-sum-of-digits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2342-max-sum-of-a-pair-with-equal-sum-of-digits/NOTES.md diff --git a/2342-max-sum-of-a-pair-with-equal-sum-of-digits/NOTES.md b/2342-max-sum-of-a-pair-with-equal-sum-of-digits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2342-max-sum-of-a-pair-with-equal-sum-of-digits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 864d46aa57236b7c3580c82bec007f6138137589 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:19:44 +0530 Subject: [PATCH 0600/3167] Time: 273 ms (29.05%), Space: 82.5 MB (5.03%) - LeetHub --- ...sum-of-a-pair-with-equal-sum-of-digits.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2342-max-sum-of-a-pair-with-equal-sum-of-digits/2342-max-sum-of-a-pair-with-equal-sum-of-digits.cpp diff --git a/2342-max-sum-of-a-pair-with-equal-sum-of-digits/2342-max-sum-of-a-pair-with-equal-sum-of-digits.cpp b/2342-max-sum-of-a-pair-with-equal-sum-of-digits/2342-max-sum-of-a-pair-with-equal-sum-of-digits.cpp new file mode 100644 index 00000000..63e6b916 --- /dev/null +++ b/2342-max-sum-of-a-pair-with-equal-sum-of-digits/2342-max-sum-of-a-pair-with-equal-sum-of-digits.cpp @@ -0,0 +1,41 @@ +#define ll long long int +class Solution { +public: + + ll sum (int num) + { + ll s = 0; + while(num > 0) + { + s += (num%10); + num /= 10; + } + return s; + } + + int maximumSum(vector& nums) { + + vector> vec; + int n = nums.size(); + + for(auto &n : nums) + { + ll s = sum(n); + vec.push_back({s,n}); + } + + sort(vec.begin(),vec.end()); + + ll ans = -1; + for(int i = 1; i < n; ++i) + { + if(vec[i].first == vec[i-1].first) + { + ll currSum = (ll)vec[i].second + vec[i-1].second; + ans = max(ans,currSum); + } + } + + return ans; + } +}; \ No newline at end of file From 5a19cb4984086b4e77fabbb497b5b7fc2a14c006 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 19:07:00 +0530 Subject: [PATCH 0601/3167] Attach NOTES - LeetHub --- 2343-query-kth-smallest-trimmed-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2343-query-kth-smallest-trimmed-number/NOTES.md diff --git a/2343-query-kth-smallest-trimmed-number/NOTES.md b/2343-query-kth-smallest-trimmed-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2343-query-kth-smallest-trimmed-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4e06cedaf1795bfe98a9f0c379cf0f00883e34a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Mar 2023 19:07:03 +0530 Subject: [PATCH 0602/3167] Time: 1310 ms (46.19%), Space: 372.7 MB (65.92%) - LeetHub --- ...2343-query-kth-smallest-trimmed-number.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2343-query-kth-smallest-trimmed-number/2343-query-kth-smallest-trimmed-number.cpp diff --git a/2343-query-kth-smallest-trimmed-number/2343-query-kth-smallest-trimmed-number.cpp b/2343-query-kth-smallest-trimmed-number/2343-query-kth-smallest-trimmed-number.cpp new file mode 100644 index 00000000..e86673fa --- /dev/null +++ b/2343-query-kth-smallest-trimmed-number/2343-query-kth-smallest-trimmed-number.cpp @@ -0,0 +1,44 @@ +#define ll long long int +class Solution { +public: + + vector smallestTrimmedNumbers(vector& nums, vector>& queries) { + + int n = nums.size(); + vector ans; + + for(auto q : queries) + { + int k = q[0]; + int trim = q[1]; + + vector> vp; // string, idx + int need = nums[0].size() - trim; + + for(int i = 0; i < n; ++i) + { + if(nums[i].size() < trim) + break; + + vp.push_back({nums[i].substr(need),i}); + } + + // debug + // for(auto itr : vp) + // cout<& a, const pair& b){ + if(a.first == b.first) + return a.second < b.second; + return a.first < b.first; + }); + + ans.push_back(vp[k-1].second); + } + + return ans; + + } +}; \ No newline at end of file From a89d0567fa11bef3507f58aa1f687941f37065c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:24:57 +0530 Subject: [PATCH 0603/3167] Create README - LeetHub --- 0129-sum-root-to-leaf-numbers/README.md | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0129-sum-root-to-leaf-numbers/README.md diff --git a/0129-sum-root-to-leaf-numbers/README.md b/0129-sum-root-to-leaf-numbers/README.md new file mode 100644 index 00000000..abefe7fb --- /dev/null +++ b/0129-sum-root-to-leaf-numbers/README.md @@ -0,0 +1,43 @@ +

129. Sum Root to Leaf Numbers

Medium


You are given the root of a binary tree containing digits from 0 to 9 only.

+ +

Each root-to-leaf path in the tree represents a number.

+ +
    +
  • For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
  • +
+ +

Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.

+ +

A leaf node is a node with no children.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3]
+Output: 25
+Explanation:
+The root-to-leaf path 1->2 represents the number 12.
+The root-to-leaf path 1->3 represents the number 13.
+Therefore, sum = 12 + 13 = 25.
+
+ +

Example 2:

+ +
Input: root = [4,9,0,5,1]
+Output: 1026
+Explanation:
+The root-to-leaf path 4->9->5 represents the number 495.
+The root-to-leaf path 4->9->1 represents the number 491.
+The root-to-leaf path 4->0 represents the number 40.
+Therefore, sum = 495 + 491 + 40 = 1026.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 9
  • +
  • The depth of the tree will not exceed 10.
  • +
+
\ No newline at end of file From bf2124b5cdeee764a7dcb8ecce1e421582355d04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:25:01 +0530 Subject: [PATCH 0604/3167] Time: 0 ms (100.00%), Space: 9.8 MB (12.22%) - LeetHub --- .../0129-sum-root-to-leaf-numbers.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp diff --git a/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp b/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp new file mode 100644 index 00000000..32ca8177 --- /dev/null +++ b/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.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) {} + * }; + */ + +#define ll long long int +class Solution { +public: + vector ans; + + void helper(TreeNode* root,string str) + { + if(!root) + return; + + str += to_string(root->val); + + if(!root->left and !root->right) + { + ans.push_back(str); + if(!str.empty()) + str.pop_back(); + } + + helper(root->left,str); + helper(root->right,str); + + } + + + int sumNumbers(TreeNode* root) { + + if(!root) + return 0; + + helper(root,""); + + ll sum = 0; + for(auto &itr : ans) + sum += stoll(itr); + + return sum; + } +}; \ No newline at end of file From c0d060bbc7cf340caf8b68fe9c5c07bda8583b84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:28:10 +0530 Subject: [PATCH 0605/3167] Attach NOTES - LeetHub --- 0129-sum-root-to-leaf-numbers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0129-sum-root-to-leaf-numbers/NOTES.md diff --git a/0129-sum-root-to-leaf-numbers/NOTES.md b/0129-sum-root-to-leaf-numbers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0129-sum-root-to-leaf-numbers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5a8c3b0951497008ecea27e9cd8190ef43c9af47 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:28:13 +0530 Subject: [PATCH 0606/3167] Time: 0 ms (100.00%), Space: 9.8 MB (12.22%) - LeetHub From e8519a59105a7d85fd354fa590ce95da1b827dab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:37:40 +0530 Subject: [PATCH 0607/3167] Attach NOTES - LeetHub From 975b1a6935af3b64e4fbcc863c6089898ef4d0e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:37:43 +0530 Subject: [PATCH 0608/3167] Time: 3 ms (62.17%), Space: 9.1 MB (98.81%) - LeetHub --- .../0129-sum-root-to-leaf-numbers.cpp | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp b/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp index 32ca8177..9e2a5664 100644 --- a/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp +++ b/0129-sum-root-to-leaf-numbers/0129-sum-root-to-leaf-numbers.cpp @@ -9,43 +9,33 @@ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ - -#define ll long long int class Solution { public: - vector ans; - void helper(TreeNode* root,string str) + + void sumToLeaf(TreeNode* root, int val, int&res) { if(!root) return; - - str += to_string(root->val); + int curr = val * 10 + root->val; if(!root->left and !root->right) - { - ans.push_back(str); - if(!str.empty()) - str.pop_back(); - } - - helper(root->left,str); - helper(root->right,str); + res += curr; + sumToLeaf(root->left,curr, res); + sumToLeaf(root->right,curr,res); } - int sumNumbers(TreeNode* root) { if(!root) return 0; - helper(root,""); + int res = 0; + + sumToLeaf(root,0,res); - ll sum = 0; - for(auto &itr : ans) - sum += stoll(itr); + return res; - return sum; } }; \ No newline at end of file From 616c10b5865331b13f1d1f6355c0e23dcd19be62 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 19:20:54 +0530 Subject: [PATCH 0609/3167] Attach NOTES - LeetHub From a613971ec1fd9b0e3b2a62334d592a2027bd4f2d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 19:20:57 +0530 Subject: [PATCH 0610/3167] Time: 539 ms (46.43%), Space: 10.3 MB (63.53%) - LeetHub --- 0312-burst-balloons/0312-burst-balloons.cpp | 38 ++++++--------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/0312-burst-balloons/0312-burst-balloons.cpp b/0312-burst-balloons/0312-burst-balloons.cpp index 6ef419c4..3ec8c44c 100644 --- a/0312-burst-balloons/0312-burst-balloons.cpp +++ b/0312-burst-balloons/0312-burst-balloons.cpp @@ -1,50 +1,32 @@ -#define ll long long int class Solution { public: - int helper(int i , int j, vector& nums, vector>& dp) + int helper(int i, int j, vector& nums, vector >& dp) { if(i > j) return 0; if(dp[i][j] != -1) return dp[i][j]; - int ans = INT_MIN; + int maxi = INT_MIN; for(int idx = i; idx <= j; ++idx) { - int coins = nums[i-1] * nums[idx] * nums[j+1] + helper(i, idx-1, nums, dp) + helper(idx+1, j,nums, dp); - ans = max(ans,coins); + int cost = nums[i-1] * nums[idx] * nums[j+1] + helper(i, idx - 1, nums, dp) + helper(idx + 1, j, nums , dp); + maxi = max(maxi, cost); } - return dp[i][j] = ans; + + return dp[i][j] = maxi; } + int maxCoins(vector& nums) { int n = nums.size(); - - nums.insert(nums.begin(),1); nums.push_back(1); + nums.insert(nums.begin(),1); - // vector> dp(n+2,vector(n+2,-1)); - vector>dp(n+2,vector(n+2,0)); - // return helper(1,n, nums, dp); + vector >dp(n+1,vector(n+1,-1)); + return helper(1,n, nums, dp); - for(int i = n; i >= 1; --i) - { - for(int j = 1; j<=n; ++j) - { - if(i > j) - continue; - ll ans = INT_MIN; - - for(int idx = i; idx <= j; ++idx) - { - ll cost = (ll)nums[i-1] * nums[idx] * nums[j+1] + dp[i][idx-1] + dp[idx+1][j]; - ans = max(ans, cost); - } - dp[i][j] = ans; - } - } - return dp[1][n]; } }; \ No newline at end of file From 6bba4b7dab290f5e80bf80573c9a4dbca92a1ddd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 19:25:23 +0530 Subject: [PATCH 0611/3167] Attach NOTES - LeetHub From 58ccaec6c99b28111bc05f7373a04c3ea50b6ca6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Mar 2023 19:25:27 +0530 Subject: [PATCH 0612/3167] Time: 364 ms (78.83%), Space: 10.2 MB (82.68%) - LeetHub --- 0312-burst-balloons/0312-burst-balloons.cpp | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/0312-burst-balloons/0312-burst-balloons.cpp b/0312-burst-balloons/0312-burst-balloons.cpp index 3ec8c44c..17a0993a 100644 --- a/0312-burst-balloons/0312-burst-balloons.cpp +++ b/0312-burst-balloons/0312-burst-balloons.cpp @@ -25,8 +25,28 @@ class Solution { nums.insert(nums.begin(),1); - vector >dp(n+1,vector(n+1,-1)); - return helper(1,n, nums, dp); + // vector >dp(n+1,vector(n+1,-1)); + // return helper(1,n, nums, dp); + vector> dp(n+2,vector(n+2,0)); + + for(int i = n; i >= 1; --i) + { + for(int j = 1; j<=n; ++j) + { + if(i > j) + continue; + int maxi = INT_MIN; + for(int idx = i; idx <= j; ++idx) + { + int cost = nums[i-1] * nums[idx] * nums[j+1] + dp[i][idx-1] + dp[idx+1][j]; + maxi = max(maxi, cost); + } + + dp[i][j] = maxi; + } + } + + return dp[1][n]; } }; \ No newline at end of file From 5710d9be1621c10533bd681d8cc66206edcbfc38 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:42:18 +0530 Subject: [PATCH 0613/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0105-construct-binary-tree-from-preorder-and-inorder-traversal/README.md diff --git a/0105-construct-binary-tree-from-preorder-and-inorder-traversal/README.md b/0105-construct-binary-tree-from-preorder-and-inorder-traversal/README.md new file mode 100644 index 00000000..c8bfae48 --- /dev/null +++ b/0105-construct-binary-tree-from-preorder-and-inorder-traversal/README.md @@ -0,0 +1,28 @@ +

105. Construct Binary Tree from Preorder and Inorder Traversal

Medium


Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

+ +

 

+

Example 1:

+ +
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
+Output: [3,9,20,null,null,15,7]
+
+ +

Example 2:

+ +
Input: preorder = [-1], inorder = [-1]
+Output: [-1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= preorder.length <= 3000
  • +
  • inorder.length == preorder.length
  • +
  • -3000 <= preorder[i], inorder[i] <= 3000
  • +
  • preorder and inorder consist of unique values.
  • +
  • Each value of inorder also appears in preorder.
  • +
  • preorder is guaranteed to be the preorder traversal of the tree.
  • +
  • inorder is guaranteed to be the inorder traversal of the tree.
  • +
+
\ No newline at end of file From d41c5cb7dd2b32e43214125b502725c8dc96b988 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:42:22 +0530 Subject: [PATCH 0614/3167] Time: 28 ms (51.48%), Space: 26.3 MB (58.89%) - LeetHub --- ...ee-from-preorder-and-inorder-traversal.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0105-construct-binary-tree-from-preorder-and-inorder-traversal/0105-construct-binary-tree-from-preorder-and-inorder-traversal.cpp diff --git a/0105-construct-binary-tree-from-preorder-and-inorder-traversal/0105-construct-binary-tree-from-preorder-and-inorder-traversal.cpp b/0105-construct-binary-tree-from-preorder-and-inorder-traversal/0105-construct-binary-tree-from-preorder-and-inorder-traversal.cpp new file mode 100644 index 00000000..10e7322d --- /dev/null +++ b/0105-construct-binary-tree-from-preorder-and-inorder-traversal/0105-construct-binary-tree-from-preorder-and-inorder-traversal.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: + + TreeNode* build(int inStart, int preStart,int inEnd, int preEnd, vector& inorder, vector& preorder, unordered_map& mp) + { + if(inStart > inEnd or preStart > preEnd) + return nullptr; + + TreeNode* root = new TreeNode(preorder[preStart]); + + int idx = mp[root->val]; + int inLeft = idx - inStart; + + root->left = build(inStart, preStart+1, idx - 1, preStart + inLeft,inorder, preorder, mp); + root->right = build(idx+1,preStart + inLeft + 1,inEnd, preEnd, inorder, preorder, mp); + + return root; + } + + + TreeNode* buildTree(vector& preorder, vector& inorder) { + + unordered_map mp; + int n = inorder.size(),m = preorder.size(); + + for(int i = 0; i < n; ++i) + mp.insert({inorder[i],i}); + + TreeNode* root = build(0,0,n-1,m-1,inorder,preorder,mp); + + return root; + + } +}; \ No newline at end of file From ad11e11313b88b6beea0591a2ec2749027244648 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Mar 2023 22:54:09 +0530 Subject: [PATCH 0615/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0106-construct-binary-tree-from-inorder-and-postorder-traversal/README.md diff --git a/0106-construct-binary-tree-from-inorder-and-postorder-traversal/README.md b/0106-construct-binary-tree-from-inorder-and-postorder-traversal/README.md new file mode 100644 index 00000000..962949b0 --- /dev/null +++ b/0106-construct-binary-tree-from-inorder-and-postorder-traversal/README.md @@ -0,0 +1,28 @@ +

106. Construct Binary Tree from Inorder and Postorder Traversal

Medium


Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.

+ +

 

+

Example 1:

+ +
Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
+Output: [3,9,20,null,null,15,7]
+
+ +

Example 2:

+ +
Input: inorder = [-1], postorder = [-1]
+Output: [-1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= inorder.length <= 3000
  • +
  • postorder.length == inorder.length
  • +
  • -3000 <= inorder[i], postorder[i] <= 3000
  • +
  • inorder and postorder consist of unique values.
  • +
  • Each value of postorder also appears in inorder.
  • +
  • inorder is guaranteed to be the inorder traversal of the tree.
  • +
  • postorder is guaranteed to be the postorder traversal of the tree.
  • +
+
\ No newline at end of file From 968272192eef82294b370a7a0b31c72d19b91ed2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Mar 2023 22:48:00 +0530 Subject: [PATCH 0616/3167] Create README - LeetHub --- 1472-design-browser-history/README.md | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1472-design-browser-history/README.md diff --git a/1472-design-browser-history/README.md b/1472-design-browser-history/README.md new file mode 100644 index 00000000..8a2a83bd --- /dev/null +++ b/1472-design-browser-history/README.md @@ -0,0 +1,45 @@ +

1472. Design Browser History

Medium


You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps.

+ +

Implement the BrowserHistory class:

+ +
    +
  • BrowserHistory(string homepage) Initializes the object with the homepage of the browser.
  • +
  • void visit(string url) Visits url from the current page. It clears up all the forward history.
  • +
  • string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
  • +
  • string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.
  • +
+ +

 

+

Example:

+ +
Input:
+["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
+[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
+Output:
+[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
+
+Explanation:
+BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
+browserHistory.visit("google.com");       // You are in "leetcode.com". Visit "google.com"
+browserHistory.visit("facebook.com");     // You are in "google.com". Visit "facebook.com"
+browserHistory.visit("youtube.com");      // You are in "facebook.com". Visit "youtube.com"
+browserHistory.back(1);                   // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
+browserHistory.back(1);                   // You are in "facebook.com", move back to "google.com" return "google.com"
+browserHistory.forward(1);                // You are in "google.com", move forward to "facebook.com" return "facebook.com"
+browserHistory.visit("linkedin.com");     // You are in "facebook.com". Visit "linkedin.com"
+browserHistory.forward(2);                // You are in "linkedin.com", you cannot move forward any steps.
+browserHistory.back(2);                   // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
+browserHistory.back(7);                   // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= homepage.length <= 20
  • +
  • 1 <= url.length <= 20
  • +
  • 1 <= steps <= 100
  • +
  • homepage and url consist of  '.' or lower case English letters.
  • +
  • At most 5000 calls will be made to visit, back, and forward.
  • +
+
\ No newline at end of file From fdbe2907d06c9c15c884d23026b9d1783f396706 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Mar 2023 22:48:04 +0530 Subject: [PATCH 0617/3167] Time: 240 ms (14.47%), Space: 122.4 MB (7.00%) - LeetHub --- .../1472-design-browser-history.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1472-design-browser-history/1472-design-browser-history.cpp diff --git a/1472-design-browser-history/1472-design-browser-history.cpp b/1472-design-browser-history/1472-design-browser-history.cpp new file mode 100644 index 00000000..a657c004 --- /dev/null +++ b/1472-design-browser-history/1472-design-browser-history.cpp @@ -0,0 +1,41 @@ +class BrowserHistory { +public: + stack history; + stack future; + + BrowserHistory(string homepage) { + history.push(homepage); + future = stack(); + } + + void visit(string url) { + history.push(url); + future = stack(); + } + + string back(int steps) { + while(steps > 0 && history.size() > 1) { + future.push(history.top()); + history.pop(); + steps--; + } + return history.top(); + } + + string forward(int steps) { + while(steps > 0 && future.size() > 0) { + history.push(future.top()); + future.pop(); + steps--; + } + return history.top(); + } +}; + +/** + * Your BrowserHistory object will be instantiated and called as such: + * BrowserHistory* obj = new BrowserHistory(homepage); + * obj->visit(url); + * string param_2 = obj->back(steps); + * string param_3 = obj->forward(steps); + */ \ No newline at end of file From 1ff4de77730635894e0d9b72a44bd5e7b3b1155d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Mar 2023 23:43:08 +0530 Subject: [PATCH 0618/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0211-design-add-and-search-words-data-structure/README.md diff --git a/0211-design-add-and-search-words-data-structure/README.md b/0211-design-add-and-search-words-data-structure/README.md new file mode 100644 index 00000000..0a1f4b12 --- /dev/null +++ b/0211-design-add-and-search-words-data-structure/README.md @@ -0,0 +1,41 @@ +

211. Design Add and Search Words Data Structure

Medium


Design a data structure that supports adding new words and finding if a string matches any previously added string.

+ +

Implement the WordDictionary class:

+ +
    +
  • WordDictionary() Initializes the object.
  • +
  • void addWord(word) Adds word to the data structure, it can be matched later.
  • +
  • bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.
  • +
+ +

 

+

Example:

+ +
Input
+["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
+[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
+Output
+[null,null,null,null,false,true,true,true]
+
+Explanation
+WordDictionary wordDictionary = new WordDictionary();
+wordDictionary.addWord("bad");
+wordDictionary.addWord("dad");
+wordDictionary.addWord("mad");
+wordDictionary.search("pad"); // return False
+wordDictionary.search("bad"); // return True
+wordDictionary.search(".ad"); // return True
+wordDictionary.search("b.."); // return True
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word.length <= 25
  • +
  • word in addWord consists of lowercase English letters.
  • +
  • word in search consist of '.' or lowercase English letters.
  • +
  • There will be at most 3 dots in word for search queries.
  • +
  • At most 104 calls will be made to addWord and search.
  • +
+
\ No newline at end of file From ad7af997b0509b35af2957ec476ac584f8787665 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Mar 2023 23:43:11 +0530 Subject: [PATCH 0619/3167] Time: 1389 ms (58.42%), Space: 566.2 MB (40.08%) - LeetHub --- ...gn-add-and-search-words-data-structure.cpp | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 0211-design-add-and-search-words-data-structure/0211-design-add-and-search-words-data-structure.cpp diff --git a/0211-design-add-and-search-words-data-structure/0211-design-add-and-search-words-data-structure.cpp b/0211-design-add-and-search-words-data-structure/0211-design-add-and-search-words-data-structure.cpp new file mode 100644 index 00000000..a10276bc --- /dev/null +++ b/0211-design-add-and-search-words-data-structure/0211-design-add-and-search-words-data-structure.cpp @@ -0,0 +1,90 @@ +class Node{ + public: + Node* child[26] = {nullptr}; + bool isWord = false; + public: + 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; + } +}; + +class WordDictionary { + + private : + Node* root; + +public: + WordDictionary() { + root = new Node(); + } + + void addWord(string word) { + Node* temp = root; + for(auto ch : word) + { + if(!temp->containsKey(ch)) + temp->put(ch,new Node()); + temp = temp->get(ch); + } + temp->setEnd(); + } + + bool search(string word) { + Node* temp = root; + return helper(word, temp); + } + + bool helper(string word, Node* node){ + Node*temp = node; + for(int i = 0; icontainsKey(ch)) + return false; + temp = temp->get(ch); + } + else + { + bool found = false; + int j = 0; + for(;j<26; ++j) + { + if(temp->child[j]) + found |= helper(word.substr(i+1),temp->child[j]); + if(found) + return true; + } + if(j == 26) + return false; + + } + + } + return temp->isWord; + } +}; + +/** + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary* obj = new WordDictionary(); + * obj->addWord(word); + * bool param_2 = obj->search(word); + */ \ No newline at end of file From 8eedee83426003c2928f17f447050ef33e8ec13c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Mar 2023 22:22:13 +0530 Subject: [PATCH 0620/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2591-distribute-money-to-maximum-children/README.md diff --git a/2591-distribute-money-to-maximum-children/README.md b/2591-distribute-money-to-maximum-children/README.md new file mode 100644 index 00000000..df923f03 --- /dev/null +++ b/2591-distribute-money-to-maximum-children/README.md @@ -0,0 +1,40 @@ +

2591. Distribute Money to Maximum Children

Easy


You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the money to.

+ +

You have to distribute the money according to the following rules:

+ +
    +
  • All money must be distributed.
  • +
  • Everyone must receive at least 1 dollar.
  • +
  • Nobody receives 4 dollars.
  • +
+ +

Return the maximum number of children who may receive exactly 8 dollars if you distribute the money according to the aforementioned rules. If there is no way to distribute the money, return -1.

+ +

 

+

Example 1:

+ +
Input: money = 20, children = 3
+Output: 1
+Explanation: 
+The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
+- 8 dollars to the first child.
+- 9 dollars to the second child. 
+- 3 dollars to the third child.
+It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
+
+ +

Example 2:

+ +
Input: money = 16, children = 2
+Output: 2
+Explanation: Each child can be given 8 dollars.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= money <= 200
  • +
  • 2 <= children <= 30
  • +
+
\ No newline at end of file From 8fde3ffe237eddda749dc4c1ad93aa1f094966b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Mar 2023 22:22:16 +0530 Subject: [PATCH 0621/3167] Time: 0 ms (100.00%), Space: 6 MB (100.00%) - LeetHub --- ...1-distribute-money-to-maximum-children.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2591-distribute-money-to-maximum-children/2591-distribute-money-to-maximum-children.cpp diff --git a/2591-distribute-money-to-maximum-children/2591-distribute-money-to-maximum-children.cpp b/2591-distribute-money-to-maximum-children/2591-distribute-money-to-maximum-children.cpp new file mode 100644 index 00000000..04e0cb7e --- /dev/null +++ b/2591-distribute-money-to-maximum-children/2591-distribute-money-to-maximum-children.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int distMoney(int money, int children) { + + + if(money < children) + return -1; + + money -= children; + + if(money / 7 == children) + { + if(money % 7 == 0) + return children; + } + else if(money/7 == children-1) + { + if(money % 7 == 3) + return children-2; + } + + return min(children-1, money/7); + } +}; From 8e62041bccfb15f0092143209fe9cfd1e160875c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Mar 2023 22:24:39 +0530 Subject: [PATCH 0622/3167] Attach NOTES - LeetHub --- 2591-distribute-money-to-maximum-children/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2591-distribute-money-to-maximum-children/NOTES.md diff --git a/2591-distribute-money-to-maximum-children/NOTES.md b/2591-distribute-money-to-maximum-children/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2591-distribute-money-to-maximum-children/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 14212cccecd244d1ad81ee25e6df02c8cc8574fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Mar 2023 22:24:42 +0530 Subject: [PATCH 0623/3167] Time: 0 ms (100.00%), Space: 6 MB (100.00%) - LeetHub From b3c9f31fc2f64523e1e31a43ea5e119e12bd23bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Mar 2023 18:27:40 +0530 Subject: [PATCH 0624/3167] Attach NOTES - LeetHub From 5bcf999b0e50d428bc07c8cd4671f262bbf31eee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 22 Mar 2023 18:27:43 +0530 Subject: [PATCH 0625/3167] Time: 531 ms (53.70%), Space: 145.8 MB (18.16%) - LeetHub --- ...mum-score-of-a-path-between-two-cities.cpp | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp b/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp index cb2ce42a..e110cd1f 100644 --- a/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp +++ b/2492-minimum-score-of-a-path-between-two-cities/2492-minimum-score-of-a-path-between-two-cities.cpp @@ -1,13 +1,13 @@ class Solution { public: - void dfs(int sv, vector& visited, vector> adj[]) + void dfs(int sv, vector> adj[], vector& visited) { visited[sv] = true; for(auto itr : adj[sv]) { if(!visited[itr.first]) - dfs(itr.first,visited,adj); + dfs(itr.first,adj,visited); } } @@ -15,20 +15,21 @@ class Solution { vector> adj[n+1]; - for(int i = 0; i visited(n+1,false); + + dfs(1,adj,visited); int ans = INT_MAX; - dfs(1,visited,adj); - - for(int i = 0; i Date: Thu, 23 Mar 2023 18:22:18 +0530 Subject: [PATCH 0626/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1319-number-of-operations-to-make-network-connected/README.md diff --git a/1319-number-of-operations-to-make-network-connected/README.md b/1319-number-of-operations-to-make-network-connected/README.md new file mode 100644 index 00000000..380130be --- /dev/null +++ b/1319-number-of-operations-to-make-network-connected/README.md @@ -0,0 +1,40 @@ +

1319. Number of Operations to Make Network Connected

Medium


There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.

+ +

You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.

+ +

Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.

+ +

 

+

Example 1:

+ +
Input: n = 4, connections = [[0,1],[0,2],[1,2]]
+Output: 1
+Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
+
+ +

Example 2:

+ +
Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
+Output: 2
+
+ +

Example 3:

+ +
Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
+Output: -1
+Explanation: There are not enough cables.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= connections.length <= min(n * (n - 1) / 2, 105)
  • +
  • connections[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • There are no repeated connections.
  • +
  • No two computers are connected by more than one cable.
  • +
+
\ No newline at end of file From f0ee900f6e2084c1d6a7fdce516194bd38b910c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Mar 2023 18:22:19 +0530 Subject: [PATCH 0627/3167] Attach NOTES - LeetHub --- 1319-number-of-operations-to-make-network-connected/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1319-number-of-operations-to-make-network-connected/NOTES.md diff --git a/1319-number-of-operations-to-make-network-connected/NOTES.md b/1319-number-of-operations-to-make-network-connected/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1319-number-of-operations-to-make-network-connected/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0d937e64476e7e6b7973362f91beb4181e42789c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Mar 2023 18:22:22 +0530 Subject: [PATCH 0628/3167] Time: 157 ms (38.88%), Space: 54.7 MB (31.17%) - LeetHub --- ...f-operations-to-make-network-connected.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1319-number-of-operations-to-make-network-connected/1319-number-of-operations-to-make-network-connected.cpp diff --git a/1319-number-of-operations-to-make-network-connected/1319-number-of-operations-to-make-network-connected.cpp b/1319-number-of-operations-to-make-network-connected/1319-number-of-operations-to-make-network-connected.cpp new file mode 100644 index 00000000..cf1c3117 --- /dev/null +++ b/1319-number-of-operations-to-make-network-connected/1319-number-of-operations-to-make-network-connected.cpp @@ -0,0 +1,43 @@ +class Solution { +public: + + void dfs(int sv, vector& visited, vector adj[]){ + visited[sv] = true; + + for(auto itr : adj[sv]) + { + if(!visited[itr]) + dfs(itr, visited, adj); + } + } + + int makeConnected(int n, vector>& connections) { + + int edges = connections.size(); + + if(n-1 > edges) + return -1; + + vector adj[n+1]; + for(auto itr : connections) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + } + + vector visited(n+1,false); + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + { + dfs(i, visited, adj); + ++cnt; + } + } + + return cnt - 1; + + } +}; \ No newline at end of file From d6f1a9684a2a1f57505b927f981b15fcda496e13 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Mar 2023 18:23:07 +0530 Subject: [PATCH 0629/3167] Attach NOTES - LeetHub From f8383dfd8dece44bc04c359ba4e456eca7842287 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Mar 2023 18:23:12 +0530 Subject: [PATCH 0630/3167] Time: 157 ms (38.88%), Space: 54.7 MB (31.17%) - LeetHub From effe7c4928c4f4cd4e2969592fa36678bfd7f7a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Mar 2023 19:36:28 +0530 Subject: [PATCH 0631/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2596-check-knight-tour-configuration/README.md diff --git a/2596-check-knight-tour-configuration/README.md b/2596-check-knight-tour-configuration/README.md new file mode 100644 index 00000000..875f7d98 --- /dev/null +++ b/2596-check-knight-tour-configuration/README.md @@ -0,0 +1,33 @@ +

2596. Check Knight Tour Configuration

Medium


There is a knight on an n x n chessboard. In a valid configuration, the knight starts at the top-left cell of the board and visits every cell on the board exactly once.

+ +

You are given an n x n integer matrix grid consisting of distinct integers from the range [0, n * n - 1] where grid[row][col] indicates that the cell (row, col) is the grid[row][col]th cell that the knight visited. The moves are 0-indexed.

+ +

Return true if grid represents a valid configuration of the knight's movements or false otherwise.

+ +

Note that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell.

+ +

 

+

Example 1:

+ +
Input: grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
+Output: true
+Explanation: The above diagram represents the grid. It can be shown that it is a valid configuration.
+
+ +

Example 2:

+ +
Input: grid = [[0,3,6],[5,8,1],[2,7,4]]
+Output: false
+Explanation: The above diagram represents the grid. The 8th move of the knight is not valid considering its position after the 7th move.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 3 <= n <= 7
  • +
  • 0 <= grid[row][col] < n * n
  • +
  • All integers in grid are unique.
  • +
+
\ No newline at end of file From a480ef21a64e32ede06a66eee89b8bdc53b04884 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 Mar 2023 19:36:33 +0530 Subject: [PATCH 0632/3167] Time: 26 ms (24.11%), Space: 28 MB (56.04%) - LeetHub --- .../2596-check-knight-tour-configuration.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2596-check-knight-tour-configuration/2596-check-knight-tour-configuration.cpp diff --git a/2596-check-knight-tour-configuration/2596-check-knight-tour-configuration.cpp b/2596-check-knight-tour-configuration/2596-check-knight-tour-configuration.cpp new file mode 100644 index 00000000..d1c03e34 --- /dev/null +++ b/2596-check-knight-tour-configuration/2596-check-knight-tour-configuration.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + + bool isValid(int x, int y, int n) + { + return (x >= 0 and y >= 0 and x < n and y < n); + } + + bool checkValidGrid(vector>& grid) { + + int n = grid.size(); + + int start = 0; int end = (n * n) - 1 ; + + int currx = 0, curry = 0; + + vector dx = {-1,-2,-1,-2,1,2,1,2}; + vector dy = {-2,-1,+2,+1,-2,-1,2,1}; + + if(grid[0][0] != start) + return false; + + while(start < end) + { + bool ok = false; + ++start; + for(int i = 0; i < 8; ++i) + { + int newx = currx + dx[i]; + int newy = curry + dy[i]; + + if(isValid(newx,newy,n) and grid[newx][newy] == start) + { + currx = newx; + curry = newy; + ok = true; + break; + } + } + if(!ok) + return false; + } + return true; + } +}; \ No newline at end of file From 60fc0c62ef8b169edfd1f09da7fa5a12900342da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:08:37 +0530 Subject: [PATCH 0633/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md diff --git a/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md b/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md new file mode 100644 index 00000000..f62d845c --- /dev/null +++ b/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md @@ -0,0 +1,42 @@ +

1466. Reorder Routes to Make All Paths Lead to the City Zero

Medium


There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.

+ +

Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi.

+ +

This year, there will be a big event in the capital (city 0), and many people want to travel to this city.

+ +

Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.

+ +

It's guaranteed that each city can reach city 0 after reorder.

+ +

 

+

Example 1:

+ +
Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
+Output: 3
+Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).
+
+ +

Example 2:

+ +
Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
+Output: 2
+Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).
+
+ +

Example 3:

+ +
Input: n = 3, connections = [[1,0],[2,0]]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 5 * 104
  • +
  • connections.length == n - 1
  • +
  • connections[i].length == 2
  • +
  • 0 <= ai, bi <= n - 1
  • +
  • ai != bi
  • +
+
\ No newline at end of file From 131fa69d43e36cee55853a0f95976a4133011194 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:08:40 +0530 Subject: [PATCH 0634/3167] Time: 404 ms (66.04%), Space: 112.8 MB (60.03%) - LeetHub --- ...o-make-all-paths-lead-to-the-city-zero.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp diff --git a/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp b/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp new file mode 100644 index 00000000..f2ec64da --- /dev/null +++ b/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + void dfs(int sv, vector> adj[], vector& visited, int& cnt) + { + visited[sv] = true; + + for(auto itr : adj[sv]) + { + if(!visited[itr.first]) + { + if(itr.second == 1) + ++cnt; + dfs(itr.first,adj,visited,cnt); + } + } + } + + int minReorder(int n, vector>& connections) { + + vector> adj[n+1]; + + for(auto itr : connections) + { + adj[itr[0]].push_back({itr[1],1}); + adj[itr[1]].push_back({itr[0],0}); + } + + vector visited(n+1,false); + int cnt = 0 ; + dfs(0,adj,visited,cnt); + + return cnt; + } +}; \ No newline at end of file From 254a3d31f8602132d6c8b222d3203d65902be9da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:11:35 +0530 Subject: [PATCH 0635/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/NOTES.md diff --git a/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/NOTES.md b/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 999301c7b38fca437aa6e0bbbf6f1da54fcfd250 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:11:38 +0530 Subject: [PATCH 0636/3167] Time: 404 ms (66.04%), Space: 112.8 MB (60.03%) - LeetHub From 98460d27efc919433dc3d73baddcccebfbb8de99 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:17:05 +0530 Subject: [PATCH 0637/3167] Attach NOTES - LeetHub From 5fde21d5db2fc1fb606aea38edae305c5bcfb465 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:17:10 +0530 Subject: [PATCH 0638/3167] Time: 404 ms (66.04%), Space: 112.8 MB (60.03%) - LeetHub From 9e89987d812eac0740dcdf42d9f8426b19646c88 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 01:07:10 +0530 Subject: [PATCH 0639/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2597-the-number-of-beautiful-subsets/README.md diff --git a/2597-the-number-of-beautiful-subsets/README.md b/2597-the-number-of-beautiful-subsets/README.md new file mode 100644 index 00000000..1c16e7be --- /dev/null +++ b/2597-the-number-of-beautiful-subsets/README.md @@ -0,0 +1,33 @@ +

2597. The Number of Beautiful Subsets

Medium


You are given an array nums of positive integers and a positive integer k.

+ +

A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k.

+ +

Return the number of non-empty beautiful subsets of the array nums.

+ +

A subset of nums is an array that can be obtained by deleting some (possibly none) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.

+ +

 

+

Example 1:

+ +
Input: nums = [2,4,6], k = 2
+Output: 4
+Explanation: The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
+It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
+
+ +

Example 2:

+ +
Input: nums = [1], k = 1
+Output: 1
+Explanation: The beautiful subset of the array nums is [1].
+It can be proved that there is only 1 beautiful subset in the array [1].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 20
  • +
  • 1 <= nums[i], k <= 1000
  • +
+
\ No newline at end of file From f36aa3aa504bb55e4aa3aab870bb31fb03946e69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 01:07:13 +0530 Subject: [PATCH 0640/3167] Time: 1094 ms (21.57%), Space: 36.5 MB (20.24%) - LeetHub --- .../2597-the-number-of-beautiful-subsets.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2597-the-number-of-beautiful-subsets/2597-the-number-of-beautiful-subsets.cpp diff --git a/2597-the-number-of-beautiful-subsets/2597-the-number-of-beautiful-subsets.cpp b/2597-the-number-of-beautiful-subsets/2597-the-number-of-beautiful-subsets.cpp new file mode 100644 index 00000000..41dc5896 --- /dev/null +++ b/2597-the-number-of-beautiful-subsets/2597-the-number-of-beautiful-subsets.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + void helper(int idx, vector& nums, unordered_map& mp, int k, int& ans) + { + if(idx == nums.size()) + { + ++ans; + } + else + { + if(!mp[nums[idx] - k] and !mp[nums[idx] + k]) + { + ++mp[nums[idx]]; + helper(idx+1, nums,mp, k, ans); + --mp[nums[idx]]; + } + helper(idx+1,nums,mp,k,ans); + } + } + + int beautifulSubsets(vector& nums, int k) { + + int ans = 0; + unordered_map mp; + + helper(0,nums,mp,k,ans); + + return ans-1; + } +}; \ No newline at end of file From f4552fa17fc8030c54247b3a787615699efa4d6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 01:36:25 +0530 Subject: [PATCH 0641/3167] Create README - LeetHub --- .../README.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2598-smallest-missing-non-negative-integer-after-operations/README.md diff --git a/2598-smallest-missing-non-negative-integer-after-operations/README.md b/2598-smallest-missing-non-negative-integer-after-operations/README.md new file mode 100644 index 00000000..6075d8f1 --- /dev/null +++ b/2598-smallest-missing-non-negative-integer-after-operations/README.md @@ -0,0 +1,45 @@ +

2598. Smallest Missing Non-negative Integer After Operations

Medium


You are given a 0-indexed integer array nums and an integer value.

+ +

In one operation, you can add or subtract value from any element of nums.

+ +
    +
  • For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] to make nums = [-1,2,3].
  • +
+ +

The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.

+ +
    +
  • For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2.
  • +
+ +

Return the maximum MEX of nums after applying the mentioned operation any number of times.

+ +

 

+

Example 1:

+ +
Input: nums = [1,-10,7,13,6,8], value = 5
+Output: 4
+Explanation: One can achieve this result by applying the following operations:
+- Add value to nums[1] twice to make nums = [1,0,7,13,6,8]
+- Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]
+- Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]
+The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
+
+ +

Example 2:

+ +
Input: nums = [1,-10,7,13,6,8], value = 7
+Output: 2
+Explanation: One can achieve this result by applying the following operation:
+- subtract value from nums[2] once to make nums = [1,-10,0,13,6,8]
+The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length, value <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file From e15d37e4cb038f53f5fdb710d0b6789b252a5d9d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 01:36:28 +0530 Subject: [PATCH 0642/3167] Time: 455 ms (24.14%), Space: 122.4 MB (33.63%) - LeetHub --- ...-non-negative-integer-after-operations.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2598-smallest-missing-non-negative-integer-after-operations/2598-smallest-missing-non-negative-integer-after-operations.cpp diff --git a/2598-smallest-missing-non-negative-integer-after-operations/2598-smallest-missing-non-negative-integer-after-operations.cpp b/2598-smallest-missing-non-negative-integer-after-operations/2598-smallest-missing-non-negative-integer-after-operations.cpp new file mode 100644 index 00000000..43b61a9e --- /dev/null +++ b/2598-smallest-missing-non-negative-integer-after-operations/2598-smallest-missing-non-negative-integer-after-operations.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int findSmallestInteger(vector& nums, int value) { + + int n = nums.size(); + + map mp; + + for(auto itr : nums) + { + int val = itr%value; + if(val < 0) + ++mp[val + value]; + else + ++mp[val]; + } + + for(int i = 0; i<=n; ++i) + { + if(mp[i % value] > 0) + { + --mp[i%value]; + continue; + } + else + return i; + } + return n; + } +}; \ No newline at end of file From 5495a36be543ea54d61f775716bd99bed8ca0712 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 09:07:02 +0530 Subject: [PATCH 0643/3167] Attach NOTES - LeetHub From b31b75db6de3307f78c345ecbc6c2fc3362e2334 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 09:07:05 +0530 Subject: [PATCH 0644/3167] Time: 223 ms (23.54%), Space: 136.4 MB (22.37%) - LeetHub --- .../0131-palindrome-partitioning.cpp | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp b/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp index bc97d9ad..1087d98c 100644 --- a/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp +++ b/0131-palindrome-partitioning/0131-palindrome-partitioning.cpp @@ -1,44 +1,44 @@ class Solution { public: - bool isPalindrome(string str , int start, int end) + + bool isPalindrome(int start, int end, string& str) { while(start <= end) { - if(str[start++] == str[end--]) - { - continue; - } - return false; + if(str[start] != str[end]) + return false; + ++start,--end; } return true; } - void helper(int idx, string& s, vector ds, vector>& res) + void helper(int idx, vector ds, vector>& res, string& s) { if(idx == s.size()) { res.push_back(ds); - return ; + return; } - for(int i = idx; i> partition(string s) { vector ds; - vector > res; + vector> res; - helper(0,s, ds, res); + helper(0,ds,res,s); return res; From a2c7fd10b037523fb69b3dd2b982d518f7141259 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:25:06 +0530 Subject: [PATCH 0645/3167] Create README - LeetHub --- 0132-palindrome-partitioning-ii/README.md | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0132-palindrome-partitioning-ii/README.md diff --git a/0132-palindrome-partitioning-ii/README.md b/0132-palindrome-partitioning-ii/README.md new file mode 100644 index 00000000..9bdf229a --- /dev/null +++ b/0132-palindrome-partitioning-ii/README.md @@ -0,0 +1,32 @@ +

132. Palindrome Partitioning II

Hard


Given a string s, partition s such that every substring of the partition is a palindrome.

+ +

Return the minimum cuts needed for a palindrome partitioning of s.

+ +

 

+

Example 1:

+ +
Input: s = "aab"
+Output: 1
+Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
+
+ +

Example 2:

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

Example 3:

+ +
Input: s = "ab"
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 2000
  • +
  • s consists of lowercase English letters only.
  • +
+
\ No newline at end of file From cb59b0acfa039b7b85fea6deb2b40b963a07b008 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:25:09 +0530 Subject: [PATCH 0646/3167] Time: 545 ms (23.31%), Space: 6.7 MB (53.88%) - LeetHub --- .../0132-palindrome-partitioning-ii.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp diff --git a/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp b/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp new file mode 100644 index 00000000..7cb7fd49 --- /dev/null +++ b/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + + bool isPalindrome(int start, int end, string& s) + { + while(start <= end) + { + if(s[start] != s[end]) + return false; + ++start, --end; + } + return true; + } + + int helper(int idx, string& s,vector& dp) + { + if(idx == s.size()) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int ans = INT_MAX; + for(int i =idx ; i < s.size(); ++i) + { + if(isPalindrome(idx, i, s)) + { + int cost = 1 + helper(i+1,s,dp); + ans = min(ans,cost); + } + } + return dp[idx] = ans; + } + + int minCut(string s) { + + int n = s.size(); + + vector dp(n+1,-1); + + return helper(0,s,dp) - 1; + + } +}; \ No newline at end of file From 4d49c6aee5482d35d4fc12c7dbe7cbe7583b2943 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:35:19 +0530 Subject: [PATCH 0647/3167] Attach NOTES - LeetHub --- 0132-palindrome-partitioning-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0132-palindrome-partitioning-ii/NOTES.md diff --git a/0132-palindrome-partitioning-ii/NOTES.md b/0132-palindrome-partitioning-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0132-palindrome-partitioning-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 30b9c9919fd2b0c2d185dcdfbe70bf4181bca733 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:35:22 +0530 Subject: [PATCH 0648/3167] Time: 536 ms (25.34%), Space: 6.5 MB (76.26%) - LeetHub --- .../0132-palindrome-partitioning-ii.cpp | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp b/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp index 7cb7fd49..49eea343 100644 --- a/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp +++ b/0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp @@ -36,9 +36,26 @@ class Solution { int n = s.size(); - vector dp(n+1,-1); + // vector dp(n+1,-1); - return helper(0,s,dp) - 1; - + // return helper(0,s,dp) - 1; + + vector dp(n+1,0); + + for(int idx = n-1; idx>=0 ; --idx) + { + int ans = INT_MAX; + for(int i =idx ; i < n; ++i) + { + if(isPalindrome(idx, i, s)) + { + int cost = 1 + dp[i+1]; + ans = min(ans,cost); + } + } + dp[idx] = ans; + } + + return dp[0] - 1; } }; \ No newline at end of file From 6198fb7f54ec85b1497a2182270b5f4d87a7cb4a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 20:57:04 +0530 Subject: [PATCH 0649/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1043-partition-array-for-maximum-sum/README.md diff --git a/1043-partition-array-for-maximum-sum/README.md b/1043-partition-array-for-maximum-sum/README.md new file mode 100644 index 00000000..c0114e41 --- /dev/null +++ b/1043-partition-array-for-maximum-sum/README.md @@ -0,0 +1,33 @@ +

1043. Partition Array for Maximum Sum

Medium


Given an integer array arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.

+ +

Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: arr = [1,15,7,9,2,5,10], k = 3
+Output: 84
+Explanation: arr becomes [15,15,15,9,10,10,10]
+
+ +

Example 2:

+ +
Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
+Output: 83
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 500
  • +
  • 0 <= arr[i] <= 109
  • +
  • 1 <= k <= arr.length
  • +
+
\ No newline at end of file From ac8bcbdddead67dd9032c81e51202fafdcdf9c5f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 20:57:08 +0530 Subject: [PATCH 0650/3167] Time: 20 ms (53.20%), Space: 8.5 MB (76.37%) - LeetHub --- .../1043-partition-array-for-maximum-sum.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp diff --git a/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp b/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp new file mode 100644 index 00000000..cd4b2e57 --- /dev/null +++ b/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + int helper(int idx, int n, vector& arr, int k, vector& dp) + { + if(idx == n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int maxSum = INT_MIN, maxi = INT_MIN, len = 0; + for(int i = idx; i < min(idx + k, n); ++i) + { + ++len; + maxi = max(maxi, arr[i]); + + int sum = len * maxi + helper(i+1, n, arr, k, dp); + + maxSum = max(maxSum, sum); + } + return dp[idx] = maxSum; + } + + int maxSumAfterPartitioning(vector& arr, int k) { + + int n = arr.size(); + vector dp(n+1,-1); + return helper(0,n,arr,k,dp); + + } +}; \ No newline at end of file From c07cd93bf4d38ef869992d9c86385fe23e2fad93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 20:59:26 +0530 Subject: [PATCH 0651/3167] Attach NOTES - LeetHub --- 1043-partition-array-for-maximum-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1043-partition-array-for-maximum-sum/NOTES.md diff --git a/1043-partition-array-for-maximum-sum/NOTES.md b/1043-partition-array-for-maximum-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1043-partition-array-for-maximum-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 87640f37ced354d46435d439566b0dd5bf25b740 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 20:59:29 +0530 Subject: [PATCH 0652/3167] Time: 11 ms (95.17%), Space: 8.5 MB (76.37%) - LeetHub --- .../1043-partition-array-for-maximum-sum.cpp | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp b/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp index cd4b2e57..acd86493 100644 --- a/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp +++ b/1043-partition-array-for-maximum-sum/1043-partition-array-for-maximum-sum.cpp @@ -25,8 +25,27 @@ class Solution { int maxSumAfterPartitioning(vector& arr, int k) { int n = arr.size(); - vector dp(n+1,-1); - return helper(0,n,arr,k,dp); + // vector dp(n+1,-1); + // return helper(0,n,arr,k,dp); + vector dp(n+1,0); + dp[n] = 0; + + for(int idx = n-1; idx >= 0; --idx) + { + int maxSum = INT_MIN, maxi = INT_MIN, len = 0; + for(int i = idx; i < min(idx + k, n); ++i) + { + ++len; + maxi = max(maxi, arr[i]); + + int sum = len * maxi + dp[i+1]; + + maxSum = max(maxSum, sum); + } + dp[idx] = maxSum; + } + + return dp[0]; } }; \ No newline at end of file From a4f3db46a8b2357e855e7198e4fcbd29d2f27add Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 23:09:06 +0530 Subject: [PATCH 0653/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/README.md diff --git a/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/README.md b/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/README.md new file mode 100644 index 00000000..8f40f2cb --- /dev/null +++ b/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/README.md @@ -0,0 +1,33 @@ +

2316. Count Unreachable Pairs of Nodes in an Undirected Graph

Medium


You are given an integer n. There is an undirected graph with n nodes, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.

+ +

Return the number of pairs of different nodes that are unreachable from each other.

+ +

 

+

Example 1:

+ +
Input: n = 3, edges = [[0,1],[0,2],[1,2]]
+Output: 0
+Explanation: There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
+
+ +

Example 2:

+ +
Input: n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
+Output: 14
+Explanation: There are 14 pairs of nodes that are unreachable from each other:
+[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
+Therefore, we return 14.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 0 <= edges.length <= 2 * 105
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • There are no repeated edges.
  • +
+
\ No newline at end of file From c916abded809f63e7c690488bfa7785e64b2ec2b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Mar 2023 23:09:09 +0530 Subject: [PATCH 0654/3167] Time: 647 ms (60.06%), Space: 197.5 MB (50.61%) - LeetHub --- ...-pairs-of-nodes-in-an-undirected-graph.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.cpp diff --git a/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.cpp b/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.cpp new file mode 100644 index 00000000..0d489239 --- /dev/null +++ b/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + + + void dfs(int sv, vector adj[], vector& visited, long long& count) + { + visited[sv] = true; + ++count; + + for(auto itr : adj[sv]) + { + if(!visited[itr]) + { + dfs(itr,adj,visited,count); + } + } + } + + + long long countPairs(int n, vector>& edges) { + + vector adj[n+1]; + + for(auto itr : edges) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + } + + long long total = (long long)n*(n-1)/2; + vector visited(n+1,false); + for(int i = 0; i Date: Sun, 26 Mar 2023 19:11:09 +0530 Subject: [PATCH 0655/3167] Attach NOTES - LeetHub --- 0084-largest-rectangle-in-histogram/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0084-largest-rectangle-in-histogram/NOTES.md diff --git a/0084-largest-rectangle-in-histogram/NOTES.md b/0084-largest-rectangle-in-histogram/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0084-largest-rectangle-in-histogram/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0daf1a4ae2d58ad4591dcb1d689dcb8e1b261ca5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Mar 2023 19:11:11 +0530 Subject: [PATCH 0656/3167] Time: 208 ms (53.09%), Space: 81.4 MB (55.79%) - LeetHub --- .../0084-largest-rectangle-in-histogram.cpp | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp diff --git a/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp b/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp new file mode 100644 index 00000000..7affd388 --- /dev/null +++ b/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp @@ -0,0 +1,82 @@ +class Solution { +public: + int largestRectangleArea(vector& heights) { + + int n = heights.size(); + + vector leftSmall(n), rightSmall(n); + + stack st; + + for(int i = 0; i < n; ++i) + { + if(st.empty()) + { + st.push(i); + leftSmall[i] = 0; + } + else if(heights[st.top()] >= heights[i]) + { + while(!st.empty() and heights[st.top()] >= heights[i]) + st.pop(); + + if(st.empty()) + leftSmall[i] = 0; + else + leftSmall[i] = st.top() +1; + st.push(i); + } + else + { + leftSmall[i] = st.top() + 1; + st.push(i); + } + } + + // for(auto itr : leftSmall) + // cout<= 0; --i) + { + if(st.empty()) + { + st.push(i); + rightSmall[i] = n-1; + } + else if(heights[st.top()] >= heights[i]) + { + while(!st.empty() and heights[st.top()] >= heights[i]) + st.pop(); + if(st.empty()) + rightSmall[i] = n-1; + else + rightSmall[i] = st.top() - 1; + st.push(i); + } + else{ + rightSmall[i] = st.top() - 1; + st.push(i); + } + } + + + // for(auto itr : rightSmall) + // cout< Date: Sun, 26 Mar 2023 23:17:57 +0530 Subject: [PATCH 0657/3167] Create README - LeetHub --- 2360-longest-cycle-in-a-graph/README.md | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2360-longest-cycle-in-a-graph/README.md diff --git a/2360-longest-cycle-in-a-graph/README.md b/2360-longest-cycle-in-a-graph/README.md new file mode 100644 index 00000000..95d8be21 --- /dev/null +++ b/2360-longest-cycle-in-a-graph/README.md @@ -0,0 +1,34 @@ +

2360. Longest Cycle in a Graph

Hard


You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.

+ +

The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from node i, then edges[i] == -1.

+ +

Return the length of the longest cycle in the graph. If no cycle exists, return -1.

+ +

A cycle is a path that starts and ends at the same node.

+ +

 

+

Example 1:

+ +
Input: edges = [3,3,4,2,3]
+Output: 3
+Explanation: The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
+The length of this cycle is 3, so 3 is returned.
+
+ +

Example 2:

+ +
Input: edges = [2,-1,3,1]
+Output: -1
+Explanation: There are no cycles in this graph.
+
+ +

 

+

Constraints:

+ +
    +
  • n == edges.length
  • +
  • 2 <= n <= 105
  • +
  • -1 <= edges[i] < n
  • +
  • edges[i] != i
  • +
+
\ No newline at end of file From 9ef3a28d19f073d46193c88aaa92ea6c495828a4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Mar 2023 23:18:00 +0530 Subject: [PATCH 0658/3167] Time: 201 ms (73.09%), Space: 101.6 MB (61.58%) - LeetHub --- .../2360-longest-cycle-in-a-graph.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2360-longest-cycle-in-a-graph/2360-longest-cycle-in-a-graph.cpp diff --git a/2360-longest-cycle-in-a-graph/2360-longest-cycle-in-a-graph.cpp b/2360-longest-cycle-in-a-graph/2360-longest-cycle-in-a-graph.cpp new file mode 100644 index 00000000..c9776390 --- /dev/null +++ b/2360-longest-cycle-in-a-graph/2360-longest-cycle-in-a-graph.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int mx=-1; + void dfs(vector &ed , vector &pvis , vector &vis , int i , int j){ + if(pvis[i]){ + mx = max(mx , j - pvis[i]); + return; + } + if(!vis[i]){ + pvis[i] =j; j++; vis[i]=1; + if(ed[i]!=-1) dfs(ed , pvis , vis , ed[i],j); + } + pvis[i] = 0; + return; + } + + int longestCycle(vector& ed) { + vector vis(ed.size(),0) , pvis(ed.size(),0); + mx = -1; + for(int i=0;i Date: Mon, 27 Mar 2023 23:48:04 +0530 Subject: [PATCH 0659/3167] Create README - LeetHub --- 1463-cherry-pickup-ii/README.md | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1463-cherry-pickup-ii/README.md diff --git a/1463-cherry-pickup-ii/README.md b/1463-cherry-pickup-ii/README.md new file mode 100644 index 00000000..577af6c0 --- /dev/null +++ b/1463-cherry-pickup-ii/README.md @@ -0,0 +1,50 @@ +

1463. Cherry Pickup II

Hard


You are given a rows x cols matrix grid representing a field of cherries where grid[i][j] represents the number of cherries that you can collect from the (i, j) cell.

+ +

You have two robots that can collect cherries for you:

+ +
    +
  • Robot #1 is located at the top-left corner (0, 0), and
  • +
  • Robot #2 is located at the top-right corner (0, cols - 1).
  • +
+ +

Return the maximum number of cherries collection using both robots by following the rules below:

+ +
    +
  • From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1).
  • +
  • When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
  • +
  • When both robots stay in the same cell, only one takes the cherries.
  • +
  • Both robots cannot move outside of the grid at any moment.
  • +
  • Both robots should reach the bottom row in grid.
  • +
+ +

 

+

Example 1:

+ +
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
+Output: 24
+Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
+Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
+Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
+Total of cherries: 12 + 12 = 24.
+
+ +

Example 2:

+ +
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
+Output: 28
+Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
+Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
+Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
+Total of cherries: 17 + 11 = 28.
+
+ +

 

+

Constraints:

+ +
    +
  • rows == grid.length
  • +
  • cols == grid[i].length
  • +
  • 2 <= rows, cols <= 70
  • +
  • 0 <= grid[i][j] <= 100
  • +
+
\ No newline at end of file From 8a6ad103bb3c65a7fcb2f0afc5bc823b79a2cc97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Mar 2023 23:48:08 +0530 Subject: [PATCH 0660/3167] Time: 140 ms (60.45%), Space: 17 MB (27.46%) - LeetHub --- .../1463-cherry-pickup-ii.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1463-cherry-pickup-ii/1463-cherry-pickup-ii.cpp diff --git a/1463-cherry-pickup-ii/1463-cherry-pickup-ii.cpp b/1463-cherry-pickup-ii/1463-cherry-pickup-ii.cpp new file mode 100644 index 00000000..cfdaff76 --- /dev/null +++ b/1463-cherry-pickup-ii/1463-cherry-pickup-ii.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + + int helper(int i,int j1, int j2, int n, int m, vector>& grid, vector>>& dp) + { + if(j1 < 0 or j1 >= m or j2 < 0 or j2 >= m) + return -1e8; + + if(i == n - 1) + { + if(j1 == j2) + return dp[i][j1][j2] = grid[i][j1]; + else return dp[i][j1][j2] = grid[i][j1] + grid[i][j2]; + } + + if(dp[i][j1][j2] != -1) + return dp[i][j1][j2]; + + int ans = -1e8; + for(int d1 = -1 ; d1 <= +1; ++d1) + { + for(int d2 = -1; d2 <= +1; ++d2) + { + int value = 0; + if(j1 == j2) + value = grid[i][j1]; + else + value = grid[i][j1] + grid[i][j2]; + + value += helper(i+1,j1 + d1 , j2 + d2, n, m, grid, dp); + ans = max(ans, value); + + } + } + return dp[i][j1][j2] = ans; + } + + int cherryPickup(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + vector>> dp(n,vector>(m,vector(m,-1))); + + return helper(0,0,m-1,n,m, grid, dp); + } +}; \ No newline at end of file From 52b08da261b960d0c3021926083560db19e25625 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Mar 2023 00:11:37 +0530 Subject: [PATCH 0661/3167] Attach NOTES - LeetHub From d5df7ad3eb2c7d38fde5afe3110b882cb16e6281 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Mar 2023 00:11:40 +0530 Subject: [PATCH 0662/3167] Time: 164 ms (88.30%), Space: 75.3 MB (93.10%) - LeetHub --- .../0084-largest-rectangle-in-histogram.cpp | 75 +++---------------- 1 file changed, 11 insertions(+), 64 deletions(-) diff --git a/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp b/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp index 7affd388..c3a3f135 100644 --- a/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp +++ b/0084-largest-rectangle-in-histogram/0084-largest-rectangle-in-histogram.cpp @@ -3,80 +3,27 @@ class Solution { int largestRectangleArea(vector& heights) { int n = heights.size(); - - vector leftSmall(n), rightSmall(n); - stack st; + int ans = 0; - for(int i = 0; i < n; ++i) + for(int i = 0; i<=n; ++i) { - if(st.empty()) + while(!st.empty() and (i == n or heights[st.top()] >= heights[i])) { - st.push(i); - leftSmall[i] = 0; - } - else if(heights[st.top()] >= heights[i]) - { - while(!st.empty() and heights[st.top()] >= heights[i]) - st.pop(); + int height = heights[st.top()]; + st.pop(); + int width; if(st.empty()) - leftSmall[i] = 0; - else - leftSmall[i] = st.top() +1; - st.push(i); - } - else - { - leftSmall[i] = st.top() + 1; - st.push(i); - } - } - - // for(auto itr : leftSmall) - // cout<= 0; --i) - { - if(st.empty()) - { - st.push(i); - rightSmall[i] = n-1; - } - else if(heights[st.top()] >= heights[i]) - { - while(!st.empty() and heights[st.top()] >= heights[i]) - st.pop(); - if(st.empty()) - rightSmall[i] = n-1; + width = i; else - rightSmall[i] = st.top() - 1; - st.push(i); - } - else{ - rightSmall[i] = st.top() - 1; - st.push(i); + width = i - st.top() - 1; + + ans = max(ans, height * width); } - } - - - // for(auto itr : rightSmall) - // cout< Date: Tue, 28 Mar 2023 00:15:28 +0530 Subject: [PATCH 0663/3167] Create README - LeetHub --- 0085-maximal-rectangle/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0085-maximal-rectangle/README.md diff --git a/0085-maximal-rectangle/README.md b/0085-maximal-rectangle/README.md new file mode 100644 index 00000000..3dcfdf06 --- /dev/null +++ b/0085-maximal-rectangle/README.md @@ -0,0 +1,32 @@ +

85. Maximal Rectangle

Hard


Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

+ +

 

+

Example 1:

+ +
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
+Output: 6
+Explanation: The maximal rectangle is shown in the above picture.
+
+ +

Example 2:

+ +
Input: matrix = [["0"]]
+Output: 0
+
+ +

Example 3:

+ +
Input: matrix = [["1"]]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • rows == matrix.length
  • +
  • cols == matrix[i].length
  • +
  • 1 <= row, cols <= 200
  • +
  • matrix[i][j] is '0' or '1'.
  • +
+
\ No newline at end of file From a769906b98e58590faf12f7354ce0c59bfe6790a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Mar 2023 00:15:31 +0530 Subject: [PATCH 0664/3167] Time: 34 ms (97.70%), Space: 13.3 MB (73.58%) - LeetHub --- .../0085-maximal-rectangle.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0085-maximal-rectangle/0085-maximal-rectangle.cpp diff --git a/0085-maximal-rectangle/0085-maximal-rectangle.cpp b/0085-maximal-rectangle/0085-maximal-rectangle.cpp new file mode 100644 index 00000000..7d380ae0 --- /dev/null +++ b/0085-maximal-rectangle/0085-maximal-rectangle.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + + int largestRectangleInHistogram(vector& heights) + { + int n = heights.size(); + stack st; + int ans = 0; + + for(int i = 0; i<=n; ++i) + { + while(!st.empty() and (i == n or heights[st.top()] >= heights[i])) + { + int height = heights[st.top()]; + st.pop(); + + int width; + if(st.empty()) + width = i; + else + width = i - st.top() - 1; + + ans = max(ans, height * width); + } + st.push(i); + } + + return ans; + } + + int maximalRectangle(vector>& matrix) { + + int n = matrix.size(), m = matrix[0].size(); + vector dp(m,0); + int maxArea = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(matrix[i][j] == '1') + dp[j] += 1; + else + dp[j] = 0; + } + + int area = largestRectangleInHistogram(dp); + maxArea = max(area,maxArea); + } + return maxArea; + } +}; \ No newline at end of file From 5c5c4598bebd9b5ca95a17a69316e02e92d46b1d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Mar 2023 00:34:45 +0530 Subject: [PATCH 0665/3167] Attach NOTES - LeetHub From 0277009603328910f98a91af30bf4b8b3a6ba4a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Mar 2023 00:34:47 +0530 Subject: [PATCH 0666/3167] Time: 78 ms (50.40%), Space: 23.6 MB (78.50%) - LeetHub --- ...-count-square-submatrices-with-all-ones.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp index dcc6504a..d32f05b9 100644 --- a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp +++ b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp @@ -2,21 +2,19 @@ class Solution { public: int countSquares(vector>& matrix) { - int count = 0; int n = matrix.size(), m = matrix[0].size(); - - for(int i = 0; i 0 and j > 0 and matrix[i][j] > 0) - { - matrix[i][j] = min({matrix[i-1][j-1],matrix[i-1][j], matrix[i][j-1]}) + 1; - } + if(j > 0 and i > 0 and matrix[i][j] > 0) + matrix[i][j] = min({matrix[i-1][j],matrix[i][j-1],matrix[i-1][j-1]}) + 1; count += matrix[i][j]; } } + return count; + } -}; - +}; \ No newline at end of file From 22a1504a0e47cddda251c17df5c92d3aad934ad9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Mar 2023 18:04:45 +0530 Subject: [PATCH 0667/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2290-minimum-obstacle-removal-to-reach-corner/README.md diff --git a/2290-minimum-obstacle-removal-to-reach-corner/README.md b/2290-minimum-obstacle-removal-to-reach-corner/README.md new file mode 100644 index 00000000..6a6de649 --- /dev/null +++ b/2290-minimum-obstacle-removal-to-reach-corner/README.md @@ -0,0 +1,40 @@ +

2290. Minimum Obstacle Removal to Reach Corner

Hard


You are given a 0-indexed 2D integer array grid of size m x n. Each cell has one of two values:

+ +
    +
  • 0 represents an empty cell,
  • +
  • 1 represents an obstacle that may be removed.
  • +
+ +

You can move up, down, left, or right from and to an empty cell.

+ +

Return the minimum number of obstacles to remove so you can move from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1).

+ +

 

+

Example 1:

+ +
Input: grid = [[0,1,1],[1,1,0],[1,1,0]]
+Output: 2
+Explanation: We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).
+It can be shown that we need to remove at least 2 obstacles, so we return 2.
+Note that there may be other ways to remove 2 obstacles to create a path.
+
+ +

Example 2:

+ +
Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
+Output: 0
+Explanation: We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 105
  • +
  • 2 <= m * n <= 105
  • +
  • grid[i][j] is either 0 or 1.
  • +
  • grid[0][0] == grid[m - 1][n - 1] == 0
  • +
+
\ No newline at end of file From 7f4d1bdefb884da0424772a259803a4b68689253 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Mar 2023 18:04:45 +0530 Subject: [PATCH 0668/3167] Attach NOTES - LeetHub --- 2290-minimum-obstacle-removal-to-reach-corner/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2290-minimum-obstacle-removal-to-reach-corner/NOTES.md diff --git a/2290-minimum-obstacle-removal-to-reach-corner/NOTES.md b/2290-minimum-obstacle-removal-to-reach-corner/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2290-minimum-obstacle-removal-to-reach-corner/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4c4a2cb0bd76f581e33e9990f71fc30b5edee89f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Mar 2023 18:04:49 +0530 Subject: [PATCH 0669/3167] Time: 1097 ms (35.92%), Space: 162 MB (36.43%) - LeetHub --- ...nimum-obstacle-removal-to-reach-corner.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2290-minimum-obstacle-removal-to-reach-corner/2290-minimum-obstacle-removal-to-reach-corner.cpp diff --git a/2290-minimum-obstacle-removal-to-reach-corner/2290-minimum-obstacle-removal-to-reach-corner.cpp b/2290-minimum-obstacle-removal-to-reach-corner/2290-minimum-obstacle-removal-to-reach-corner.cpp new file mode 100644 index 00000000..793c71bb --- /dev/null +++ b/2290-minimum-obstacle-removal-to-reach-corner/2290-minimum-obstacle-removal-to-reach-corner.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + int minimumObstacles(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + queue> q; + + vector dx = {1,0,0,-1}; + vector dy = {0,-1,+1,0}; + + vector> dp(n, vector(m,INT_MAX)); + + q.push({0,0}); + dp[0][0] = 0; + + while(!q.empty()) + { + int x = q.front().first; + int y = q.front().second; + q.pop(); + + for(int i = 0; i<4; ++i) + { + int newx = x + dx[i]; + int newy = y + dy[i]; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and dp[x][y] + grid[newx][newy] < dp[newx][newy]) + { + dp[newx][newy] = dp[x][y] + grid[newx][newy]; + q.push({newx,newy}); + } + } + } + + return dp[n-1][m-1]; + } +}; \ No newline at end of file From 894af53bdad64ae50ab0a62efe9f55374febba0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 08:05:29 +0530 Subject: [PATCH 0670/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/README.md diff --git a/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/README.md b/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/README.md new file mode 100644 index 00000000..cf02414f --- /dev/null +++ b/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/README.md @@ -0,0 +1,35 @@ +

2556. Disconnect Path in a Binary Matrix by at Most One Flip

Medium


You are given a 0-indexed m x n binary matrix grid. You can move from a cell (row, col) to any of the cells (row + 1, col) or (row, col + 1) that has the value 1. The matrix is disconnected if there is no path from (0, 0) to (m - 1, n - 1).

+ +

You can flip the value of at most one (possibly none) cell. You cannot flip the cells (0, 0) and (m - 1, n - 1).

+ +

Return true if it is possible to make the matrix disconnect or false otherwise.

+ +

Note that flipping a cell changes its value from 0 to 1 or from 1 to 0.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,1,1],[1,0,0],[1,1,1]]
+Output: true
+Explanation: We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid.
+
+ +

Example 2:

+ +
Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
+Output: false
+Explanation: It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2).
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 1000
  • +
  • 1 <= m * n <= 105
  • +
  • grid[i][j] is either 0 or 1.
  • +
  • grid[0][0] == grid[m - 1][n - 1] == 1
  • +
+
\ No newline at end of file From ce0252a34d912a879caf91e8af55522ec7a90e5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 08:05:29 +0530 Subject: [PATCH 0671/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/NOTES.md diff --git a/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/NOTES.md b/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 112cfab26f69d9548e7a60eb02fe94b6d494495f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 08:05:33 +0530 Subject: [PATCH 0672/3167] Time: 104 ms (84.45%), Space: 38.3 MB (48.03%) - LeetHub --- ...in-a-binary-matrix-by-at-most-one-flip.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.cpp diff --git a/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.cpp b/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.cpp new file mode 100644 index 00000000..1a0deb48 --- /dev/null +++ b/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + + vector dx = {1,0}; + vector dy = {0,1}; + + bool dfs(int i, int j, int n, int m, vector>& grid) + { + if(i == n-1 and j == m-1) + return true; + + grid[i][j] = 0; + + for(int k = 0; k < 2; ++k) + { + int newx = i + dx[k]; + int newy = j + dy[k]; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and grid[newx][newy] == 1) + { + if(dfs(newx,newy, n,m,grid)) + return true; + } + } + return false; + } + + bool isPossibleToCutPath(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + if(dfs(0,0,n,m,grid) == false) + return true; + + if(dfs(0,0,n,m,grid) == false) + return true; + + return false; + } +}; \ No newline at end of file From 6428481a1c5d8c97bc5b351cae68dea2eea2f759 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 09:34:47 +0530 Subject: [PATCH 0673/3167] Attach NOTES - LeetHub From 9b8b73d5b453a0becbc55bf26a6fac78f0493515 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 09:34:50 +0530 Subject: [PATCH 0674/3167] Time: 104 ms (84.45%), Space: 38.3 MB (48.03%) - LeetHub From d1a6a4ea235a0fb76a475caf7a9d4f97bafe31c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 11:01:07 +0530 Subject: [PATCH 0675/3167] Create README - LeetHub --- 0087-scramble-string/README.md | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0087-scramble-string/README.md diff --git a/0087-scramble-string/README.md b/0087-scramble-string/README.md new file mode 100644 index 00000000..b641e0ac --- /dev/null +++ b/0087-scramble-string/README.md @@ -0,0 +1,52 @@ +

87. Scramble String

Hard


We can scramble a string s to get a string t using the following algorithm:

+ +
    +
  1. If the length of the string is 1, stop.
  2. +
  3. If the length of the string is > 1, do the following: +
      +
    • Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
    • +
    • Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
    • +
    • Apply step 1 recursively on each of the two substrings x and y.
    • +
    +
  4. +
+ +

Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: s1 = "great", s2 = "rgeat"
+Output: true
+Explanation: One possible scenario applied on s1 is:
+"great" --> "gr/eat" // divide at random index.
+"gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order.
+"gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at random index each of them.
+"g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order.
+"r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t".
+"r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order.
+The algorithm stops now, and the result string is "rgeat" which is s2.
+As one possible scenario led s1 to be scrambled to s2, we return true.
+
+ +

Example 2:

+ +
Input: s1 = "abcde", s2 = "caebd"
+Output: false
+
+ +

Example 3:

+ +
Input: s1 = "a", s2 = "a"
+Output: true
+
+ +

 

+

Constraints:

+ +
    +
  • s1.length == s2.length
  • +
  • 1 <= s1.length <= 30
  • +
  • s1 and s2 consist of lowercase English letters.
  • +
+
\ No newline at end of file From cd46cd5bd807f4feea14b8a4cc5dd25d6576ff12 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 11:01:08 +0530 Subject: [PATCH 0676/3167] Attach NOTES - LeetHub --- 0087-scramble-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0087-scramble-string/NOTES.md diff --git a/0087-scramble-string/NOTES.md b/0087-scramble-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0087-scramble-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 666bff14e39f746adc8f8725e8ae7b0966a9831e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Mar 2023 11:01:11 +0530 Subject: [PATCH 0677/3167] Time: 226 ms (18.65%), Space: 45.6 MB (8.94%) - LeetHub --- 0087-scramble-string/0087-scramble-string.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0087-scramble-string/0087-scramble-string.cpp diff --git a/0087-scramble-string/0087-scramble-string.cpp b/0087-scramble-string/0087-scramble-string.cpp new file mode 100644 index 00000000..1b4bacbf --- /dev/null +++ b/0087-scramble-string/0087-scramble-string.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + + bool helper(string s1, string s2, unordered_map& mp) + { + int n = s1.size(); + + if(s1 == s2) + { + mp.insert({s1+s2, true}); + return true; + } + + if(mp.find(s1 + s2) != mp.end()) + return mp[s1+s2]; + + for(int i = 1; i < n; ++i) + { + if(helper(s1.substr(0,i), s2.substr(n-i), mp) and helper(s1.substr(i),s2.substr(0,n-i), mp)) + { + mp.insert({s1+s2,true}); + return true; + } + if(helper(s1.substr(0,i), s2.substr(0,i), mp) and helper(s1.substr(i), s2.substr(i),mp)) + { + mp.insert({s1+s2,true}); + return true; + } + } + + mp.insert({s1+s2,false}); + return false; + } + + bool isScramble(string s1, string s2) { + + unordered_map mp; + return helper(s1, s2, mp); + + } +}; \ No newline at end of file From fbbfd9097c0ec0cde4d590bb337577a4f42e5cc0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 Mar 2023 22:16:52 +0530 Subject: [PATCH 0678/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1444-number-of-ways-of-cutting-a-pizza/README.md diff --git a/1444-number-of-ways-of-cutting-a-pizza/README.md b/1444-number-of-ways-of-cutting-a-pizza/README.md new file mode 100644 index 00000000..e1714ba4 --- /dev/null +++ b/1444-number-of-ways-of-cutting-a-pizza/README.md @@ -0,0 +1,38 @@ +

1444. Number of Ways of Cutting a Pizza

Hard


Given a rectangular pizza represented as a rows x cols matrix containing the following characters: 'A' (an apple) and '.' (empty cell) and given the integer k. You have to cut the pizza into k pieces using k-1 cuts. 

+ +

For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.

+ +

Return the number of ways of cutting the pizza such that each piece contains at least one apple. Since the answer can be a huge number, return this modulo 10^9 + 7.

+ +

 

+

Example 1:

+ +

+ +
Input: pizza = ["A..","AAA","..."], k = 3
+Output: 3 
+Explanation: The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple.
+
+ +

Example 2:

+ +
Input: pizza = ["A..","AA.","..."], k = 3
+Output: 1
+
+ +

Example 3:

+ +
Input: pizza = ["A..","A..","..."], k = 1
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= rows, cols <= 50
  • +
  • rows == pizza.length
  • +
  • cols == pizza[i].length
  • +
  • 1 <= k <= 10
  • +
  • pizza consists of characters 'A' and '.' only.
  • +
\ No newline at end of file From 2d22fc05d8225c1d96dbee444c68d9c80d5e90f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 Mar 2023 22:16:55 +0530 Subject: [PATCH 0679/3167] Time: 30 ms (67.54%), Space: 8.6 MB (23.04%) - LeetHub --- ...1444-number-of-ways-of-cutting-a-pizza.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 1444-number-of-ways-of-cutting-a-pizza/1444-number-of-ways-of-cutting-a-pizza.cpp diff --git a/1444-number-of-ways-of-cutting-a-pizza/1444-number-of-ways-of-cutting-a-pizza.cpp b/1444-number-of-ways-of-cutting-a-pizza/1444-number-of-ways-of-cutting-a-pizza.cpp new file mode 100644 index 00000000..19ac1a2e --- /dev/null +++ b/1444-number-of-ways-of-cutting-a-pizza/1444-number-of-ways-of-cutting-a-pizza.cpp @@ -0,0 +1,69 @@ +class Solution { + + int check(int row, int col, vector& p) + { + for(int i = row ; i < p.size() ; i++){ + for(int j = col ; j < p[0].length() ; j++){ + if( p[i][j] == 'A' ) return true ; + } + } + + return false ; + } + + bool hIsApple(int st, int cur, int c, vector& p) + { + for(int row = st; row < cur ; row++){ + for(int col = c ; col < p[0].length() ; col++){ + if( p[row][col] == 'A' ) return true ; + } + } + return false ; + } + + bool vIsApple(int r, int st, int cur, vector& p) + { + for(int row = r ; row < p.size() ; row++){ + for(int col = st ; col < cur ; col++){ + if( p[row][col] == 'A' ) return true ; + } + } + return false ; + } + + int helper(int &i, int &j, int k, vector& p,vector>> &dp) + { + if( k == 0 ) + { + return check(i,j,p) ; + } + + if( dp[i][j][k] != -1 ) return dp[i][j][k] ; + + int ans = 0 ; + for(int row = i+1 ; row < p.size() ; row++) + { + if( hIsApple( i,row,j,p ) ) + ans = (ans + helper(row,j,k-1,p,dp))%mod ; + } + + for(int col = j+1 ; col < p[0].length() ; col++) + { + if( vIsApple(i,j,col,p) ) + ans = (ans + helper(i,col,k-1,p,dp))%mod ; + } + + return dp[i][j][k] = (ans)%mod ; + } + +public: + int mod = 1e9+7 ; + int ways(vector& pizza, int k) { + + int n = pizza.size(); + int m = pizza[0].length() ; + int i =0,j = 0; + vector>> dp(n+1,vector> (m+1, vector (k+1,-1))) ; + return helper(i,j,k-1,pizza,dp) ; + } +}; \ No newline at end of file From b4285b84247491fc188a170ba04760a631af77ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Apr 2023 07:36:29 +0530 Subject: [PATCH 0680/3167] Create README - LeetHub --- 0704-binary-search/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0704-binary-search/README.md diff --git a/0704-binary-search/README.md b/0704-binary-search/README.md new file mode 100644 index 00000000..2a0b6637 --- /dev/null +++ b/0704-binary-search/README.md @@ -0,0 +1,29 @@ +

704. Binary Search

Easy


Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [-1,0,3,5,9,12], target = 9
+Output: 4
+Explanation: 9 exists in nums and its index is 4
+
+ +

Example 2:

+ +
Input: nums = [-1,0,3,5,9,12], target = 2
+Output: -1
+Explanation: 2 does not exist in nums so return -1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 104
  • +
  • -104 < nums[i], target < 104
  • +
  • All the integers in nums are unique.
  • +
  • nums is sorted in ascending order.
  • +
+
\ No newline at end of file From 693e134c29d2acdc81dd46f4eb0d0b8249ecd82c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Apr 2023 07:36:32 +0530 Subject: [PATCH 0681/3167] Time: 42 ms (45.40%), Space: 27.6 MB (69.64%) - LeetHub --- 0704-binary-search/0704-binary-search.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0704-binary-search/0704-binary-search.cpp diff --git a/0704-binary-search/0704-binary-search.cpp b/0704-binary-search/0704-binary-search.cpp new file mode 100644 index 00000000..fda7754e --- /dev/null +++ b/0704-binary-search/0704-binary-search.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int search(vector& nums, int target) { + + int start = 0, end = nums.size()-1; + + while(start <= end) + { + int mid = start + (end - start)/2; + + if(nums[mid] == target) + return mid; + else if(nums[mid] > target) + end = mid - 1; + else + start = mid + 1; + } + + return -1; + + } +}; \ No newline at end of file From de422d63b6b35f709d7a9728472f529890e3eb6c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Apr 2023 09:50:10 +0530 Subject: [PATCH 0682/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2300-successful-pairs-of-spells-and-potions/README.md diff --git a/2300-successful-pairs-of-spells-and-potions/README.md b/2300-successful-pairs-of-spells-and-potions/README.md new file mode 100644 index 00000000..c97383bd --- /dev/null +++ b/2300-successful-pairs-of-spells-and-potions/README.md @@ -0,0 +1,40 @@ +

2300. Successful Pairs of Spells and Potions

Medium


You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion.

+ +

You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at least success.

+ +

Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the ith spell.

+ +

 

+

Example 1:

+ +
Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
+Output: [4,0,3]
+Explanation:
+- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
+- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
+- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
+Thus, [4,0,3] is returned.
+
+ +

Example 2:

+ +
Input: spells = [3,1,2], potions = [8,5,8], success = 16
+Output: [2,0,2]
+Explanation:
+- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
+- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. 
+- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. 
+Thus, [2,0,2] is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • n == spells.length
  • +
  • m == potions.length
  • +
  • 1 <= n, m <= 105
  • +
  • 1 <= spells[i], potions[i] <= 105
  • +
  • 1 <= success <= 1010
  • +
+
\ No newline at end of file From 7ab0fecfa40a98e4933e87159e7dc54f876a1677 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Apr 2023 09:50:10 +0530 Subject: [PATCH 0683/3167] Attach NOTES - LeetHub --- 2300-successful-pairs-of-spells-and-potions/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2300-successful-pairs-of-spells-and-potions/NOTES.md diff --git a/2300-successful-pairs-of-spells-and-potions/NOTES.md b/2300-successful-pairs-of-spells-and-potions/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2300-successful-pairs-of-spells-and-potions/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 81384ccf44800e4283e4087de4c65e03ffd3d867 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Apr 2023 09:50:14 +0530 Subject: [PATCH 0684/3167] Time: 254 ms (59.60%), Space: 99.9 MB (51.66%) - LeetHub --- ...successful-pairs-of-spells-and-potions.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2300-successful-pairs-of-spells-and-potions/2300-successful-pairs-of-spells-and-potions.cpp diff --git a/2300-successful-pairs-of-spells-and-potions/2300-successful-pairs-of-spells-and-potions.cpp b/2300-successful-pairs-of-spells-and-potions/2300-successful-pairs-of-spells-and-potions.cpp new file mode 100644 index 00000000..ca644387 --- /dev/null +++ b/2300-successful-pairs-of-spells-and-potions/2300-successful-pairs-of-spells-and-potions.cpp @@ -0,0 +1,37 @@ +#define ll long long int +class Solution { +public: + vector successfulPairs(vector& spells, vector& potions, long long success) { + + + int n = spells.size(); + + sort(potions.begin(),potions.end()); + vector v; + + for(int i = 0; i Date: Tue, 4 Apr 2023 07:28:45 +0530 Subject: [PATCH 0685/3167] Create README - LeetHub --- 2405-optimal-partition-of-string/README.md | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2405-optimal-partition-of-string/README.md diff --git a/2405-optimal-partition-of-string/README.md b/2405-optimal-partition-of-string/README.md new file mode 100644 index 00000000..bb11394a --- /dev/null +++ b/2405-optimal-partition-of-string/README.md @@ -0,0 +1,32 @@ +

2405. Optimal Partition of String

Medium


Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once.

+ +

Return the minimum number of substrings in such a partition.

+ +

Note that each character should belong to exactly one substring in a partition.

+ +

 

+

Example 1:

+ +
Input: s = "abacaba"
+Output: 4
+Explanation:
+Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
+It can be shown that 4 is the minimum number of substrings needed.
+
+ +

Example 2:

+ +
Input: s = "ssssss"
+Output: 6
+Explanation:
+The only valid partition is ("s","s","s","s","s","s").
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of only English lowercase letters.
  • +
+
\ No newline at end of file From e24facfd366287af1cc1b4efaacb2f0ffaa7adca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:28:45 +0530 Subject: [PATCH 0686/3167] Attach NOTES - LeetHub --- 2405-optimal-partition-of-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2405-optimal-partition-of-string/NOTES.md diff --git a/2405-optimal-partition-of-string/NOTES.md b/2405-optimal-partition-of-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2405-optimal-partition-of-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 211dd544bf17dc8808fd76862856a9343b148e70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:28:49 +0530 Subject: [PATCH 0687/3167] Time: 272 ms (15.85%), Space: 71.9 MB (21.72%) - LeetHub --- .../2405-optimal-partition-of-string.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2405-optimal-partition-of-string/2405-optimal-partition-of-string.cpp diff --git a/2405-optimal-partition-of-string/2405-optimal-partition-of-string.cpp b/2405-optimal-partition-of-string/2405-optimal-partition-of-string.cpp new file mode 100644 index 00000000..7566a6f8 --- /dev/null +++ b/2405-optimal-partition-of-string/2405-optimal-partition-of-string.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int partitionString(string s) { + + int i = 0, j = s.size(); + + int part = 1; + map mp; + + while(i < j) + { + if(mp.find(s[i]) != mp.end()) + { + ++part; + mp.clear(); + } + ++mp[s[i]]; + ++i; + } + + return part; + + } +}; \ No newline at end of file From 1d7789d1fa47b12545b8af7e1ab7df5cc079ad7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:30:46 +0530 Subject: [PATCH 0688/3167] Attach NOTES - LeetHub From 82d4f0ec969525a8854e30ff51fc92392da343fd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:30:49 +0530 Subject: [PATCH 0689/3167] Time: 272 ms (15.85%), Space: 71.9 MB (21.72%) - LeetHub From d4db4d7ede326086978f7d0fc5993c0f3be2a58e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:31:35 +0530 Subject: [PATCH 0690/3167] Attach NOTES - LeetHub From 1234f9ae7d09fa7152b8e204f6d38c7f929347ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:31:38 +0530 Subject: [PATCH 0691/3167] Time: 272 ms (15.85%), Space: 71.9 MB (21.72%) - LeetHub From b618840c409d5be8257b1515a0fc0d9a5cf628f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:32:48 +0530 Subject: [PATCH 0692/3167] Attach NOTES - LeetHub From 19ccd34098428119192105ca3d820efe48d932e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 07:32:50 +0530 Subject: [PATCH 0693/3167] Time: 272 ms (15.85%), Space: 71.9 MB (21.72%) - LeetHub From bf4ef23bb9e8db1497969779efb376ec3c4715b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:01:00 +0530 Subject: [PATCH 0694/3167] Create README - LeetHub --- 2607-make-k-subarray-sums-equal/README.md | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2607-make-k-subarray-sums-equal/README.md diff --git a/2607-make-k-subarray-sums-equal/README.md b/2607-make-k-subarray-sums-equal/README.md new file mode 100644 index 00000000..6794e757 --- /dev/null +++ b/2607-make-k-subarray-sums-equal/README.md @@ -0,0 +1,45 @@ +

2607. Make K-Subarray Sums Equal

Medium


You are given a 0-indexed integer array arr and an integer k. The array arr is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.

+ +

You can do the following operation any number of times:

+ +
    +
  • Pick any element from arr and increase or decrease it by 1.
  • +
+ +

Return the minimum number of operations such that the sum of each subarray of length k is equal.

+ +

A subarray is a contiguous part of the array.

+ +

 

+

Example 1:

+ +
Input: arr = [1,4,1,3], k = 2
+Output: 1
+Explanation: we can do one operation on index 1 to make its value equal to 3.
+The array after the operation is [1,3,1,3]
+- Subarray starts at index 0 is [1, 3], and its sum is 4 
+- Subarray starts at index 1 is [3, 1], and its sum is 4 
+- Subarray starts at index 2 is [1, 3], and its sum is 4 
+- Subarray starts at index 3 is [3, 1], and its sum is 4 
+
+ +

Example 2:

+ +
Input: arr = [2,5,5,7], k = 3
+Output: 5
+Explanation: we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
+The array after the operations is [5,5,5,5]
+- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
+- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
+- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
+- Subarray starts at index 3 is [5, 5, 5], and its sum is 15 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= arr.length <= 105
  • +
  • 1 <= arr[i] <= 109
  • +
+
\ No newline at end of file From 964e10a2034cdd041e6b8ff1d8a33a571c231770 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:01:04 +0530 Subject: [PATCH 0695/3167] Time: 217 ms (80.21%), Space: 109 MB (54.34%) - LeetHub --- .../2607-make-k-subarray-sums-equal.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2607-make-k-subarray-sums-equal/2607-make-k-subarray-sums-equal.cpp diff --git a/2607-make-k-subarray-sums-equal/2607-make-k-subarray-sums-equal.cpp b/2607-make-k-subarray-sums-equal/2607-make-k-subarray-sums-equal.cpp new file mode 100644 index 00000000..66abb41e --- /dev/null +++ b/2607-make-k-subarray-sums-equal/2607-make-k-subarray-sums-equal.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long makeSubKSumEqual(vector& arr, int k) { + + long long n = arr.size(), ans = 0; + + for(int i = 0; i < n; ++i) + { + vector cycles; + + for(int j = i; arr[j] != 0 ; j = (j+k)%n) + { + cycles.push_back(arr[j]); + arr[j] = 0; + } + + nth_element(cycles.begin(),cycles.begin()+cycles.size()/2,cycles.end()); + + for(int i = 0; i < cycles.size(); ++i) + { + ans += abs(cycles[i] - cycles[cycles.size()/2]); + } + + } + + return ans; + } +}; \ No newline at end of file From a1ad9685071c56050b9e1edb48f84c879fbdde76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:01:33 +0530 Subject: [PATCH 0696/3167] Attach NOTES - LeetHub --- 2607-make-k-subarray-sums-equal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2607-make-k-subarray-sums-equal/NOTES.md diff --git a/2607-make-k-subarray-sums-equal/NOTES.md b/2607-make-k-subarray-sums-equal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2607-make-k-subarray-sums-equal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 076434f7d193fadda670d6279accefdd563d336a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:03:38 +0530 Subject: [PATCH 0697/3167] Attach NOTES - LeetHub From db808956f98df18ec17f2518bf09d3df5d932b6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:03:41 +0530 Subject: [PATCH 0698/3167] Time: 217 ms (80.21%), Space: 109 MB (54.34%) - LeetHub From 6c44710f571ce2df1f1b4ab1a44797b4b41d9662 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:03:51 +0530 Subject: [PATCH 0699/3167] Attach NOTES - LeetHub From a0e688853d40885a915a0ee2627a285ad0494db8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:03:55 +0530 Subject: [PATCH 0700/3167] Time: 217 ms (80.21%), Space: 109 MB (54.34%) - LeetHub From aa4fbdda61bfd60081cfc609976bacd84f07d419 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:13:29 +0530 Subject: [PATCH 0701/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2358-maximum-number-of-groups-entering-a-competition/README.md diff --git a/2358-maximum-number-of-groups-entering-a-competition/README.md b/2358-maximum-number-of-groups-entering-a-competition/README.md new file mode 100644 index 00000000..1c1cacc0 --- /dev/null +++ b/2358-maximum-number-of-groups-entering-a-competition/README.md @@ -0,0 +1,36 @@ +

2358. Maximum Number of Groups Entering a Competition

Medium


You are given a positive integer array grades which represents the grades of students in a university. You would like to enter all these students into a competition in ordered non-empty groups, such that the ordering meets the following conditions:

+ +
    +
  • The sum of the grades of students in the ith group is less than the sum of the grades of students in the (i + 1)th group, for all groups (except the last).
  • +
  • The total number of students in the ith group is less than the total number of students in the (i + 1)th group, for all groups (except the last).
  • +
+ +

Return the maximum number of groups that can be formed.

+ +

 

+

Example 1:

+ +
Input: grades = [10,6,12,7,3,5]
+Output: 3
+Explanation: The following is a possible way to form 3 groups of students:
+- 1st group has the students with grades = [12]. Sum of grades: 12. Student count: 1
+- 2nd group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2
+- 3rd group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
+It can be shown that it is not possible to form more than 3 groups.
+
+ +

Example 2:

+ +
Input: grades = [8,8]
+Output: 1
+Explanation: We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= grades.length <= 105
  • +
  • 1 <= grades[i] <= 105
  • +
+
\ No newline at end of file From 4de8256a2f52ba5e5bc6f7005cddaab6e1832840 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:13:32 +0530 Subject: [PATCH 0702/3167] Time: 136 ms (25.13%), Space: 56.7 MB (12.23%) - LeetHub --- ...umber-of-groups-entering-a-competition.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2358-maximum-number-of-groups-entering-a-competition/2358-maximum-number-of-groups-entering-a-competition.cpp diff --git a/2358-maximum-number-of-groups-entering-a-competition/2358-maximum-number-of-groups-entering-a-competition.cpp b/2358-maximum-number-of-groups-entering-a-competition/2358-maximum-number-of-groups-entering-a-competition.cpp new file mode 100644 index 00000000..4c14b4cf --- /dev/null +++ b/2358-maximum-number-of-groups-entering-a-competition/2358-maximum-number-of-groups-entering-a-competition.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int maximumGroups(vector& grades) { + + int n = grades.size(); + sort(grades.begin(),grades.end()); + + int curr = 0, prev = 0, currSum = 0, prevSum = 0, res = 0; + + for(auto itr : grades) + { + currSum += itr; + ++curr; + + if(currSum > prevSum and curr > prev) + { + ++res; + prevSum = currSum, prev = curr; + currSum = 0, curr = 0; + } + } + + return res; + } +}; \ No newline at end of file From c5bed834c8a7f196824a914037f395e2022e2cb5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:20:18 +0530 Subject: [PATCH 0703/3167] Attach NOTES - LeetHub --- 2358-maximum-number-of-groups-entering-a-competition/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2358-maximum-number-of-groups-entering-a-competition/NOTES.md diff --git a/2358-maximum-number-of-groups-entering-a-competition/NOTES.md b/2358-maximum-number-of-groups-entering-a-competition/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2358-maximum-number-of-groups-entering-a-competition/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3c82c4f0dfcf2b80ffb54913930f2122cf82049f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:20:22 +0530 Subject: [PATCH 0704/3167] Time: 136 ms (25.13%), Space: 56.7 MB (12.23%) - LeetHub From 9c7b13c57300567c85333369dc1b541a9053ba8d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:22:49 +0530 Subject: [PATCH 0705/3167] Attach NOTES - LeetHub From 0dbca48504c88426b4ad48f8e68210f972e8e745 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:22:52 +0530 Subject: [PATCH 0706/3167] Time: 136 ms (25.13%), Space: 56.7 MB (12.23%) - LeetHub From 72f5b1af77a94dd4fc4372a3f5b12b91c8f58741 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Apr 2023 23:18:18 +0530 Subject: [PATCH 0707/3167] Attach NOTES - LeetHub --- 2439-minimize-maximum-of-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2439-minimize-maximum-of-array/NOTES.md diff --git a/2439-minimize-maximum-of-array/NOTES.md b/2439-minimize-maximum-of-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2439-minimize-maximum-of-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4e8692cb1dda7c49fef3c86483904ae06885ce27 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Apr 2023 23:18:22 +0530 Subject: [PATCH 0708/3167] Time: 140 ms (71.74%), Space: 71.4 MB (61.54%) - LeetHub --- .../2439-minimize-maximum-of-array.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2439-minimize-maximum-of-array/2439-minimize-maximum-of-array.cpp diff --git a/2439-minimize-maximum-of-array/2439-minimize-maximum-of-array.cpp b/2439-minimize-maximum-of-array/2439-minimize-maximum-of-array.cpp new file mode 100644 index 00000000..06122c95 --- /dev/null +++ b/2439-minimize-maximum-of-array/2439-minimize-maximum-of-array.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int minimizeArrayValue(vector& nums) { + + int n = nums.size(); + long long sum = nums[0]; + long long mini = nums[0]; + + for(int i = 1; i mini) + { + long long avg = sum/(i+1); + if(avg >= mini) + { + if(sum % (i+1)) + mini = avg + 1; + else + mini = avg; + } + } + } + + return mini; + } +}; \ No newline at end of file From 9d8759340d28fd718a0527a66b5fa781e65ab54d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Apr 2023 22:22:17 +0530 Subject: [PATCH 0709/3167] Create README - LeetHub --- 1254-number-of-closed-islands/README.md | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1254-number-of-closed-islands/README.md diff --git a/1254-number-of-closed-islands/README.md b/1254-number-of-closed-islands/README.md new file mode 100644 index 00000000..b3f4dc8a --- /dev/null +++ b/1254-number-of-closed-islands/README.md @@ -0,0 +1,42 @@ +

1254. Number of Closed Islands

Medium


Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.

+ +

Return the number of closed islands.

+ +

 

+

Example 1:

+ +

+ +
Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
+Output: 2
+Explanation: 
+Islands in gray are closed because they are completely surrounded by water (group of 1s).
+ +

Example 2:

+ +

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= grid.length, grid[0].length <= 100
  • +
  • 0 <= grid[i][j] <=1
  • +
+
\ No newline at end of file From 4845405a0058ff9b64050395fbcfb4b9203c69f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Apr 2023 22:22:20 +0530 Subject: [PATCH 0710/3167] Time: 9 ms (92.15%), Space: 9.4 MB (77.35%) - LeetHub --- .../1254-number-of-closed-islands.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1254-number-of-closed-islands/1254-number-of-closed-islands.cpp diff --git a/1254-number-of-closed-islands/1254-number-of-closed-islands.cpp b/1254-number-of-closed-islands/1254-number-of-closed-islands.cpp new file mode 100644 index 00000000..9aea186b --- /dev/null +++ b/1254-number-of-closed-islands/1254-number-of-closed-islands.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int closedIsland(vector>& grid) { + int res = 0; + for (int i = 0; i < grid.size(); i++){ + for (int j = 0; j < grid[0].size(); j++){ + if (grid[i][j] == 0){ + res += dfs(grid, i, j) ? 1 : 0; + } + } + } + return res; + } + bool dfs(vector>& g, int i, int j){ + if (i < 0 || j < 0 || i >= g.size() || j >= g[0].size()){ + return false; + } + if (g[i][j] == 1){ + return true; + } + g[i][j] = 1; + + bool d1 = dfs(g, i+1, j); + bool d2 = dfs(g, i, j+1); + bool d3 = dfs(g, i-1, j); + bool d4 = dfs(g, i, j-1); + return d1 && d2 && d3 && d4; + } +}; \ No newline at end of file From 755d6f3c297e1c2840140f7660ed47082ce6ee7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:30:35 +0530 Subject: [PATCH 0711/3167] Create README - LeetHub --- 1020-number-of-enclaves/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1020-number-of-enclaves/README.md diff --git a/1020-number-of-enclaves/README.md b/1020-number-of-enclaves/README.md new file mode 100644 index 00000000..b2758cbf --- /dev/null +++ b/1020-number-of-enclaves/README.md @@ -0,0 +1,31 @@ +

1020. Number of Enclaves

Medium


You are given an m x n 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.

+ +

Return 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: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
+
+ +

Example 2:

+ +
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
+Output: 0
+Explanation: All 1s are either on the boundary or can reach the boundary.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 500
  • +
  • grid[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From 524728d821148ab551ab2075f244d623ebd0d3aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:30:35 +0530 Subject: [PATCH 0712/3167] Attach NOTES - LeetHub --- 1020-number-of-enclaves/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1020-number-of-enclaves/NOTES.md diff --git a/1020-number-of-enclaves/NOTES.md b/1020-number-of-enclaves/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1020-number-of-enclaves/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5ac2d869eafe625d8eb40ef9cab3824a347ec5f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:30:38 +0530 Subject: [PATCH 0713/3167] Time: 63 ms (97.72%), Space: 28.3 MB (68.59%) - LeetHub --- .../1020-number-of-enclaves.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1020-number-of-enclaves/1020-number-of-enclaves.cpp diff --git a/1020-number-of-enclaves/1020-number-of-enclaves.cpp b/1020-number-of-enclaves/1020-number-of-enclaves.cpp new file mode 100644 index 00000000..fe9dc0c3 --- /dev/null +++ b/1020-number-of-enclaves/1020-number-of-enclaves.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + + int dfs(int i, int j, int n, int m, vector>& grid) + { + if(i < 0 or j < 0 or i >= n or j >= m or grid[i][j] == 0) + return 0; + + grid[i][j] = 0; + + return 1 + dfs(i-1,j,n,m,grid) + dfs(i+1,j,n,m,grid) + dfs(i,j-1,n,m,grid) + dfs(i,j+1,n,m,grid); + } + + int numEnclaves(vector>& grid) { + + int n = grid.size(), m = grid[0].size() ,allOne = 0, closeOne = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + ++allOne; + } + } + + 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) + closeOne += dfs(i,j,n,m,grid); + } + } + + return allOne - closeOne; + } +}; \ No newline at end of file From a7342b4bc0e6ff852227c329d917fcaa65195508 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:31:35 +0530 Subject: [PATCH 0714/3167] Attach NOTES - LeetHub From fe44620d6c9df704c70e6bdcc8c4340e1c21e751 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:31:38 +0530 Subject: [PATCH 0715/3167] Time: 63 ms (97.72%), Space: 28.3 MB (68.59%) - LeetHub From 58946b17398a7630911c23da7876dce26e76bc18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:32:41 +0530 Subject: [PATCH 0716/3167] Attach NOTES - LeetHub From cb56aece8cec4777bf463aa11acc0c0f478a3f1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Apr 2023 08:44:48 +0530 Subject: [PATCH 0717/3167] Create README - LeetHub --- 0133-clone-graph/README.md | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 0133-clone-graph/README.md diff --git a/0133-clone-graph/README.md b/0133-clone-graph/README.md new file mode 100644 index 00000000..8bfc4689 --- /dev/null +++ b/0133-clone-graph/README.md @@ -0,0 +1,59 @@ +

133. Clone Graph

Medium


Given a reference of a node in a connected undirected graph.

+ +

Return a deep copy (clone) of the graph.

+ +

Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.

+ +
class Node {
+    public int val;
+    public List<Node> neighbors;
+}
+
+ +

 

+ +

Test case format:

+ +

For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.

+ +

An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.

+ +

The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.

+ +

 

+

Example 1:

+ +
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
+Output: [[2,4],[1,3],[2,4],[1,3]]
+Explanation: There are 4 nodes in the graph.
+1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
+2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
+3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
+4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
+
+ +

Example 2:

+ +
Input: adjList = [[]]
+Output: [[]]
+Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
+
+ +

Example 3:

+ +
Input: adjList = []
+Output: []
+Explanation: This an empty graph, it does not have any nodes.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the graph is in the range [0, 100].
  • +
  • 1 <= Node.val <= 100
  • +
  • Node.val is unique for each node.
  • +
  • There are no repeated edges and no self-loops in the graph.
  • +
  • The Graph is connected and all nodes can be visited starting from the given node.
  • +
+
\ No newline at end of file From fea5cbf90a0a49030294ca2011998465f4fd60d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Apr 2023 08:44:51 +0530 Subject: [PATCH 0718/3167] Time: 3 ms (95.03%), Space: 8.9 MB (34.83%) - LeetHub --- 0133-clone-graph/0133-clone-graph.cpp | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0133-clone-graph/0133-clone-graph.cpp diff --git a/0133-clone-graph/0133-clone-graph.cpp b/0133-clone-graph/0133-clone-graph.cpp new file mode 100644 index 00000000..0352196f --- /dev/null +++ b/0133-clone-graph/0133-clone-graph.cpp @@ -0,0 +1,55 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } +}; +*/ + +class Solution { +public: + + Node* clone(Node* node, unordered_map& mp) + { + Node* newNode = new Node(node->val); + mp.insert({node->val,newNode}); + + for(auto itr : node->neighbors) + { + if(mp.find(itr->val) == mp.end()) + { + Node* cn = clone(itr,mp); + newNode->neighbors.push_back(cn); + } + else + newNode->neighbors.push_back(mp[itr->val]); + } + + return newNode; + } + + + Node* cloneGraph(Node* node) { + + if(node == nullptr) + return nullptr; + + unordered_map mp; + + return clone(node, mp); + + } +}; \ No newline at end of file From d1f49cb75101a0f2550393b42643a270aab9e03d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Apr 2023 23:02:08 +0530 Subject: [PATCH 0719/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1857-largest-color-value-in-a-directed-graph/README.md diff --git a/1857-largest-color-value-in-a-directed-graph/README.md b/1857-largest-color-value-in-a-directed-graph/README.md new file mode 100644 index 00000000..cd221a6b --- /dev/null +++ b/1857-largest-color-value-in-a-directed-graph/README.md @@ -0,0 +1,38 @@ +

1857. Largest Color Value in a Directed Graph

Hard


There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.

+ +

You are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [aj, bj] indicates that there is a directed edge from node aj to node bj.

+ +

A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk such that there is a directed edge from xi to xi+1 for every 1 <= i < k. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.

+ +

Return the largest color value of any valid path in the given graph, or -1 if the graph contains a cycle.

+ +

 

+

Example 1:

+ +

+ +
Input: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
+Output: 3
+Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).
+
+ +

Example 2:

+ +

+ +
Input: colors = "a", edges = [[0,0]]
+Output: -1
+Explanation: There is a cycle from 0 to 0.
+
+ +

 

+

Constraints:

+ +
    +
  • n == colors.length
  • +
  • m == edges.length
  • +
  • 1 <= n <= 105
  • +
  • 0 <= m <= 105
  • +
  • colors consists of lowercase English letters.
  • +
  • 0 <= aj, bj < n
  • +
\ No newline at end of file From 57c16e5a0cc1b70aa074f36e47d13dd7b0c4a41f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Apr 2023 23:02:08 +0530 Subject: [PATCH 0720/3167] Attach NOTES - LeetHub --- 1857-largest-color-value-in-a-directed-graph/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1857-largest-color-value-in-a-directed-graph/NOTES.md diff --git a/1857-largest-color-value-in-a-directed-graph/NOTES.md b/1857-largest-color-value-in-a-directed-graph/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1857-largest-color-value-in-a-directed-graph/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c6f950bcf358606b7ee31d1f27ea626435395290 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Apr 2023 23:02:12 +0530 Subject: [PATCH 0721/3167] Time: 775 ms (23.07%), Space: 188.5 MB (41.45%) - LeetHub --- ...argest-color-value-in-a-directed-graph.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1857-largest-color-value-in-a-directed-graph/1857-largest-color-value-in-a-directed-graph.cpp diff --git a/1857-largest-color-value-in-a-directed-graph/1857-largest-color-value-in-a-directed-graph.cpp b/1857-largest-color-value-in-a-directed-graph/1857-largest-color-value-in-a-directed-graph.cpp new file mode 100644 index 00000000..b8a82a32 --- /dev/null +++ b/1857-largest-color-value-in-a-directed-graph/1857-largest-color-value-in-a-directed-graph.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + int largestPathValue(string colors, vector>& edges) { + int n = colors.size(),k=26; + vector indegrees(n, 0); + vector> graph(n, vector()); + for (auto edge : edges) { + int u = edge[0]; + int v = edge[1]; + graph[u].push_back(v); + indegrees[v]++; + } + unordered_set zero_indegree; + for (int i = 0; i < n; i++) { + if (indegrees[i] == 0) { + zero_indegree.insert(i); + } + } + vector> counts(n, vector(k, 0)); + for (int i = 0; i < n; i++) { + counts[i][colors[i] - 'a']++; + } + int max_count = 0,visited = 0; + while (!zero_indegree.empty()) { + int u = *zero_indegree.begin(); + zero_indegree.erase(u); + visited++; + for (int v : graph[u]) { + for (int i = 0; i < k; i++) + counts[v][i] = max(counts[v][i], counts[u][i] + (colors[v] - 'a' == i ? 1 : 0)); + indegrees[v]--; + if (indegrees[v] == 0) { + zero_indegree.insert(v); + } + } + max_count = max(max_count, *max_element(counts[u].begin(), counts[u].end())); + } + return visited == n ? max_count : -1; + } +}; \ No newline at end of file From e6429acbbc273f538031014d2b6c50fad0a8ba5c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 10 Apr 2023 18:21:10 +0530 Subject: [PATCH 0722/3167] Create README - LeetHub --- 0020-valid-parentheses/README.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0020-valid-parentheses/README.md diff --git a/0020-valid-parentheses/README.md b/0020-valid-parentheses/README.md new file mode 100644 index 00000000..5c5bf1e2 --- /dev/null +++ b/0020-valid-parentheses/README.md @@ -0,0 +1,37 @@ +

20. Valid Parentheses

Easy


Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

+ +

An input string is valid if:

+ +
    +
  1. Open brackets must be closed by the same type of brackets.
  2. +
  3. Open brackets must be closed in the correct order.
  4. +
  5. Every close bracket has a corresponding open bracket of the same type.
  6. +
+ +

 

+

Example 1:

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

Example 2:

+ +
Input: s = "()[]{}"
+Output: true
+
+ +

Example 3:

+ +
Input: s = "(]"
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of parentheses only '()[]{}'.
  • +
+
\ No newline at end of file From 62438f2f456644392ff5103df26dbbd19b8e9269 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 10 Apr 2023 18:21:13 +0530 Subject: [PATCH 0723/3167] Time: 0 ms (100.00%), Space: 6.4 MB (12.09%) - LeetHub --- .../0020-valid-parentheses.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0020-valid-parentheses/0020-valid-parentheses.cpp diff --git a/0020-valid-parentheses/0020-valid-parentheses.cpp b/0020-valid-parentheses/0020-valid-parentheses.cpp new file mode 100644 index 00000000..21bea9f4 --- /dev/null +++ b/0020-valid-parentheses/0020-valid-parentheses.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + bool isValid(string s) { + + + stack st; + + for(auto itr : s) + { + if(itr == '(' or itr == '{' or itr == '[') + st.push(itr); + else if(!st.empty()) + { + if(st.top() == '(' and itr != ')') + return false; + else if(st.top() == '{' and itr != '}') + return false; + else if(st.top() =='[' and itr != ']') + return false; + + st.pop(); + } + else + return false; + } + + return st.empty(); + + } +}; \ No newline at end of file From a3e9c97a2fbaff816d987a04afe84f4795748cce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:21:13 +0530 Subject: [PATCH 0724/3167] Create README - LeetHub --- 2390-removing-stars-from-a-string/README.md | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2390-removing-stars-from-a-string/README.md diff --git a/2390-removing-stars-from-a-string/README.md b/2390-removing-stars-from-a-string/README.md new file mode 100644 index 00000000..341e4236 --- /dev/null +++ b/2390-removing-stars-from-a-string/README.md @@ -0,0 +1,45 @@ +

2390. Removing Stars From a String

Medium


You are given a string s, which contains stars *.

+ +

In one operation, you can:

+ +
    +
  • Choose a star in s.
  • +
  • Remove the closest non-star character to its left, as well as remove the star itself.
  • +
+ +

Return the string after all stars have been removed.

+ +

Note:

+ +
    +
  • The input will be generated such that the operation is always possible.
  • +
  • It can be shown that the resulting string will always be unique.
  • +
+ +

 

+

Example 1:

+ +
Input: s = "leet**cod*e"
+Output: "lecoe"
+Explanation: Performing the removals from left to right:
+- The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e".
+- The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e".
+- The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe".
+There are no more stars, so we return "lecoe".
+ +

Example 2:

+ +
Input: s = "erase*****"
+Output: ""
+Explanation: The entire string is removed, so we return an empty string.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase English letters and stars *.
  • +
  • The operation above can be performed on s.
  • +
+
\ No newline at end of file From f302e47a97cd3a9822e37f9e991606ea0ec12841 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:21:17 +0530 Subject: [PATCH 0725/3167] Time: 110 ms (14.34%), Space: 26.9 MB (50.08%) - LeetHub --- .../2390-removing-stars-from-a-string.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2390-removing-stars-from-a-string/2390-removing-stars-from-a-string.cpp diff --git a/2390-removing-stars-from-a-string/2390-removing-stars-from-a-string.cpp b/2390-removing-stars-from-a-string/2390-removing-stars-from-a-string.cpp new file mode 100644 index 00000000..453c3e9f --- /dev/null +++ b/2390-removing-stars-from-a-string/2390-removing-stars-from-a-string.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string removeStars(string s) { + + stack st; + string ans; + + for(auto itr : s) + { + if(!st.empty() and itr == '*') + st.pop(); + else + st.push(itr); + } + + while(!st.empty()) + { + ans += st.top(); + st.pop(); + } + + reverse(ans.begin(),ans.end()); + + return ans; + } +}; \ No newline at end of file From 6d7e392069418fbc135b3559c80ed18ffb16261b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:23:10 +0530 Subject: [PATCH 0726/3167] Attach NOTES - LeetHub --- 2390-removing-stars-from-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2390-removing-stars-from-a-string/NOTES.md diff --git a/2390-removing-stars-from-a-string/NOTES.md b/2390-removing-stars-from-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2390-removing-stars-from-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cd519a98b1c59a333541960f95b4508cfd93478f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:23:13 +0530 Subject: [PATCH 0727/3167] Time: 110 ms (14.34%), Space: 26.9 MB (50.08%) - LeetHub From 0fc87b1826ea671c361916848a8d355c996de6bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:35:00 +0530 Subject: [PATCH 0728/3167] Attach NOTES - LeetHub From 727980849ec1a136bc9e7a67bf7dc9d1e3871bce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:35:03 +0530 Subject: [PATCH 0729/3167] Time: 110 ms (14.34%), Space: 26.9 MB (50.08%) - LeetHub From 618f4907bbbb38fe66fbb36a575ca0193c096ac6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Apr 2023 18:58:40 +0530 Subject: [PATCH 0730/3167] Create README - LeetHub --- 1143-longest-common-subsequence/README.md | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1143-longest-common-subsequence/README.md diff --git a/1143-longest-common-subsequence/README.md b/1143-longest-common-subsequence/README.md new file mode 100644 index 00000000..41371569 --- /dev/null +++ b/1143-longest-common-subsequence/README.md @@ -0,0 +1,40 @@ +

1143. Longest Common Subsequence

Medium


Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

+ +

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".
  • +
+ +

A common subsequence of two strings is a subsequence that is common to both strings.

+ +

 

+

Example 1:

+ +
Input: text1 = "abcde", text2 = "ace" 
+Output: 3  
+Explanation: The longest common subsequence is "ace" and its length is 3.
+
+ +

Example 2:

+ +
Input: text1 = "abc", text2 = "abc"
+Output: 3
+Explanation: The longest common subsequence is "abc" and its length is 3.
+
+ +

Example 3:

+ +
Input: text1 = "abc", text2 = "def"
+Output: 0
+Explanation: There is no such common subsequence, so the result is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= text1.length, text2.length <= 1000
  • +
  • text1 and text2 consist of only lowercase English characters.
  • +
+
\ No newline at end of file From a2f93eb4955c5a8b645f20accf16af606a352d8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Apr 2023 18:58:44 +0530 Subject: [PATCH 0731/3167] Time: 108 ms (6.10%), Space: 24.7 MB (9.65%) - LeetHub --- .../1143-longest-common-subsequence.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1143-longest-common-subsequence/1143-longest-common-subsequence.cpp diff --git a/1143-longest-common-subsequence/1143-longest-common-subsequence.cpp b/1143-longest-common-subsequence/1143-longest-common-subsequence.cpp new file mode 100644 index 00000000..c4f5a4ae --- /dev/null +++ b/1143-longest-common-subsequence/1143-longest-common-subsequence.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + + int helper(int i, int j, string& text1, string& text2, int n, int m, vector>& dp) + { + if(i == n or j == m) + return 0; + if(dp[i][j] != -1) + return dp[i][j]; + if(text1[i] == text2[j]) + return dp[i][j] = 1 + helper(i+1, j+1, text1, text2, n ,m, dp); + return dp[i][j] = max(helper(i,j+1, text1, text2, n, m, dp), helper(i+1, j, text1, text2, n, m, dp)); + } + + + int longestCommonSubsequence(string text1, string text2) { + + int n = text1.size(), m = text2.size(); + vector> dp(n+1, vector(m+1,-1)); + return helper(0,0,text1, text2, text1.size(), text2.size(),dp); + + } +}; \ No newline at end of file From 4f6426be15b9aa6a19d462221a7004447d83e6f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Apr 2023 22:43:09 +0530 Subject: [PATCH 0732/3167] Create README - LeetHub --- 0071-simplify-path/README.md | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0071-simplify-path/README.md diff --git a/0071-simplify-path/README.md b/0071-simplify-path/README.md new file mode 100644 index 00000000..9649b293 --- /dev/null +++ b/0071-simplify-path/README.md @@ -0,0 +1,46 @@ +

71. Simplify Path

Medium


Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

+ +

In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.

+ +

The canonical path should have the following format:

+ +
    +
  • The path starts with a single slash '/'.
  • +
  • Any two directories are separated by a single slash '/'.
  • +
  • The path does not end with a trailing '/'.
  • +
  • The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')
  • +
+ +

Return the simplified canonical path.

+ +

 

+

Example 1:

+ +
Input: path = "/home/"
+Output: "/home"
+Explanation: Note that there is no trailing slash after the last directory name.
+
+ +

Example 2:

+ +
Input: path = "/../"
+Output: "/"
+Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
+
+ +

Example 3:

+ +
Input: path = "/home//foo/"
+Output: "/home/foo"
+Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= path.length <= 3000
  • +
  • path consists of English letters, digits, period '.', slash '/' or '_'.
  • +
  • path is a valid absolute Unix path.
  • +
+
\ No newline at end of file From 808ae47374f7ce5475dc2704935c3b30ba42db4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Apr 2023 22:43:12 +0530 Subject: [PATCH 0733/3167] Time: 4 ms (78.99%), Space: 10.7 MB (31.86%) - LeetHub --- 0071-simplify-path/0071-simplify-path.cpp | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0071-simplify-path/0071-simplify-path.cpp diff --git a/0071-simplify-path/0071-simplify-path.cpp b/0071-simplify-path/0071-simplify-path.cpp new file mode 100644 index 00000000..4c357b5b --- /dev/null +++ b/0071-simplify-path/0071-simplify-path.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + string simplifyPath(string path) { + + stack st; + string res; + + for(int i = 0; i Date: Thu, 13 Apr 2023 18:15:46 +0530 Subject: [PATCH 0734/3167] Create README - LeetHub --- 0946-validate-stack-sequences/README.md | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0946-validate-stack-sequences/README.md diff --git a/0946-validate-stack-sequences/README.md b/0946-validate-stack-sequences/README.md new file mode 100644 index 00000000..0412a3e2 --- /dev/null +++ b/0946-validate-stack-sequences/README.md @@ -0,0 +1,32 @@ +

946. Validate Stack Sequences

Medium


Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
+Output: true
+Explanation: We might do the following sequence:
+push(1), push(2), push(3), push(4),
+pop() -> 4,
+push(5),
+pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
+
+ +

Example 2:

+ +
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
+Output: false
+Explanation: 1 cannot be popped before 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= pushed.length <= 1000
  • +
  • 0 <= pushed[i] <= 1000
  • +
  • All the elements of pushed are unique.
  • +
  • popped.length == pushed.length
  • +
  • popped is a permutation of pushed.
  • +
+
\ No newline at end of file From 3e069cd884496f83c81deeec79defde7937daf6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:15:49 +0530 Subject: [PATCH 0735/3167] Time: 9 ms (66.37%), Space: 15.5 MB (24.28%) - LeetHub --- .../0946-validate-stack-sequences.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0946-validate-stack-sequences/0946-validate-stack-sequences.cpp diff --git a/0946-validate-stack-sequences/0946-validate-stack-sequences.cpp b/0946-validate-stack-sequences/0946-validate-stack-sequences.cpp new file mode 100644 index 00000000..11e8f431 --- /dev/null +++ b/0946-validate-stack-sequences/0946-validate-stack-sequences.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + bool validateStackSequences(vector& pushed, vector& popped) { + + + stack st; + int n = pushed.size(); + int k = 0; + + st.push(pushed[0]); + + for(int i =1 ; i Date: Thu, 13 Apr 2023 18:17:42 +0530 Subject: [PATCH 0736/3167] Attach NOTES - LeetHub --- 0946-validate-stack-sequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0946-validate-stack-sequences/NOTES.md diff --git a/0946-validate-stack-sequences/NOTES.md b/0946-validate-stack-sequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0946-validate-stack-sequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1067043ce33d686c41e1bb105f973dfc88f6473b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:17:45 +0530 Subject: [PATCH 0737/3167] Time: 9 ms (66.37%), Space: 15.5 MB (24.28%) - LeetHub From ce6b8084fd094e740509328ad5b3353d79785459 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:19:19 +0530 Subject: [PATCH 0738/3167] Attach NOTES - LeetHub From dae688d6fc7b11eb9cce570373a6d97dcac2ffae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:19:21 +0530 Subject: [PATCH 0739/3167] Time: 9 ms (66.37%), Space: 15.5 MB (24.28%) - LeetHub From b9df1c35c41c82ed7fdae11b101e7217fef2ff99 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 00:00:59 +0530 Subject: [PATCH 0740/3167] Attach NOTES - LeetHub --- 0020-valid-parentheses/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0020-valid-parentheses/NOTES.md diff --git a/0020-valid-parentheses/NOTES.md b/0020-valid-parentheses/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0020-valid-parentheses/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6dbcb724b95c778b94ece025a6252d4a52ff7101 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 00:01:02 +0530 Subject: [PATCH 0741/3167] Time: 0 ms (100.00%), Space: 6.4 MB (56.51%) - LeetHub --- .../0020-valid-parentheses.cpp | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/0020-valid-parentheses/0020-valid-parentheses.cpp b/0020-valid-parentheses/0020-valid-parentheses.cpp index 21bea9f4..c7d1d939 100644 --- a/0020-valid-parentheses/0020-valid-parentheses.cpp +++ b/0020-valid-parentheses/0020-valid-parentheses.cpp @@ -2,26 +2,24 @@ class Solution { public: bool isValid(string s) { - stack st; for(auto itr : s) { - if(itr == '(' or itr == '{' or itr == '[') - st.push(itr); - else if(!st.empty()) - { - if(st.top() == '(' and itr != ')') - return false; - else if(st.top() == '{' and itr != '}') - return false; - else if(st.top() =='[' and itr != ']') - return false; - - st.pop(); - } - else - return false; + if(itr == '(' or itr == '{' or itr == '[') + st.push(itr); + else if(!st.empty()) + { + if(st.top() == '(' and itr != ')') + return false; + if(st.top() == '{' and itr != '}') + return false; + if(st.top() == '[' and itr != ']') + return false; + st.pop(); + } + else + return false; } return st.empty(); From b185b9961406b1f08190ee2201ac922482a3f5bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 00:25:51 +0530 Subject: [PATCH 0742/3167] Attach NOTES - LeetHub From baeee79786ab2dc4948290a3b7747e059a8f3937 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 00:25:55 +0530 Subject: [PATCH 0743/3167] Time: 4 ms (38.99%), Space: 6.3 MB (82.24%) - LeetHub --- .../0020-valid-parentheses.cpp | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/0020-valid-parentheses/0020-valid-parentheses.cpp b/0020-valid-parentheses/0020-valid-parentheses.cpp index c7d1d939..71580a51 100644 --- a/0020-valid-parentheses/0020-valid-parentheses.cpp +++ b/0020-valid-parentheses/0020-valid-parentheses.cpp @@ -6,22 +6,22 @@ class Solution { for(auto itr : s) { - if(itr == '(' or itr == '{' or itr == '[') - st.push(itr); - else if(!st.empty()) - { - if(st.top() == '(' and itr != ')') - return false; - if(st.top() == '{' and itr != '}') - return false; - if(st.top() == '[' and itr != ']') - return false; - st.pop(); - } - else - return false; + if(itr == '(' or itr == '{' or itr == '[') + st.push(itr); + else if(!st.empty()) + { + if(st.top() == '(' and itr != ')') + return false; + if(st.top() == '{' and itr != '}') + return false; + if(st.top() == '[' and itr != ']') + return false; + st.pop(); + } + else + return false; } - + return st.empty(); } From 9d2e28baf7d5251b5253fe477198769250ead056 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 01:46:43 +0530 Subject: [PATCH 0744/3167] Attach NOTES - LeetHub From 79518c5a85ff7770dd9d4fb89324fc6ff03df9b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 01:46:46 +0530 Subject: [PATCH 0745/3167] Time: 99 ms (12.54%), Space: 31.1 MB (10.42%) - LeetHub --- ...apacity-to-ship-packages-within-d-days.cpp | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp b/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp index 8f6d933e..56072fbf 100644 --- a/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp +++ b/1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp @@ -1,17 +1,21 @@ +#define ll long long int class Solution { public: - int binarySearch(vector& weights, int days, int mid) + int helper(vector& weights, int mid, int days) { + ll sum = 0; int cnt = 1; int n = weights.size(); - int sum = 0; + for(int i = 0; i mid) + if(weights[i] > mid) + return false; + else if(sum + weights[i] > mid) { - ++cnt; sum = weights[i]; + ++cnt; } else sum += weights[i]; @@ -20,23 +24,25 @@ class Solution { return (cnt <= days); } - int shipWithinDays(vector& weights, int days) { - int right = accumulate(weights.begin(),weights.end(),0); - int left = *max_element(weights.begin(),weights.end()); + ll sum = accumulate(weights.begin(),weights.end(),0); + int mini = *min_element(weights.begin(),weights.end()); + + ll low = mini , high = sum; + ll ans; - int ans = left; - while(left <= right) + while(low <= high) { - int mid = (right + left)/2; - if(binarySearch(weights,days, mid)) + ll mid = low + (high - low)/2; + + if(helper(weights,mid,days)) { - ans = mid; - right = mid - 1; + ans = mid; + high = mid - 1; } else - left = mid + 1; + low = mid + 1; } return ans; From f9f0907f7493e3fe1a259772d4d8377bf8dbb29e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 10:43:40 +0530 Subject: [PATCH 0746/3167] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0516-longest-palindromic-subsequence/README.md diff --git a/0516-longest-palindromic-subsequence/README.md b/0516-longest-palindromic-subsequence/README.md new file mode 100644 index 00000000..4bf9ad9e --- /dev/null +++ b/0516-longest-palindromic-subsequence/README.md @@ -0,0 +1,27 @@ +

516. Longest Palindromic Subsequence

Medium


Given a string s, find the longest palindromic subsequence's length in s.

+ +

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

+ +

 

+

Example 1:

+ +
Input: s = "bbbab"
+Output: 4
+Explanation: One possible longest palindromic subsequence is "bbbb".
+
+ +

Example 2:

+ +
Input: s = "cbbd"
+Output: 2
+Explanation: One possible longest palindromic subsequence is "bb".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists only of lowercase English letters.
  • +
+
\ No newline at end of file From fec3b5c93c4fa559ec45d7eaf016494454e521b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Apr 2023 10:43:43 +0530 Subject: [PATCH 0747/3167] Time: 143 ms (56.24%), Space: 73.3 MB (5.06%) - LeetHub --- .../0516-longest-palindromic-subsequence.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0516-longest-palindromic-subsequence/0516-longest-palindromic-subsequence.cpp diff --git a/0516-longest-palindromic-subsequence/0516-longest-palindromic-subsequence.cpp b/0516-longest-palindromic-subsequence/0516-longest-palindromic-subsequence.cpp new file mode 100644 index 00000000..cd968a2f --- /dev/null +++ b/0516-longest-palindromic-subsequence/0516-longest-palindromic-subsequence.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + + int helper(int i, int j, string& s, string& t, int n, vector>& dp) + { + 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, s, t, n, dp); + + return dp[i][j] = max(helper(i+1,j, s, t, n, dp), helper(i, j+1, s, t, n, dp)); + } + + int longestPalindromeSubseq(string s) { + + int n = s.size(); + string t = s; + reverse(t.begin(),t.end()); + + vector> dp(n+1, vector(n+1,-1)); + + return helper(0, 0, s, t, n, dp); + } +}; \ No newline at end of file From 62538ffd93178ca19ac9f89a947a4da705fc70bb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 15 Apr 2023 18:14:48 +0530 Subject: [PATCH 0748/3167] Attach NOTES - LeetHub --- 2218-maximum-value-of-k-coins-from-piles/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2218-maximum-value-of-k-coins-from-piles/NOTES.md diff --git a/2218-maximum-value-of-k-coins-from-piles/NOTES.md b/2218-maximum-value-of-k-coins-from-piles/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2218-maximum-value-of-k-coins-from-piles/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9923e73726dd4a48cac905d02552c415198c1abc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 15 Apr 2023 18:14:52 +0530 Subject: [PATCH 0749/3167] Time: 285 ms (64.40%), Space: 18.3 MB (51.05%) - LeetHub --- ...18-maximum-value-of-k-coins-from-piles.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2218-maximum-value-of-k-coins-from-piles/2218-maximum-value-of-k-coins-from-piles.cpp diff --git a/2218-maximum-value-of-k-coins-from-piles/2218-maximum-value-of-k-coins-from-piles.cpp b/2218-maximum-value-of-k-coins-from-piles/2218-maximum-value-of-k-coins-from-piles.cpp new file mode 100644 index 00000000..532b5af4 --- /dev/null +++ b/2218-maximum-value-of-k-coins-from-piles/2218-maximum-value-of-k-coins-from-piles.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + + int helper(int i, int n, vector>& piles, int k, vector>& dp) + { + if(i >= n or k == 0) + return 0; + + if(dp[i][k] != -1) + return dp[i][k]; + + int take = 0, notTake = 0; + int sum = 0; + int size = piles[i].size(); + + notTake = helper(i+1, n , piles, k, dp); + + for(int j = 0; j < min(k,size); ++j ) + { + sum += piles[i][j]; + take = max(take, sum + helper(i+1, n, piles, k-j-1, dp)); + } + + return dp[i][k] = max(take,notTake); + } + + int maxValueOfCoins(vector>& piles, int k) { + + int n = piles.size(); + vector> dp(n+1, vector(k+1,-1)); + return helper(0, n, piles, k, dp); + + } +}; \ No newline at end of file From f29370b4a735820aa977ba352b8c192d8fb9c07a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Apr 2023 19:19:30 +0530 Subject: [PATCH 0750/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1639-number-of-ways-to-form-a-target-string-given-a-dictionary/NOTES.md diff --git a/1639-number-of-ways-to-form-a-target-string-given-a-dictionary/NOTES.md b/1639-number-of-ways-to-form-a-target-string-given-a-dictionary/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1639-number-of-ways-to-form-a-target-string-given-a-dictionary/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9669021b11a961e604bee3e4fc296516d0d26cc3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Apr 2023 19:19:33 +0530 Subject: [PATCH 0751/3167] Time: 293 ms (58.62%), Space: 80.3 MB (15.52%) - LeetHub --- ...orm-a-target-string-given-a-dictionary.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 1639-number-of-ways-to-form-a-target-string-given-a-dictionary/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.cpp diff --git a/1639-number-of-ways-to-form-a-target-string-given-a-dictionary/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.cpp b/1639-number-of-ways-to-form-a-target-string-given-a-dictionary/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.cpp new file mode 100644 index 00000000..6b578038 --- /dev/null +++ b/1639-number-of-ways-to-form-a-target-string-given-a-dictionary/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.cpp @@ -0,0 +1,56 @@ +#define ll long long int +class Solution { +public: + + int mod = 1e9+7; + + int helper(int i, int j, int n, int m , vector& words, string& target, vector>& dp, vector>& freq) + { + if(j >= m) + return 1; + + if(i >= n) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + ll take = 0, notTake = 0; + + notTake = helper(i+1, j, n, m, words, target, dp, freq); + + + if(freq[i][target[j] - 'a']) + take = ((freq[i][target[j]-'a']) * (helper(i+1, j+1, n, m, words, target, dp, freq)))%mod; + + + return dp[i][j] = (take + notTake)%mod; + } + + + int numWays(vector& words, string target) { + + int n = words[0].size(), m = target.size(); + + vector> dp(n+1, vector (m+1, -1)); + + vector> freq(n, vector(26,0)); + + for(auto &itr : words) + { + for(int i =0 ; i Date: Mon, 17 Apr 2023 07:36:56 +0530 Subject: [PATCH 0752/3167] Attach NOTES - LeetHub --- 1431-kids-with-the-greatest-number-of-candies/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1431-kids-with-the-greatest-number-of-candies/NOTES.md diff --git a/1431-kids-with-the-greatest-number-of-candies/NOTES.md b/1431-kids-with-the-greatest-number-of-candies/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1431-kids-with-the-greatest-number-of-candies/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a2f3075e22fa14bfc9838a68cdd3002f6aab023a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Apr 2023 07:37:00 +0530 Subject: [PATCH 0753/3167] Time: 8 ms (18.19%), Space: 8.8 MB (86.24%) - LeetHub --- ...ds-with-the-greatest-number-of-candies.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1431-kids-with-the-greatest-number-of-candies/1431-kids-with-the-greatest-number-of-candies.cpp diff --git a/1431-kids-with-the-greatest-number-of-candies/1431-kids-with-the-greatest-number-of-candies.cpp b/1431-kids-with-the-greatest-number-of-candies/1431-kids-with-the-greatest-number-of-candies.cpp new file mode 100644 index 00000000..4567cd88 --- /dev/null +++ b/1431-kids-with-the-greatest-number-of-candies/1431-kids-with-the-greatest-number-of-candies.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector kidsWithCandies(vector& candies, int extraCandies) { + + int n = candies.size(); + + vector res(n,0); + + int maxi = *max_element(candies.begin(),candies.end()); + + for(int i = 0; i < n; ++i) + { + if(candies[i] + extraCandies >= maxi) + res[i] = 1; + } + + return res; + + } +}; \ No newline at end of file From 629e001e2f7c40f5e9c543183825c7856f1303b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Apr 2023 07:37:41 +0530 Subject: [PATCH 0754/3167] Attach NOTES - LeetHub From f0530829e9cc76243d4df43ff65273a104ce29a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Apr 2023 07:37:44 +0530 Subject: [PATCH 0755/3167] Time: 8 ms (18.19%), Space: 8.8 MB (86.24%) - LeetHub From 9722f1103f353e752941e9968e9f59cd7542832f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:08:30 +0530 Subject: [PATCH 0756/3167] Attach NOTES - LeetHub --- 1768-merge-strings-alternately/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1768-merge-strings-alternately/NOTES.md diff --git a/1768-merge-strings-alternately/NOTES.md b/1768-merge-strings-alternately/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1768-merge-strings-alternately/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f08754172582e7c8dfc60526337f3d886cfb8b9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:08:33 +0530 Subject: [PATCH 0757/3167] Time: 4 ms (38.78%), Space: 6.4 MB (60.87%) - LeetHub --- .../1768-merge-strings-alternately.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1768-merge-strings-alternately/1768-merge-strings-alternately.cpp diff --git a/1768-merge-strings-alternately/1768-merge-strings-alternately.cpp b/1768-merge-strings-alternately/1768-merge-strings-alternately.cpp new file mode 100644 index 00000000..6789bc84 --- /dev/null +++ b/1768-merge-strings-alternately/1768-merge-strings-alternately.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string mergeAlternately(string word1, string word2) { + + int n = word1.size(), m = word2.size(); + + int i = 0, j = 0; + + string ans = ""; + + while(i < n and j < m) + { + ans += word1[i++]; + ans += word2[j++]; + } + + while(i < n) + ans += word1[i++]; + + while(j < m) + ans += word2[j++]; + + return ans; + + } +}; \ No newline at end of file From 60982ab6f9edb29a9d68d53430c51c07d0a392a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:09:11 +0530 Subject: [PATCH 0758/3167] Attach NOTES - LeetHub From 7cd9f728afe813064d9eac66fa6bd7d80b677a44 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:09:13 +0530 Subject: [PATCH 0759/3167] Time: 4 ms (38.78%), Space: 6.4 MB (60.87%) - LeetHub From c877961677b56e880195a3d5f6b40a3bc848a6b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:44:49 +0530 Subject: [PATCH 0760/3167] Create README - LeetHub --- 0015-3sum/README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0015-3sum/README.md diff --git a/0015-3sum/README.md b/0015-3sum/README.md new file mode 100644 index 00000000..6dd16400 --- /dev/null +++ b/0015-3sum/README.md @@ -0,0 +1,39 @@ +

15. 3Sum

Medium


Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

+ +

Notice that the solution set must not contain duplicate triplets.

+ +

 

+

Example 1:

+ +
Input: nums = [-1,0,1,2,-1,-4]
+Output: [[-1,-1,2],[-1,0,1]]
+Explanation: 
+nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
+nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
+nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
+The distinct triplets are [-1,0,1] and [-1,-1,2].
+Notice that the order of the output and the order of the triplets does not matter.
+
+ +

Example 2:

+ +
Input: nums = [0,1,1]
+Output: []
+Explanation: The only possible triplet does not sum up to 0.
+
+ +

Example 3:

+ +
Input: nums = [0,0,0]
+Output: [[0,0,0]]
+Explanation: The only possible triplet sums up to 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 3000
  • +
  • -105 <= nums[i] <= 105
  • +
+
\ No newline at end of file From a853c60cdf169f1b0ec30abede3d18d3b8aa6fb5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:44:53 +0530 Subject: [PATCH 0761/3167] Time: 1406 ms (22.45%), Space: 191.5 MB (21.58%) - LeetHub --- 0015-3sum/0015-3sum.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0015-3sum/0015-3sum.cpp diff --git a/0015-3sum/0015-3sum.cpp b/0015-3sum/0015-3sum.cpp new file mode 100644 index 00000000..d58d496e --- /dev/null +++ b/0015-3sum/0015-3sum.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + + set> s; + vector> ans; + + sort(nums.begin(),nums.end()); + + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + int j = i+1, k = n-1; + + while(j < k) + { + int sum = nums[i] + nums[j] + nums[k]; + if((sum) == 0) + { + s.insert({nums[i],nums[j],nums[k]}); + ++j,--k; + } + else if(sum < 0) + ++j; + else + --k; + } + } + + for(auto itr : s) + ans.push_back(itr); + + return ans; + } +}; \ No newline at end of file From b734c73229f934862ed989d5e3603dad84d3bb4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:45:55 +0530 Subject: [PATCH 0762/3167] Attach NOTES - LeetHub --- 0015-3sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0015-3sum/NOTES.md diff --git a/0015-3sum/NOTES.md b/0015-3sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0015-3sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e1bf3b26b812d49cd729f82cff26aa7a87b5ad65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:46:29 +0530 Subject: [PATCH 0763/3167] Attach NOTES - LeetHub From 507e1e4eb04e63ad28a5ff8c7b3faf65f6fe1c5f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:46:32 +0530 Subject: [PATCH 0764/3167] Time: 1406 ms (22.45%), Space: 191.5 MB (21.58%) - LeetHub From 48e855b8b16bd2cb2445fc37808ac0346294adc0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:46:58 +0530 Subject: [PATCH 0765/3167] Attach NOTES - LeetHub From 64f593e7ab0e561ddb95e80ea42dc8f2fdd36853 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:47:01 +0530 Subject: [PATCH 0766/3167] Time: 1406 ms (22.45%), Space: 191.5 MB (21.58%) - LeetHub From a9b5b101c9284d396a2749518cfb1e44ddd3908e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Apr 2023 20:13:35 +0530 Subject: [PATCH 0767/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1372-longest-zigzag-path-in-a-binary-tree/README.md diff --git a/1372-longest-zigzag-path-in-a-binary-tree/README.md b/1372-longest-zigzag-path-in-a-binary-tree/README.md new file mode 100644 index 00000000..c56b9308 --- /dev/null +++ b/1372-longest-zigzag-path-in-a-binary-tree/README.md @@ -0,0 +1,44 @@ +

1372. Longest ZigZag Path in a Binary Tree

Medium


You are given the root of a binary tree.

+ +

A ZigZag path for a binary tree is defined as follow:

+ +
    +
  • Choose any node in the binary tree and a direction (right or left).
  • +
  • If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
  • +
  • Change the direction from right to left or from left to right.
  • +
  • Repeat the second and third steps until you can't move in the tree.
  • +
+ +

Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).

+ +

Return the longest ZigZag path contained in that tree.

+ +

 

+

Example 1:

+ +
Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
+Output: 3
+Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
+
+ +

Example 2:

+ +
Input: root = [1,1,1,null,1,null,null,1,1,null,1]
+Output: 4
+Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right).
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 5 * 104].
  • +
  • 1 <= Node.val <= 100
  • +
+
\ No newline at end of file From dc9db2cab68ddaf1214c2d1c667bcdef094da2c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Apr 2023 20:13:38 +0530 Subject: [PATCH 0768/3167] Time: 162 ms (86.76%), Space: 94.2 MB (63.41%) - LeetHub --- ...2-longest-zigzag-path-in-a-binary-tree.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1372-longest-zigzag-path-in-a-binary-tree/1372-longest-zigzag-path-in-a-binary-tree.cpp diff --git a/1372-longest-zigzag-path-in-a-binary-tree/1372-longest-zigzag-path-in-a-binary-tree.cpp b/1372-longest-zigzag-path-in-a-binary-tree/1372-longest-zigzag-path-in-a-binary-tree.cpp new file mode 100644 index 00000000..4eb706c6 --- /dev/null +++ b/1372-longest-zigzag-path-in-a-binary-tree/1372-longest-zigzag-path-in-a-binary-tree.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 maxi; + + void helper(TreeNode* root, int dir, int len) + { + if(!root) + return; + + maxi = max(maxi,len); + + if(dir == 0) + { + helper(root->left, dir, 1); + helper(root->right, dir ^ 1, len + 1); + } + else + { + helper(root->left, dir ^ 1, len + 1); + helper(root->right, dir, 1); + } + } + + int longestZigZag(TreeNode* root) { + + maxi = 0; + + helper(root->left, 0, 1); + + helper(root->right, 1, 1); + + return maxi; + + } +}; \ No newline at end of file From 65d8965e8f30bd1b0c81f7f904f1f48cc6a74ea4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:41:55 +0530 Subject: [PATCH 0769/3167] Create README - LeetHub --- 0662-maximum-width-of-binary-tree/README.md | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0662-maximum-width-of-binary-tree/README.md diff --git a/0662-maximum-width-of-binary-tree/README.md b/0662-maximum-width-of-binary-tree/README.md new file mode 100644 index 00000000..3a8386e1 --- /dev/null +++ b/0662-maximum-width-of-binary-tree/README.md @@ -0,0 +1,38 @@ +

662. Maximum Width of Binary Tree

Medium


Given the root of a binary tree, return the maximum width of the given tree.

+ +

The maximum width of a tree is the maximum width among all levels.

+ +

The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.

+ +

It is guaranteed that the answer will in the range of a 32-bit signed integer.

+ +

 

+

Example 1:

+ +
Input: root = [1,3,2,5,3,null,9]
+Output: 4
+Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
+
+ +

Example 2:

+ +
Input: root = [1,3,2,5,null,null,9,6,null,7]
+Output: 7
+Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
+
+ +

Example 3:

+ +
Input: root = [1,3,2,5]
+Output: 2
+Explanation: The maximum width exists in the second level with length 2 (3,2).
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 3000].
  • +
  • -100 <= Node.val <= 100
  • +
+
\ No newline at end of file From 3702e7eff5d703716caba8555009109028a94cba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:41:58 +0530 Subject: [PATCH 0770/3167] Time: 14 ms (22.50%), Space: 17.3 MB (63.85%) - LeetHub --- .../0662-maximum-width-of-binary-tree.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0662-maximum-width-of-binary-tree/0662-maximum-width-of-binary-tree.cpp diff --git a/0662-maximum-width-of-binary-tree/0662-maximum-width-of-binary-tree.cpp b/0662-maximum-width-of-binary-tree/0662-maximum-width-of-binary-tree.cpp new file mode 100644 index 00000000..8effb2e0 --- /dev/null +++ b/0662-maximum-width-of-binary-tree/0662-maximum-width-of-binary-tree.cpp @@ -0,0 +1,52 @@ +/** + * 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) {} + * }; + */ +#define ll long long int + +class Solution { +public: + int widthOfBinaryTree(TreeNode* root) { + + if(!root) + return 0; + + ll res = 1; + + queue> q; + + q.push({root,0}); + + while(!q.empty()) + { + ll start = q.front().second; + ll end = q.back().second; + + res = max(res, end - start + 1); + + ll size = q.size(); + + for(int i = 0; i < size; ++i) + { + ll idx = q.front().second - start; + TreeNode* curr = q.front().first; + + q.pop(); + + if(curr->left) + q.push({curr->left,(ll)(2*idx)}); + if(curr->right) + q.push({curr->right,(ll)(2*idx)+ 1}); + } + } + + return (int)res; + } +}; \ No newline at end of file From 380387380c5b58f90db810fe16e6fb0a9e3dd84e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:48:12 +0530 Subject: [PATCH 0771/3167] Attach NOTES - LeetHub --- 0662-maximum-width-of-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0662-maximum-width-of-binary-tree/NOTES.md diff --git a/0662-maximum-width-of-binary-tree/NOTES.md b/0662-maximum-width-of-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0662-maximum-width-of-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 13d30a79d7e8665963874d2f77364dae7045e4e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:48:15 +0530 Subject: [PATCH 0772/3167] Time: 14 ms (22.50%), Space: 17.3 MB (63.85%) - LeetHub From 206b72e6c370f7a0b56bb73d874a86a74664f7ae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Apr 2023 22:31:31 +0530 Subject: [PATCH 0773/3167] Create README - LeetHub --- 0879-profitable-schemes/README.md | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0879-profitable-schemes/README.md diff --git a/0879-profitable-schemes/README.md b/0879-profitable-schemes/README.md new file mode 100644 index 00000000..175aaf7f --- /dev/null +++ b/0879-profitable-schemes/README.md @@ -0,0 +1,33 @@ +

879. Profitable Schemes

Hard


There is a group of n members, and a list of various crimes they could commit. The ith crime generates a profit[i] and requires group[i] members to participate in it. If a member participates in one crime, that member can't participate in another crime.

+ +

Let's call a profitable scheme any subset of these crimes that generates at least minProfit profit, and the total number of members participating in that subset of crimes is at most n.

+ +

Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 5, minProfit = 3, group = [2,2], profit = [2,3]
+Output: 2
+Explanation: To make a profit of at least 3, the group could either commit crimes 0 and 1, or just crime 1.
+In total, there are 2 schemes.
+ +

Example 2:

+ +
Input: n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
+Output: 7
+Explanation: To make a profit of at least 5, the group could commit any crimes, as long as they commit one.
+There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= minProfit <= 100
  • +
  • 1 <= group.length <= 100
  • +
  • 1 <= group[i] <= 100
  • +
  • profit.length == group.length
  • +
  • 0 <= profit[i] <= 100
  • +
+
\ No newline at end of file From 035aa7bd95ad7c8a5f5a146f4284e1e14ed40687 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Apr 2023 22:31:34 +0530 Subject: [PATCH 0774/3167] Time: 167 ms (63.21%), Space: 12.9 MB (66.51%) - LeetHub --- .../0879-profitable-schemes.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0879-profitable-schemes/0879-profitable-schemes.cpp diff --git a/0879-profitable-schemes/0879-profitable-schemes.cpp b/0879-profitable-schemes/0879-profitable-schemes.cpp new file mode 100644 index 00000000..ed871928 --- /dev/null +++ b/0879-profitable-schemes/0879-profitable-schemes.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int dp[101][101][101]; + const int mod = 1e9 + 7; + int help(int idx, int members, int pro, int n, int minProfit, vector &group, vector &profit) + { + if (idx == group.size()) + return pro >= minProfit; + + if (dp[idx][members][pro] != -1) + return dp[idx][members][pro]; + + int take = 0; + if (members + group[idx] <= n) + take = help(idx + 1, members + group[idx], min(minProfit, pro + profit[idx]), n, minProfit, group, profit); + + int nottake = help(idx + 1, members, pro, n, minProfit, group, profit); + return dp[idx][members][pro] = ((long)take + (long)nottake) % mod; + } + int profitableSchemes(int n, int minProfit, vector &group, vector &profit) + { + memset(dp, -1, sizeof(dp)); + return help(0, 0, 0, n, minProfit, group, profit); + } +}; \ No newline at end of file From 00cf3e175cc2db130609a7e218142225585e4852 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 09:12:20 +0530 Subject: [PATCH 0775/3167] Attach NOTES - LeetHub --- 0214-shortest-palindrome/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0214-shortest-palindrome/NOTES.md diff --git a/0214-shortest-palindrome/NOTES.md b/0214-shortest-palindrome/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0214-shortest-palindrome/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 18d1ac7770438271f0d417db1b4182174fcc67f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 09:12:23 +0530 Subject: [PATCH 0776/3167] Time: 4 ms (66.31%), Space: 7.9 MB (52.16%) - LeetHub --- .../0214-shortest-palindrome.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0214-shortest-palindrome/0214-shortest-palindrome.cpp diff --git a/0214-shortest-palindrome/0214-shortest-palindrome.cpp b/0214-shortest-palindrome/0214-shortest-palindrome.cpp new file mode 100644 index 00000000..96fd9852 --- /dev/null +++ b/0214-shortest-palindrome/0214-shortest-palindrome.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + string shortestPalindrome(string s) { + + string rev = s; + int n = s.size(); + + reverse(rev.begin(), rev.end()); + + string str = s + '#' + rev; + + int size = str.size(); + + vector lps(size,0); + + for(int i = 1; i < size; ++i) + { + int j = lps[i-1]; + + while(j > 0 and str[i] != str[j]) + j = lps[j-1]; + + if(str[i] == str[j]) + ++j; + + lps[i] = j; + } + + string ans = rev.substr(0, n - lps[size - 1]) + s; + + return ans; + } +}; \ No newline at end of file From bfb2f658181b44ade89c95d7c00f2a89d4e4b406 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 11:00:41 +0530 Subject: [PATCH 0777/3167] Attach NOTES - LeetHub --- 0048-rotate-image/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0048-rotate-image/NOTES.md diff --git a/0048-rotate-image/NOTES.md b/0048-rotate-image/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0048-rotate-image/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9cd962aadef647a0dbad2d57e4a54a5022108d6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 11:00:44 +0530 Subject: [PATCH 0778/3167] Time: 0 ms (100.00%), Space: 6.9 MB (94.42%) - LeetHub --- 0048-rotate-image/0048-rotate-image.cpp | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0048-rotate-image/0048-rotate-image.cpp diff --git a/0048-rotate-image/0048-rotate-image.cpp b/0048-rotate-image/0048-rotate-image.cpp new file mode 100644 index 00000000..93b35e47 --- /dev/null +++ b/0048-rotate-image/0048-rotate-image.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + void rotate(vector>& matrix) { + + + int n = matrix.size(); + int m = matrix[0].size(); + + for(int i = 0; i Date: Sat, 22 Apr 2023 13:44:19 +0530 Subject: [PATCH 0779/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1312-minimum-insertion-steps-to-make-a-string-palindrome/README.md diff --git a/1312-minimum-insertion-steps-to-make-a-string-palindrome/README.md b/1312-minimum-insertion-steps-to-make-a-string-palindrome/README.md new file mode 100644 index 00000000..3049ba4a --- /dev/null +++ b/1312-minimum-insertion-steps-to-make-a-string-palindrome/README.md @@ -0,0 +1,36 @@ +

1312. Minimum Insertion Steps to Make a String Palindrome

Hard


Given a string s. In one step you can insert any character at any index of the string.

+ +

Return the minimum number of steps to make s palindrome.

+ +

Palindrome String is one that reads the same backward as well as forward.

+ +

 

+

Example 1:

+ +
Input: s = "zzazz"
+Output: 0
+Explanation: The string "zzazz" is already palindrome we do not need any insertions.
+
+ +

Example 2:

+ +
Input: s = "mbadm"
+Output: 2
+Explanation: String can be "mbdadbm" or "mdbabdm".
+
+ +

Example 3:

+ +
Input: s = "leetcode"
+Output: 5
+Explanation: Inserting 5 characters the string becomes "leetcodocteel".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 500
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From 64a580c9b871d9309bae551e11a8d24963bbcad0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 18:22:20 +0530 Subject: [PATCH 0780/3167] Create README - LeetHub From 8c04fcf6df43cda3a1f0e59de8cca353612e39a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 18:22:20 +0530 Subject: [PATCH 0781/3167] Attach NOTES - LeetHub From 1e4988375e8aeda91184a43b2a9632fb039c5696 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:10:45 +0530 Subject: [PATCH 0782/3167] Attach NOTES - LeetHub --- 0050-powx-n/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0050-powx-n/NOTES.md diff --git a/0050-powx-n/NOTES.md b/0050-powx-n/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0050-powx-n/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 551695288edc1c991eecfef1f04021b83355c7c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:10:47 +0530 Subject: [PATCH 0783/3167] Time: 0 ms (100.00%), Space: 5.9 MB (94.86%) - LeetHub --- 0050-powx-n/0050-powx-n.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/0050-powx-n/0050-powx-n.cpp b/0050-powx-n/0050-powx-n.cpp index c5487890..9c155c85 100644 --- a/0050-powx-n/0050-powx-n.cpp +++ b/0050-powx-n/0050-powx-n.cpp @@ -2,27 +2,32 @@ class Solution { public: double myPow(double x, int n) { - // fast power + double res = 1.0; long long num = n; - if(num < 0) num *= -1; - - double result = 1.0; + if(num < 0) + { + num *= -1; + } while(num > 0) { if(num & 1) - result = result * x; - + res = res * x; + x = (x * x); + num >>= 1; } - + if(n < 0) - result = (double)1.0/(double)result; + { + res = (double)1.0/(double)res; + } + + return res; - return result; } }; \ No newline at end of file From 611b04793428c0739e75516a1f2bfe0cc92d2d3e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Apr 2023 07:26:01 +0530 Subject: [PATCH 0784/3167] Create README - LeetHub --- 0018-4sum/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0018-4sum/README.md diff --git a/0018-4sum/README.md b/0018-4sum/README.md new file mode 100644 index 00000000..31fd5edd --- /dev/null +++ b/0018-4sum/README.md @@ -0,0 +1,32 @@ +

18. 4Sum

Medium


Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

+ +
    +
  • 0 <= a, b, c, d < n
  • +
  • a, b, c, and d are distinct.
  • +
  • nums[a] + nums[b] + nums[c] + nums[d] == target
  • +
+ +

You may return the answer in any order.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: nums = [2,2,2,2,2], target = 8
+Output: [[2,2,2,2]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 200
  • +
  • -109 <= nums[i] <= 109
  • +
  • -109 <= target <= 109
  • +
+
\ No newline at end of file From 7172bae4bc591a1bc14771740e7e1220c03d1bb5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Apr 2023 07:26:04 +0530 Subject: [PATCH 0785/3167] Time: 387 ms (21.80%), Space: 49.5 MB (23.45%) - LeetHub --- 0018-4sum/0018-4sum.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0018-4sum/0018-4sum.cpp diff --git a/0018-4sum/0018-4sum.cpp b/0018-4sum/0018-4sum.cpp new file mode 100644 index 00000000..a3d337f6 --- /dev/null +++ b/0018-4sum/0018-4sum.cpp @@ -0,0 +1,43 @@ +class Solution { +public: + vector> fourSum(vector& nums, int target) { + + int n = nums.size(); + + int mod = 1e9 + 7; + + vector> ans; + + set> used; + + sort(begin(nums), end(nums)); + + for(int i = 0; i < n; ++i) + { + for(int j = i+1; j < n; ++j) + { + int k = j+1, l = n-1; + + while(k < l) + { + long long sum = (((((nums[i] % mod) + nums[j])%mod + nums[k])% mod + nums[l])% mod) % mod; + + if(sum == target) + { + used.insert({nums[i], nums[j], nums[k], nums[l]}); + ++k,--l; + } + else if(sum < target) + ++k; + else + --l; + } + } + } + + for(auto itr : used) + ans.push_back(itr); + + return ans; + } +}; \ No newline at end of file From 6a5f6e18db40a847504f8e4c5543c3d79839ac35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Apr 2023 07:26:23 +0530 Subject: [PATCH 0786/3167] Attach NOTES - LeetHub --- 0018-4sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0018-4sum/NOTES.md diff --git a/0018-4sum/NOTES.md b/0018-4sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0018-4sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8ba6b5efb2798a12b3d098b5132959b2a43478b7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Apr 2023 07:26:26 +0530 Subject: [PATCH 0787/3167] Time: 387 ms (21.80%), Space: 49.5 MB (23.45%) - LeetHub From 5d3d4b8f72fd5d4a2a4f4cc325a95af6794ee365 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 00:40:27 +0530 Subject: [PATCH 0788/3167] Attach NOTES - LeetHub --- 1416-restore-the-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1416-restore-the-array/NOTES.md diff --git a/1416-restore-the-array/NOTES.md b/1416-restore-the-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1416-restore-the-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fc76b3f3b2db3f3862ee2394b3c43bb7b14a6657 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 00:40:31 +0530 Subject: [PATCH 0789/3167] Time: 76 ms (52.91%), Space: 20.8 MB (36.05%) - LeetHub --- .../1416-restore-the-array.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1416-restore-the-array/1416-restore-the-array.cpp diff --git a/1416-restore-the-array/1416-restore-the-array.cpp b/1416-restore-the-array/1416-restore-the-array.cpp new file mode 100644 index 00000000..17041e34 --- /dev/null +++ b/1416-restore-the-array/1416-restore-the-array.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + int MOD = 1000000007; + + int solve(string& s, int& k, vector&dp, int startIdx) + { + if (startIdx == s.size()) return 1; + + if (dp[startIdx] != -1) return dp[startIdx]; + long long currNum = 0, ways = 0; + + for (int i = startIdx; i < s.size(); i++) + { + int currDig = s[i] - '0'; + currNum = (currNum * 10) + currDig; + + if (currNum < 1 || currNum > k) break; + int nextWays = solve(s, k, dp, i + 1); + ways = (ways + nextWays) % MOD; + } + + return dp[startIdx] = ways; + } + + int numberOfArrays(string s, int k) + { + vectordp(s.size(), -1); + int ans = solve(s, k, dp, 0); + return ans; + } +}; \ No newline at end of file From f5b082a42c06d33313275d3a901e1259fa06cf10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 08:10:30 +0530 Subject: [PATCH 0790/3167] Create README - LeetHub --- 1046-last-stone-weight/README.md | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1046-last-stone-weight/README.md diff --git a/1046-last-stone-weight/README.md b/1046-last-stone-weight/README.md new file mode 100644 index 00000000..34945b64 --- /dev/null +++ b/1046-last-stone-weight/README.md @@ -0,0 +1,39 @@ +

1046. Last Stone Weight

Easy


You are given an array of integers stones where stones[i] is the weight of the ith stone.

+ +

We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:

+ +
    +
  • If x == y, both stones are destroyed, and
  • +
  • If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
  • +
+ +

At the end of the game, there is at most one stone left.

+ +

Return the weight of the last remaining stone. If there are no stones left, return 0.

+ +

 

+

Example 1:

+ +
Input: stones = [2,7,4,1,8,1]
+Output: 1
+Explanation: 
+We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
+we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
+we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
+we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
+
+ +

Example 2:

+ +
Input: stones = [1]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= stones.length <= 30
  • +
  • 1 <= stones[i] <= 1000
  • +
+
\ No newline at end of file From c23f352f5523554371384e8420eb00a2ce255318 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 08:10:31 +0530 Subject: [PATCH 0791/3167] Attach NOTES - LeetHub --- 1046-last-stone-weight/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1046-last-stone-weight/NOTES.md diff --git a/1046-last-stone-weight/NOTES.md b/1046-last-stone-weight/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1046-last-stone-weight/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7f1a7a2b6a2c2913a39f158458672083ae4b451c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 08:10:34 +0530 Subject: [PATCH 0792/3167] Time: 5 ms (28.30%), Space: 7.6 MB (41.71%) - LeetHub --- .../1046-last-stone-weight.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1046-last-stone-weight/1046-last-stone-weight.cpp diff --git a/1046-last-stone-weight/1046-last-stone-weight.cpp b/1046-last-stone-weight/1046-last-stone-weight.cpp new file mode 100644 index 00000000..c5b2e06e --- /dev/null +++ b/1046-last-stone-weight/1046-last-stone-weight.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int lastStoneWeight(vector& stones) { + + priority_queue pq(stones.begin(),stones.end()); + + while(!pq.empty()) + { + int a, b; + + a = pq.top(); + pq.pop(); + + if(pq.empty()) + return a; + else + { + b = pq.top(); + pq.pop(); + } + + if(a == b) + continue; + else + pq.push(a - b); + } + + return 0; + } +}; \ No newline at end of file From 738fc7d85f33075c9e13d9d27efa5d3703e70ad8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 08:11:56 +0530 Subject: [PATCH 0793/3167] Attach NOTES - LeetHub From 0818a1d0219d324512054df31fbabb30644dd44d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 22:52:39 +0530 Subject: [PATCH 0794/3167] Create README - LeetHub --- 0062-unique-paths/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0062-unique-paths/README.md diff --git a/0062-unique-paths/README.md b/0062-unique-paths/README.md new file mode 100644 index 00000000..69eab055 --- /dev/null +++ b/0062-unique-paths/README.md @@ -0,0 +1,30 @@ +

62. Unique Paths

Medium


There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

+ +

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

+ +

The test cases are generated so that the answer will be less than or equal to 2 * 109.

+ +

 

+

Example 1:

+ +
Input: m = 3, n = 7
+Output: 28
+
+ +

Example 2:

+ +
Input: m = 3, n = 2
+Output: 3
+Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
+1. Right -> Down -> Down
+2. Down -> Down -> Right
+3. Down -> Right -> Down
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= m, n <= 100
  • +
+
\ No newline at end of file From 5749be2eb6876311d0dde92f3e35f79c8f247437 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 22:52:43 +0530 Subject: [PATCH 0795/3167] Time: 0 ms (100.00%), Space: 6.1 MB (75.92%) - LeetHub --- 0062-unique-paths/0062-unique-paths.cpp | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0062-unique-paths/0062-unique-paths.cpp diff --git a/0062-unique-paths/0062-unique-paths.cpp b/0062-unique-paths/0062-unique-paths.cpp new file mode 100644 index 00000000..acfca4e2 --- /dev/null +++ b/0062-unique-paths/0062-unique-paths.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + + int helper(int i , int j, int n, int m, vector>& dp) + { + if(i >= n or j >= m) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + if(i == n-1 and j == m-1) + return 1; + + return dp[i][j] = helper(i+1,j, n, m, dp) + helper(i, j + 1, n, m, dp); + } + + + int uniquePaths(int m, int n) { + + // vector> dp(m+1, vector(n+1,0)); + + vector prev(n+1,0), curr(n+1,0); + + // return helper(0,0,m,n, dp); + + for(int i = m-1; i>=0; --i) + { + for(int j = n-1; j >= 0; --j) + { + if(i == m-1 and j == n-1) + curr[j] = 1; + else + curr[j] = prev[j] + curr[j + 1]; + } + prev = curr; + } + + return prev[0]; + } +}; \ No newline at end of file From 78c12815b53ead831271ee8e4a3ae0c3bff163b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 23:58:51 +0530 Subject: [PATCH 0796/3167] Attach NOTES - LeetHub --- 0062-unique-paths/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0062-unique-paths/NOTES.md diff --git a/0062-unique-paths/NOTES.md b/0062-unique-paths/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0062-unique-paths/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0cac1769cf71d9035568007009a6bd5ca771b2d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Apr 2023 23:58:54 +0530 Subject: [PATCH 0797/3167] Time: 0 ms (100.00%), Space: 6 MB (89.80%) - LeetHub --- 0062-unique-paths/0062-unique-paths.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/0062-unique-paths/0062-unique-paths.cpp b/0062-unique-paths/0062-unique-paths.cpp index acfca4e2..c2c876bf 100644 --- a/0062-unique-paths/0062-unique-paths.cpp +++ b/0062-unique-paths/0062-unique-paths.cpp @@ -1,37 +1,36 @@ class Solution { public: - int helper(int i , int j, int n, int m, vector>& dp) + int helper(int i , int j, int n , int m, vector>& dp) { - if(i >= n or j >= m) - return 0; + if(i == n-1 and j == m-1) + return 1; if(dp[i][j] != -1) return dp[i][j]; - if(i == n-1 and j == m-1) - return 1; + if(i >= n or j >= m) + return 0; - return dp[i][j] = helper(i+1,j, n, m, dp) + helper(i, j + 1, n, m, dp); + return dp[i][j] = helper(i+1, j, n, m, dp) + helper(i, j+1, n, m, dp); } - int uniquePaths(int m, int n) { // vector> dp(m+1, vector(n+1,0)); - vector prev(n+1,0), curr(n+1,0); + // return helper(0,0,m ,n, dp); - // return helper(0,0,m,n, dp); + vector prev(n+1, 0), curr(n+1, 0); - for(int i = m-1; i>=0; --i) + for(int i = m-1; i>=0 ; --i) { for(int j = n-1; j >= 0; --j) { if(i == m-1 and j == n-1) curr[j] = 1; else - curr[j] = prev[j] + curr[j + 1]; + curr[j] = prev[j] + curr[j+1]; } prev = curr; } From 6c4a42e9a133cd9e72a8740e63a4d0aebc7a04a2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:30:17 +0530 Subject: [PATCH 0798/3167] Attach NOTES - LeetHub From 1249292d6cfbdf99346d5ebda29581a3f985f3fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:30:20 +0530 Subject: [PATCH 0799/3167] Time: 0 ms (100.00%), Space: 6 MB (89.80%) - LeetHub From 1e1e32e860f32fef580f3b599afe24a30e8180ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 07:09:58 +0530 Subject: [PATCH 0800/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2336-smallest-number-in-infinite-set/README.md diff --git a/2336-smallest-number-in-infinite-set/README.md b/2336-smallest-number-in-infinite-set/README.md new file mode 100644 index 00000000..7e19ea4f --- /dev/null +++ b/2336-smallest-number-in-infinite-set/README.md @@ -0,0 +1,40 @@ +

2336. Smallest Number in Infinite Set

Medium


You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].

+ +

Implement the SmallestInfiniteSet class:

+ +
    +
  • SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
  • +
  • int popSmallest() Removes and returns the smallest integer contained in the infinite set.
  • +
  • void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.
  • +
+ +

 

+

Example 1:

+ +
Input
+["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
+[[], [2], [], [], [], [1], [], [], []]
+Output
+[null, null, 1, 2, 3, null, 1, 4, 5]
+
+Explanation
+SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
+smallestInfiniteSet.addBack(2);    // 2 is already in the set, so no change is made.
+smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
+smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
+smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
+smallestInfiniteSet.addBack(1);    // 1 is added back to the set.
+smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
+                                   // is the smallest number, and remove it from the set.
+smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
+smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num <= 1000
  • +
  • At most 1000 calls will be made in total to popSmallest and addBack.
  • +
+
\ No newline at end of file From 0e4a341e3d1633c156ea75d9a573c324afc32629 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 07:09:59 +0530 Subject: [PATCH 0801/3167] Attach NOTES - LeetHub --- 2336-smallest-number-in-infinite-set/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2336-smallest-number-in-infinite-set/NOTES.md diff --git a/2336-smallest-number-in-infinite-set/NOTES.md b/2336-smallest-number-in-infinite-set/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2336-smallest-number-in-infinite-set/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1fa654954d3a053f3f62539a2b47fa7dd057f09f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 07:10:02 +0530 Subject: [PATCH 0802/3167] Time: 128 ms (32.50%), Space: 46.2 MB (25.04%) - LeetHub --- .../2336-smallest-number-in-infinite-set.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2336-smallest-number-in-infinite-set/2336-smallest-number-in-infinite-set.cpp diff --git a/2336-smallest-number-in-infinite-set/2336-smallest-number-in-infinite-set.cpp b/2336-smallest-number-in-infinite-set/2336-smallest-number-in-infinite-set.cpp new file mode 100644 index 00000000..2302502f --- /dev/null +++ b/2336-smallest-number-in-infinite-set/2336-smallest-number-in-infinite-set.cpp @@ -0,0 +1,31 @@ +class SmallestInfiniteSet { +public: + + set st; + + SmallestInfiniteSet() { + for(int i = 1 ; i<= 1000; ++i) + st.insert(i); + } + + int popSmallest() { + + int val = *st.begin(); + st.erase(val); + return val; + + } + + void addBack(int num) { + + if(st.find(num) == st.end()) + st.insert(num); + } +}; + +/** + * Your SmallestInfiniteSet object will be instantiated and called as such: + * SmallestInfiniteSet* obj = new SmallestInfiniteSet(); + * int param_1 = obj->popSmallest(); + * obj->addBack(num); + */ \ No newline at end of file From 7b4147707a0ec45e2e788d966b4e5e36560c40f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 07:12:45 +0530 Subject: [PATCH 0803/3167] Attach NOTES - LeetHub From 3c885d65b7f73870a687f1262004aa8f5c20b291 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 07:12:47 +0530 Subject: [PATCH 0804/3167] Time: 128 ms (32.50%), Space: 46.2 MB (25.04%) - LeetHub From e7d7f9356ff1bf332d191b2ad8609a369b746034 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:44:31 +0530 Subject: [PATCH 0805/3167] Create README - LeetHub --- 0128-longest-consecutive-sequence/README.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0128-longest-consecutive-sequence/README.md diff --git a/0128-longest-consecutive-sequence/README.md b/0128-longest-consecutive-sequence/README.md new file mode 100644 index 00000000..2938c99d --- /dev/null +++ b/0128-longest-consecutive-sequence/README.md @@ -0,0 +1,26 @@ +

128. Longest Consecutive Sequence

Medium


Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [100,4,200,1,3,2]
+Output: 4
+Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 590c78cc6dd764ff9ddfdcc7908d58d67512be33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:44:31 +0530 Subject: [PATCH 0806/3167] Attach NOTES - LeetHub --- 0128-longest-consecutive-sequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0128-longest-consecutive-sequence/NOTES.md diff --git a/0128-longest-consecutive-sequence/NOTES.md b/0128-longest-consecutive-sequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0128-longest-consecutive-sequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d2105948d6fe7e2277e85fc7a70adbb46826f6d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:44:34 +0530 Subject: [PATCH 0807/3167] Time: 70 ms (92.92%), Space: 33.8 MB (91.28%) - LeetHub --- .../0128-longest-consecutive-sequence.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp diff --git a/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp b/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp new file mode 100644 index 00000000..a658327b --- /dev/null +++ b/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int longestConsecutive(vector& nums) { + + int n = nums.size(); + + if(n == 0) + return 0; + + sort(nums.begin(),nums.end()); + + int ans = 1, curr = 1; + + for(int i = 1; i Date: Tue, 25 Apr 2023 22:44:49 +0530 Subject: [PATCH 0808/3167] Attach NOTES - LeetHub From 3c47b9f2ef22e764b1a32339b10a761e9cbf2046 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:44:52 +0530 Subject: [PATCH 0809/3167] Time: 70 ms (92.92%), Space: 33.8 MB (91.28%) - LeetHub From 4b940b6ff148f5d0025a116445aac763d02b0f84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:46:29 +0530 Subject: [PATCH 0810/3167] Attach NOTES - LeetHub From c7fd5861bb0e58d74b856492e8614d58b98b0f71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:46:32 +0530 Subject: [PATCH 0811/3167] Time: 70 ms (92.92%), Space: 33.8 MB (91.28%) - LeetHub From 3904ef13d7face3708d24c7db4ee5061878960d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:47:05 +0530 Subject: [PATCH 0812/3167] Attach NOTES - LeetHub From 10a62298c97cc5a1220ec6be104cf2dde36d03d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:47:09 +0530 Subject: [PATCH 0813/3167] Time: 70 ms (92.92%), Space: 33.8 MB (91.28%) - LeetHub From c36f0a656b31b5bd6b08178b558c793b553970f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 23:03:07 +0530 Subject: [PATCH 0814/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0003-longest-substring-without-repeating-characters/README.md diff --git a/0003-longest-substring-without-repeating-characters/README.md b/0003-longest-substring-without-repeating-characters/README.md new file mode 100644 index 00000000..857ec8b4 --- /dev/null +++ b/0003-longest-substring-without-repeating-characters/README.md @@ -0,0 +1,33 @@ +

3. Longest Substring Without Repeating Characters

Medium


Given a string s, find the length of the longest substring without repeating characters.

+ +

 

+

Example 1:

+ +
Input: s = "abcabcbb"
+Output: 3
+Explanation: The answer is "abc", with the length of 3.
+
+ +

Example 2:

+ +
Input: s = "bbbbb"
+Output: 1
+Explanation: The answer is "b", with the length of 1.
+
+ +

Example 3:

+ +
Input: s = "pwwkew"
+Output: 3
+Explanation: The answer is "wke", with the length of 3.
+Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 5 * 104
  • +
  • s consists of English letters, digits, symbols and spaces.
  • +
+
\ No newline at end of file From 1691e3c48795c10d41835f035060e8b91c5d9b51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Apr 2023 23:03:11 +0530 Subject: [PATCH 0815/3167] Time: 24 ms (49.67%), Space: 8.8 MB (46.92%) - LeetHub --- ...substring-without-repeating-characters.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.cpp diff --git a/0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.cpp b/0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.cpp new file mode 100644 index 00000000..03a436d5 --- /dev/null +++ b/0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + + int i = 0, j = 0, n = s.size(); + + unordered_map mp; + + int ans = 0; + + while( j < n) + { + ++mp[s[j]]; + + if(mp.size() == j - i + 1) + ans = max(ans, j - i + 1); + else if(mp.size() < j - i + 1) + { + --mp[s[i]]; + if(mp[s[i]] == 0) + mp.erase(s[i]); + ++i; + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file From 9fcb97b7f988be9ce96ba3f84627854e6836160f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Apr 2023 07:05:12 +0530 Subject: [PATCH 0816/3167] Create README - LeetHub --- 0258-add-digits/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0258-add-digits/README.md diff --git a/0258-add-digits/README.md b/0258-add-digits/README.md new file mode 100644 index 00000000..cc5f53bd --- /dev/null +++ b/0258-add-digits/README.md @@ -0,0 +1,29 @@ +

258. Add Digits

Easy


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

+ +

 

+

Example 1:

+ +
Input: num = 38
+Output: 2
+Explanation: The process is
+38 --> 3 + 8 --> 11
+11 --> 1 + 1 --> 2 
+Since 2 has only one digit, return it.
+
+ +

Example 2:

+ +
Input: num = 0
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= num <= 231 - 1
  • +
+ +

 

+

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

+
\ No newline at end of file From 75d1b3be1e633591bd17490b9203af55c14e1250 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Apr 2023 07:05:13 +0530 Subject: [PATCH 0817/3167] Attach NOTES - LeetHub --- 0258-add-digits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0258-add-digits/NOTES.md diff --git a/0258-add-digits/NOTES.md b/0258-add-digits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0258-add-digits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5d48f0e27d5b3bd49976e18dabfd7ab085de7249 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Apr 2023 07:05:16 +0530 Subject: [PATCH 0818/3167] Time: 0 ms (100.00%), Space: 6 MB (7.98%) - LeetHub --- 0258-add-digits/0258-add-digits.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0258-add-digits/0258-add-digits.cpp diff --git a/0258-add-digits/0258-add-digits.cpp b/0258-add-digits/0258-add-digits.cpp new file mode 100644 index 00000000..4c0f7eee --- /dev/null +++ b/0258-add-digits/0258-add-digits.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int addDigits(int num) { + + string str = to_string(num); + + while(true) + { + string curr; + int sum = 0; + + for(auto itr : str) + { + sum += itr-'0'; + } + + curr = to_string(sum); + + if(curr.size() == 1) + return stoi(curr); + + str = curr; + } + + return 0; + } +}; \ No newline at end of file From 6aa55edcf77d240bde7df6713d8a8908a519b50c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Apr 2023 19:36:33 +0530 Subject: [PATCH 0819/3167] Create README - LeetHub --- 0319-bulb-switcher/README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0319-bulb-switcher/README.md diff --git a/0319-bulb-switcher/README.md b/0319-bulb-switcher/README.md new file mode 100644 index 00000000..03dde920 --- /dev/null +++ b/0319-bulb-switcher/README.md @@ -0,0 +1,36 @@ +

319. Bulb Switcher

Medium


There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.

+ +

On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.

+ +

Return the number of bulbs that are on after n rounds.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 1
+Explanation: At first, the three bulbs are [off, off, off].
+After the first round, the three bulbs are [on, on, on].
+After the second round, the three bulbs are [on, off, on].
+After the third round, the three bulbs are [on, off, off]. 
+So you should return 1 because there is only one bulb is on.
+ +

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 0 <= n <= 109
  • +
+
\ No newline at end of file From ce2852c8b2573c2ba97035194e3a26ad47ecbb8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Apr 2023 19:36:36 +0530 Subject: [PATCH 0820/3167] Time: 0 ms (100.00%), Space: 6.1 MB (9.91%) - LeetHub --- 0319-bulb-switcher/0319-bulb-switcher.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 0319-bulb-switcher/0319-bulb-switcher.cpp diff --git a/0319-bulb-switcher/0319-bulb-switcher.cpp b/0319-bulb-switcher/0319-bulb-switcher.cpp new file mode 100644 index 00000000..d1f9e968 --- /dev/null +++ b/0319-bulb-switcher/0319-bulb-switcher.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + int bulbSwitch(int n) { + + return sqrt(n); + + } +}; \ No newline at end of file From 093fe0430c425d6644bfc5e891b6c1e3ae3a7ced Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 19:04:43 +0530 Subject: [PATCH 0821/3167] Attach NOTES - LeetHub From a6729a6d7a585fd426f1337d29cf2c41caecc0c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 19:04:45 +0530 Subject: [PATCH 0822/3167] Time: 2263 ms (5.01%), Space: 51.1 MB (33.26%) - LeetHub --- .../0128-longest-consecutive-sequence.cpp | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp b/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp index a658327b..3bfdc6bb 100644 --- a/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp +++ b/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp @@ -2,28 +2,25 @@ class Solution { public: int longestConsecutive(vector& nums) { - int n = nums.size(); + set st(nums.begin(),nums.end()); - if(n == 0) - return 0; + int ans = 0, n = nums.size(); - sort(nums.begin(),nums.end()); - - int ans = 1, curr = 1; - - for(int i = 1; i Date: Fri, 28 Apr 2023 19:06:38 +0530 Subject: [PATCH 0823/3167] Attach NOTES - LeetHub From ecb0fdce67b552343778003cc00895561637d27d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 19:06:41 +0530 Subject: [PATCH 0824/3167] Time: 728 ms (17.02%), Space: 45.7 MB (69.05%) - LeetHub --- .../0128-longest-consecutive-sequence.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp b/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp index 3bfdc6bb..92f0a4e5 100644 --- a/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp +++ b/0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp @@ -2,7 +2,7 @@ class Solution { public: int longestConsecutive(vector& nums) { - set st(nums.begin(),nums.end()); + unordered_set st(nums.begin(),nums.end()); int ans = 0, n = nums.size(); @@ -10,9 +10,9 @@ class Solution { { if(!st.count(nums[i] - 1)) { - int curr = 0; + int curr = 1; int var = nums[i]; - while(st.count(var)) + while(st.count(var+1)) { ++var; ++curr; From f19d435737b6452ba3fb55c2d730f9c79f05e2e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 20:50:26 +0530 Subject: [PATCH 0825/3167] Create README - LeetHub --- 0493-reverse-pairs/README.md | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0493-reverse-pairs/README.md diff --git a/0493-reverse-pairs/README.md b/0493-reverse-pairs/README.md new file mode 100644 index 00000000..958affd1 --- /dev/null +++ b/0493-reverse-pairs/README.md @@ -0,0 +1,37 @@ +

493. Reverse Pairs

Hard


Given an integer array nums, return the number of reverse pairs in the array.

+ +

A reverse pair is a pair (i, j) where:

+ +
    +
  • 0 <= i < j < nums.length and
  • +
  • nums[i] > 2 * nums[j].
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [1,3,2,3,1]
+Output: 2
+Explanation: The reverse pairs are:
+(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1
+(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1
+
+ +

Example 2:

+ +
Input: nums = [2,4,3,5,1]
+Output: 3
+Explanation: The reverse pairs are:
+(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1
+(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1
+(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
+
\ No newline at end of file From fd410b8bc223b224dcb58d510b081f1820918ecf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 20:50:29 +0530 Subject: [PATCH 0826/3167] Time: 578 ms (54.37%), Space: 46.7 MB (82.56%) - LeetHub --- 0493-reverse-pairs/0493-reverse-pairs.cpp | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 0493-reverse-pairs/0493-reverse-pairs.cpp diff --git a/0493-reverse-pairs/0493-reverse-pairs.cpp b/0493-reverse-pairs/0493-reverse-pairs.cpp new file mode 100644 index 00000000..51d3c6f5 --- /dev/null +++ b/0493-reverse-pairs/0493-reverse-pairs.cpp @@ -0,0 +1,75 @@ +#define ll long long int + +class Solution { +public: + + int merge(int start, int mid, int end, vector& nums, vector& temp) + { + int i = start, j = mid+1, k = i; + + int cnt = 0; + + int right = mid+1; + + for(int left = start; left <= mid; ++left) + { + while(right <= end and nums[left] > (ll) 2 * nums[right]) + ++right; + + cnt += (right - (mid + 1)); + } + + while(i <= mid and j <= end) + { + if(nums[i] <= nums[j]) + temp[k++] = nums[i++]; + else + temp[k++] = nums[j++]; + } + + while(i <= mid) + temp[k++] = nums[i++]; + + while(j <= end) + temp[k++] = nums[j++]; + + for(int i = start; i<= end; ++i) + nums[i] = temp[i]; + + return cnt; + } + + + int mergeSort(int i, int j, vector& nums, vector& temp) + { + int cnt = 0; + + if( i < j) + { + int mid = i + (j - i)/2; + + cnt += mergeSort(i, mid, nums, temp); + cnt += mergeSort(mid+1, j, nums, temp); + cnt += merge(i, mid, j, nums, temp); + } + + return cnt; + } + + + int reversePairs(vector& nums) { + + int n = nums.size(); + + vector temp(n); + + int ans = mergeSort(0, n-1 , nums, temp); + + for(auto itr : nums) + cout< Date: Fri, 28 Apr 2023 23:29:57 +0530 Subject: [PATCH 0827/3167] Create README - LeetHub --- 0839-similar-string-groups/README.md | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0839-similar-string-groups/README.md diff --git a/0839-similar-string-groups/README.md b/0839-similar-string-groups/README.md new file mode 100644 index 00000000..1406f862 --- /dev/null +++ b/0839-similar-string-groups/README.md @@ -0,0 +1,31 @@ +

839. Similar String Groups

Hard


Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. Also two strings X and Y are similar if they are equal.

+ +

For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".

+ +

Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}.  Notice that "tars" and "arts" are in the same group even though they are not similar.  Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.

+ +

We are given a list strs of strings where every string in strs is an anagram of every other string in strs. How many groups are there?

+ +

 

+

Example 1:

+ +
Input: strs = ["tars","rats","arts","star"]
+Output: 2
+
+ +

Example 2:

+ +
Input: strs = ["omv","ovm"]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= strs.length <= 300
  • +
  • 1 <= strs[i].length <= 300
  • +
  • strs[i] consists of lowercase letters only.
  • +
  • All words in strs have the same length and are anagrams of each other.
  • +
+
\ No newline at end of file From b42e292553fe953fb64ed6d7a1f96d1018c5f616 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 23:30:01 +0530 Subject: [PATCH 0828/3167] Time: 168 ms (50.47%), Space: 9.7 MB (100.00%) - LeetHub --- .../0839-similar-string-groups.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0839-similar-string-groups/0839-similar-string-groups.cpp diff --git a/0839-similar-string-groups/0839-similar-string-groups.cpp b/0839-similar-string-groups/0839-similar-string-groups.cpp new file mode 100644 index 00000000..4c225422 --- /dev/null +++ b/0839-similar-string-groups/0839-similar-string-groups.cpp @@ -0,0 +1,51 @@ +class Solution { +public: + + int numSimilarGroups(vector& strs) { + + int n = strs.size(); + + vector visited(n,0); + + int cnt = 0; + + auto isSimilar = [&](string& a, string& b) + { + int n = a.size(), m = b.size(); + int c = 0; + if(n != m) + return false; + + for(int i = 0; i dfs = [&](int sv) { + visited[sv] = true; + + for(int i = 0; i Date: Fri, 28 Apr 2023 23:33:23 +0530 Subject: [PATCH 0829/3167] Attach NOTES - LeetHub --- 0839-similar-string-groups/NOTES.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 0839-similar-string-groups/NOTES.md diff --git a/0839-similar-string-groups/NOTES.md b/0839-similar-string-groups/NOTES.md new file mode 100644 index 00000000..b211a6ff --- /dev/null +++ b/0839-similar-string-groups/NOTES.md @@ -0,0 +1,4 @@ +function isSimilar = [&](string a, string b) +{ +}; +​ \ No newline at end of file From a3c4c106f9724d066ff9bbfad8772568a451f495 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Apr 2023 23:33:26 +0530 Subject: [PATCH 0830/3167] Time: 168 ms (50.47%), Space: 9.7 MB (100.00%) - LeetHub From 12d42c4f6986f60a75090480d1b130c4ab259c23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Apr 2023 22:10:00 +0530 Subject: [PATCH 0831/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1697-checking-existence-of-edge-length-limited-paths/README.md diff --git a/1697-checking-existence-of-edge-length-limited-paths/README.md b/1697-checking-existence-of-edge-length-limited-paths/README.md new file mode 100644 index 00000000..e57678fb --- /dev/null +++ b/1697-checking-existence-of-edge-length-limited-paths/README.md @@ -0,0 +1,38 @@ +

1697. Checking Existence of Edge Length Limited Paths

Hard


An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between nodes ui and vi with distance disi. Note that there may be multiple edges between two nodes.

+ +

Given an array queries, where queries[j] = [pj, qj, limitj], your task is to determine for each queries[j] whether there is a path between pj and qj such that each edge on the path has a distance strictly less than limitj .

+ +

Return a boolean array answer, where answer.length == queries.length and the jth value of answer is true if there is a path for queries[j] is true, and false otherwise.

+ +

 

+

Example 1:

+ +
Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]]
+Output: [false,true]
+Explanation: The above figure shows the given graph. Note that there are two overlapping edges between 0 and 1 with distances 2 and 16.
+For the first query, between 0 and 1 there is no path where each distance is less than 2, thus we return false for this query.
+For the second query, there is a path (0 -> 1 -> 2) of two edges with distances less than 5, thus we return true for this query.
+
+ +

Example 2:

+ +
Input: n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]], queries = [[0,4,14],[1,4,13]]
+Output: [true,false]
+Exaplanation: The above figure shows the given graph.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 105
  • +
  • 1 <= edgeList.length, queries.length <= 105
  • +
  • edgeList[i].length == 3
  • +
  • queries[j].length == 3
  • +
  • 0 <= ui, vi, pj, qj <= n - 1
  • +
  • ui != vi
  • +
  • pj != qj
  • +
  • 1 <= disi, limitj <= 109
  • +
  • There may be multiple edges between two nodes.
  • +
+
\ No newline at end of file From 81d8c5143ba62b24e0f608958be7d66f97cd78c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Apr 2023 22:10:03 +0530 Subject: [PATCH 0832/3167] Time: 717 ms (15.77%), Space: 116.1 MB (31.95%) - LeetHub --- ...existence-of-edge-length-limited-paths.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 1697-checking-existence-of-edge-length-limited-paths/1697-checking-existence-of-edge-length-limited-paths.cpp diff --git a/1697-checking-existence-of-edge-length-limited-paths/1697-checking-existence-of-edge-length-limited-paths.cpp b/1697-checking-existence-of-edge-length-limited-paths/1697-checking-existence-of-edge-length-limited-paths.cpp new file mode 100644 index 00000000..d9c04026 --- /dev/null +++ b/1697-checking-existence-of-edge-length-limited-paths/1697-checking-existence-of-edge-length-limited-paths.cpp @@ -0,0 +1,51 @@ +class DSU { + public: + vector Parent, Rank; + DSU(int n) { + Parent.resize(n); + Rank.resize(n, 0); + for (int i = 0; i < n; i++) Parent[i] = i; + } + int Find(int x) { + return Parent[x] = Parent[x] == x ? x : Find(Parent[x]); + } + bool Union(int x, int y) { + int xset = Find(x), yset = Find(y); + if (xset != yset) { + Rank[xset] < Rank[yset] ? Parent[xset] = yset : Parent[yset] = xset; + Rank[xset] += Rank[xset] == Rank[yset]; + return true; + } + return false; + } +}; + +class Solution { +public: + vector distanceLimitedPathsExist(int n, vector>& edgeList, vector>& queries) { + DSU dsu(n); + for(int i=0;i res(queries.size(), false); + for(auto q: queries){ + while(i Date: Sun, 30 Apr 2023 19:28:35 +0530 Subject: [PATCH 0833/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2661-first-completely-painted-row-or-column/README.md diff --git a/2661-first-completely-painted-row-or-column/README.md b/2661-first-completely-painted-row-or-column/README.md new file mode 100644 index 00000000..2c71aa53 --- /dev/null +++ b/2661-first-completely-painted-row-or-column/README.md @@ -0,0 +1,35 @@ +

2661. First Completely Painted Row or Column

Medium


You are given a 0-indexed integer array arr, and an m x n integer matrix mat. arr and mat both contain all the integers in the range [1, m * n].

+ +

Go through each index i in arr starting from index 0 and paint the cell in mat containing the integer arr[i].

+ +

Return the smallest index i at which either a row or a column will be completely painted in mat.

+ +

 

+

Example 1:

+image explanation for example 1 +
Input: arr = [1,3,4,2], mat = [[1,4],[2,3]]
+Output: 2
+Explanation: The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].
+
+ +

Example 2:

+image explanation for example 2 +
Input: arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
+Output: 3
+Explanation: The second column becomes fully painted at arr[3].
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n = mat[i].length
  • +
  • arr.length == m * n
  • +
  • 1 <= m, n <= 105
  • +
  • 1 <= m * n <= 105
  • +
  • 1 <= arr[i], mat[r][c] <= m * n
  • +
  • All the integers of arr are unique.
  • +
  • All the integers of mat are unique.
  • +
+
\ No newline at end of file From 53404dd2ba390d4633856874a2f2188a7c5fb8e7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Apr 2023 19:28:38 +0530 Subject: [PATCH 0834/3167] Time: 584 ms (33.33%), Space: 159.2 MB (33.33%) - LeetHub --- ...first-completely-painted-row-or-column.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2661-first-completely-painted-row-or-column/2661-first-completely-painted-row-or-column.cpp diff --git a/2661-first-completely-painted-row-or-column/2661-first-completely-painted-row-or-column.cpp b/2661-first-completely-painted-row-or-column/2661-first-completely-painted-row-or-column.cpp new file mode 100644 index 00000000..3f1b41c3 --- /dev/null +++ b/2661-first-completely-painted-row-or-column/2661-first-completely-painted-row-or-column.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int firstCompleteIndex(vector& arr, vector>& mat) { + + int n = mat.size(), m = mat[0].size(); + + vector row(n), col(m); + + map> mp; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + mp[mat[i][j]] = {i,j}; + } + } + + for(int i = 0; i < arr.size(); ++i) + { + int r= mp[arr[i]].first; + int c = mp[arr[i]].second; + + ++row[r], ++col[c]; + + if(row[r] == m or col[c] == n) + return i; + } + + return -1; + + } +}; \ No newline at end of file From 9aed65c7f791ca73ae6fc8f6755004fd0e2857d3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Apr 2023 19:29:55 +0530 Subject: [PATCH 0835/3167] Attach NOTES - LeetHub --- 2661-first-completely-painted-row-or-column/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2661-first-completely-painted-row-or-column/NOTES.md diff --git a/2661-first-completely-painted-row-or-column/NOTES.md b/2661-first-completely-painted-row-or-column/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2661-first-completely-painted-row-or-column/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3bcce16763a0ba604d6680a34ff79875a6684fb3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Apr 2023 19:29:59 +0530 Subject: [PATCH 0836/3167] Time: 584 ms (33.33%), Space: 159.2 MB (33.33%) - LeetHub From 0552cde92226a68d89b4838bcf6c8e6268a2245f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Apr 2023 22:51:37 +0530 Subject: [PATCH 0837/3167] Create README - LeetHub --- .../README.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/README.md diff --git a/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/README.md b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/README.md new file mode 100644 index 00000000..3aa33f3e --- /dev/null +++ b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/README.md @@ -0,0 +1,53 @@ +

1579. Remove Max Number of Edges to Keep Graph Fully Traversable

Hard


Alice and Bob have an undirected graph of n nodes and three types of edges:

+ +
    +
  • Type 1: Can be traversed by Alice only.
  • +
  • Type 2: Can be traversed by Bob only.
  • +
  • Type 3: Can be traversed by both Alice and Bob.
  • +
+ +

Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

+ +

Return the maximum number of edges you can remove, or return -1 if Alice and Bob cannot fully traverse the graph.

+ +

 

+

Example 1:

+ +

+ +
Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
+Output: 2
+Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.
+
+ +

Example 2:

+ +

+ +
Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
+Output: 0
+Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
+
+ +

Example 3:

+ +

+ +
Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
+Output: -1
+Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.
+ +

 

+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= edges.length <= min(105, 3 * n * (n - 1) / 2)
  • +
  • edges[i].length == 3
  • +
  • 1 <= typei <= 3
  • +
  • 1 <= ui < vi <= n
  • +
  • All tuples (typei, ui, vi) are distinct.
  • +
+
\ No newline at end of file From 9ef220e82e905aeebf2a6d99a3a98a9091d6a7ec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Apr 2023 22:51:41 +0530 Subject: [PATCH 0838/3167] Time: 563 ms (68.12%), Space: 139.4 MB (69.57%) - LeetHub --- ...-edges-to-keep-graph-fully-traversable.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.cpp diff --git a/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.cpp b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.cpp new file mode 100644 index 00000000..8a20e1d5 --- /dev/null +++ b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + int find(vector &par,int u) + { + if(u==par[u]) + return u; + return par[u]=find(par,par[u]); + } + void unions(vector &par,int u,int v) + { + int pu=find(par,u); + int pv=find(par,v); + par[pu]=pv; + } + int maxNumEdgesToRemove(int n, vector>& edges) + { + vector a(n+1),b(n+1); + int ans=0; + for(int i=0;i<=n;i++) + { + a[i]=i; + b[i]=i; + } + for(auto &e:edges) + { + if(e[0]!=3) + continue; + if(find(a,e[1])==find(a,e[2])) + { + ans++; + continue; + } + unions(a,e[1],e[2]); + unions(b,e[1],e[2]); + } + for(auto &e:edges) + { + int u=e[1],v=e[2]; + if(e[0]==1) + { + if(find(a,u)==find(a,v)) + { + ans++; + } + else + { + unions(a,u,v); + } + } + else if(e[0]==2) + { + if(find(b,u)==find(b,v)) + { + ans++; + } + else + { + unions(b,u,v); + } + } + } + for(int i=1;i<=n;i++) + { + if(find(a,i)!=find(a,n)||find(b,i)!=find(b,n)) + { + return -1; + } + } + return ans; + + } +}; From 1932f691defd097a99e7792af157b02d104fd724 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 May 2023 20:57:43 +0530 Subject: [PATCH 0839/3167] Create README - LeetHub --- 0994-rotting-oranges/README.md | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0994-rotting-oranges/README.md diff --git a/0994-rotting-oranges/README.md b/0994-rotting-oranges/README.md new file mode 100644 index 00000000..1eb3cfc3 --- /dev/null +++ b/0994-rotting-oranges/README.md @@ -0,0 +1,43 @@ +

994. Rotting Oranges

Medium


You are given an m x n grid where each cell can have one of three values:

+ +
    +
  • 0 representing an empty cell,
  • +
  • 1 representing a fresh orange, or
  • +
  • 2 representing a rotten orange.
  • +
+ +

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

+ +

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

+ +

 

+

Example 1:

+ +
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
+Output: 4
+
+ +

Example 2:

+ +
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
+Output: -1
+Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
+
+ +

Example 3:

+ +
Input: grid = [[0,2]]
+Output: 0
+Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 10
  • +
  • grid[i][j] is 0, 1, or 2.
  • +
+
\ No newline at end of file From 17f49f7592a8ec14a9178b2bc0f696d3e3e75d5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 May 2023 20:57:47 +0530 Subject: [PATCH 0840/3167] Time: 12 ms (23.08%), Space: 13.3 MB (40.87%) - LeetHub --- 0994-rotting-oranges/0994-rotting-oranges.cpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 0994-rotting-oranges/0994-rotting-oranges.cpp diff --git a/0994-rotting-oranges/0994-rotting-oranges.cpp b/0994-rotting-oranges/0994-rotting-oranges.cpp new file mode 100644 index 00000000..20427f73 --- /dev/null +++ b/0994-rotting-oranges/0994-rotting-oranges.cpp @@ -0,0 +1,76 @@ +class Solution { +public: + + bool isValid (int x, int y, vector>& grid, vector>& visited) + { + int n = grid.size(); + int m = grid[0].size(); + + return(x >= 0 and y >= 0 and x < n and y < m and !visited[x][y] and grid[x][y] == 1); + } + + int orangesRotting(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + queue, int>> q; + + vector> visited(n,vector(m, false)); + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1, 0}; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 2) + { + q.push({{i,j},0}); + visited[i][j] = true; + } + } + } + + int step = 0, ans = 0; + + while(!q.empty()) + { + auto curr = q.front(); + q.pop(); + + int x = curr.first.first; + int y = curr.first.second; + step = curr.second; + + ans = max(ans, step); + + for(int i = 0; i < 4; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(isValid(newx, newy, grid, visited)) + { + visited[newx][newy] = true; + q.push({{newx,newy},step+1}); + grid[newx][newy] = 2; + } + } + + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + return -1; + } + } + + return ans; + + } +}; \ No newline at end of file From 0e20ad55fb8a4fd03806553843a8fc14dae0fa2e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 May 2023 20:58:24 +0530 Subject: [PATCH 0841/3167] Attach NOTES - LeetHub --- 0994-rotting-oranges/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0994-rotting-oranges/NOTES.md diff --git a/0994-rotting-oranges/NOTES.md b/0994-rotting-oranges/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0994-rotting-oranges/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fcddd7f35afe99843ab181bd046faa54ca5806ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 May 2023 20:58:27 +0530 Subject: [PATCH 0842/3167] Time: 12 ms (23.08%), Space: 13.3 MB (40.87%) - LeetHub From f899e2faa7bef549d9604e0be8e303a676d5b336 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 07:09:51 +0530 Subject: [PATCH 0843/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1822-sign-of-the-product-of-an-array/README.md diff --git a/1822-sign-of-the-product-of-an-array/README.md b/1822-sign-of-the-product-of-an-array/README.md new file mode 100644 index 00000000..f7b477f0 --- /dev/null +++ b/1822-sign-of-the-product-of-an-array/README.md @@ -0,0 +1,42 @@ +

1822. Sign of the Product of an Array

Easy


There is a function signFunc(x) that returns:

+ +
    +
  • 1 if x is positive.
  • +
  • -1 if x is negative.
  • +
  • 0 if x is equal to 0.
  • +
+ +

You are given an integer array nums. Let product be the product of all values in the array nums.

+ +

Return signFunc(product).

+ +

 

+

Example 1:

+ +
Input: nums = [-1,-2,-3,-4,3,2,1]
+Output: 1
+Explanation: The product of all values in the array is 144, and signFunc(144) = 1
+
+ +

Example 2:

+ +
Input: nums = [1,5,0,2,-3]
+Output: 0
+Explanation: The product of all values in the array is 0, and signFunc(0) = 0
+
+ +

Example 3:

+ +
Input: nums = [-1,1,-1,1,-1]
+Output: -1
+Explanation: The product of all values in the array is -1, and signFunc(-1) = -1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -100 <= nums[i] <= 100
  • +
+
\ No newline at end of file From 6c2fdb8ed444cbfe0e6fbfdf77a7051bcc1a10ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 07:09:52 +0530 Subject: [PATCH 0844/3167] Attach NOTES - LeetHub --- 1822-sign-of-the-product-of-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1822-sign-of-the-product-of-an-array/NOTES.md diff --git a/1822-sign-of-the-product-of-an-array/NOTES.md b/1822-sign-of-the-product-of-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1822-sign-of-the-product-of-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 40d2fdcd0cda7c4f75759a2b7fc7f61ec7711ed4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 07:09:55 +0530 Subject: [PATCH 0845/3167] Time: 12 ms (16.81%), Space: 10.3 MB (45.94%) - LeetHub --- .../1822-sign-of-the-product-of-an-array.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1822-sign-of-the-product-of-an-array/1822-sign-of-the-product-of-an-array.cpp diff --git a/1822-sign-of-the-product-of-an-array/1822-sign-of-the-product-of-an-array.cpp b/1822-sign-of-the-product-of-an-array/1822-sign-of-the-product-of-an-array.cpp new file mode 100644 index 00000000..7d836b76 --- /dev/null +++ b/1822-sign-of-the-product-of-an-array/1822-sign-of-the-product-of-an-array.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int arraySign(vector& nums) { + + int neg(0), pos(0), rest(0); + + for(auto itr : nums) + { + if(itr < 0) + ++neg; + else if(itr > 0) + ++pos; + else + ++rest; + } + + if(rest) + return 0; + if(neg & 1) + return -1; + return 1; + } +}; \ No newline at end of file From fbddc187b0c372dac837aed93d8637d23d490454 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 07:10:27 +0530 Subject: [PATCH 0846/3167] Attach NOTES - LeetHub From d8035151c14c897f1bea757532656aa3585a8be3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 07:10:30 +0530 Subject: [PATCH 0847/3167] Time: 12 ms (16.81%), Space: 10.3 MB (45.94%) - LeetHub From 21f61415ce999f62d119fd6abbe5a4efc89877af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 21:25:51 +0530 Subject: [PATCH 0848/3167] Create README - LeetHub --- 0130-surrounded-regions/README.md | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0130-surrounded-regions/README.md diff --git a/0130-surrounded-regions/README.md b/0130-surrounded-regions/README.md new file mode 100644 index 00000000..335f1f77 --- /dev/null +++ b/0130-surrounded-regions/README.md @@ -0,0 +1,32 @@ +

130. Surrounded Regions

Medium


Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X'.

+ +

A region is captured by flipping all 'O's into 'X's in that surrounded region.

+ +

 

+

Example 1:

+ +
Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
+Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
+Explanation: Notice that an 'O' should not be flipped if:
+- It is on the border, or
+- It is adjacent to an 'O' that should not be flipped.
+The bottom 'O' is on the border, so it is not flipped.
+The other three 'O' form a surrounded region, so they are flipped.
+
+ +

Example 2:

+ +
Input: board = [["X"]]
+Output: [["X"]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == board.length
  • +
  • n == board[i].length
  • +
  • 1 <= m, n <= 200
  • +
  • board[i][j] is 'X' or 'O'.
  • +
+
\ No newline at end of file From 35646afa486effe234a84ea5f418e7eb2b25ca2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 21:25:55 +0530 Subject: [PATCH 0849/3167] Time: 16 ms (33.92%), Space: 10.1 MB (72.55%) - LeetHub --- .../0130-surrounded-regions.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0130-surrounded-regions/0130-surrounded-regions.cpp diff --git a/0130-surrounded-regions/0130-surrounded-regions.cpp b/0130-surrounded-regions/0130-surrounded-regions.cpp new file mode 100644 index 00000000..6a28517b --- /dev/null +++ b/0130-surrounded-regions/0130-surrounded-regions.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + void dfs(int x , int y, int n, int m, vector>& visited, vector>& board, vector& dx, vector& dy) + { + visited[x][y] = true; + + for(int i = 0; i < 4; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and !visited[newx][newy] and board[newx][newy] == 'O') + dfs(newx, newy, n, m, visited, board, dx, dy); + } + } + + + void solve(vector>& board) { + + int n = board.size(); + int m = board[0].size(); + + vector> visited(n, vector(m, false)); + + vector dx = {-1,0,0,+1}; + vector dy = {0,-1,+1,0}; + + for(int i = 0; i < n; ++i) + { + if(!visited[i][0] and board[i][0] == 'O') + dfs(i, 0, n, m, visited, board, dx, dy); + + if(!visited[i][m-1] and board[i][m-1] == 'O') + dfs(i, m-1, n, m, visited, board, dx, dy); + } + + for(int j = 0; j < m; ++j) + { + if(!visited[0][j] and board[0][j] == 'O') + dfs(0, j, n, m , visited, board, dx, dy); + if(!visited[n-1][j] and board[n-1][j] == 'O') + dfs(n-1, j, n, m, visited, board, dx, dy); + } + + for(int i = 0 ; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited[i][j] and board[i][j] == 'O') + board[i][j] = 'X'; + } + } + + } +}; \ No newline at end of file From 31982a7fdaf3b1d268653d5b9f7bb7912fd84bf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 22:43:27 +0530 Subject: [PATCH 0850/3167] Attach NOTES - LeetHub From 12407e9f8756ecf473c5e89cc3c30264c98a684c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 May 2023 22:43:31 +0530 Subject: [PATCH 0851/3167] Time: 98 ms (23.16%), Space: 27.6 MB (91.04%) - LeetHub --- .../1020-number-of-enclaves.cpp | 76 ++++++++++++++----- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/1020-number-of-enclaves/1020-number-of-enclaves.cpp b/1020-number-of-enclaves/1020-number-of-enclaves.cpp index fe9dc0c3..c94761f5 100644 --- a/1020-number-of-enclaves/1020-number-of-enclaves.cpp +++ b/1020-number-of-enclaves/1020-number-of-enclaves.cpp @@ -1,38 +1,76 @@ class Solution { public: - - int dfs(int i, int j, int n, int m, vector>& grid) - { - if(i < 0 or j < 0 or i >= n or j >= m or grid[i][j] == 0) - return 0; - - grid[i][j] = 0; - - return 1 + dfs(i-1,j,n,m,grid) + dfs(i+1,j,n,m,grid) + dfs(i,j-1,n,m,grid) + dfs(i,j+1,n,m,grid); - } - int numEnclaves(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); - int n = grid.size(), m = grid[0].size() ,allOne = 0, closeOne = 0; + vector> visited(n, vector(m, false)); + + queue> q; for(int i = 0; i < n; ++i) { - for(int j = 0; j < m; ++j) + if(!visited[i][0] and grid[i][0] == 1) + { + q.push({i,0}); + visited[i][0] = true; + } + if(!visited[i][m-1] and grid[i][m-1] == 1) { - if(grid[i][j] == 1) - ++allOne; + q.push({i,m-1}); + visited[i][m-1] = true; } } - for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) + { + if(!visited[0][j] and grid[0][j] == 1) + { + q.push({0,j}); + visited[0][j] = true; + } + if(!visited[n-1][j] and grid[n-1][j] == 1) + { + q.push({n-1,j}); + visited[n-1][j] = true; + } + } + + vector dx = {-1, 0, 0, 1}; + vector dy = {0, -1, +1, 0}; + + while(!q.empty()) + { + int x = q.front().first; + int y = q.front().second; + + q.pop(); + + for(int i = 0; i < 4; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and !visited[newx][newy] and grid[newx][newy] == 1) + { + visited[newx][newy] = true; + q.push({newx, newy}); + } + } + } + + int cnt = 0; + + 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) - closeOne += dfs(i,j,n,m,grid); + if(!visited[i][j] and grid[i][j] == 1) + ++cnt; } } - return allOne - closeOne; + return cnt; + } }; \ No newline at end of file From 326cb634b67f854ca2947b58246f1f1d10e04db3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 May 2023 21:50:54 +0530 Subject: [PATCH 0852/3167] Create README - LeetHub --- 0802-find-eventual-safe-states/README.md | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0802-find-eventual-safe-states/README.md diff --git a/0802-find-eventual-safe-states/README.md b/0802-find-eventual-safe-states/README.md new file mode 100644 index 00000000..45fa72c3 --- /dev/null +++ b/0802-find-eventual-safe-states/README.md @@ -0,0 +1,36 @@ +

802. Find Eventual Safe States

Medium


There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i].

+ +

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 (or another safe node).

+ +

Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.

+ +

 

+

Example 1:

+Illustration of graph +
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
+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: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
+Output: [4]
+Explanation:
+Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
+
+ +

 

+

Constraints:

+ +
    +
  • n == graph.length
  • +
  • 1 <= n <= 104
  • +
  • 0 <= graph[i].length <= n
  • +
  • 0 <= graph[i][j] <= n - 1
  • +
  • graph[i] is sorted in a strictly increasing order.
  • +
  • The graph may contain self-loops.
  • +
  • The number of edges in the graph will be in the range [1, 4 * 104].
  • +
+
\ No newline at end of file From 17fbb5969fcd3fbf4ed31c5afcab675c5566f21d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 May 2023 21:50:57 +0530 Subject: [PATCH 0853/3167] Time: 155 ms (80.82%), Space: 46.7 MB (95.38%) - LeetHub --- .../0802-find-eventual-safe-states.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp diff --git a/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp b/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp new file mode 100644 index 00000000..2c7eb376 --- /dev/null +++ b/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + bool dfs(int sv, vector>& graph, vector& visited, vector& pathVisited, vector& check) + { + visited[sv] = true; + pathVisited[sv] = true; + check[sv] = false; + + for(auto itr : graph[sv]) + { + if(!visited[itr]) + { + if(dfs(itr, graph, visited, pathVisited, check) == true) + { + check[itr] = false; + return true; + } + } + else if(pathVisited[itr]) + { + check[itr] = false; + return true; + } + } + + pathVisited[sv] = false; + check[sv] = true; + + return false; + } + + vector eventualSafeNodes(vector>& graph) { + + int n = graph.size(); + + vector visited(n+1, false), pathVisited(n+1, false), check(n+1, false); + + vector safe; + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + dfs(i, graph, visited, pathVisited, check); + } + + for(int i = 0; i < n; ++i) + { + if(check[i]) + safe.push_back(i); + } + + return safe; + + } +}; \ No newline at end of file From e1117d63208cc57d0a4afb60a1d47cdec60fd81d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 May 2023 22:21:09 +0530 Subject: [PATCH 0854/3167] Attach NOTES - LeetHub --- 0649-dota2-senate/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0649-dota2-senate/NOTES.md diff --git a/0649-dota2-senate/NOTES.md b/0649-dota2-senate/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0649-dota2-senate/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 69b740182026c35f795aaf4d2e97f45cc6a07d70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 May 2023 22:21:12 +0530 Subject: [PATCH 0855/3167] Time: 8 ms (72.14%), Space: 8.1 MB (12.21%) - LeetHub --- 0649-dota2-senate/0649-dota2-senate.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0649-dota2-senate/0649-dota2-senate.cpp diff --git a/0649-dota2-senate/0649-dota2-senate.cpp b/0649-dota2-senate/0649-dota2-senate.cpp new file mode 100644 index 00000000..3c5e6518 --- /dev/null +++ b/0649-dota2-senate/0649-dota2-senate.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + string predictPartyVictory(string senate) { + queue q1, q2; + int n = senate.length(); + for(int i = 0; i q2.size())? "Radiant" : "Dire"; + } +}; \ No newline at end of file From 397a94b44fd7f0e7747f53753e51952525fd58e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 07:19:43 +0530 Subject: [PATCH 0856/3167] Create README - LeetHub --- 2667-create-hello-world-function/README.md | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2667-create-hello-world-function/README.md diff --git a/2667-create-hello-world-function/README.md b/2667-create-hello-world-function/README.md new file mode 100644 index 00000000..48112974 --- /dev/null +++ b/2667-create-hello-world-function/README.md @@ -0,0 +1,31 @@ +

2667. Create Hello World Function

Easy


Write a function createHelloWorld. It should return a new function that always returns "Hello World". +

 

+

Example 1:

+ +
Input: args = []
+Output: "Hello World"
+Explanation:
+const f = createHelloWorld();
+f(); // "Hello World"
+
+The function returned by createHelloWorld should always return "Hello World".
+
+ +

Example 2:

+ +
Input: args = [{},null,42]
+Output: "Hello World"
+Explanation:
+const f = createHelloWorld();
+f({}, null, 42); // "Hello World"
+
+Any arguments could be passed to the function but it should still always return "Hello World".
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= args.length <= 10
  • +
+
\ No newline at end of file From c81de3c6617c336fc19ce0152976e391483e33d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 07:19:46 +0530 Subject: [PATCH 0857/3167] Time: 56 ms (100.00%), Space: 41.7 MB (100.00%) - LeetHub --- .../2667-create-hello-world-function.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 2667-create-hello-world-function/2667-create-hello-world-function.js diff --git a/2667-create-hello-world-function/2667-create-hello-world-function.js b/2667-create-hello-world-function/2667-create-hello-world-function.js new file mode 100644 index 00000000..e1dc4900 --- /dev/null +++ b/2667-create-hello-world-function/2667-create-hello-world-function.js @@ -0,0 +1,13 @@ +/** + * @return {Function} + */ +var createHelloWorld = function() { + return function(...args) { + return "Hello World"; + } +}; + +/** + * const f = createHelloWorld(); + * f(); // "Hello World" + */ \ No newline at end of file From 5e2954ae1b47a5eb4168c507f008d80c963241d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 07:32:17 +0530 Subject: [PATCH 0858/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1456-maximum-number-of-vowels-in-a-substring-of-given-length/README.md diff --git a/1456-maximum-number-of-vowels-in-a-substring-of-given-length/README.md b/1456-maximum-number-of-vowels-in-a-substring-of-given-length/README.md new file mode 100644 index 00000000..ab9e2fee --- /dev/null +++ b/1456-maximum-number-of-vowels-in-a-substring-of-given-length/README.md @@ -0,0 +1,35 @@ +

1456. Maximum Number of Vowels in a Substring of Given Length

Medium


Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.

+ +

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

+ +

 

+

Example 1:

+ +
Input: s = "abciiidef", k = 3
+Output: 3
+Explanation: The substring "iii" contains 3 vowel letters.
+
+ +

Example 2:

+ +
Input: s = "aeiou", k = 2
+Output: 2
+Explanation: Any substring of length 2 contains 2 vowels.
+
+ +

Example 3:

+ +
Input: s = "leetcode", k = 3
+Output: 2
+Explanation: "lee", "eet" and "ode" contain 2 vowels.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase English letters.
  • +
  • 1 <= k <= s.length
  • +
+
\ No newline at end of file From 4597b3656c28e4a71e002c25640f5c006c110e4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 07:32:20 +0530 Subject: [PATCH 0859/3167] Time: 61 ms (6.96%), Space: 10.3 MB (10.40%) - LeetHub --- ...-vowels-in-a-substring-of-given-length.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1456-maximum-number-of-vowels-in-a-substring-of-given-length/1456-maximum-number-of-vowels-in-a-substring-of-given-length.cpp diff --git a/1456-maximum-number-of-vowels-in-a-substring-of-given-length/1456-maximum-number-of-vowels-in-a-substring-of-given-length.cpp b/1456-maximum-number-of-vowels-in-a-substring-of-given-length/1456-maximum-number-of-vowels-in-a-substring-of-given-length.cpp new file mode 100644 index 00000000..a70abcd2 --- /dev/null +++ b/1456-maximum-number-of-vowels-in-a-substring-of-given-length/1456-maximum-number-of-vowels-in-a-substring-of-given-length.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + int maxVowels(string s, int k) { + + int i = 0, j = 0, n = s.size(); + + int currCnt = 0, ans = 0; + + unordered_map mp; + + + auto isVowel = [&](char ch){ + return (ch == 'a' or ch == 'A' or ch == 'e' or ch == 'E' or ch == 'i' or ch == 'I' or ch == 'o' or ch == 'O' or ch == 'u' or ch == 'U'); + }; + + + while(j < n) + { + ++mp[s[j]]; + + if(isVowel(s[j])) + ++currCnt; + + if(j - i + 1 == k) + { + ans = max(ans, currCnt); + + if(isVowel(s[i])) + --currCnt; + + --mp[s[i]]; + + if(mp[s[i]] == 0) + mp.erase(s[i]); + + ++i; + + } + ++j; + } + + + return ans; + + } +}; From 11c45df3ed6f6971c28b931551abbfd03c33424a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 07:33:53 +0530 Subject: [PATCH 0860/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1456-maximum-number-of-vowels-in-a-substring-of-given-length/NOTES.md diff --git a/1456-maximum-number-of-vowels-in-a-substring-of-given-length/NOTES.md b/1456-maximum-number-of-vowels-in-a-substring-of-given-length/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1456-maximum-number-of-vowels-in-a-substring-of-given-length/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5cae510850b34158c8ded52d47a0076a4a15348f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 07:33:56 +0530 Subject: [PATCH 0861/3167] Time: 61 ms (6.96%), Space: 10.3 MB (10.40%) - LeetHub From ce33f3151518ab55ddc4aedf711a87d0e8c6f94b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:27:07 +0530 Subject: [PATCH 0862/3167] Create README - LeetHub --- 0207-course-schedule/README.md | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0207-course-schedule/README.md diff --git a/0207-course-schedule/README.md b/0207-course-schedule/README.md new file mode 100644 index 00000000..c9f7b97e --- /dev/null +++ b/0207-course-schedule/README.md @@ -0,0 +1,36 @@ +

207. Course Schedule

Medium


There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

+ +
    +
  • For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
  • +
+ +

Return true if you can finish all courses. Otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: numCourses = 2, prerequisites = [[1,0]]
+Output: true
+Explanation: There are a total of 2 courses to take. 
+To take course 1 you should have finished course 0. So it is possible.
+
+ +

Example 2:

+ +
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
+Output: false
+Explanation: There are a total of 2 courses to take. 
+To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= numCourses <= 2000
  • +
  • 0 <= prerequisites.length <= 5000
  • +
  • prerequisites[i].length == 2
  • +
  • 0 <= ai, bi < numCourses
  • +
  • All the pairs prerequisites[i] are unique.
  • +
+
\ No newline at end of file From c382c836c77ce6c0cc118a5e8183872886318b2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:27:10 +0530 Subject: [PATCH 0863/3167] Time: 34 ms (11.98%), Space: 14.2 MB (35.80%) - LeetHub --- 0207-course-schedule/0207-course-schedule.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0207-course-schedule/0207-course-schedule.cpp diff --git a/0207-course-schedule/0207-course-schedule.cpp b/0207-course-schedule/0207-course-schedule.cpp new file mode 100644 index 00000000..7fec6a49 --- /dev/null +++ b/0207-course-schedule/0207-course-schedule.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + + bool dfs(int sv, vector adj[], vector& visited, vector& pathVisited) + { + visited[sv] = true; + pathVisited[sv] = true; + + for(auto itr : adj[sv]) + { + if(!visited[itr]) + { + if(dfs(itr, adj, visited, pathVisited) == true) + return true; + } + else if(pathVisited[itr]) + return true; + } + + pathVisited[sv] = false; + return false; + } + + bool canFinish(int numCourses, vector>& prerequisites) { + + int n = numCourses; + + vector adj[n+1]; + + for(auto itr : prerequisites) + adj[itr[1]].push_back(itr[0]); + + vector visited(n+1, false), pathVisited(n+1, false); + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + { + if(dfs(i, adj, visited, pathVisited)) + return false; + } + } + + return true; + } +}; \ No newline at end of file From baf5afb0fec865d576c24bb9a1e1a62883906a78 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:29:04 +0530 Subject: [PATCH 0864/3167] Attach NOTES - LeetHub --- 0207-course-schedule/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0207-course-schedule/NOTES.md diff --git a/0207-course-schedule/NOTES.md b/0207-course-schedule/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0207-course-schedule/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c37d7f15766d05c8760687d52c26631c513beb7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:29:07 +0530 Subject: [PATCH 0865/3167] Time: 34 ms (11.98%), Space: 14.2 MB (35.80%) - LeetHub From 557628471c2748e85c27cd1fcc174c36ac92791f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:31:58 +0530 Subject: [PATCH 0866/3167] Attach NOTES - LeetHub From 13f63f60db8efc32b4c0619963f29608f0d4cce7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:32:01 +0530 Subject: [PATCH 0867/3167] Time: 26 ms (39.62%), Space: 13.7 MB (63.42%) - LeetHub --- 0207-course-schedule/0207-course-schedule.cpp | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/0207-course-schedule/0207-course-schedule.cpp b/0207-course-schedule/0207-course-schedule.cpp index 7fec6a49..e3c97222 100644 --- a/0207-course-schedule/0207-course-schedule.cpp +++ b/0207-course-schedule/0207-course-schedule.cpp @@ -1,26 +1,6 @@ class Solution { public: - bool dfs(int sv, vector adj[], vector& visited, vector& pathVisited) - { - visited[sv] = true; - pathVisited[sv] = true; - - for(auto itr : adj[sv]) - { - if(!visited[itr]) - { - if(dfs(itr, adj, visited, pathVisited) == true) - return true; - } - else if(pathVisited[itr]) - return true; - } - - pathVisited[sv] = false; - return false; - } - bool canFinish(int numCourses, vector>& prerequisites) { int n = numCourses; @@ -30,17 +10,39 @@ class Solution { for(auto itr : prerequisites) adj[itr[1]].push_back(itr[0]); - vector visited(n+1, false), pathVisited(n+1, false); + vector indegree(n+1, 0); for(int i = 0; i < n; ++i) { - if(!visited[i]) + for(auto itr : adj[i]) + ++indegree[itr]; + } + + queue q; + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + if(indegree[i] == 0) + q.push(i); + } + + while(!q.empty()) + { + int curr = q.front(); + q.pop(); + + ++cnt; + + for(auto itr : adj[curr]) { - if(dfs(i, adj, visited, pathVisited)) - return false; + --indegree[itr]; + if(indegree[itr] == 0) + q.push(itr); } } - return true; + return cnt == n; } }; \ No newline at end of file From 0aee06cace947aa5102beec1d6b8307b97830eeb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:33:19 +0530 Subject: [PATCH 0868/3167] Attach NOTES - LeetHub From 0c38f303904b38a345fd5561fce0a7760fcd3776 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:33:22 +0530 Subject: [PATCH 0869/3167] Time: 26 ms (39.62%), Space: 13.7 MB (63.42%) - LeetHub From 9635006ceac1bf8871bae55a497dcdd69713b7ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:45:33 +0530 Subject: [PATCH 0870/3167] Create README - LeetHub --- 0210-course-schedule-ii/README.md | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0210-course-schedule-ii/README.md diff --git a/0210-course-schedule-ii/README.md b/0210-course-schedule-ii/README.md new file mode 100644 index 00000000..84a22833 --- /dev/null +++ b/0210-course-schedule-ii/README.md @@ -0,0 +1,42 @@ +

210. Course Schedule II

Medium


There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

+ +
    +
  • For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
  • +
+ +

Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.

+ +

 

+

Example 1:

+ +
Input: numCourses = 2, prerequisites = [[1,0]]
+Output: [0,1]
+Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
+
+ +

Example 2:

+ +
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
+Output: [0,2,1,3]
+Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
+So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
+
+ +

Example 3:

+ +
Input: numCourses = 1, prerequisites = []
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= numCourses <= 2000
  • +
  • 0 <= prerequisites.length <= numCourses * (numCourses - 1)
  • +
  • prerequisites[i].length == 2
  • +
  • 0 <= ai, bi < numCourses
  • +
  • ai != bi
  • +
  • All the pairs [ai, bi] are distinct.
  • +
+
\ No newline at end of file From b50ad10b8efdf88c5c9670a85b6eb347eed64fe3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:45:34 +0530 Subject: [PATCH 0871/3167] Attach NOTES - LeetHub --- 0210-course-schedule-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0210-course-schedule-ii/NOTES.md diff --git a/0210-course-schedule-ii/NOTES.md b/0210-course-schedule-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0210-course-schedule-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7e19503ce21e0fc9b14fa52a7ecee2101cd01500 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:45:38 +0530 Subject: [PATCH 0872/3167] Time: 34 ms (10.47%), Space: 14.6 MB (25.82%) - LeetHub --- .../0210-course-schedule-ii.cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 0210-course-schedule-ii/0210-course-schedule-ii.cpp diff --git a/0210-course-schedule-ii/0210-course-schedule-ii.cpp b/0210-course-schedule-ii/0210-course-schedule-ii.cpp new file mode 100644 index 00000000..b9e7b822 --- /dev/null +++ b/0210-course-schedule-ii/0210-course-schedule-ii.cpp @@ -0,0 +1,62 @@ +class Solution { + +private: + bool dfs(int sv, vector adj[], vector& visited, stack& st, vector& pathVisited) + { + visited[sv] = true; + pathVisited[sv] = true; + + for(auto itr : adj[sv]) + { + if(!visited[itr]) + { + if(dfs(itr, adj, visited, st, pathVisited) == true) + return true; + } + else if(pathVisited[itr]) + return true; + } + + pathVisited[sv] = false; + + st.push(sv); + + return false; + } + +public: + vector findOrder(int numCourses, vector>& prerequisites) { + + int n = numCourses; + + vector adj[n+1]; + + for(auto itr : prerequisites) + { + adj[itr[1]].push_back(itr[0]); + } + + vector visited(n, false), pathVisited(n, false); + + vector topo; + + stack st; + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + { + if(dfs(i, adj, visited, st, pathVisited) == true) + return {}; + } + } + + while(!st.empty()) + { + topo.push_back(st.top()); + st.pop(); + } + + return topo; + } +}; \ No newline at end of file From 560d971980db0ab19d0a04a109bbcde526e060f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:49:58 +0530 Subject: [PATCH 0873/3167] Attach NOTES - LeetHub From bd89a0e4c529efe604cc00aad759db67a63b7635 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 18:50:01 +0530 Subject: [PATCH 0874/3167] Time: 31 ms (16.25%), Space: 13.8 MB (60.72%) - LeetHub --- .../0210-course-schedule-ii.cpp | 59 ++++++++----------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/0210-course-schedule-ii/0210-course-schedule-ii.cpp b/0210-course-schedule-ii/0210-course-schedule-ii.cpp index b9e7b822..7d885a02 100644 --- a/0210-course-schedule-ii/0210-course-schedule-ii.cpp +++ b/0210-course-schedule-ii/0210-course-schedule-ii.cpp @@ -1,29 +1,5 @@ class Solution { -private: - bool dfs(int sv, vector adj[], vector& visited, stack& st, vector& pathVisited) - { - visited[sv] = true; - pathVisited[sv] = true; - - for(auto itr : adj[sv]) - { - if(!visited[itr]) - { - if(dfs(itr, adj, visited, st, pathVisited) == true) - return true; - } - else if(pathVisited[itr]) - return true; - } - - pathVisited[sv] = false; - - st.push(sv); - - return false; - } - public: vector findOrder(int numCourses, vector>& prerequisites) { @@ -36,27 +12,40 @@ class Solution { adj[itr[1]].push_back(itr[0]); } - vector visited(n, false), pathVisited(n, false); + vector indegree(n, 0), topo; - vector topo; + for(int i = 0; i < n; ++i) + { + for(auto itr : adj[i]) + ++indegree[itr]; + } - stack st; + queue q; for(int i = 0; i < n; ++i) { - if(!visited[i]) - { - if(dfs(i, adj, visited, st, pathVisited) == true) - return {}; - } + if(indegree[i] == 0) + q.push(i); } - while(!st.empty()) + while(!q.empty()) { - topo.push_back(st.top()); - st.pop(); + int curr = q.front(); + q.pop(); + + topo.push_back(curr); + + for(auto itr : adj[curr]) + { + --indegree[itr]; + if(indegree[itr] == 0) + q.push(itr); + } } + if(topo.size() < n) + return {}; + return topo; } }; \ No newline at end of file From 5dcf395cff332f7c1e07b7905ec8319146c73b5c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 23:57:30 +0530 Subject: [PATCH 0875/3167] Attach NOTES - LeetHub --- 0802-find-eventual-safe-states/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0802-find-eventual-safe-states/NOTES.md diff --git a/0802-find-eventual-safe-states/NOTES.md b/0802-find-eventual-safe-states/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0802-find-eventual-safe-states/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9eecac04bb1752bd6342e6fd9ccef8452559e2bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 May 2023 23:57:33 +0530 Subject: [PATCH 0876/3167] Time: 249 ms (11.50%), Space: 58.9 MB (34.92%) - LeetHub --- .../0802-find-eventual-safe-states.cpp | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp b/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp index 2c7eb376..01856737 100644 --- a/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp +++ b/0802-find-eventual-safe-states/0802-find-eventual-safe-states.cpp @@ -1,55 +1,55 @@ class Solution { public: - - bool dfs(int sv, vector>& graph, vector& visited, vector& pathVisited, vector& check) - { - visited[sv] = true; - pathVisited[sv] = true; - check[sv] = false; - - for(auto itr : graph[sv]) - { - if(!visited[itr]) - { - if(dfs(itr, graph, visited, pathVisited, check) == true) - { - check[itr] = false; - return true; - } - } - else if(pathVisited[itr]) - { - check[itr] = false; - return true; - } - } - - pathVisited[sv] = false; - check[sv] = true; - - return false; - } - vector eventualSafeNodes(vector>& graph) { int n = graph.size(); - vector visited(n+1, false), pathVisited(n+1, false), check(n+1, false); + vector adj[n+1]; - vector safe; + for(int i = 0; i < n; ++i) + { + for(auto itr : graph[i]) + adj[itr].push_back(i); + + } + + + vector indegree(n, 0); for(int i = 0; i < n; ++i) { - if(!visited[i]) - dfs(i, graph, visited, pathVisited, check); + for(auto itr : adj[i]) + ++indegree[itr]; } + queue q; + for(int i = 0; i < n; ++i) { - if(check[i]) - safe.push_back(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 : adj[curr]) + { + --indegree[itr]; + + if(indegree[itr] == 0) + q.push(itr); + } + } + + sort(safe.begin(),safe.end()); + return safe; } From d038c91a156acfa8d58e8f47b2a53a7374fb8bc4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:34:36 +0530 Subject: [PATCH 0877/3167] Create README - LeetHub --- 2620-counter/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2620-counter/README.md diff --git a/2620-counter/README.md b/2620-counter/README.md new file mode 100644 index 00000000..26ee6dd6 --- /dev/null +++ b/2620-counter/README.md @@ -0,0 +1,32 @@ +

2620. Counter

Easy


Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc).

+ +

 

+

Example 1:

+ +
Input: 
+n = 10 
+["call","call","call"]
+Output: [10,11,12]
+Explanation: 
+counter() = 10 // The first time counter() is called, it returns n.
+counter() = 11 // Returns 1 more than the previous time.
+counter() = 12 // Returns 1 more than the previous time.
+
+ +

Example 2:

+ +
Input: 
+n = -2
+["call","call","call","call","call"]
+Output: [-2,-1,0,1,2]
+Explanation: counter() initially returns -2. Then increases after each sebsequent call.
+
+ +

 

+

Constraints:

+ +
    +
  • -1000 <= n <= 1000
  • +
  • At most 1000 calls to counter() will be made
  • +
+
\ No newline at end of file From 0a46afd7965d5b3be5aa24c5eff385e4a1778e9c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:34:40 +0530 Subject: [PATCH 0878/3167] Time: 70 ms (6.77%), Space: 41.5 MB (90.25%) - LeetHub --- 2620-counter/2620-counter.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2620-counter/2620-counter.js diff --git a/2620-counter/2620-counter.js b/2620-counter/2620-counter.js new file mode 100644 index 00000000..25d6e6b4 --- /dev/null +++ b/2620-counter/2620-counter.js @@ -0,0 +1,17 @@ +/** + * @param {number} n + * @return {Function} counter + */ +var createCounter = function(n) { + return function() { + ++n; + return n-1; + }; +}; + +/** + * const counter = createCounter(10) + * counter() // 10 + * counter() // 11 + * counter() // 12 + */ \ No newline at end of file From fffca35fca6a5d3db5ddc9eac0f1844bff471321 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:35:05 +0530 Subject: [PATCH 0879/3167] Attach NOTES - LeetHub --- 2620-counter/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2620-counter/NOTES.md diff --git a/2620-counter/NOTES.md b/2620-counter/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2620-counter/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0f7970a0c53cfbc8e4b9e9c1f003e435fde4c6f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:35:08 +0530 Subject: [PATCH 0880/3167] Time: 70 ms (6.77%), Space: 41.5 MB (90.25%) - LeetHub From 6f1a34543209c9f793f05f6a81a191caa3acbab0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:36:03 +0530 Subject: [PATCH 0881/3167] Attach NOTES - LeetHub From e1ea75eae9c41c3e2d95f5c270b908612a6d16a2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:36:06 +0530 Subject: [PATCH 0882/3167] Time: 70 ms (6.77%), Space: 41.5 MB (90.25%) - LeetHub From d011f788386880343dd614b7721746b67a86ebd0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 07:36:38 +0530 Subject: [PATCH 0883/3167] Attach NOTES - LeetHub From 8e06a3b1d0659e884803518fbb0f288161a96460 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 08:01:36 +0530 Subject: [PATCH 0884/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1498-number-of-subsequences-that-satisfy-the-given-sum-condition/README.md diff --git a/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/README.md b/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/README.md new file mode 100644 index 00000000..f6ad7ca1 --- /dev/null +++ b/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/README.md @@ -0,0 +1,41 @@ +

1498. Number of Subsequences That Satisfy the Given Sum Condition

Medium


You are given an array of integers nums and an integer target.

+ +

Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: nums = [3,5,6,7], target = 9
+Output: 4
+Explanation: There are 4 subsequences that satisfy the condition.
+[3] -> Min value + max value <= target (3 + 3 <= 9)
+[3,5] -> (3 + 5 <= 9)
+[3,5,6] -> (3 + 6 <= 9)
+[3,6] -> (3 + 6 <= 9)
+
+ +

Example 2:

+ +
Input: nums = [3,3,6,8], target = 10
+Output: 6
+Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
+[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
+
+ +

Example 3:

+ +
Input: nums = [2,3,3,4,6,7], target = 12
+Output: 61
+Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
+Number of valid subsequences (63 - 2 = 61).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
  • 1 <= target <= 106
  • +
+
\ No newline at end of file From b9219d6d571486ee9032f73bee6363d77279e9ec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 08:01:36 +0530 Subject: [PATCH 0885/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1498-number-of-subsequences-that-satisfy-the-given-sum-condition/NOTES.md diff --git a/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/NOTES.md b/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9c8692ae3c4c4d3ef501f532dcb2a921a24175dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 May 2023 08:01:39 +0530 Subject: [PATCH 0886/3167] Time: 177 ms (19.73%), Space: 47.7 MB (93.20%) - LeetHub --- ...s-that-satisfy-the-given-sum-condition.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1498-number-of-subsequences-that-satisfy-the-given-sum-condition/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.cpp diff --git a/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.cpp b/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.cpp new file mode 100644 index 00000000..aaf84d08 --- /dev/null +++ b/1498-number-of-subsequences-that-satisfy-the-given-sum-condition/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.cpp @@ -0,0 +1,49 @@ +class Solution { + +private: + + long long expo(long long x, long long y) + { + long long res = 1; + + while( y > 0) + { + if(y & 1) + res = (res * x) % mod; + + y >>= 1; + + x = (x*x) % mod; + } + + return res % mod; + } + +public: + + int mod = 1e9 + 7; + + int numSubseq(vector& nums, int target) { + + long long cnt = 0, n = nums.size(); + + sort(nums.begin(),nums.end()); + + long long l = 0, r = n - 1; + + while(l <= r) + { + if(nums[l] + nums[r] <= target) + { + cnt += expo(2,(r-l)); + cnt %= mod; + ++l; + } + else + --r; + } + + return cnt; + + } +}; \ No newline at end of file From b98d3ed20164f45366aa2e99de6aa0119bb095da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 May 2023 07:14:54 +0530 Subject: [PATCH 0887/3167] Create README - LeetHub --- 2665-counter-ii/README.md | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2665-counter-ii/README.md diff --git a/2665-counter-ii/README.md b/2665-counter-ii/README.md new file mode 100644 index 00000000..83e4bfe1 --- /dev/null +++ b/2665-counter-ii/README.md @@ -0,0 +1,43 @@ +

2665. Counter II

Easy


Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.

+ +

The three functions are:

+ +
    +
  • increment() increases the current value by 1 and then returns it.
  • +
  • decrement() reduces the current value by 1 and then returns it.
  • +
  • reset() sets the current value to init and then returns it.
  • +
+ +

 

+

Example 1:

+ +
Input: init = 5, calls = ["increment","reset","decrement"]
+Output: [6,5,4]
+Explanation:
+const counter = createCounter(5);
+counter.increment(); // 6
+counter.reset(); // 5
+counter.decrement(); // 4
+
+ +

Example 2:

+ +
Input: init = 0, calls = ["increment","increment","decrement","reset","reset"]
+Output: [1,2,1,0,0]
+Explanation:
+const counter = createCounter(0);
+counter.increment(); // 1
+counter.increment(); // 2
+counter.decrement(); // 1
+counter.reset(); // 0
+counter.reset(); // 0
+
+ +

 

+

Constraints:

+ +
    +
  • -1000 <= init <= 1000
  • +
  • total calls not to exceed 1000
  • +
+
\ No newline at end of file From a423c59d0a5c067bbcc877b275a8bdf352f48994 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 May 2023 07:14:57 +0530 Subject: [PATCH 0888/3167] Time: 69 ms (46.34%), Space: 44.6 MB (74.53%) - LeetHub --- 2665-counter-ii/2665-counter-ii.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2665-counter-ii/2665-counter-ii.js diff --git a/2665-counter-ii/2665-counter-ii.js b/2665-counter-ii/2665-counter-ii.js new file mode 100644 index 00000000..0c55439c --- /dev/null +++ b/2665-counter-ii/2665-counter-ii.js @@ -0,0 +1,18 @@ +/** + * @param {integer} init + * @return { increment: Function, decrement: Function, reset: Function } + */ +var createCounter = function(init) { + let cnt = init; + return{ + increment: () => cnt+=1, decrement: () => cnt-=1, reset: () => (cnt=init), + } +}; + +/** + * const counter = createCounter(5) + * counter.increment(); // 6 + * counter.reset(); // 5 + * counter.decrement(); // 4 + */ + From 299a37bb58cdf1ff4adee192ed885bab08aab9b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 May 2023 09:53:54 +0530 Subject: [PATCH 0889/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2672-number-of-adjacent-elements-with-the-same-color/README.md diff --git a/2672-number-of-adjacent-elements-with-the-same-color/README.md b/2672-number-of-adjacent-elements-with-the-same-color/README.md new file mode 100644 index 00000000..07f8e95a --- /dev/null +++ b/2672-number-of-adjacent-elements-with-the-same-color/README.md @@ -0,0 +1,42 @@ +

2672. Number of Adjacent Elements With the Same Color

Medium


There is a 0-indexed array nums of length n. Initially, all elements are uncolored (has a value of 0).

+ +

You are given a 2D integer array queries where queries[i] = [indexi, colori].

+ +

For each query, you color the index indexi with the color colori in the array nums.

+ +

Return an array answer of the same length as queries where answer[i] is the number of adjacent elements with the same color after the ith query.

+ +

More formally, answer[i] is the number of indices j, such that 0 <= j < n - 1 and nums[j] == nums[j + 1] and nums[j] != 0 after the ith query.

+ +

 

+

Example 1:

+ +
Input: n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
+Output: [0,1,1,0,2]
+Explanation: Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
+- After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
+- After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
+- After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
+- After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
+- After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.
+
+ +

Example 2:

+ +
Input: n = 1, queries = [[0,100000]]
+Output: [0]
+Explanation: Initially array nums = [0], where 0 denotes uncolored elements of the array.
+- After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= queries.length <= 105
  • +
  • queries[i].length == 2
  • +
  • 0 <= indexi <= n - 1
  • +
  • 1 <=  colori <= 105
  • +
+
\ No newline at end of file From e1fb9da0c6777b541453293cef9599c8c3d96107 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 May 2023 09:53:58 +0530 Subject: [PATCH 0890/3167] Time: 720 ms (44.44%), Space: 198.5 MB (33.33%) - LeetHub --- ...-adjacent-elements-with-the-same-color.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2672-number-of-adjacent-elements-with-the-same-color/2672-number-of-adjacent-elements-with-the-same-color.cpp diff --git a/2672-number-of-adjacent-elements-with-the-same-color/2672-number-of-adjacent-elements-with-the-same-color.cpp b/2672-number-of-adjacent-elements-with-the-same-color/2672-number-of-adjacent-elements-with-the-same-color.cpp new file mode 100644 index 00000000..55f9304a --- /dev/null +++ b/2672-number-of-adjacent-elements-with-the-same-color/2672-number-of-adjacent-elements-with-the-same-color.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + vector colorTheArray(int n, vector>& queries) { + + vector v(n,0); + + vector ans; + + int cnt = 0; + + for(auto itr : queries) + { + + int idx = itr[0]; + int col = itr[1]; + + if(idx > 0 and v[idx-1] == v[idx] and v[idx]) + --cnt; + if(idx+1 < n and v[idx+1] == v[idx] and v[idx]) + --cnt; + + v[idx] = col; + + if(idx > 0 and v[idx-1] == v[idx]) + ++cnt; + if(idx+1 < n and v[idx+1] == v[idx]) + ++cnt; + + ans.push_back(cnt); + } + + return ans; + + } + + +}; \ No newline at end of file From f07dc1a4a847e6633fcccd72899103744508e631 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 May 2023 10:55:33 +0530 Subject: [PATCH 0891/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1964-find-the-longest-valid-obstacle-course-at-each-position/NOTES.md diff --git a/1964-find-the-longest-valid-obstacle-course-at-each-position/NOTES.md b/1964-find-the-longest-valid-obstacle-course-at-each-position/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1964-find-the-longest-valid-obstacle-course-at-each-position/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From dc8519b99f709c919d593d5f1bbc1827f5595ee7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 May 2023 10:55:37 +0530 Subject: [PATCH 0892/3167] Time: 375 ms (27.08%), Space: 118.9 MB (56.94%) - LeetHub --- ...valid-obstacle-course-at-each-position.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1964-find-the-longest-valid-obstacle-course-at-each-position/1964-find-the-longest-valid-obstacle-course-at-each-position.cpp diff --git a/1964-find-the-longest-valid-obstacle-course-at-each-position/1964-find-the-longest-valid-obstacle-course-at-each-position.cpp b/1964-find-the-longest-valid-obstacle-course-at-each-position/1964-find-the-longest-valid-obstacle-course-at-each-position.cpp new file mode 100644 index 00000000..94ffaf96 --- /dev/null +++ b/1964-find-the-longest-valid-obstacle-course-at-each-position/1964-find-the-longest-valid-obstacle-course-at-each-position.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + int helper(int idx, int n, int prev, vector& obstacles) + { + if(idx > n) + return 0; + + int take = 0 , notTake = 0; + + notTake = helper(idx+1, n, prev, obstacles); + + if(prev == -1 or obstacles[idx] >= obstacles[prev] ) + take = max(helper(idx+1, n, prev, obstacles), 1 + helper(idx+1, n, idx, obstacles)); + + return max(take, notTake); + } + + + vector longestObstacleCourseAtEachPosition(vector& obstacles) { + + vector dp (obstacles.size(),1); + int result=1; + vector b(obstacles.size()+1, INT_MAX); + b[0]=INT_MIN; + + for(int i=0; i Date: Mon, 8 May 2023 07:26:01 +0530 Subject: [PATCH 0893/3167] Create README - LeetHub --- 1572-matrix-diagonal-sum/README.md | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1572-matrix-diagonal-sum/README.md diff --git a/1572-matrix-diagonal-sum/README.md b/1572-matrix-diagonal-sum/README.md new file mode 100644 index 00000000..98d612e2 --- /dev/null +++ b/1572-matrix-diagonal-sum/README.md @@ -0,0 +1,39 @@ +

1572. Matrix Diagonal Sum

Easy


Given a square matrix mat, return the sum of the matrix diagonals.

+ +

Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

+ +

 

+

Example 1:

+ +
Input: mat = [[1,2,3],
+              [4,5,6],
+              [7,8,9]]
+Output: 25
+Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
+Notice that element mat[1][1] = 5 is counted only once.
+
+ +

Example 2:

+ +
Input: mat = [[1,1,1,1],
+              [1,1,1,1],
+              [1,1,1,1],
+              [1,1,1,1]]
+Output: 8
+
+ +

Example 3:

+ +
Input: mat = [[5]]
+Output: 5
+
+ +

 

+

Constraints:

+ +
    +
  • n == mat.length == mat[i].length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= mat[i][j] <= 100
  • +
+
\ No newline at end of file From 0647dcc64d01efac5d01752d52650b295816fbcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:26:02 +0530 Subject: [PATCH 0894/3167] Attach NOTES - LeetHub --- 1572-matrix-diagonal-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1572-matrix-diagonal-sum/NOTES.md diff --git a/1572-matrix-diagonal-sum/NOTES.md b/1572-matrix-diagonal-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1572-matrix-diagonal-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 420b0786121312dda8f7c3308ae7658ac8c6a67d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:26:05 +0530 Subject: [PATCH 0895/3167] Time: 13 ms (75.43%), Space: 11.3 MB (64.67%) - LeetHub --- .../1572-matrix-diagonal-sum.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1572-matrix-diagonal-sum/1572-matrix-diagonal-sum.cpp diff --git a/1572-matrix-diagonal-sum/1572-matrix-diagonal-sum.cpp b/1572-matrix-diagonal-sum/1572-matrix-diagonal-sum.cpp new file mode 100644 index 00000000..7025a6aa --- /dev/null +++ b/1572-matrix-diagonal-sum/1572-matrix-diagonal-sum.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int diagonalSum(vector>& mat) { + + int n = mat.size(), m = mat[0].size(); + + int sum = 0; + + for(int i = 0; i < n; ++i) + { + sum += (mat[i][n-i-1] + mat[i][i]); + } + + sum = (n & 1) ? sum - mat[n/2][n/2] : sum; + + return sum; + + } +}; \ No newline at end of file From c2515923e4022823b595b4e8e620557283236e8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:29:18 +0530 Subject: [PATCH 0896/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2635-apply-transform-over-each-element-in-array/README.md diff --git a/2635-apply-transform-over-each-element-in-array/README.md b/2635-apply-transform-over-each-element-in-array/README.md new file mode 100644 index 00000000..eea14ea6 --- /dev/null +++ b/2635-apply-transform-over-each-element-in-array/README.md @@ -0,0 +1,39 @@ +

2635. Apply Transform Over Each Element in Array

Easy


Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element.

+ +

The returned array should be created such that returnedArray[i] = fn(arr[i], i).

+ +

Please solve it without the built-in Array.map method.

+ +

 

+

Example 1:

+ +
Input: arr = [1,2,3], fn = function plusone(n) { return n + 1; }
+Output: [2,3,4]
+Explanation:
+const newArray = map(arr, plusone); // [2,3,4]
+The function increases each value in the array by one. 
+
+ +

Example 2:

+ +
Input: arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
+Output: [1,3,5]
+Explanation: The function increases each value by the index it resides in.
+
+ +

Example 3:

+ +
Input: arr = [10,20,30], fn = function constant() { return 42; }
+Output: [42,42,42]
+Explanation: The function always returns 42.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= arr.length <= 1000
  • +
  • -109 <= arr[i] <= 109
  • +
  • fn returns a number
  • +
+
\ No newline at end of file From ccd2d68ea573f454dff0726f494a601c7ed669e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:29:22 +0530 Subject: [PATCH 0897/3167] Time: 55 ms (72.79%), Space: 42.6 MB (10.87%) - LeetHub --- ...35-apply-transform-over-each-element-in-array.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.js diff --git a/2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.js b/2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.js new file mode 100644 index 00000000..dafe7e12 --- /dev/null +++ b/2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.js @@ -0,0 +1,13 @@ +/** + * @param {number[]} arr + * @param {Function} fn + * @return {number[]} + */ +var map = function(arr, fn) { + var mp = []; + + for(var i = 0; i < arr.length; ++i) + mp.push(fn(arr[i],i)); + + return mp; +}; From 8c6f59d8f8da517a8c89c0142676694a68754bba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:38:43 +0530 Subject: [PATCH 0898/3167] Create README - LeetHub From 34ed0c5a45b77d349cd6d77e6d79f7a63f32aec4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:38:44 +0530 Subject: [PATCH 0899/3167] Attach NOTES - LeetHub --- 2635-apply-transform-over-each-element-in-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2635-apply-transform-over-each-element-in-array/NOTES.md diff --git a/2635-apply-transform-over-each-element-in-array/NOTES.md b/2635-apply-transform-over-each-element-in-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2635-apply-transform-over-each-element-in-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a0c2d79155cfde27cf2bb2587e451f42916a28ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 May 2023 07:38:47 +0530 Subject: [PATCH 0900/3167] Time: 67 ms (26.81%), Space: 43.3 MB (40.43%) - LeetHub --- .../2635-apply-transform-over-each-element-in-array.ts | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.ts diff --git a/2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.ts b/2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.ts new file mode 100644 index 00000000..0ff19d6d --- /dev/null +++ b/2635-apply-transform-over-each-element-in-array/2635-apply-transform-over-each-element-in-array.ts @@ -0,0 +1,9 @@ +function map(arr: number[], fn: (n: number, i: number) => number): number[] { + + const mp : number[] = []; + + for(var i = 0; i < arr.length; ++i) + mp.push(fn(arr[i],i)); + + return mp; +}; \ No newline at end of file From a4061f63b66a0aa8c6c98d576e49787a80de247e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:56:27 +0530 Subject: [PATCH 0901/3167] Create README - LeetHub --- 2634-filter-elements-from-array/README.md | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2634-filter-elements-from-array/README.md diff --git a/2634-filter-elements-from-array/README.md b/2634-filter-elements-from-array/README.md new file mode 100644 index 00000000..e168964e --- /dev/null +++ b/2634-filter-elements-from-array/README.md @@ -0,0 +1,40 @@ +

2634. Filter Elements from Array

Easy


Given an integer array arr and a filtering function fn, return a new array with a fewer or equal number of elements.

+ +

The returned array should only contain elements where fn(arr[i], i) evaluated to a truthy value.

+ +

Please solve it without the built-in Array.filter method.

+ +

 

+

Example 1:

+ +
Input: arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
+Output: [20,30]
+Explanation:
+const newArray = filter(arr, fn); // [20, 30]
+The function filters out values that are not greater than 10
+ +

Example 2:

+ +
Input: arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
+Output: [1]
+Explanation:
+fn can also accept the index of each element
+In this case, the function removes elements not at index 0
+
+ +

Example 3:

+ +
Input: arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
+Output: [-2,0,1,2]
+Explanation:
+Falsey values such as 0 should be filtered out
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= arr.length <= 1000
  • +
  • -109 <= arr[i] <= 109
  • +
+
\ No newline at end of file From e49fce76402601c21791cc10c2195e978d63bee7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:56:30 +0530 Subject: [PATCH 0902/3167] Time: 69 ms (5.78%), Space: 42.1 MB (50.33%) - LeetHub --- .../2634-filter-elements-from-array.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2634-filter-elements-from-array/2634-filter-elements-from-array.js diff --git a/2634-filter-elements-from-array/2634-filter-elements-from-array.js b/2634-filter-elements-from-array/2634-filter-elements-from-array.js new file mode 100644 index 00000000..09e068af --- /dev/null +++ b/2634-filter-elements-from-array/2634-filter-elements-from-array.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} arr + * @param {Function} fn + * @return {number[]} + */ +var filter = function(arr, fn) { + + var ans = []; + + for(var i = 0; i < arr.length; ++i) + { + if(fn(arr[i], i)) + ans.push(arr[i]); + } + + return ans; + +}; + From 338956d4c86c8fb29b14eb4ad050e5db076a2909 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:57:11 +0530 Subject: [PATCH 0903/3167] Attach NOTES - LeetHub --- 2634-filter-elements-from-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2634-filter-elements-from-array/NOTES.md diff --git a/2634-filter-elements-from-array/NOTES.md b/2634-filter-elements-from-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2634-filter-elements-from-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d7ea9062f6abe954c8548d8c159f30cb91045132 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:57:14 +0530 Subject: [PATCH 0904/3167] Time: 69 ms (5.78%), Space: 42.1 MB (50.33%) - LeetHub From f60538dca3f81e2d8ebb0875b5fb763e8d1e26bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:57:52 +0530 Subject: [PATCH 0905/3167] Create README - LeetHub From bfb97f73b31b83d7bf86ce18823131b8db2479e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:57:52 +0530 Subject: [PATCH 0906/3167] Attach NOTES - LeetHub From 5d7119c352e709e8f353b0deefca7f1e9a1ca59e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:57:56 +0530 Subject: [PATCH 0907/3167] Time: 69 ms (5.78%), Space: 42.1 MB (50.33%) - LeetHub --- .../2634-filter-elements-from-array.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2634-filter-elements-from-array/2634-filter-elements-from-array.ts diff --git a/2634-filter-elements-from-array/2634-filter-elements-from-array.ts b/2634-filter-elements-from-array/2634-filter-elements-from-array.ts new file mode 100644 index 00000000..09e068af --- /dev/null +++ b/2634-filter-elements-from-array/2634-filter-elements-from-array.ts @@ -0,0 +1,19 @@ +/** + * @param {number[]} arr + * @param {Function} fn + * @return {number[]} + */ +var filter = function(arr, fn) { + + var ans = []; + + for(var i = 0; i < arr.length; ++i) + { + if(fn(arr[i], i)) + ans.push(arr[i]); + } + + return ans; + +}; + From 04d3faefa443a39c80184f177a3febc509df9031 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:59:53 +0530 Subject: [PATCH 0908/3167] Attach NOTES - LeetHub From c605b8ab7048e37866b167f5deebcf46ca285406 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 19:59:55 +0530 Subject: [PATCH 0909/3167] Time: 69 ms (5.78%), Space: 42.1 MB (50.33%) - LeetHub From f97b1476b0bfb0c393a6ec9c324aee44686a899d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 20:01:40 +0530 Subject: [PATCH 0910/3167] Attach NOTES - LeetHub From 7bba74988323bbcb1e4c7b59d6b29cf712721ea4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 May 2023 20:01:43 +0530 Subject: [PATCH 0911/3167] Time: 67 ms (31.60%), Space: 42.5 MB (90.77%) - LeetHub --- .../2634-filter-elements-from-array.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/2634-filter-elements-from-array/2634-filter-elements-from-array.ts b/2634-filter-elements-from-array/2634-filter-elements-from-array.ts index 09e068af..b321d81e 100644 --- a/2634-filter-elements-from-array/2634-filter-elements-from-array.ts +++ b/2634-filter-elements-from-array/2634-filter-elements-from-array.ts @@ -1,19 +1,12 @@ -/** - * @param {number[]} arr - * @param {Function} fn - * @return {number[]} - */ -var filter = function(arr, fn) { - - var ans = []; +function filter(arr: number[], fn: (n: number, i: number) => any): number[] { + + const ans : number[] = []; for(var i = 0; i < arr.length; ++i) { - if(fn(arr[i], i)) + if(fn(arr[i],i)) ans.push(arr[i]); } return ans; - }; - From 48f5ca456805ec9605a9b0bb9d939a7a94177355 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 19:34:01 +0530 Subject: [PATCH 0912/3167] Create README - LeetHub --- 0005-longest-palindromic-substring/README.md | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0005-longest-palindromic-substring/README.md diff --git a/0005-longest-palindromic-substring/README.md b/0005-longest-palindromic-substring/README.md new file mode 100644 index 00000000..ea3d6571 --- /dev/null +++ b/0005-longest-palindromic-substring/README.md @@ -0,0 +1,24 @@ +

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 From 22663387e8e7341ff47c36a92895b3b2b38de480 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 19:34:05 +0530 Subject: [PATCH 0913/3167] Time: 1679 ms (6.02%), Space: 686.7 MB (5.23%) - LeetHub --- .../0005-longest-palindromic-substring.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp diff --git a/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp b/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp new file mode 100644 index 00000000..d32c36fc --- /dev/null +++ b/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + string longestPalindrome(string s) { + + int n = s.size(); + + vector> dp(n+1, vector(n+1, 0)); + + string ans; + + int maxLength = 0; + + for(int diff = 0; diff < n; ++diff) + { + for(int i = 0, j = i+diff; i < n and j < n; ++i, ++j) + { + if(i == j) + dp[i][j] = 1; + else if(diff == 1) + { + if(s[i] == s[j]) + { + dp[i][j] = 2; + } + } + else + { + if(s[i] == s[j]) + { + if(dp[i+1][j-1]) + { + dp[i][j] = dp[i+1][j-1] + 2; + } + } + } + + int len = j - i + 1; + + if(dp[i][j] > maxLength) + ans = s.substr(i,len); + + } + } + + return ans; + } +}; \ No newline at end of file From 0181f2d9354103318ca3fd16fd2372eba2b9deb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 19:34:49 +0530 Subject: [PATCH 0914/3167] Attach NOTES - LeetHub --- 0005-longest-palindromic-substring/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0005-longest-palindromic-substring/NOTES.md diff --git a/0005-longest-palindromic-substring/NOTES.md b/0005-longest-palindromic-substring/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0005-longest-palindromic-substring/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 56cec9cd6fce638df55fa42bf288720c4d12ccf1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 19:34:53 +0530 Subject: [PATCH 0915/3167] Time: 1679 ms (6.02%), Space: 686.7 MB (5.23%) - LeetHub From 7c16f6c807ce77ce4d0d73b558b2b59a1f6c9180 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:17:04 +0530 Subject: [PATCH 0916/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2140-solving-questions-with-brainpower/README.md diff --git a/2140-solving-questions-with-brainpower/README.md b/2140-solving-questions-with-brainpower/README.md new file mode 100644 index 00000000..f93585af --- /dev/null +++ b/2140-solving-questions-with-brainpower/README.md @@ -0,0 +1,49 @@ +

2140. Solving Questions With Brainpower

Medium


You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri].

+ +

The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question 0) and make a decision whether to solve or skip each question. Solving question i will earn you pointsi points but you will be unable to solve each of the next brainpoweri questions. If you skip question i, you get to make the decision on the next question.

+ +
    +
  • For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]: + +
      +
    • If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.
    • +
    • If instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.
    • +
    +
  • +
+ +

Return the maximum points you can earn for the exam.

+ +

 

+

Example 1:

+ +
Input: questions = [[3,2],[4,3],[4,4],[2,5]]
+Output: 5
+Explanation: The maximum points can be earned by solving questions 0 and 3.
+- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
+- Unable to solve questions 1 and 2
+- Solve question 3: Earn 2 points
+Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
+
+ +

Example 2:

+ +
Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
+Output: 7
+Explanation: The maximum points can be earned by solving questions 1 and 4.
+- Skip question 0
+- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
+- Unable to solve questions 2 and 3
+- Solve question 4: Earn 5 points
+Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= questions.length <= 105
  • +
  • questions[i].length == 2
  • +
  • 1 <= pointsi, brainpoweri <= 105
  • +
+
\ No newline at end of file From 397163a2fe2f7e417206efcc9c0836387256176b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:17:08 +0530 Subject: [PATCH 0917/3167] Time: 393 ms (31.91%), Space: 123 MB (44.72%) - LeetHub --- ...2140-solving-questions-with-brainpower.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2140-solving-questions-with-brainpower/2140-solving-questions-with-brainpower.cpp diff --git a/2140-solving-questions-with-brainpower/2140-solving-questions-with-brainpower.cpp b/2140-solving-questions-with-brainpower/2140-solving-questions-with-brainpower.cpp new file mode 100644 index 00000000..df489838 --- /dev/null +++ b/2140-solving-questions-with-brainpower/2140-solving-questions-with-brainpower.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + long long helper(int i, int n, vector>& questions, vector& dp) + { + if(i >= n) + return 0; + + if(dp[i] != -1) + return dp[i]; + + long long sum = 0; + + long long take = questions[i][0] + helper(i + questions[i][1] + 1, n, questions, dp); + + long long notTake = helper(i+1, n, questions, dp); + + return dp[i] = max(take , notTake); + } + + long long mostPoints(vector>& questions) { + + int n = questions.size(); + + vector dp(n+1, -1); + + return helper(0, n, questions, dp); + + } +}; \ No newline at end of file From 705b8cb95c6242ec8880e785659d8f35eabfc365 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:34:45 +0530 Subject: [PATCH 0918/3167] Create README - LeetHub --- 2666-allow-one-function-call/README.md | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2666-allow-one-function-call/README.md diff --git a/2666-allow-one-function-call/README.md b/2666-allow-one-function-call/README.md new file mode 100644 index 00000000..0e0e4d60 --- /dev/null +++ b/2666-allow-one-function-call/README.md @@ -0,0 +1,38 @@ +

2666. Allow One Function Call

Easy


Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.

+ +
    +
  • The first time the returned function is called, it should return the same result as fn.
  • +
  • Every subsequent time it is called, it should return undefined.
  • +
+ +

 

+

Example 1:

+ +
Input: fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
+Output: [{"calls":1,"value":6}]
+Explanation:
+const onceFn = once(fn);
+onceFn(1, 2, 3); // 6
+onceFn(2, 3, 6); // undefined, fn was not called
+
+ +

Example 2:

+ +
Input: fn = (a,b,c) => (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]
+Output: [{"calls":1,"value":140}]
+Explanation:
+const onceFn = once(fn);
+onceFn(5, 7, 4); // 140
+onceFn(2, 3, 6); // undefined, fn was not called
+onceFn(4, 6, 8); // undefined, fn was not called
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= calls.length <= 10
  • +
  • 1 <= calls[i].length <= 100
  • +
  • 2 <= JSON.stringify(calls).length <= 1000
  • +
+
\ No newline at end of file From efa596437a6be7a7c6ed419dd8117882ced1ad32 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:34:46 +0530 Subject: [PATCH 0919/3167] Attach NOTES - LeetHub --- 2666-allow-one-function-call/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2666-allow-one-function-call/NOTES.md diff --git a/2666-allow-one-function-call/NOTES.md b/2666-allow-one-function-call/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2666-allow-one-function-call/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 57fadd73a8325c837926d16d5a33f58cc704a122 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:34:49 +0530 Subject: [PATCH 0920/3167] Time: 64 ms (25.47%), Space: 42.1 MB (30.84%) - LeetHub --- .../2666-allow-one-function-call.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2666-allow-one-function-call/2666-allow-one-function-call.js diff --git a/2666-allow-one-function-call/2666-allow-one-function-call.js b/2666-allow-one-function-call/2666-allow-one-function-call.js new file mode 100644 index 00000000..d78e0aac --- /dev/null +++ b/2666-allow-one-function-call/2666-allow-one-function-call.js @@ -0,0 +1,23 @@ +/** + * @param {Function} fn + * @return {Function} + */ +var once = function(fn) { + + var ok = false; + + return function(...args){ + if(ok) + return undefined; + ok = true; + return fn(...args); + } +}; + +/** + * let fn = (a,b,c) => (a + b + c) + * let onceFn = once(fn) + * + * onceFn(1,2,3); // 6 + * onceFn(2,3,6); // returns undefined without calling fn + */ From 9fcdbff194bff36a6d408f7e2ce86aeb330fbc2e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:36:06 +0530 Subject: [PATCH 0921/3167] Create README - LeetHub From 70b96d0ec26b6a20e3723d33e69f3a45a140ae43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:36:06 +0530 Subject: [PATCH 0922/3167] Attach NOTES - LeetHub From 670d456b7110d749dd7c827e9c684aaf25c47efe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:36:10 +0530 Subject: [PATCH 0923/3167] Time: 64 ms (25.47%), Space: 42.1 MB (30.84%) - LeetHub --- .../2666-allow-one-function-call.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2666-allow-one-function-call/2666-allow-one-function-call.ts diff --git a/2666-allow-one-function-call/2666-allow-one-function-call.ts b/2666-allow-one-function-call/2666-allow-one-function-call.ts new file mode 100644 index 00000000..d78e0aac --- /dev/null +++ b/2666-allow-one-function-call/2666-allow-one-function-call.ts @@ -0,0 +1,23 @@ +/** + * @param {Function} fn + * @return {Function} + */ +var once = function(fn) { + + var ok = false; + + return function(...args){ + if(ok) + return undefined; + ok = true; + return fn(...args); + } +}; + +/** + * let fn = (a,b,c) => (a + b + c) + * let onceFn = once(fn) + * + * onceFn(1,2,3); // 6 + * onceFn(2,3,6); // returns undefined without calling fn + */ From 84e70b62874402c907d93d0d85ca57deffcecef5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:36:38 +0530 Subject: [PATCH 0924/3167] Attach NOTES - LeetHub From 26ee08f2b40267cb96ccbebc152976f3592585c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 22:36:43 +0530 Subject: [PATCH 0925/3167] Time: 67 ms (28.88%), Space: 42.8 MB (62.03%) - LeetHub --- .../2666-allow-one-function-call.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/2666-allow-one-function-call/2666-allow-one-function-call.ts b/2666-allow-one-function-call/2666-allow-one-function-call.ts index d78e0aac..efeb5ee3 100644 --- a/2666-allow-one-function-call/2666-allow-one-function-call.ts +++ b/2666-allow-one-function-call/2666-allow-one-function-call.ts @@ -1,18 +1,14 @@ -/** - * @param {Function} fn - * @return {Function} - */ -var once = function(fn) { - - var ok = false; - - return function(...args){ - if(ok) +function once any>(fn: T): + ((...args: Parameters) => ReturnType | undefined) { + + var ok = false; + return function (...args) { + if(ok) return undefined; ok = true; return fn(...args); - } -}; + }; +} /** * let fn = (a,b,c) => (a + b + c) @@ -20,4 +16,4 @@ var once = function(fn) { * * onceFn(1,2,3); // 6 * onceFn(2,3,6); // returns undefined without calling fn - */ + */ \ No newline at end of file From 4d00091856200a369d0d2b1f9bc493468f3e3e5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 23:51:37 +0530 Subject: [PATCH 0926/3167] Create README - LeetHub --- 0509-fibonacci-number/README.md | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0509-fibonacci-number/README.md diff --git a/0509-fibonacci-number/README.md b/0509-fibonacci-number/README.md new file mode 100644 index 00000000..328aa572 --- /dev/null +++ b/0509-fibonacci-number/README.md @@ -0,0 +1,37 @@ +

509. Fibonacci Number

Easy


The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

+ +
F(0) = 0, F(1) = 1
+F(n) = F(n - 1) + F(n - 2), for n > 1.
+
+ +

Given n, calculate F(n).

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 1
+Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
+
+ +

Example 2:

+ +
Input: n = 3
+Output: 2
+Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
+
+ +

Example 3:

+ +
Input: n = 4
+Output: 3
+Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 30
  • +
+
\ No newline at end of file From 6cc926a40c65d6d05ff1adea7f90af4b6b44c851 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 23:51:38 +0530 Subject: [PATCH 0927/3167] Attach NOTES - LeetHub --- 0509-fibonacci-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0509-fibonacci-number/NOTES.md diff --git a/0509-fibonacci-number/NOTES.md b/0509-fibonacci-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0509-fibonacci-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5eb3a49638e004f87393bd546ed1615f1b117344 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 May 2023 23:51:41 +0530 Subject: [PATCH 0928/3167] Time: 4 ms (50.58%), Space: 6.3 MB (9.77%) - LeetHub --- .../0509-fibonacci-number.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0509-fibonacci-number/0509-fibonacci-number.cpp diff --git a/0509-fibonacci-number/0509-fibonacci-number.cpp b/0509-fibonacci-number/0509-fibonacci-number.cpp new file mode 100644 index 00000000..ed9538c8 --- /dev/null +++ b/0509-fibonacci-number/0509-fibonacci-number.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + + vector> expo(vector> mat, int n) + { + vector> ans = mat; + + if(n == 1) + return mat; + + + while( n > 0) + { + if(n & 1) + ans = mult(ans, mat); + + mat = mult(mat, mat); + + n >>= 1; + } + + return ans; + } + + vector> mult(vector>mat1, vector> mat2) + { + vector> ans(2, vector(2)); + + for(int i = 0; i < 2; ++i) + { + for(int j = 0; j < 2; ++j) + { + for(int k = 0; k < 2; ++k) + { + ans[i][j] += mat1[i][k] * mat2[k][j]; + } + } + } + + return ans; + + } + + int fib(int n) { + + if(n == 0) + return 0; + + vector> mat = {{1,1}, {1,0}}; + + vector> ans = expo(mat, n-1); + + return ans[0][1]; + } +}; \ No newline at end of file From 60babea6f28ebd1b38d847dca099f3074ff28825 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 May 2023 14:36:39 +0530 Subject: [PATCH 0929/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2466-count-ways-to-build-good-strings/README.md diff --git a/2466-count-ways-to-build-good-strings/README.md b/2466-count-ways-to-build-good-strings/README.md new file mode 100644 index 00000000..91b10e79 --- /dev/null +++ b/2466-count-ways-to-build-good-strings/README.md @@ -0,0 +1,39 @@ +

2466. Count Ways To Build Good Strings

Medium


Given the integers zero, one, low, and high, we can construct a string by starting with an empty string, and then at each step perform either of the following:

+ +
    +
  • Append the character '0' zero times.
  • +
  • Append the character '1' one times.
  • +
+ +

This can be performed any number of times.

+ +

A good string is a string constructed by the above process having a length between low and high (inclusive).

+ +

Return the number of different good strings that can be constructed satisfying these properties. Since the answer can be large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: low = 3, high = 3, zero = 1, one = 1
+Output: 8
+Explanation: 
+One possible valid good string is "011". 
+It can be constructed as follows: "" -> "0" -> "01" -> "011". 
+All binary strings from "000" to "111" are good strings in this example.
+
+ +

Example 2:

+ +
Input: low = 2, high = 3, zero = 1, one = 2
+Output: 5
+Explanation: The good strings are "00", "11", "000", "110", and "011".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= low <= high <= 105
  • +
  • 1 <= zero, one <= low
  • +
+
\ No newline at end of file From 0da021562ab57e6309db608ffd18274e095d256e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 May 2023 14:36:43 +0530 Subject: [PATCH 0930/3167] Time: 16 ms (66.48%), Space: 17.2 MB (30.66%) - LeetHub --- .../2466-count-ways-to-build-good-strings.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2466-count-ways-to-build-good-strings/2466-count-ways-to-build-good-strings.cpp diff --git a/2466-count-ways-to-build-good-strings/2466-count-ways-to-build-good-strings.cpp b/2466-count-ways-to-build-good-strings/2466-count-ways-to-build-good-strings.cpp new file mode 100644 index 00000000..09515186 --- /dev/null +++ b/2466-count-ways-to-build-good-strings/2466-count-ways-to-build-good-strings.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + int mod = 1e9 + 7; + + int helper(int i, int low, int high, int zero, int one, vector& dp) + { + if( i > high) + return 0; + + if(dp[i] != -1) + return dp[i]; + + int ans = 0; + + if( i >= low and i <= high) + ++ans; + + return dp[i] = (ans + helper(i + zero,low, high, zero , one, dp) + helper(i + one, low, high, zero, one, dp)) % mod; + } + + int countGoodStrings(int low, int high, int zero, int one) { + + vector dp(high+1, -1); + + return helper(0, low, high, zero, one, dp) % mod; + + } +}; + + + From 4ec9003fcc3361179685ed067bfc29fa1f1bf95e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 May 2023 14:50:33 +0530 Subject: [PATCH 0931/3167] Create README - LeetHub --- 2623-memoize/README.md | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 2623-memoize/README.md diff --git a/2623-memoize/README.md b/2623-memoize/README.md new file mode 100644 index 00000000..2f1ce95f --- /dev/null +++ b/2623-memoize/README.md @@ -0,0 +1,78 @@ +

2623. Memoize

Medium


Given a function fn, return a memoized version of that function.

+ +

memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.

+ +

You can assume there are possible input functions: sum, fiband factorial.

+ +
    +
  • sum accepts two integers a and b and returns a + b.
  • +
  • fib accepts a single integer n and returns 1 if n <= 1 or fib(n - 1) + fib(n - 2) otherwise.
  • +
  • factorial accepts a single integer n and returns 1 if n <= 1 or factorial(n - 1) * n otherwise.
  • +
+ +

 

+

Example 1:

+ +
Input
+"sum"
+["call","call","getCallCount","call","getCallCount"]
+[[2,2],[2,2],[],[1,2],[]]
+Output
+[4,4,1,3,2]
+
+Explanation
+const sum = (a, b) => a + b;
+const memoizedSum = memoize(sum);
+memoizedSum(2, 2); // Returns 4. sum() was called as (2, 2) was not seen before.
+memoizedSum(2, 2); // Returns 4. However sum() was not called because the same inputs were seen before.
+// Total call count: 1
+memoizedSum(1, 2); // Returns 3. sum() was called as (1, 2) was not seen before.
+// Total call count: 2
+
+ +

Example 2:

+ +
Input
+"factorial"
+["call","call","call","getCallCount","call","getCallCount"]
+[[2],[3],[2],[],[3],[]]
+Output
+[2,6,2,2,6,2]
+
+Explanation
+const factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));
+const memoFactorial = memoize(factorial);
+memoFactorial(2); // Returns 2.
+memoFactorial(3); // Returns 6.
+memoFactorial(2); // Returns 2. However factorial was not called because 2 was seen before.
+// Total call count: 2
+memoFactorial(3); // Returns 6. However factorial was not called because 3 was seen before.
+// Total call count: 2
+
+ +

Example 3:

+ +
Input
+"fib"
+["call","getCallCount"]
+[[5],[]]
+Output
+[8,1]
+
+Explanation
+fib(5) = 8
+// Total call count: 1
+
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= a, b <= 105
  • +
  • 1 <= n <= 10
  • +
  • at most 105 function calls
  • +
  • at most 105 attempts to access callCount
  • +
  • input function is sum, fib, or factorial
  • +
+
\ No newline at end of file From eb58abe07785572c746d9f92cb0401695fc967d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 May 2023 14:50:33 +0530 Subject: [PATCH 0932/3167] Attach NOTES - LeetHub --- 2623-memoize/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2623-memoize/NOTES.md diff --git a/2623-memoize/NOTES.md b/2623-memoize/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2623-memoize/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From efdbbb5bfaada61349a553fe2cb9a65eb82dad4e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 May 2023 14:50:36 +0530 Subject: [PATCH 0933/3167] Time: 464 ms (12.31%), Space: 103.8 MB (37.88%) - LeetHub --- 2623-memoize/2623-memoize.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2623-memoize/2623-memoize.js diff --git a/2623-memoize/2623-memoize.js b/2623-memoize/2623-memoize.js new file mode 100644 index 00000000..f35a333b --- /dev/null +++ b/2623-memoize/2623-memoize.js @@ -0,0 +1,23 @@ +/** + * @param {Function} fn + */ +function memoize(fn) { + const mem = {}; + return function(...args) { + if (mem[args] !== undefined) return mem[args]; + mem[args] = fn(...args); + return mem[args] + } +} + + +/** + * let callCount = 0; + * const memoizedFn = memoize(function (a, b) { + * callCount += 1; + * return a + b; + * }) + * memoizedFn(2, 3) // 5 + * memoizedFn(2, 3) // 5 + * console.log(callCount) // 1 + */ \ No newline at end of file From dbd1c36657ffed4cffc3e9011481f1f1ee452c30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 May 2023 22:06:44 +0530 Subject: [PATCH 0934/3167] Attach NOTES - LeetHub --- 2632-curry/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2632-curry/NOTES.md diff --git a/2632-curry/NOTES.md b/2632-curry/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2632-curry/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9dd13360c3da902886d3c44ca24de303ac96f6f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 May 2023 22:06:47 +0530 Subject: [PATCH 0935/3167] Time: 87 ms (6.28%), Space: 48.7 MB (33.26%) - LeetHub --- 2632-curry/2632-curry.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2632-curry/2632-curry.js diff --git a/2632-curry/2632-curry.js b/2632-curry/2632-curry.js new file mode 100644 index 00000000..3975dc73 --- /dev/null +++ b/2632-curry/2632-curry.js @@ -0,0 +1,21 @@ +/** + * @param {Function} fn + * @return {Function} + */ +var curry = function(fn) { + return function curried(...args) { + if (fn.length === args.length) { + return fn(...args); + } else { + return function(...newArgs) { + return curried(...args, ...newArgs) + } + } + }; +}; + +/** + * function sum(a, b) { return a + b; } + * const csum = curry(sum); + * csum(1)(2) // 3 + */ From 00930918e3a0486fa51b3f81858234f125405026 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 May 2023 22:10:54 +0530 Subject: [PATCH 0936/3167] Attach NOTES - LeetHub --- 1799-maximize-score-after-n-operations/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1799-maximize-score-after-n-operations/NOTES.md diff --git a/1799-maximize-score-after-n-operations/NOTES.md b/1799-maximize-score-after-n-operations/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1799-maximize-score-after-n-operations/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bf5ed9941f0c901a62a70c54dba5c76b06b627be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 May 2023 22:10:58 +0530 Subject: [PATCH 0937/3167] Time: 2634 ms (5.19%), Space: 30 MB (10.73%) - LeetHub --- ...1799-maximize-score-after-n-operations.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1799-maximize-score-after-n-operations/1799-maximize-score-after-n-operations.cpp diff --git a/1799-maximize-score-after-n-operations/1799-maximize-score-after-n-operations.cpp b/1799-maximize-score-after-n-operations/1799-maximize-score-after-n-operations.cpp new file mode 100644 index 00000000..b2adf2ca --- /dev/null +++ b/1799-maximize-score-after-n-operations/1799-maximize-score-after-n-operations.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int solve(vector&nums, unordered_map, int>&mp, vector&visited, int operation) + { + if (mp.count(visited)) return mp[visited]; //use stored result + + int maxScore = 0; + for (int i = 0; i < nums.size() - 1; i++) + { + if (visited[i]) continue; + for (int j = i + 1; j < nums.size(); j++) + { + if (visited[j]) continue; + visited[i] = true; + visited[j] = true; + + int currScore = operation * __gcd(nums[i], nums[j]); + int nextMaxScore = solve(nums, mp, visited, operation + 1); + int totalScore = currScore + nextMaxScore; + maxScore = max(maxScore, totalScore); + + visited[i] = false; + visited[j] = false; + } + } + return mp[visited] = maxScore; //store the result + } + int maxScore(vector& nums) + { + int n = nums.size(); + vectorvisited(n, false); + unordered_map, int>mp; + int ans = solve(nums, mp, visited, 1); + return ans; + + } +}; \ No newline at end of file From d36b9f72b971bba2121676c893b727f905508956 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 May 2023 08:56:10 +0530 Subject: [PATCH 0938/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1721-swapping-nodes-in-a-linked-list/README.md diff --git a/1721-swapping-nodes-in-a-linked-list/README.md b/1721-swapping-nodes-in-a-linked-list/README.md new file mode 100644 index 00000000..2d781629 --- /dev/null +++ b/1721-swapping-nodes-in-a-linked-list/README.md @@ -0,0 +1,26 @@ +

1721. Swapping Nodes in a Linked List

Medium


You are given the head of a linked list, and an integer k.

+ +

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
+Output: [7,9,6,6,8,7,3,0,9,5]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is n.
  • +
  • 1 <= k <= n <= 105
  • +
  • 0 <= Node.val <= 100
  • +
+
\ No newline at end of file From 3336ba12514c8dddb5b91c12d1e5b9a8367cc08f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 May 2023 08:56:10 +0530 Subject: [PATCH 0939/3167] Attach NOTES - LeetHub --- 1721-swapping-nodes-in-a-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1721-swapping-nodes-in-a-linked-list/NOTES.md diff --git a/1721-swapping-nodes-in-a-linked-list/NOTES.md b/1721-swapping-nodes-in-a-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1721-swapping-nodes-in-a-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0c55451731782195f1f9ae73ce03477c5c18d9f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 May 2023 08:59:37 +0530 Subject: [PATCH 0940/3167] Create README - LeetHub --- 2621-sleep/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2621-sleep/README.md diff --git a/2621-sleep/README.md b/2621-sleep/README.md new file mode 100644 index 00000000..3366c206 --- /dev/null +++ b/2621-sleep/README.md @@ -0,0 +1,28 @@ +

2621. Sleep

Easy


Given a positive integer millis, write an asyncronous function that sleeps for millis milliseconds. It can resolve any value.

+ +

 

+

Example 1:

+ +
Input: millis = 100
+Output: 100
+Explanation: It should return a promise that resolves after 100ms.
+let t = Date.now();
+sleep(100).then(() => {
+  console.log(Date.now() - t); // 100
+});
+
+ +

Example 2:

+ +
Input: millis = 200
+Output: 200
+Explanation: It should return a promise that resolves after 200ms.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= millis <= 1000
  • +
+
\ No newline at end of file From d72039e2ebde9dffb831de170da30bde71ca7619 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 May 2023 08:59:38 +0530 Subject: [PATCH 0941/3167] Attach NOTES - LeetHub --- 2621-sleep/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2621-sleep/NOTES.md diff --git a/2621-sleep/NOTES.md b/2621-sleep/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2621-sleep/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 28ff41d62e2ba9128e2329eb5835735d615b13b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 May 2023 08:59:41 +0530 Subject: [PATCH 0942/3167] Time: 65 ms (25.43%), Space: 42 MB (27.35%) - LeetHub --- 2621-sleep/2621-sleep.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 2621-sleep/2621-sleep.js diff --git a/2621-sleep/2621-sleep.js b/2621-sleep/2621-sleep.js new file mode 100644 index 00000000..03f0e2a7 --- /dev/null +++ b/2621-sleep/2621-sleep.js @@ -0,0 +1,11 @@ +/** + * @param {number} millis + */ +async function sleep(millis) { + await new Promise(resolve => setTimeout(resolve, millis)); +} + +/** + * let t = Date.now() + * sleep(100).then(() => console.log(Date.now() - t)) // 100 + */ \ No newline at end of file From 8269a89e38a6dbe025fc858a3c8374d0800557b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 May 2023 20:55:52 +0530 Subject: [PATCH 0943/3167] Create README - LeetHub --- 0126-word-ladder-ii/README.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0126-word-ladder-ii/README.md diff --git a/0126-word-ladder-ii/README.md b/0126-word-ladder-ii/README.md new file mode 100644 index 00000000..35e3785c --- /dev/null +++ b/0126-word-ladder-ii/README.md @@ -0,0 +1,41 @@ +

126. Word Ladder II

Hard


A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:

+ +
    +
  • Every adjacent pair of words differs by a single letter.
  • +
  • Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
  • +
  • sk == endWord
  • +
+ +

Given two words, beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s1, s2, ..., sk].

+ +

 

+

Example 1:

+ +
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
+Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
+Explanation: There are 2 shortest transformation sequences:
+"hit" -> "hot" -> "dot" -> "dog" -> "cog"
+"hit" -> "hot" -> "lot" -> "log" -> "cog"
+
+ +

Example 2:

+ +
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
+Output: []
+Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= beginWord.length <= 5
  • +
  • endWord.length == beginWord.length
  • +
  • 1 <= wordList.length <= 500
  • +
  • wordList[i].length == beginWord.length
  • +
  • beginWord, endWord, and wordList[i] consist of lowercase English letters.
  • +
  • beginWord != endWord
  • +
  • All the words in wordList are unique.
  • +
  • The sum of all shortest transformation sequences does not exceed 105.
  • +
+
\ No newline at end of file From bd2bf1d2d6e80a254a4c24051bae332a6fd034f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 May 2023 20:55:56 +0530 Subject: [PATCH 0944/3167] Time: 7 ms (99.78%), Space: 9 MB (88.22%) - LeetHub --- 0126-word-ladder-ii/0126-word-ladder-ii.cpp | 108 ++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 0126-word-ladder-ii/0126-word-ladder-ii.cpp diff --git a/0126-word-ladder-ii/0126-word-ladder-ii.cpp b/0126-word-ladder-ii/0126-word-ladder-ii.cpp new file mode 100644 index 00000000..22500a2c --- /dev/null +++ b/0126-word-ladder-ii/0126-word-ladder-ii.cpp @@ -0,0 +1,108 @@ +class Solution { + +private: + + void dfs(string word, vector& seq) + { + if(word == start) + { + reverse(seq.begin(), seq.end()); + + ans.push_back(seq); + + reverse(seq.begin(), seq.end()); + + return; + } + + int steps = mp[word]; + + for(int i = 0; i < word.size(); ++i) + { + char orignal = word[i]; + + for(char ch = 'a'; ch <= 'z'; ++ch) + { + word[i] = ch; + + if(mp.find(word) != mp.end() and mp[word] + 1 == steps) + { + seq.push_back(word); + + dfs(word, seq); + + seq.pop_back(); + } + } + + word[i] = orignal; + } + } + + +public: + + unordered_map mp; + vector> ans; + string start; + + vector> findLadders(string beginWord, string endWord, vector& wordList) { + + start = beginWord; + + unordered_set s(wordList.begin(), wordList.end()); + + queue q; + + q.push({beginWord}); + + mp[beginWord] = 1; + + s.erase(beginWord); + + while(!q.empty()) + { + string curr = q.front(); + + int steps = mp[curr]; + + q.pop(); + + if(curr == endWord) + break; + + for(int i = 0; i < curr.size(); ++i) + { + char orignal = curr[i]; + + for(char ch = 'a'; ch <= 'z'; ++ch) + { + curr[i] = ch; + + if(s.count(curr)) + { + q.push(curr); + + s.erase(curr); + + mp[curr] = steps + 1; + } + } + + curr[i] = orignal; + } + } + + if(mp.find(endWord) != mp.end()) + { + vector seq; + + seq.push_back(endWord); + + dfs(endWord, seq); + } + + return ans; + + } +}; \ No newline at end of file From ff901988b1ba6934a9154f53ced7ae8413a3bb53 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 May 2023 21:16:54 +0530 Subject: [PATCH 0945/3167] Create README - LeetHub --- 2637-promise-time-limit/README.md | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 2637-promise-time-limit/README.md diff --git a/2637-promise-time-limit/README.md b/2637-promise-time-limit/README.md new file mode 100644 index 00000000..bcd8a36d --- /dev/null +++ b/2637-promise-time-limit/README.md @@ -0,0 +1,68 @@ +

2637. Promise Time Limit

Easy


Given an asyncronous function fn and a time t in milliseconds, return a new time limited version of the input function.

+ +

time limited function is a function that is identical to the original unless it takes longer than t milliseconds to fullfill. In that case, it will reject with "Time Limit Exceeded".  Note that it should reject with a string, not an Error.

+ +

 

+

Example 1:

+ +
Input: 
+fn = async (n) => { 
+  await new Promise(res => setTimeout(res, 100)); 
+  return n * n; 
+}
+inputs = [5]
+t = 50
+Output: {"rejected":"Time Limit Exceeded","time":50}
+Explanation:
+The provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.
+
+ +

Example 2:

+ +
Input: 
+fn = async (n) => { 
+  await new Promise(res => setTimeout(res, 100)); 
+  return n * n; 
+}
+inputs = [5]
+t = 150
+Output: {"resolved":25,"time":100}
+Explanation:
+The function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.
+
+ +

Example 3:

+ +
Input: 
+fn = async (a, b) => { 
+  await new Promise(res => setTimeout(res, 120)); 
+  return a + b; 
+}
+inputs = [5,10]
+t = 150
+Output: {"resolved":15,"time":120}
+Explanation:
+The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.
+
+ +

Example 4:

+ +
Input: 
+fn = async () => { 
+  throw "Error";
+}
+inputs = []
+t = 1000
+Output: {"rejected":"Error","time":0}
+Explanation:
+The function immediately throws an error.
+ +

 

+

Constraints:

+ +
    +
  • 0 <= inputs.length <= 10
  • +
  • 0 <= t <= 1000
  • +
  • fn returns a promise
  • +
+
\ No newline at end of file From c7bcc3899ee4c66609e5e4f7965c38f466c1dabd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 May 2023 21:16:55 +0530 Subject: [PATCH 0946/3167] Attach NOTES - LeetHub --- 2637-promise-time-limit/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2637-promise-time-limit/NOTES.md diff --git a/2637-promise-time-limit/NOTES.md b/2637-promise-time-limit/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2637-promise-time-limit/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5ad9287262a9736ceef4392565f4acc1deb4a5b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 May 2023 21:16:58 +0530 Subject: [PATCH 0947/3167] Time: 53 ms (89.64%), Space: 41.8 MB (80.11%) - LeetHub --- .../2637-promise-time-limit.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2637-promise-time-limit/2637-promise-time-limit.js diff --git a/2637-promise-time-limit/2637-promise-time-limit.js b/2637-promise-time-limit/2637-promise-time-limit.js new file mode 100644 index 00000000..6301c08e --- /dev/null +++ b/2637-promise-time-limit/2637-promise-time-limit.js @@ -0,0 +1,23 @@ +/** + * @param {Function} fn + * @param {number} t + * @return {Function} + */ +var timeLimit = function(fn, t) { + return async function(...args) { + + return new Promise((resolve, reject) =>{ + const id = setTimeout(() => reject("Time Limit Exceeded"), t); + + fn(...args) + .then((res) => resolve(res)) + .catch((err) => reject(err)) + .finally(() => clearTimeout(id)); + }); + } +}; + +/** + * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100); + * limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms + */ \ No newline at end of file From 24d031d491d61e167ea46f15eba5a5bd42a7075d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 May 2023 22:49:35 +0530 Subject: [PATCH 0948/3167] Create README - LeetHub --- 2636-promise-pool/README.md | 74 +++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 2636-promise-pool/README.md diff --git a/2636-promise-pool/README.md b/2636-promise-pool/README.md new file mode 100644 index 00000000..69f5907c --- /dev/null +++ b/2636-promise-pool/README.md @@ -0,0 +1,74 @@ +

2636. Promise Pool

Medium


Given an array of asyncronous functions functions and a pool limit n, return an asyncronous function promisePool. It should return a promise that resolves when all the input functions resolve.

+ +

Pool limit is defined as the maximum number promises that can be pending at once. promisePool should begin execution of as many functions as possible and continue executing new functions when old promises resolve. promisePool should execute functions[i] then functions[i + 1] then functions[i + 2], etc. When the last promise resolves, promisePool should also resolve.

+ +

For example, if n = 1, promisePool will execute one function at a time in series. However, if n = 2, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.

+ +

You can assume all functions never reject. It is acceptable for promisePool to return a promise that resolves any value.

+ +

 

+

Example 1:

+ +
Input: 
+functions = [
+  () => new Promise(res => setTimeout(res, 300)),
+  () => new Promise(res => setTimeout(res, 400)),
+  () => new Promise(res => setTimeout(res, 200))
+]
+n = 2
+Output: [[300,400,500],500]
+Explanation:
+Three functions are passed in. They sleep for 300ms, 400ms, and 200ms respectively.
+They resolve at 300ms, 400ms, and 500ms respectively. The returned promise resolves at 500ms.
+At t=0, the first 2 functions are executed. The pool size limit of 2 is reached.
+At t=300, the 1st function resolves, and the 3rd function is executed. Pool size is 2.
+At t=400, the 2nd function resolves. There is nothing left to execute. Pool size is 1.
+At t=500, the 3rd function resolves. Pool size is zero so the returned promise also resolves.
+
+ +

Example 2:

+ +
Input:
+functions = [
+  () => new Promise(res => setTimeout(res, 300)),
+  () => new Promise(res => setTimeout(res, 400)),
+  () => new Promise(res => setTimeout(res, 200))
+]
+n = 5
+Output: [[300,400,200],400]
+Explanation:
+The three input promises resolve at 300ms, 400ms, and 200ms respectively.
+The returned promise resolves at 400ms.
+At t=0, all 3 functions are executed. The pool limit of 5 is never met.
+At t=200, the 3rd function resolves. Pool size is 2.
+At t=300, the 1st function resolved. Pool size is 1.
+At t=400, the 2nd function resolves. Pool size is 0, so the returned promise also resolves.
+
+ +

Example 3:

+ +
Input:
+functions = [
+  () => new Promise(res => setTimeout(res, 300)),
+  () => new Promise(res => setTimeout(res, 400)),
+  () => new Promise(res => setTimeout(res, 200))
+]
+n = 1
+Output: [[300,700,900],900]
+Explanation:
+The three input promises resolve at 300ms, 700ms, and 900ms respectively.
+The returned promise resolves at 900ms.
+At t=0, the 1st function is executed. Pool size is 1.
+At t=300, the 1st function resolves and the 2nd function is executed. Pool size is 1.
+At t=700, the 2nd function resolves and the 3rd function is executed. Pool size is 1.
+At t=900, the 3rd function resolves. Pool size is 0 so the returned promise resolves.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= functions.length <= 10
  • +
  • 1 <= n <= 10
  • +
+
\ No newline at end of file From dcf9d0f6b4c620c113740259c2b68600421aab4e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 00:11:12 +0530 Subject: [PATCH 0949/3167] Create README - LeetHub --- 2622-cache-with-time-limit/README.md | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2622-cache-with-time-limit/README.md diff --git a/2622-cache-with-time-limit/README.md b/2622-cache-with-time-limit/README.md new file mode 100644 index 00000000..c5a91d65 --- /dev/null +++ b/2622-cache-with-time-limit/README.md @@ -0,0 +1,55 @@ +

2622. Cache With Time Limit

Medium


Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.

+ +

The class has three public methods:

+ +

set(key, value, duration): accepts an integer key, an integer value, and a duration in milliseconds. Once the duration has elapsed, the key should be inaccessible. The method should return true if the same un-expired key already exists and false otherwise. Both the value and duration should be overwritten if the key already exists.

+ +

get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1.

+ +

count(): returns the count of un-expired keys.

+ +

 

+

Example 1:

+ +
Input: 
+["TimeLimitedCache", "set", "get", "count", "get"]
+[[], [1, 42, 100], [1], [], [1]]
+[0, 0, 50, 50, 150]
+Output: [null, false, 42, 1, -1]
+Explanation:
+At t=0, the cache is constructed.
+At t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.
+At t=50, key=1 is requested and the value of 42 is returned.
+At t=50, count() is called and there is one active key in the cache.
+At t=100, key=1 expires.
+At t=150, get(1) is called but -1 is returned because the cache is empty.
+
+ +

Example 2:

+ +
Input: 
+["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]
+[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]
+[0, 0, 40, 50, 120, 200, 250]
+Output: [null, false, true, 50, 50, -1]
+Explanation:
+At t=0, the cache is constructed.
+At t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.
+At t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.
+At t=50, get(1) is called which returned 50.
+At t=120, get(1) is called which returned 50.
+At t=140, key=1 expires.
+At t=200, get(1) is called but the cache is empty so -1 is returned.
+At t=250, count() returns 0 because the cache is empty.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= key <= 109
  • +
  • 0 <= value <= 109
  • +
  • 0 <= duration <= 1000
  • +
  • total method calls will not exceed 100
  • +
+
\ No newline at end of file From 576f8d863523d7c553cd8f277b2385f0dccd0e64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 00:11:12 +0530 Subject: [PATCH 0950/3167] Attach NOTES - LeetHub --- 2622-cache-with-time-limit/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2622-cache-with-time-limit/NOTES.md diff --git a/2622-cache-with-time-limit/NOTES.md b/2622-cache-with-time-limit/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2622-cache-with-time-limit/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 75ae873cd41538be10c3f4e2903401446b61c2fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 00:11:15 +0530 Subject: [PATCH 0951/3167] Time: 51 ms (94.32%), Space: 42.3 MB (16.88%) - LeetHub --- .../2622-cache-with-time-limit.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2622-cache-with-time-limit/2622-cache-with-time-limit.js diff --git a/2622-cache-with-time-limit/2622-cache-with-time-limit.js b/2622-cache-with-time-limit/2622-cache-with-time-limit.js new file mode 100644 index 00000000..75c890b8 --- /dev/null +++ b/2622-cache-with-time-limit/2622-cache-with-time-limit.js @@ -0,0 +1,30 @@ + +const TimeLimitedCache = function() { + this.cache = new Map(); // Using Map so we don't need a size variable +}; + +TimeLimitedCache.prototype.set = function(key, value, duration) { + let found = this.cache.has(key); + if (found) clearTimeout(this.cache.get(key).ref); // Cancel previous timeout + this.cache.set(key, { + value, // Equivalent to `value: value` + ref: setTimeout(() => this.cache.delete(key), duration) + }); + return found; +}; + +TimeLimitedCache.prototype.get = function(key) { + return this.cache.has(key) ? this.cache.get(key).value : -1; +}; + +TimeLimitedCache.prototype.count = function() { + return this.cache.size; +}; + +/** + * Your TimeLimitedCache object will be instantiated and called as such: + * var obj = new TimeLimitedCache() + * obj.set(1, 42, 1000); // false + * obj.get(1) // 42 + * obj.count() // 1 + */ \ No newline at end of file From 6716d6fa28c89da952699472bc66cde823f80b53 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 00:50:37 +0530 Subject: [PATCH 0952/3167] Create README - LeetHub --- 0684-redundant-connection/README.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0684-redundant-connection/README.md diff --git a/0684-redundant-connection/README.md b/0684-redundant-connection/README.md new file mode 100644 index 00000000..73428588 --- /dev/null +++ b/0684-redundant-connection/README.md @@ -0,0 +1,32 @@ +

684. Redundant Connection

Medium


In this problem, a tree is an undirected graph that is connected and has no cycles.

+ +

You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

+ +

Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • n == edges.length
  • +
  • 3 <= n <= 1000
  • +
  • edges[i].length == 2
  • +
  • 1 <= ai < bi <= edges.length
  • +
  • ai != bi
  • +
  • There are no repeated edges.
  • +
  • The given graph is connected.
  • +
+
\ No newline at end of file From ef517c1922d51a6ee0c680cc283b5e114a24e5b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 00:50:37 +0530 Subject: [PATCH 0953/3167] Attach NOTES - LeetHub --- 0684-redundant-connection/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0684-redundant-connection/NOTES.md diff --git a/0684-redundant-connection/NOTES.md b/0684-redundant-connection/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0684-redundant-connection/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9178e5adaffc393ae0a2816c3546ea2dbc7aa330 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 00:50:40 +0530 Subject: [PATCH 0954/3167] Time: 13 ms (27.11%), Space: 9 MB (54.35%) - LeetHub --- .../0684-redundant-connection.cpp | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 0684-redundant-connection/0684-redundant-connection.cpp diff --git a/0684-redundant-connection/0684-redundant-connection.cpp b/0684-redundant-connection/0684-redundant-connection.cpp new file mode 100644 index 00000000..f9c2bb42 --- /dev/null +++ b/0684-redundant-connection/0684-redundant-connection.cpp @@ -0,0 +1,104 @@ +class DSU +{ + private: + vector rank, parent, size; + + public: + DSU(int n) + { + rank.resize(n+1, 0); + parent.resize(n+1); + size.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); + } +}; + +class Solution { +public: + vector findRedundantConnection(vector>& edges) { + + int n = edges.size(); + + DSU dsu(n); + + vector ans; + + for(auto itr : edges) + { + int u = itr[0]; + int v = itr[1]; + + if(dsu.isSame(u,v)) + { + ans.clear(); + ans.push_back(u); + ans.push_back(v); + } + else + dsu.unionByRank(u,v); + } + + return ans; + + } +}; \ No newline at end of file From f1054dff1e0b0520f7cb22427a17b45554f11648 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 09:33:58 +0530 Subject: [PATCH 0955/3167] Create README - LeetHub --- 0721-accounts-merge/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0721-accounts-merge/README.md diff --git a/0721-accounts-merge/README.md b/0721-accounts-merge/README.md new file mode 100644 index 00000000..3304916c --- /dev/null +++ b/0721-accounts-merge/README.md @@ -0,0 +1,35 @@ +

721. Accounts Merge

Medium


Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.

+ +

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

+ +

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

+ +

 

+

Example 1:

+ +
Input: accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
+Output: [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
+Explanation:
+The first and second John's are the same person as they have the common email "johnsmith@mail.com".
+The third John and Mary are different people as none of their email addresses are used by other accounts.
+We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], 
+['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
+
+ +

Example 2:

+ +
Input: accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]]
+Output: [["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= accounts.length <= 1000
  • +
  • 2 <= accounts[i].length <= 10
  • +
  • 1 <= accounts[i][j].length <= 30
  • +
  • accounts[i][0] consists of English letters.
  • +
  • accounts[i][j] (for j > 0) is a valid email.
  • +
+
\ No newline at end of file From 3bb88ac485ad66fde12651b77e8b3ec6eecac651 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 09:34:01 +0530 Subject: [PATCH 0956/3167] Time: 92 ms (63.06%), Space: 34.3 MB (61.77%) - LeetHub --- 0721-accounts-merge/0721-accounts-merge.cpp | 136 ++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 0721-accounts-merge/0721-accounts-merge.cpp diff --git a/0721-accounts-merge/0721-accounts-merge.cpp b/0721-accounts-merge/0721-accounts-merge.cpp new file mode 100644 index 00000000..f9b6740f --- /dev/null +++ b/0721-accounts-merge/0721-accounts-merge.cpp @@ -0,0 +1,136 @@ +class DSU{ + + private: + + vector rank, parent, size; + + public: + + DSU(int n) + { + rank.resize(n+1, 0); + parent.resize(n+1); + size.resize(n+1, 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: + vector> accountsMerge(vector>& accounts) { + + int n = accounts.size(); + + unordered_map mp; + + DSU dsu(n); + + for(int i = 0; i < n; ++i) + { + for(int j = 1; j < accounts[i].size(); ++j) + { + string mail = accounts[i][j]; + + if(mp.find(mail) == mp.end()) + { + mp.insert({mail, i}); + } + else + { + dsu.unionBySize(i, mp[mail]); + } + } + } + + vector merge[n]; + + for(auto itr : mp) + { + string mail = itr.first; + int idx = dsu.findParent(itr.second); + + merge[idx].push_back(mail); + } + + + vector> ans; + + for(int i = 0; i < n; ++i) + { + vector temp; + + sort(merge[i].begin(),merge[i].end()); + + if(!merge[i].empty()) + { + temp.push_back(accounts[i][0]); + + for(auto itr : merge[i]) + temp.push_back(itr); + + ans.push_back(temp); + } + } + + return ans; + + } +}; \ No newline at end of file From 122601e473dceedc884176370305883729de6304 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 17:59:26 +0530 Subject: [PATCH 0957/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0947-most-stones-removed-with-same-row-or-column/README.md diff --git a/0947-most-stones-removed-with-same-row-or-column/README.md b/0947-most-stones-removed-with-same-row-or-column/README.md new file mode 100644 index 00000000..3c4401fb --- /dev/null +++ b/0947-most-stones-removed-with-same-row-or-column/README.md @@ -0,0 +1,47 @@ +

947. Most Stones Removed with Same Row or Column

Medium


On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.

+ +

A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.

+ +

Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.

+ +

 

+

Example 1:

+ +
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
+Output: 5
+Explanation: One way to remove 5 stones is as follows:
+1. Remove stone [2,2] because it shares the same row as [2,1].
+2. Remove stone [2,1] because it shares the same column as [0,1].
+3. Remove stone [1,2] because it shares the same row as [1,0].
+4. Remove stone [1,0] because it shares the same column as [0,0].
+5. Remove stone [0,1] because it shares the same row as [0,0].
+Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
+
+ +

Example 2:

+ +
Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
+Output: 3
+Explanation: One way to make 3 moves is as follows:
+1. Remove stone [2,2] because it shares the same row as [2,0].
+2. Remove stone [2,0] because it shares the same column as [0,0].
+3. Remove stone [0,2] because it shares the same row as [0,0].
+Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
+
+ +

Example 3:

+ +
Input: stones = [[0,0]]
+Output: 0
+Explanation: [0,0] is the only stone on the plane, so you cannot remove it.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= stones.length <= 1000
  • +
  • 0 <= xi, yi <= 104
  • +
  • No two stones are at the same coordinate point.
  • +
+
\ No newline at end of file From 6a6d36ac1ad08c7da699a5cd8e15060c68438414 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 17:59:30 +0530 Subject: [PATCH 0958/3167] Time: 56 ms (65.21%), Space: 21.4 MB (23.31%) - LeetHub --- ...stones-removed-with-same-row-or-column.cpp | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 0947-most-stones-removed-with-same-row-or-column/0947-most-stones-removed-with-same-row-or-column.cpp diff --git a/0947-most-stones-removed-with-same-row-or-column/0947-most-stones-removed-with-same-row-or-column.cpp b/0947-most-stones-removed-with-same-row-or-column/0947-most-stones-removed-with-same-row-or-column.cpp new file mode 100644 index 00000000..361361b5 --- /dev/null +++ b/0947-most-stones-removed-with-same-row-or-column/0947-most-stones-removed-with-same-row-or-column.cpp @@ -0,0 +1,111 @@ +class DSU{ + private: + vector rank, parent, size; + public: + DSU(int n) + { + rank.resize(n+1, 0); + parent.resize(n+1); + size.resize(n+1, 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: + int removeStones(vector>& stones) { + + int n = stones.size(); + + int maxRow = 0, maxCol = 0; + + for(auto itr : stones) + { + maxRow = max(maxRow, itr[0]); + maxCol = max(maxCol, itr[1]); + } + + DSU dsu(maxRow + maxCol + 1); + + unordered_map mp; + + for(auto itr : stones) + { + int row = itr[0]; + int col = itr[1] + maxRow + 1; + + dsu.unionBySize(row, col); + + mp[row] = 1; + mp[col] = 1; + } + + int cnt = 0; + + for(auto itr : mp) + { + if(itr.first == dsu.findParent(itr.first)) + ++cnt; + } + + return n - cnt; + + } +}; \ No newline at end of file From b3175410867f85827751f476a2534736c52922df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 18:03:58 +0530 Subject: [PATCH 0959/3167] Attach NOTES - LeetHub --- 0947-most-stones-removed-with-same-row-or-column/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0947-most-stones-removed-with-same-row-or-column/NOTES.md diff --git a/0947-most-stones-removed-with-same-row-or-column/NOTES.md b/0947-most-stones-removed-with-same-row-or-column/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0947-most-stones-removed-with-same-row-or-column/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fc57dcf1bd8af87c9d45b76a6717287731522f14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 18:04:01 +0530 Subject: [PATCH 0960/3167] Time: 56 ms (65.21%), Space: 21.4 MB (23.31%) - LeetHub From 4f58ac7b8edb40d6650d34bed3d212e5608a8d34 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 19:07:22 +0530 Subject: [PATCH 0961/3167] Attach NOTES - LeetHub --- 0785-is-graph-bipartite/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0785-is-graph-bipartite/NOTES.md diff --git a/0785-is-graph-bipartite/NOTES.md b/0785-is-graph-bipartite/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0785-is-graph-bipartite/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 11a832fb4fe7b16df7338bec3e0319a3d7c2fed9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 19:07:25 +0530 Subject: [PATCH 0962/3167] Time: 29 ms (38.04%), Space: 13.6 MB (61.58%) - LeetHub --- .../0785-is-graph-bipartite.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0785-is-graph-bipartite/0785-is-graph-bipartite.cpp diff --git a/0785-is-graph-bipartite/0785-is-graph-bipartite.cpp b/0785-is-graph-bipartite/0785-is-graph-bipartite.cpp new file mode 100644 index 00000000..4c41acad --- /dev/null +++ b/0785-is-graph-bipartite/0785-is-graph-bipartite.cpp @@ -0,0 +1,40 @@ +class Solution { +private: + bool check(int sv, int col, vector>& graph, vector& color, vector& visited) + { + color[sv] = col; + visited[sv] = true; + + for(auto itr : graph[sv]) + { + if(!visited[itr]) + { + if(!check(itr, col ^ 1, graph, color, visited)) + return false; + } + else if(color[sv] == color[itr]) + return false; + } + + return true; + } + +public: + bool isBipartite(vector>& graph) { + + int n = graph.size(); + + vector visited(n+1, false); + + vector color(n+1,-1); + + for(int i = 0; i < n; ++i) + { + if(!visited[i] and !check(i, 0, graph,color, visited)) + return false; + } + + return true; + + } +}; \ No newline at end of file From 3716d16513bbd018a7d543e33f8530ec42c691f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 22:13:01 +0530 Subject: [PATCH 0963/3167] Create README - LeetHub --- 2627-debounce/README.md | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 2627-debounce/README.md diff --git a/2627-debounce/README.md b/2627-debounce/README.md new file mode 100644 index 00000000..3d3a68c0 --- /dev/null +++ b/2627-debounce/README.md @@ -0,0 +1,75 @@ +

2627. Debounce

Medium


Given a function fn and a time in milliseconds t, return a debounced version of that function.

+ +

debounced function is a function whose execution is delayed by t milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also recieve the passed parameters.

+ +

For example, let's say t = 50ms, and the function was called at 30ms60ms, and 100ms. The first 2 function calls would be cancelled, and the 3rd function call would be executed at 150ms. If instead t = 35ms, The 1st call would be cancelled, the 2nd would be executed at 95ms, and the 3rd would be executed at 135ms.

+ +

Debounce Schematic

+ +

The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.

+ +

Please solve it without using lodash's _.debounce() function.

+ +

 

+

Example 1:

+ +
Input: 
+t = 50
+calls = [
+  {"t": 50, inputs: [1]},
+  {"t": 75, inputs: [2]}
+]
+Output: [{"t": 125, inputs: [2]}]
+Explanation:
+let start = Date.now();
+function log(...inputs) { 
+  console.log([Date.now() - start, inputs ])
+}
+const dlog = debounce(log, 50);
+setTimeout(() => dlog(1), 50);
+setTimeout(() => dlog(2), 75);
+
+The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
+The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
+
+ +

Example 2:

+ +
Input: 
+t = 20
+calls = [
+  {"t": 50, inputs: [1]},
+  {"t": 100, inputs: [2]}
+]
+Output: [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
+Explanation:
+The 1st call is delayed until 70ms. The inputs were (1).
+The 2nd call is delayed until 120ms. The inputs were (2).
+
+ +

Example 3:

+ +
Input: 
+t = 150
+calls = [
+  {"t": 50, inputs: [1, 2]},
+  {"t": 300, inputs: [3, 4]},
+  {"t": 300, inputs: [5, 6]}
+]
+Output: [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
+Explanation:
+The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
+The 2nd call is cancelled by the 3rd call
+The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= t <= 1000
  • +
  • 1 <= calls.length <= 10
  • +
  • 0 <= calls[i].t <= 1000
  • +
  • 0 <= calls[i].inputs.length <= 10
  • +
+
\ No newline at end of file From fb153fc9a80bf04a07d91cd39beff4720ab8966f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 May 2023 22:13:04 +0530 Subject: [PATCH 0964/3167] Time: 61 ms (55.97%), Space: 42.2 MB (32.10%) - LeetHub --- 2627-debounce/2627-debounce.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2627-debounce/2627-debounce.js diff --git a/2627-debounce/2627-debounce.js b/2627-debounce/2627-debounce.js new file mode 100644 index 00000000..6b7a70ba --- /dev/null +++ b/2627-debounce/2627-debounce.js @@ -0,0 +1,20 @@ +/** + * @param {Function} fn + * @param {number} t milliseconds + * @return {Function} + */ + +var debounce = function(fn, t) { + let timeout = null + return (...args) => { + clearTimeout(timeout) + timeout = setTimeout(fn, t, ...args) + } +}; + +/** + * const log = debounce(console.log, 100); + * log('Hello'); // cancelled + * log('Hello'); // cancelled + * log('Hello'); // Logged at t=100ms + */ \ No newline at end of file From a99607d6486f5ab1287a318abbbc7fb112574b70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 18:05:44 +0530 Subject: [PATCH 0965/3167] Attach NOTES - LeetHub From 5dba15e776356497a711adc68a517d66a16cf4cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 18:05:46 +0530 Subject: [PATCH 0966/3167] Time: 732 ms (19.50%), Space: 174 MB (52.54%) - LeetHub --- ...1192-critical-connections-in-a-network.cpp | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp b/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp index f438b177..dcbe31ca 100644 --- a/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp +++ b/1192-critical-connections-in-a-network/1192-critical-connections-in-a-network.cpp @@ -1,42 +1,40 @@ class Solution { - private: int timer = 0; -public: - void dfs(int sv, int par, vector adj[], vector& visited, vector& disc, vector& low, vector>& bridges) + + void dfs(int sv, int par, vector adj[], vector& visited, vector& disc, vector& low, vector>& bridges) { - low[sv] = disc[sv] = ++timer; - visited[sv] = 1; + visited[sv] = true; - for(auto child : adj[sv]) + disc[sv] = low[sv] = ++timer; + + for(auto itr : adj[sv]) { - if(child == par) + if(itr == par) continue; - if(!visited[child]) + + if(!visited[itr]) { - dfs(child, sv, adj, visited, disc, low, bridges); - low[sv] = min(low[sv],low[child]); + dfs(itr, sv, adj, visited, disc, low, bridges); - if(low[child] > disc[sv]) + low[sv] = min(low[sv], low[itr]); + + if(low[itr] > disc[sv]) { - bridges.push_back({sv, child}); + bridges.push_back({sv, itr}); } } else { - low[sv] = min(low[sv],low[child]); + low[sv] = min(low[sv], low[itr]); } } } +public: vector> criticalConnections(int n, vector>& connections) { - // no of bridges in a graph - - vector adj[n+1]; - vector visited(n,false); - vector disc(n,-1), low(n,-1); - vector > bridges; + vector adj[n]; for(auto itr : connections) { @@ -44,6 +42,12 @@ class Solution { adj[itr[1]].push_back(itr[0]); } + vector visited(n, false); + + vector disc(n, 0) , low(n, 0); + + vector> bridges; + for(int i = 0; i < n; ++i) { if(!visited[i]) From e0ccb2222fb14dc4a4f7f891ae13501bdbdf6411 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 22:06:22 +0530 Subject: [PATCH 0967/3167] Create README - LeetHub --- 0399-evaluate-division/README.md | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0399-evaluate-division/README.md diff --git a/0399-evaluate-division/README.md b/0399-evaluate-division/README.md new file mode 100644 index 00000000..1e00055c --- /dev/null +++ b/0399-evaluate-division/README.md @@ -0,0 +1,46 @@ +

399. Evaluate Division

Medium


You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

+ +

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.

+ +

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

+ +

Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.

+ +

 

+

Example 1:

+ +
Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
+Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
+Explanation: 
+Given: a / b = 2.0, b / c = 3.0
+queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
+return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
+
+ +

Example 2:

+ +
Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
+Output: [3.75000,0.40000,5.00000,0.20000]
+
+ +

Example 3:

+ +
Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
+Output: [0.50000,2.00000,-1.00000,-1.00000]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= equations.length <= 20
  • +
  • equations[i].length == 2
  • +
  • 1 <= Ai.length, Bi.length <= 5
  • +
  • values.length == equations.length
  • +
  • 0.0 < values[i] <= 20.0
  • +
  • 1 <= queries.length <= 20
  • +
  • queries[i].length == 2
  • +
  • 1 <= Cj.length, Dj.length <= 5
  • +
  • Ai, Bi, Cj, Dj consist of lower case English letters and digits.
  • +
+
\ No newline at end of file From b9177ceea704eb667a06333a281af35718dc48d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 22:06:25 +0530 Subject: [PATCH 0968/3167] Time: 3 ms (57.42%), Space: 8.3 MB (50.69%) - LeetHub --- .../0399-evaluate-division.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0399-evaluate-division/0399-evaluate-division.cpp diff --git a/0399-evaluate-division/0399-evaluate-division.cpp b/0399-evaluate-division/0399-evaluate-division.cpp new file mode 100644 index 00000000..e2187e6a --- /dev/null +++ b/0399-evaluate-division/0399-evaluate-division.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + unordered_map>> graph; + + double dfs(string src, string dst, unordered_set& visited) + { + if(graph.find(src) == graph.end()) + return -1; + + if(src == dst) + return 1; + + for(auto itr : graph[src]) + { + if(visited.count(itr.first)) + continue; + visited.insert(itr.first); + + double res = dfs(itr.first, dst, visited); + + if(res != -1) + return res * itr.second; + } + + return -1; + } + + vector calcEquation(vector>& equations, vector& values, vector>& queries) { + + int n = equations.size(); + + for(int i = 0; i < n; ++i) + { + string u = equations[i][0]; + string v = equations[i][1]; + + double val = values[i]; + + graph[u].push_back({v, val}); + graph[v].push_back({u, (double)1/val}); + } + + vector result; + + for(auto itr : queries) + { + unordered_set visited; + + result.push_back(dfs(itr[0], itr[1], visited)); + } + + return result; + + } +}; \ No newline at end of file From 852311bcdd45b36aef24710c25e225199a898ac7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 23:18:41 +0530 Subject: [PATCH 0969/3167] Create README - LeetHub --- 2676-throttle/README.md | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2676-throttle/README.md diff --git a/2676-throttle/README.md b/2676-throttle/README.md new file mode 100644 index 00000000..f38ccfca --- /dev/null +++ b/2676-throttle/README.md @@ -0,0 +1,46 @@ +

2676. Throttle

Medium


Given a function fn and a time in milliseconds t, return a throttled version of that function.

+ +

A throttled function is first called without delay and then, for a time interval of t milliseconds, can't be executed but should store the latest function arguments provided to call fn with them after the end of the delay.

+ +

For instance, t = 50ms, and the function was called at 30ms, 40ms, and 60ms. The first function call would block calling functions for the following t milliseconds. The second function call would save arguments, and the third call arguments should overwrite currently stored arguments from the second call because the second and third calls are called before 80ms. Once the delay has passed, the throttled function should be called with the latest arguments provided during the delay period, and it should also create another delay period of 80ms + t.

+ +

Throttle DiagramThe above diagram shows how throttle will transform events. Each rectangle represents 100ms and the throttle time is 400ms. Each color represents a different set of inputs.

+ +

 

+

Example 1:

+ +
Input: t = 100, calls = [{"t":20,"inputs":[1]}]
+Output: [{"t":20,"inputs":[1]}]
+Explanation: The 1st call is always called without delay
+
+ +

Example 2:

+ +
Input: t = 50, calls = [{"t":50,"inputs":[1]},{"t":75,"inputs":[2]}]
+Output: [{"t":50,"inputs":[1]},{"t":100,"inputs":[2]}]
+Explanation: 
+The 1st is called a function with arguments (1) without delay.
+The 2nd is called at 75ms, within the delay period because 50ms + 50ms = 100ms, so the next call can be reached at 100ms. Therefore, we save arguments from the 2nd call to use them at the callback of the 1st call.
+
+ +

Example 3:

+ +
Input: t = 70, calls = [{"t":50,"inputs":[1]},{"t":75,"inputs":[2]},{"t":90,"inputs":[8]},{"t": 140, "inputs":[5,7]},{"t": 300, "inputs": [9,4]}]
+Output: [{"t":50,"inputs":[1]},{"t":120,"inputs":[8]},{"t":190,"inputs":[5,7]},{"t":300,"inputs":[9,4]}]
+Explanation: 
+The 1st is called a function with arguments (1) without delay.
+The 2nd is called at 75ms within the delay period because 50ms + 70ms = 120ms, so it should only save arguments. 
+The 3rd is also called within the delay period, and because we need just the latest function arguments, we overwrite previous ones. After the delay period, we do a callback at 120ms with saved arguments. That callback makes another delay period of 120ms + 70ms = 190ms so that the next function can be called at 190ms.
+The 4th is called at 140ms in the delay period, so it should be called as a callback at 190ms. That will create another delay period of 190ms + 70ms = 260ms.
+The 5th is called at 300ms, but it is after 260ms, so it should be called immediately and should create another delay period of 300ms + 70ms = 370ms.
+ +

 

+

Constraints:

+ +
    +
  • 0 <= t <= 1000
  • +
  • 1 <= calls.length <= 10
  • +
  • 0 <= calls[i].t <= 1000
  • +
  • 0 <= calls[i].inputs[i], calls[i].inputs.length <= 10
  • +
+
\ No newline at end of file From bbd51ba7f55fe57be968a600e962dcdf2a547adb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 23:18:42 +0530 Subject: [PATCH 0970/3167] Attach NOTES - LeetHub --- 2676-throttle/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2676-throttle/NOTES.md diff --git a/2676-throttle/NOTES.md b/2676-throttle/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2676-throttle/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 04d7494207e2146817dee5fe734c82d6f153b131 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 May 2023 23:18:44 +0530 Subject: [PATCH 0971/3167] Time: 75 ms (5.94%), Space: 42.1 MB (47.85%) - LeetHub --- 2676-throttle/2676-throttle.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2676-throttle/2676-throttle.js diff --git a/2676-throttle/2676-throttle.js b/2676-throttle/2676-throttle.js new file mode 100644 index 00000000..85ea9e64 --- /dev/null +++ b/2676-throttle/2676-throttle.js @@ -0,0 +1,23 @@ +/** + * @param {Function} fn + * @param {number} t + * @return {Function} + */ +var throttle = function(fn, t) { + let id = null + let nextTime = 0 + return function(...args) { + const delay = Math.max(0, nextTime - Date.now()) + clearTimeout(id) + id = setTimeout(() => { + fn(...args) + nextTime = Date.now() + t; + }, delay) + } +}; + +/** + * const throttled = throttle(console.log, 100); + * throttled("log"); // logged immediately. + * throttled("log"); // logged at t=100ms. + */ \ No newline at end of file From c9fb36c3a1df0ac51d45fbe6a88d7d5d42e97c85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 13:39:45 +0530 Subject: [PATCH 0972/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2698-find-the-punishment-number-of-an-integer/README.md diff --git a/2698-find-the-punishment-number-of-an-integer/README.md b/2698-find-the-punishment-number-of-an-integer/README.md new file mode 100644 index 00000000..6f307670 --- /dev/null +++ b/2698-find-the-punishment-number-of-an-integer/README.md @@ -0,0 +1,40 @@ +

2698. Find the Punishment Number of an Integer

Medium


Given a positive integer n, return the punishment number of n.

+ +

The punishment number of n is defined as the sum of the squares of all integers i such that:

+ +
    +
  • 1 <= i <= n
  • +
  • The decimal representation of i * i can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals i.
  • +
+ +

 

+

Example 1:

+ +
Input: n = 10
+Output: 182
+Explanation: There are exactly 3 integers i that satisfy the conditions in the statement:
+- 1 since 1 * 1 = 1
+- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
+- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
+Hence, the punishment number of 10 is 1 + 81 + 100 = 182
+
+ +

Example 2:

+ +
Input: n = 37
+Output: 1478
+Explanation: There are exactly 4 integers i that satisfy the conditions in the statement:
+- 1 since 1 * 1 = 1. 
+- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1. 
+- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0. 
+- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
+Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file From 1d9faa012981dfc6d944272be6b750e811090cff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 13:39:46 +0530 Subject: [PATCH 0973/3167] Attach NOTES - LeetHub --- 2698-find-the-punishment-number-of-an-integer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2698-find-the-punishment-number-of-an-integer/NOTES.md diff --git a/2698-find-the-punishment-number-of-an-integer/NOTES.md b/2698-find-the-punishment-number-of-an-integer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2698-find-the-punishment-number-of-an-integer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6ff0326d48b2f87b6208bf1597b630a389a68182 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 13:39:49 +0530 Subject: [PATCH 0974/3167] Time: 454 ms (41.18%), Space: 6 MB (23.53%) - LeetHub --- ...nd-the-punishment-number-of-an-integer.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2698-find-the-punishment-number-of-an-integer/2698-find-the-punishment-number-of-an-integer.cpp diff --git a/2698-find-the-punishment-number-of-an-integer/2698-find-the-punishment-number-of-an-integer.cpp b/2698-find-the-punishment-number-of-an-integer/2698-find-the-punishment-number-of-an-integer.cpp new file mode 100644 index 00000000..6443b82f --- /dev/null +++ b/2698-find-the-punishment-number-of-an-integer/2698-find-the-punishment-number-of-an-integer.cpp @@ -0,0 +1,45 @@ +class Solution { + +private: + + bool possible(string str, int target) + { + if(str == "" and target == 0) + return true; + + if(target < 0) + return false; + + for(int i = 0; i < str.size(); ++i) + { + string left = str.substr(0, i+1); + string right = str.substr(i+1); + + int leftPart = stoi(left); + + if(possible(right, target - leftPart)) + return true; + } + + return false; + } + +public: + + int punishmentNumber(int n) { + + int ans = 0; + + for(int i = 1; i <=n; ++i) + { + int num = (i*i); + string str = to_string(num); + + if(possible(str , i )) + ans += num; + } + + return ans; + + } +}; \ No newline at end of file From aa2bb226db0900901c81c512ce6be7dfe1fe1203 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 13:42:51 +0530 Subject: [PATCH 0975/3167] Attach NOTES - LeetHub From 2db748c5e9cfe8d993538d14dfca411e52ee22af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 13:42:54 +0530 Subject: [PATCH 0976/3167] Time: 454 ms (41.18%), Space: 6 MB (23.53%) - LeetHub From 2a8327ccbea91b51af64adeb0d4d9c0dd59d366d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 18:56:10 +0530 Subject: [PATCH 0977/3167] Create README - LeetHub --- 0934-shortest-bridge/README.md | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0934-shortest-bridge/README.md diff --git a/0934-shortest-bridge/README.md b/0934-shortest-bridge/README.md new file mode 100644 index 00000000..91de5946 --- /dev/null +++ b/0934-shortest-bridge/README.md @@ -0,0 +1,37 @@ +

934. Shortest Bridge

Medium


You are given an n x n binary matrix grid where 1 represents land and 0 represents water.

+ +

An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.

+ +

You may change 0's to 1's to connect the two islands to form one island.

+ +

Return the smallest number of 0's you must flip to connect the two islands.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 2 <= n <= 100
  • +
  • grid[i][j] is either 0 or 1.
  • +
  • There are exactly two islands in grid.
  • +
+
\ No newline at end of file From 8143b9b1ed10bd705dadc3a52875999373d17e3e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 18:56:13 +0530 Subject: [PATCH 0978/3167] Time: 91 ms (24.77%), Space: 22.7 MB (30.28%) - LeetHub --- 0934-shortest-bridge/0934-shortest-bridge.cpp | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 0934-shortest-bridge/0934-shortest-bridge.cpp diff --git a/0934-shortest-bridge/0934-shortest-bridge.cpp b/0934-shortest-bridge/0934-shortest-bridge.cpp new file mode 100644 index 00000000..068f8b80 --- /dev/null +++ b/0934-shortest-bridge/0934-shortest-bridge.cpp @@ -0,0 +1,97 @@ +class Solution { + +private: + + vector dx = {-1, 0, 0, 1}; + vector dy = {0, -1, 1, 0}; + + void dfs(int x, int y, int n, int m, vector>& grid, vector>& visited, int track) + { + + visited[x][y] = track; + + for(int i = 0; i < 4; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and grid[newx][newy] == 1 and !visited[newx][newy]) + { + dfs(newx, newy, n, n, grid, visited, track); + } + } + } + +public: + int shortestBridge(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector> visited(n, vector(m, 0)); + + int track = 1; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1 and !visited[i][j]) + { + ++track; + dfs(i, j, n, m, grid, visited, track); + } + } + } + + queue, int>> q; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(visited[i][j] == 2) + q.push({{i,j}, 0}); + } + } + + int ans = 1e9; + + // for(auto itr : visited) + // { + // for(auto x : itr) + // cout<= 0 and newy >= 0 and newx < n and newy < m and visited[newx][newy] == 3) + { + // cout<= 0 and newy >= 0 and newx < n and newy < m and visited[newx][newy] == 0) + { + visited[newx][newy] = true; + q.push({{newx,newy}, step + 1}); + } + } + } + + return ans; + + } +}; \ No newline at end of file From 4711c2d2a1f478d805de3742a56760a2b41df7f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 18:56:56 +0530 Subject: [PATCH 0979/3167] Attach NOTES - LeetHub --- 0934-shortest-bridge/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0934-shortest-bridge/NOTES.md diff --git a/0934-shortest-bridge/NOTES.md b/0934-shortest-bridge/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0934-shortest-bridge/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d3f4dc5e9e558d84cdbe54c9db9257c8e31ed748 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 18:56:59 +0530 Subject: [PATCH 0980/3167] Time: 91 ms (24.77%), Space: 22.7 MB (30.28%) - LeetHub From 4ec830a75cb6bef744e32012d1349534630f35bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 22:00:29 +0530 Subject: [PATCH 0981/3167] Create README - LeetHub --- 2628-json-deep-equal/README.md | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2628-json-deep-equal/README.md diff --git a/2628-json-deep-equal/README.md b/2628-json-deep-equal/README.md new file mode 100644 index 00000000..10f6565c --- /dev/null +++ b/2628-json-deep-equal/README.md @@ -0,0 +1,45 @@ +

2628. JSON Deep Equal

Medium


Given two objects o1 and o2, check if they are deeply equal.

+ +

For two objects to be deeply equal, they must contain the same keys, and the associated values must also be deeply equal. Two objects are also considered deeply equal if they pass the === equality check.

+ +

You may assume both objects are the output of JSON.parse. In other words, they are valid JSON.

+ +

Please solve it without using lodash's _.isEqual() function.

+ +

 

+

Example 1:

+ +
Input: o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}
+Output: true
+Explanation: The keys and values match exactly.
+
+ +

Example 2:

+ +
Input: o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}
+Output: true
+Explanation: Although the keys are in a different order, they still match exactly.
+
+ +

Example 3:

+ +
Input: o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}
+Output: false
+Explanation: The array of numbers is different from the array of strings.
+
+ +

Example 4:

+ +
Input: o1 = true, o2 = false
+Output: false
+Explanation: true !== false
+ +

 

+

Constraints:

+ +
    +
  • 1 <= JSON.stringify(o1).length <= 105
  • +
  • 1 <= JSON.stringify(o2).length <= 105
  • +
  • maxNestingDepth <= 1000
  • +
+
\ No newline at end of file From 46a2b4b818da0bb3e239f5692e74421941cfb9de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 22:00:32 +0530 Subject: [PATCH 0982/3167] Time: 131 ms (9.27%), Space: 52.5 MB (8.92%) - LeetHub --- 2628-json-deep-equal/2628-json-deep-equal.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 2628-json-deep-equal/2628-json-deep-equal.js diff --git a/2628-json-deep-equal/2628-json-deep-equal.js b/2628-json-deep-equal/2628-json-deep-equal.js new file mode 100644 index 00000000..3c7cf269 --- /dev/null +++ b/2628-json-deep-equal/2628-json-deep-equal.js @@ -0,0 +1,10 @@ +/** + * @param {any} o1 + * @param {any} o2 + * @return {boolean} + */ +var areDeeplyEqual = function(o1, o2) { + + + return _.isEqual(o1,o2); +}; \ No newline at end of file From b7d1caef430421b6efea8e13eba4769019d829ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 22:02:13 +0530 Subject: [PATCH 0983/3167] Attach NOTES - LeetHub --- 2628-json-deep-equal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2628-json-deep-equal/NOTES.md diff --git a/2628-json-deep-equal/NOTES.md b/2628-json-deep-equal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2628-json-deep-equal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 291304b0ee73c2de2da2f2f7b5f0c1741ea9c0cb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 May 2023 22:02:16 +0530 Subject: [PATCH 0984/3167] Time: 131 ms (9.27%), Space: 52.5 MB (8.92%) - LeetHub From 0c8bc1ff69c4967d0a6232a059598a5e55611475 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 May 2023 18:19:25 +0530 Subject: [PATCH 0985/3167] Attach NOTES - LeetHub --- 1339-maximum-product-of-splitted-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1339-maximum-product-of-splitted-binary-tree/NOTES.md diff --git a/1339-maximum-product-of-splitted-binary-tree/NOTES.md b/1339-maximum-product-of-splitted-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1339-maximum-product-of-splitted-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 964e51c2cccb161e2e7d99c4e21e82e61c37f499 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 May 2023 18:19:28 +0530 Subject: [PATCH 0986/3167] Time: 155 ms (21.40%), Space: 77.3 MB (76.95%) - LeetHub --- ...aximum-product-of-splitted-binary-tree.cpp | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp b/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp index 6ae761bd..b65b0497 100644 --- a/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp +++ b/1339-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp @@ -10,20 +10,39 @@ * }; */ class Solution { -public: - int rec(TreeNode* root) { - if (!root) return 0; - int subtree = rec(root->left) + rec(root->right) + root->val; - res = max(res, (total - subtree) * subtree); - return subtree; + +private: + + long long total = 0 , ans = 0; + + int mod = 1e9 + 7; + + int helper(TreeNode* root) + { + if(!root) + return 0; + + int left = helper(root->left); + int right = helper(root->right); + + int subTree = left + right + root->val; + + ans = max(ans, ((total - subTree) * subTree)); + + return subTree; } + +public: int maxProduct(TreeNode* root) { - total = rec(root); - rec(root); - return res % int(pow(10,9)+7); + + total = helper(root); + + helper(root); + + return ans % mod;; + + + } - -private: - long res = 0, total = 0; }; \ No newline at end of file From 3ec0ba799d3a044458f37920805ef3b8e3980b2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 May 2023 22:07:29 +0530 Subject: [PATCH 0987/3167] Create README - LeetHub --- 2633-convert-object-to-json-string/README.md | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2633-convert-object-to-json-string/README.md diff --git a/2633-convert-object-to-json-string/README.md b/2633-convert-object-to-json-string/README.md new file mode 100644 index 00000000..59ecac38 --- /dev/null +++ b/2633-convert-object-to-json-string/README.md @@ -0,0 +1,46 @@ +

2633. Convert Object to JSON String

Medium


Given an object, return a valid JSON string of that object. You may assume the object only inludes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by Object.keys().

+ +

Please solve it without using the built-in JSON.stringify method.

+ +

 

+

Example 1:

+ +
Input: object = {"y":1,"x":2}
+Output: {"y":1,"x":2}
+Explanation: 
+Return the JSON representation.
+Note that the order of keys should be the same as the order returned by Object.keys().
+ +

Example 2:

+ +
Input: object = {"a":"str","b":-12,"c":true,"d":null}
+Output: {"a":"str","b":-12,"c":true,"d":null}
+Explanation:
+The primitives of JSON are strings, numbers, booleans, and null.
+
+ +

Example 3:

+ +
Input: object = {"key":{"a":1,"b":[{},null,"Hello"]}}
+Output: {"key":{"a":1,"b":[{},null,"Hello"]}}
+Explanation:
+Objects and arrays can include other objects and arrays.
+
+ +

Example 4:

+ +
Input: object = true
+Output: true
+Explanation:
+Primitive types are valid inputs.
+ +

 

+

Constraints:

+ +
    +
  • object includes strings, integers, booleans, arrays, objects, and null
  • +
  • 1 <= JSON.stringify(object).length <= 105
  • +
  • maxNestingLevel <= 1000
  • +
  • all strings will only contain alphanumeric characters
  • +
+
\ No newline at end of file From 02ad3ab9d2b0e61ddf8992b575a7772a964f2902 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 May 2023 22:08:07 +0530 Subject: [PATCH 0988/3167] Create README - LeetHub From 0dfe35803fa03c0aea1f788686c02c60ce1bd923 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 May 2023 22:08:07 +0530 Subject: [PATCH 0989/3167] Attach NOTES - LeetHub --- 2633-convert-object-to-json-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2633-convert-object-to-json-string/NOTES.md diff --git a/2633-convert-object-to-json-string/NOTES.md b/2633-convert-object-to-json-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2633-convert-object-to-json-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 129275372dcc1b15f27047e98564cc6b91686ed4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 May 2023 22:08:11 +0530 Subject: [PATCH 0990/3167] Time: 76 ms (62.27%), Space: 44.1 MB (88.26%) - LeetHub --- .../2633-convert-object-to-json-string.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 2633-convert-object-to-json-string/2633-convert-object-to-json-string.js diff --git a/2633-convert-object-to-json-string/2633-convert-object-to-json-string.js b/2633-convert-object-to-json-string/2633-convert-object-to-json-string.js new file mode 100644 index 00000000..439d8256 --- /dev/null +++ b/2633-convert-object-to-json-string/2633-convert-object-to-json-string.js @@ -0,0 +1,10 @@ +/** + * @param {any} object + * @return {string} + */ +var jsonStringify = function(object) { + + var ans = JSON.stringify(object); + + return ans; +}; \ No newline at end of file From d71575103fe005e8dba77f078de213261c14f651 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 18:00:55 +0530 Subject: [PATCH 0991/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0703-kth-largest-element-in-a-stream/README.md diff --git a/0703-kth-largest-element-in-a-stream/README.md b/0703-kth-largest-element-in-a-stream/README.md new file mode 100644 index 00000000..009d56ff --- /dev/null +++ b/0703-kth-largest-element-in-a-stream/README.md @@ -0,0 +1,39 @@ +

703. Kth Largest Element in a Stream

Easy


Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

+ +

Implement KthLargest class:

+ +
    +
  • KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
  • +
  • int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream.
  • +
+ +

 

+

Example 1:

+ +
Input
+["KthLargest", "add", "add", "add", "add", "add"]
+[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
+Output
+[null, 4, 5, 5, 8, 8]
+
+Explanation
+KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
+kthLargest.add(3);   // return 4
+kthLargest.add(5);   // return 5
+kthLargest.add(10);  // return 5
+kthLargest.add(9);   // return 8
+kthLargest.add(4);   // return 8
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= 104
  • +
  • 0 <= nums.length <= 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • -104 <= val <= 104
  • +
  • At most 104 calls will be made to add.
  • +
  • It is guaranteed that there will be at least k elements in the array when you search for the kth element.
  • +
+
\ No newline at end of file From fc8013849a6c81283b974a1a71c5b4c78587b0e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 18:00:55 +0530 Subject: [PATCH 0992/3167] Attach NOTES - LeetHub --- 0703-kth-largest-element-in-a-stream/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0703-kth-largest-element-in-a-stream/NOTES.md diff --git a/0703-kth-largest-element-in-a-stream/NOTES.md b/0703-kth-largest-element-in-a-stream/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0703-kth-largest-element-in-a-stream/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 24eb3b5ca157d024da1c3872881435ccc0c5b238 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 18:00:58 +0530 Subject: [PATCH 0993/3167] Time: 32 ms (82.89%), Space: 19.7 MB (86.52%) - LeetHub --- .../0703-kth-largest-element-in-a-stream.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0703-kth-largest-element-in-a-stream/0703-kth-largest-element-in-a-stream.cpp diff --git a/0703-kth-largest-element-in-a-stream/0703-kth-largest-element-in-a-stream.cpp b/0703-kth-largest-element-in-a-stream/0703-kth-largest-element-in-a-stream.cpp new file mode 100644 index 00000000..9b60ab35 --- /dev/null +++ b/0703-kth-largest-element-in-a-stream/0703-kth-largest-element-in-a-stream.cpp @@ -0,0 +1,37 @@ +class KthLargest { + +private: + priority_queue, greater> pq; + int size; + +public: + KthLargest(int k, vector& nums) { + + size = k; + + for(auto itr : nums) + { + pq.push(itr); + + if(pq.size() > k) + pq.pop(); + } + } + + int add(int val) { + + pq.push(val); + + if(pq.size() > size) + pq.pop(); + + return pq.top(); + + } +}; + +/** + * Your KthLargest object will be instantiated and called as such: + * KthLargest* obj = new KthLargest(k, nums); + * int param_1 = obj->add(val); + */ \ No newline at end of file From 75b9451c0d9599ef718f3543d5baf8d78834a88e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 18:01:03 +0530 Subject: [PATCH 0994/3167] Attach NOTES - LeetHub From ce59e92cbb7c9d03fe7a97bda94649f30e56ccca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 18:01:07 +0530 Subject: [PATCH 0995/3167] Time: 32 ms (82.89%), Space: 19.7 MB (86.52%) - LeetHub From 2d7572f5630d019a7c6cf74006c99c9be6e5ba59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 22:52:10 +0530 Subject: [PATCH 0996/3167] Create README - LeetHub --- 2675-array-of-objects-to-matrix/README.md | 121 ++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 2675-array-of-objects-to-matrix/README.md diff --git a/2675-array-of-objects-to-matrix/README.md b/2675-array-of-objects-to-matrix/README.md new file mode 100644 index 00000000..fc024862 --- /dev/null +++ b/2675-array-of-objects-to-matrix/README.md @@ -0,0 +1,121 @@ +

2675. Array of Objects to Matrix

Medium


Write a function that converts an array of objects arr into a matrix m.

+ +

arr is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and null values.

+ +

The first row m should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names are the respective paths in the object separated by ".".

+ +

Each of the remaining rows corresponds to an object in arr. Each value in the matrix corresponds to a value in an object. If a given object doesn't contain a value for a given column, the cell should contain an empty string "".

+ +

The colums in the matrix should be in lexographically ascending order.

+ +

 

+

Example 1:

+ +
Input: 
+arr = [
+  {"b": 1, "a": 2},
+  {"b": 3, "a": 4}
+]
+Output: 
+[
+  ["a", "b"],
+  [2, 1],
+  [4, 3]
+]
+
+Explanation:
+There are two unique column names in the two objects: "a" and "b".
+"a" corresponds with [2, 4].
+"b" coresponds with [1, 3].
+
+ +

Example 2:

+ +
Input: 
+arr = [
+  {"a": 1, "b": 2},
+  {"c": 3, "d": 4},
+  {}
+]
+Output: 
+[
+  ["a", "b", "c", "d"],
+  [1, 2, "", ""],
+  ["", "", 3, 4],
+  ["", "", "", ""]
+]
+
+Explanation:
+There are 4 unique column names: "a", "b", "c", "d".
+The first object has values associated with "a" and "b".
+The second object has values associated with "c" and "d".
+The third object has no keys, so it is just a row of empty strings.
+
+ +

Example 3:

+ +
Input: 
+arr = [
+  {"a": {"b": 1, "c": 2}},
+  {"a": {"b": 3, "d": 4}}
+]
+Output: 
+[
+  ["a.b", "a.c", "a.d"],
+  [1, 2, ""],
+  [3, "", 4]
+]
+
+Explanation:
+In this example, the objects are nested. The keys represent the full path to each value separated by periods.
+There are three paths: "a.b", "a.c", "a.d".
+
+ +

Example 4:

+ +
Input: 
+arr = [
+  [{"a": null}],
+  [{"b": true}],
+  [{"c": "x"}]
+]
+Output: 
+[
+  ["0.a", "0.b", "0.c"],
+  [null, "", ""],
+  ["", true, ""],
+  ["", "", "x"]
+]
+
+Explanation:
+Arrays are also considered objects with their keys being their indices.
+Each array has one element so the keys are "0.a", "0.b", and "0.c".
+
+ +

Example 5:

+ +
Input: 
+arr = [
+  {},
+  {},
+  {},
+]
+Output: 
+[
+  [],
+  [],
+  [],
+  []
+]
+
+Explanation:
+There are no keys so every row is an empty array.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • unique keys <= 1000
  • +
+
\ No newline at end of file From 1f8b7decc542d71a08bbd75a3fb5a5dc53925d39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 22:52:10 +0530 Subject: [PATCH 0997/3167] Attach NOTES - LeetHub --- 2675-array-of-objects-to-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2675-array-of-objects-to-matrix/NOTES.md diff --git a/2675-array-of-objects-to-matrix/NOTES.md b/2675-array-of-objects-to-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2675-array-of-objects-to-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 08aa1f453e9c60a787e0d1a63e85f51f0aa343be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 May 2023 22:52:14 +0530 Subject: [PATCH 0998/3167] Time: 8918 ms (5.32%), Space: 116 MB (31.56%) - LeetHub --- .../2675-array-of-objects-to-matrix.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2675-array-of-objects-to-matrix/2675-array-of-objects-to-matrix.js diff --git a/2675-array-of-objects-to-matrix/2675-array-of-objects-to-matrix.js b/2675-array-of-objects-to-matrix/2675-array-of-objects-to-matrix.js new file mode 100644 index 00000000..b8d5b21a --- /dev/null +++ b/2675-array-of-objects-to-matrix/2675-array-of-objects-to-matrix.js @@ -0,0 +1,14 @@ +/** + * @param {Array} arr + * @return {Matrix} + */ +const flatten = (a, prefix = []) => Object.entries(a) + .reduce((r, [k, v]) => (typeof v !== 'object' || v === null) + ? { ...r, [[...prefix, k].join('.')]: v } + : { ...r, ...flatten(v, [...prefix, k]) }, {}) + +const jsonToMatrix = (a) => { + a = a.map(i => flatten(i)) + const keys = Object.keys(a.reduce((r, i) => ({ ...r, ...i }), {})).sort() + return a.reduce((r, i) => [ ...r, keys.map(key => (key in i) ? i[key] : '')], [ keys ]) +} From 9e556ec726b28238d86fb8635519b75d346e4501 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 24 May 2023 23:09:10 +0530 Subject: [PATCH 0999/3167] Attach NOTES - LeetHub --- 2542-maximum-subsequence-score/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2542-maximum-subsequence-score/NOTES.md diff --git a/2542-maximum-subsequence-score/NOTES.md b/2542-maximum-subsequence-score/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2542-maximum-subsequence-score/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a47b4c2e81588578b7771e2e09f0ddbb9a1e3795 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 24 May 2023 23:09:13 +0530 Subject: [PATCH 1000/3167] Time: 320 ms (34.73%), Space: 92 MB (41.63%) - LeetHub --- .../2542-maximum-subsequence-score.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2542-maximum-subsequence-score/2542-maximum-subsequence-score.cpp diff --git a/2542-maximum-subsequence-score/2542-maximum-subsequence-score.cpp b/2542-maximum-subsequence-score/2542-maximum-subsequence-score.cpp new file mode 100644 index 00000000..ab2bc228 --- /dev/null +++ b/2542-maximum-subsequence-score/2542-maximum-subsequence-score.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + + + vector> v; + + for(int i=0;i,greater> pq; + + for(int i=0;i Date: Wed, 24 May 2023 23:12:26 +0530 Subject: [PATCH 1001/3167] Create README - LeetHub --- .../README.md | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 2700-differences-between-two-objects/README.md diff --git a/2700-differences-between-two-objects/README.md b/2700-differences-between-two-objects/README.md new file mode 100644 index 00000000..f2f72fb7 --- /dev/null +++ b/2700-differences-between-two-objects/README.md @@ -0,0 +1,113 @@ +

2700. Differences Between Two Objects

Medium


Write a function that accepts two deeply nested objects or arrays obj1 and obj2 and returns a new object representing their differences.

+ +

The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from obj1 to obj2. For each changed key, the value should be represented as an array [obj1 value, obj2 value]. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.

+ +

You may assume that both objects are the output of JSON.parse.

+ +

 

+

Example 1:

+ +
Input: 
+obj1 = {}
+obj2 = {
+  "a": 1, 
+  "b": 2
+}
+Output: {}
+Explanation: There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.
+
+ +

Example 2:

+ +
Input: 
+obj1 = {
+  "a": 1,
+  "v": 3,
+  "x": [],
+  "z": {
+    "a": null
+  }
+}
+obj2 = {
+  "a": 2,
+  "v": 4,
+  "x": [],
+  "z": {
+    "a": 2
+  }
+}
+Output: 
+{
+  "a": [1, 2],
+  "v": [3, 4],
+  "z": {
+    "a": [null, 2]
+  }
+}
+Explanation: The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.
+
+ +

Example 3:

+ +
Input: 
+obj1 = {
+  "a": 5, 
+  "v": 6, 
+  "z": [1, 2, 4, [2, 5, 7]]
+}
+obj2 = {
+  "a": 5, 
+  "v": 7, 
+  "z": [1, 2, 3, [1]]
+}
+Output: 
+{
+  "v": [6, 7],
+  "z": {
+    "2": [4, 3],
+    "3": {
+      "0": [2, 1]
+    }
+  }
+}
+Explanation: In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.
+
+ +

Example 4:

+ +
Input: 
+obj1 = {
+  "a": {"b": 1}, 
+}
+obj2 = {
+  "a": [5],
+}
+Output: 
+{
+  "a": [{"b": 1}, [5]]
+}
+Explanation: The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.
+ +

Example 5:

+ +
Input: 
+obj1 = {
+  "a": [1, 2, {}], 
+  "b": false
+}
+obj2 = {   
+  "b": false,
+  "a": [1, 2, {}]
+}
+Output: 
+{}
+Explanation: Apart from a different ordering of keys, the two objects are identical so an empty object is returned.
+ +

 

+

Constraints:

+ +
    +
  • 2 <= JSON.stringify(obj1).length <= 104
  • +
  • 2 <= JSON.stringify(obj2).length <= 104
  • +
+
\ No newline at end of file From 1dbb91b08a33a8916418df8c696cd7f7b2a28ebd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 May 2023 23:12:22 +0530 Subject: [PATCH 1002/3167] Create README - LeetHub --- 0837-new-21-game/README.md | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0837-new-21-game/README.md diff --git a/0837-new-21-game/README.md b/0837-new-21-game/README.md new file mode 100644 index 00000000..eae53092 --- /dev/null +++ b/0837-new-21-game/README.md @@ -0,0 +1,40 @@ +

837. New 21 Game

Medium


Alice plays the following game, loosely based on the card game "21".

+ +

Alice starts with 0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where maxPts is an integer. Each draw is independent and the outcomes have equal probabilities.

+ +

Alice stops drawing numbers when she gets k or more points.

+ +

Return the probability that Alice has n or fewer points.

+ +

Answers within 10-5 of the actual answer are considered accepted.

+ +

 

+

Example 1:

+ +
Input: n = 10, k = 1, maxPts = 10
+Output: 1.00000
+Explanation: Alice gets a single card, then stops.
+
+ +

Example 2:

+ +
Input: n = 6, k = 1, maxPts = 10
+Output: 0.60000
+Explanation: Alice gets a single card, then stops.
+In 6 out of 10 possibilities, she is at or below 6 points.
+
+ +

Example 3:

+ +
Input: n = 21, k = 17, maxPts = 10
+Output: 0.73278
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= k <= n <= 104
  • +
  • 1 <= maxPts <= 104
  • +
+
\ No newline at end of file From b500cea5f2f31dcfee8ab5860c592f07bb95fa04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 May 2023 23:14:15 +0530 Subject: [PATCH 1003/3167] Create README - LeetHub --- 2677-chunk-array/README.md | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2677-chunk-array/README.md diff --git a/2677-chunk-array/README.md b/2677-chunk-array/README.md new file mode 100644 index 00000000..3f945f0a --- /dev/null +++ b/2677-chunk-array/README.md @@ -0,0 +1,43 @@ +

2677. Chunk Array

Easy


Given an array arr and a chunk size size, return a chunked array. A chunked array contains the original elements in arr, but consists of subarrays each of length size. The length of the last subarray may be less than size if arr.length is not evenly divisible by size.

+ +

You may assume the array is the output of JSON.parse. In other words, it is valid JSON.

+ +

Please solve it without using lodash's _.chunk function.

+ +

 

+

Example 1:

+ +
Input: arr = [1,2,3,4,5], size = 1
+Output: [[1],[2],[3],[4],[5]]
+Explanation: The arr has been split into subarrays each with 1 element.
+
+ +

Example 2:

+ +
Input: arr = [1,9,6,3,2], size = 3
+Output: [[1,9,6],[3,2]]
+Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
+
+ +

Example 3:

+ +
Input: arr = [8,5,3,2,6], size = 6
+Output: [[8,5,3,2,6]]
+Explanation: Size is greater than arr.length thus all elements are in the first subarray.
+
+ +

Example 4:

+ +
Input: arr = [], size = 1
+Output: []
+Explanation: There are no elements to be chunked so an empty array is returned.
+ +

 

+

Constraints:

+ +
    +
  • arr is a valid JSON array
  • +
  • 2 <= JSON.stringify(arr).length <= 105
  • +
  • 1 <= size <= arr.length + 1
  • +
+
\ No newline at end of file From 058ec5b9ceb18999f62ab8da6cf62882f25a0672 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 May 2023 23:14:15 +0530 Subject: [PATCH 1004/3167] Attach NOTES - LeetHub --- 2677-chunk-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2677-chunk-array/NOTES.md diff --git a/2677-chunk-array/NOTES.md b/2677-chunk-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2677-chunk-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8433545d8e38d487e5404b6e8e45c16ee91e87da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 May 2023 23:14:19 +0530 Subject: [PATCH 1005/3167] Time: 63 ms (63.27%), Space: 44.6 MB (51.68%) - LeetHub --- 2677-chunk-array/2677-chunk-array.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 2677-chunk-array/2677-chunk-array.js diff --git a/2677-chunk-array/2677-chunk-array.js b/2677-chunk-array/2677-chunk-array.js new file mode 100644 index 00000000..589d37bf --- /dev/null +++ b/2677-chunk-array/2677-chunk-array.js @@ -0,0 +1,16 @@ +/** + * @param {Array} arr + * @param {number} size + * @return {Array[]} + */ +var chunk = function(arr, size) { + var ans = []; + var index = 0; + + while (index < arr.length) { + ans.push(arr.slice(index, index + size)); + index += size; + } + + return ans; +}; From cc8d2fedf0fee287854a29c10ac68aa2f9b2ef2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 20:11:40 +0530 Subject: [PATCH 1006/3167] Create README - LeetHub --- 1091-shortest-path-in-binary-matrix/README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1091-shortest-path-in-binary-matrix/README.md diff --git a/1091-shortest-path-in-binary-matrix/README.md b/1091-shortest-path-in-binary-matrix/README.md new file mode 100644 index 00000000..b59fa875 --- /dev/null +++ b/1091-shortest-path-in-binary-matrix/README.md @@ -0,0 +1,40 @@ +

1091. Shortest Path in Binary Matrix

Medium


Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1.

+ +

A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that:

+ +
    +
  • All the visited cells of the path are 0.
  • +
  • All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
  • +
+ +

The length of a clear path is the number of visited cells of this path.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • n == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= n <= 100
  • +
  • grid[i][j] is 0 or 1
  • +
+
\ No newline at end of file From b96fb97208513a73ae3540046fe2ffe2684109d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 20:11:44 +0530 Subject: [PATCH 1007/3167] Time: 116 ms (28.24%), Space: 17.5 MB (99.83%) - LeetHub --- .../1091-shortest-path-in-binary-matrix.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 1091-shortest-path-in-binary-matrix/1091-shortest-path-in-binary-matrix.cpp diff --git a/1091-shortest-path-in-binary-matrix/1091-shortest-path-in-binary-matrix.cpp b/1091-shortest-path-in-binary-matrix/1091-shortest-path-in-binary-matrix.cpp new file mode 100644 index 00000000..3125c601 --- /dev/null +++ b/1091-shortest-path-in-binary-matrix/1091-shortest-path-in-binary-matrix.cpp @@ -0,0 +1,57 @@ +class Solution { + +private: + bool isValid(int x, int y, int n, int m) + { + return (x >= 0 and y >= 0 and x < n and y < m); + } + +public: + int shortestPathBinaryMatrix(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + priority_queue>, vector>> ,greater>> > pq; + + if(grid[0][0] == 0) + { + pq.push({1,{0, 0}}); + } + else + return -1; + + vector dx = {-1, -1, -1, 0, 1, 1, 1, 0}; + vector dy = {-1, 0, +1, +1, +1, 0, -1, -1}; + + while(!pq.empty()) + { + int dist = pq.top().first; + + int x = pq.top().second.first; + int y = pq.top().second.second; + + pq.pop(); + + if(x == n-1 and y == m-1) + { + return dist; + } + + for(int i = 0; i < 8; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(isValid(newx, newy, n, m) and grid[newx][newy] == 0) + { + grid[newx][newy] = 1; + + pq.push({dist + 1, {newx, newy}}); + } + } + } + + return -1; + } +}; \ No newline at end of file From 1a58b48bfe62052e5d957553b53351c5940e281b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 23:13:09 +0530 Subject: [PATCH 1008/3167] Create README - LeetHub --- 1140-stone-game-ii/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1140-stone-game-ii/README.md diff --git a/1140-stone-game-ii/README.md b/1140-stone-game-ii/README.md new file mode 100644 index 00000000..7add6be1 --- /dev/null +++ b/1140-stone-game-ii/README.md @@ -0,0 +1,32 @@ +

1140. Stone Game II

Medium


Alice and Bob continue their games with piles of stones.  There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].  The objective of the game is to end with the most stones. 

+ +

Alice and Bob take turns, with Alice starting first.  Initially, M = 1.

+ +

On each player's turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M.  Then, we set M = max(M, X).

+ +

The game continues until all the stones have been taken.

+ +

Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.

+ +

 

+

Example 1:

+ +
Input: piles = [2,7,9,4,4]
+Output: 10
+Explanation:  If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get 2 + 4 + 4 = 10 piles in total. If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice get 2 + 7 = 9 piles in total. So we return 10 since it's larger. 
+
+ +

Example 2:

+ +
Input: piles = [1,2,3,4,5,100]
+Output: 104
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= piles.length <= 100
  • +
  • 1 <= piles[i] <= 104
  • +
+
\ No newline at end of file From 694fb5d59b05ac549690a5a6f8de1712a6dd3b4a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 23:13:12 +0530 Subject: [PATCH 1009/3167] Time: 21 ms (67.02%), Space: 8.3 MB (81.38%) - LeetHub --- 1140-stone-game-ii/1140-stone-game-ii.cpp | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1140-stone-game-ii/1140-stone-game-ii.cpp diff --git a/1140-stone-game-ii/1140-stone-game-ii.cpp b/1140-stone-game-ii/1140-stone-game-ii.cpp new file mode 100644 index 00000000..c2506848 --- /dev/null +++ b/1140-stone-game-ii/1140-stone-game-ii.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + + int dp[101][201]; + + int helper(int i,int m,vector& piles){ + + if(i >= piles.size()) + return 0; + + if(dp[i][m] != -1) + return dp[i][m]; + + int total = 0; + int ans = INT_MIN; + + for(int j=0;j<2*m;j++){ + + if(i+j < piles.size()) + total += piles[i+j]; + + ans = max(ans,total - helper(i+j+1,max(m,j+1),piles)); + } + + return dp[i][m] = ans; + } + int stoneGameII(vector& piles) { + + + memset(dp,-1,sizeof dp); + + int sum = 0; + for(auto x:piles) + sum += x; + + + int diff = helper(0,1,piles); + + return (sum+diff)/2; + } +}; \ No newline at end of file From 3aec35fd86ee453b086b4aede05e27cfbd6d4e16 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 23:16:31 +0530 Subject: [PATCH 1010/3167] Create README - LeetHub --- 2625-flatten-deeply-nested-array/README.md | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2625-flatten-deeply-nested-array/README.md diff --git a/2625-flatten-deeply-nested-array/README.md b/2625-flatten-deeply-nested-array/README.md new file mode 100644 index 00000000..ca03a77f --- /dev/null +++ b/2625-flatten-deeply-nested-array/README.md @@ -0,0 +1,53 @@ +

2625. Flatten Deeply Nested Array

Medium


Given a multi-dimensional array arr and a depth n, return a flattened version of that array.

+ +

A multi-dimensional array is a recursive data structure that contains integers or other multi-dimensional arrays.

+ +

flattened array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is less than n. The depth of the elements in the first array are considered to be 0.

+ +

Please solve it without the built-in Array.flat method.

+ +

 

+

Example 1:

+ +
Input
+arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
+n = 0
+Output
+[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
+
+Explanation
+Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. 
+ +

Example 2:

+ +
Input
+arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
+n = 1
+Output
+[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
+
+Explanation
+The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.
+ +

Example 3:

+ +
Input
+arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
+n = 2
+Output
+[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
+
+Explanation
+The maximum depth of any subarray is 1. Thus, all of them are flattened.
+ +

 

+

Constraints:

+ +
    +
  • 0 <= count of numbers in arr <= 105
  • +
  • 0 <= count of subarrays in arr <= 105
  • +
  • maxDepth <= 1000
  • +
  • -1000 <= each number <= 1000
  • +
  • 0 <= n <= 1000
  • +
+
\ No newline at end of file From a9fd0e77ab7d6591878ed2b7926a734a8a799f88 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 23:16:32 +0530 Subject: [PATCH 1011/3167] Attach NOTES - LeetHub --- 2625-flatten-deeply-nested-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2625-flatten-deeply-nested-array/NOTES.md diff --git a/2625-flatten-deeply-nested-array/NOTES.md b/2625-flatten-deeply-nested-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2625-flatten-deeply-nested-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1ef485f0264f11eb83d1a7b902c6a929a586a265 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 May 2023 23:16:35 +0530 Subject: [PATCH 1012/3167] Time: 150 ms (69.82%), Space: 117.2 MB (5.94%) - LeetHub --- .../2625-flatten-deeply-nested-array.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2625-flatten-deeply-nested-array/2625-flatten-deeply-nested-array.js diff --git a/2625-flatten-deeply-nested-array/2625-flatten-deeply-nested-array.js b/2625-flatten-deeply-nested-array/2625-flatten-deeply-nested-array.js new file mode 100644 index 00000000..3fad1af4 --- /dev/null +++ b/2625-flatten-deeply-nested-array/2625-flatten-deeply-nested-array.js @@ -0,0 +1,17 @@ +/** + * @param {any[]} arr + * @param {number} depth + * @return {any[]} + */ +var flat = function (arr, n) { + + if(n == 0 || arr.every((item) => !Array.isArray(item))) return arr; + const result = []; + for(let i=0;i Date: Sat, 27 May 2023 21:43:35 +0530 Subject: [PATCH 1013/3167] Attach NOTES - LeetHub --- 1406-stone-game-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1406-stone-game-iii/NOTES.md diff --git a/1406-stone-game-iii/NOTES.md b/1406-stone-game-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1406-stone-game-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a4826d0e4e83b7be2104b9a4989d072331e00efb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 May 2023 21:43:38 +0530 Subject: [PATCH 1014/3167] Time: 270 ms (55.92%), Space: 126.3 MB (97.87%) - LeetHub --- 1406-stone-game-iii/1406-stone-game-iii.cpp | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1406-stone-game-iii/1406-stone-game-iii.cpp diff --git a/1406-stone-game-iii/1406-stone-game-iii.cpp b/1406-stone-game-iii/1406-stone-game-iii.cpp new file mode 100644 index 00000000..23c3de22 --- /dev/null +++ b/1406-stone-game-iii/1406-stone-game-iii.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + string stoneGameIII(vector& stoneValue) { + int n = stoneValue.size(); + vector dp(3, 0); + + for (int i = n - 1; i >= 0; i--) { + int takeOne = stoneValue[i] - dp[(i + 1) % 3]; + + int takeTwo = INT_MIN; + if (i + 1 < n) takeTwo = stoneValue[i] + stoneValue[i + 1] - dp[(i + 2) % 3]; + + int takeThree = INT_MIN; + if (i + 2 < n) takeThree = stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] - dp[(i + 3) % 3]; + + dp[i % 3] = max({takeOne, takeTwo, takeThree}); + } + + int value = dp[0]; + if(value > 0) + return "Alice"; + else if(value < 0) + return "Bob"; + else + return "Tie"; + } +}; \ No newline at end of file From 2eb85bdf6027546bda601147f1c4b7337db74eb3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 May 2023 21:49:31 +0530 Subject: [PATCH 1015/3167] Attach NOTES - LeetHub --- 2619-array-prototype-last/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2619-array-prototype-last/NOTES.md diff --git a/2619-array-prototype-last/NOTES.md b/2619-array-prototype-last/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2619-array-prototype-last/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3ef0fe61d3d47e3a492d2b1cabe7ed4f1cf27dc4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 May 2023 21:49:35 +0530 Subject: [PATCH 1016/3167] Time: 54 ms (72.76%), Space: 42.3 MB (10.65%) - LeetHub --- .../2619-array-prototype-last.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 2619-array-prototype-last/2619-array-prototype-last.js diff --git a/2619-array-prototype-last/2619-array-prototype-last.js b/2619-array-prototype-last/2619-array-prototype-last.js new file mode 100644 index 00000000..4e556a1c --- /dev/null +++ b/2619-array-prototype-last/2619-array-prototype-last.js @@ -0,0 +1,12 @@ +Array.prototype.last = function() { + if (this.length === 0) { + return -1; + } else { + return this[this.length - 1]; + } +}; + +/** + * const arr = [1, 2, 3]; + * arr.last(); // 3 + */ \ No newline at end of file From a925962f07aec20258736d2df452230f64f2bae9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 May 2023 18:38:23 +0530 Subject: [PATCH 1017/3167] Attach NOTES - LeetHub --- 1976-number-of-ways-to-arrive-at-destination/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1976-number-of-ways-to-arrive-at-destination/NOTES.md diff --git a/1976-number-of-ways-to-arrive-at-destination/NOTES.md b/1976-number-of-ways-to-arrive-at-destination/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1976-number-of-ways-to-arrive-at-destination/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7734b70984e26b23005ed21724b6050b90d54e0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 May 2023 18:38:26 +0530 Subject: [PATCH 1018/3167] Time: 92 ms (63.00%), Space: 32.5 MB (79.35%) - LeetHub --- ...umber-of-ways-to-arrive-at-destination.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 1976-number-of-ways-to-arrive-at-destination/1976-number-of-ways-to-arrive-at-destination.cpp diff --git a/1976-number-of-ways-to-arrive-at-destination/1976-number-of-ways-to-arrive-at-destination.cpp b/1976-number-of-ways-to-arrive-at-destination/1976-number-of-ways-to-arrive-at-destination.cpp new file mode 100644 index 00000000..f8b06a11 --- /dev/null +++ b/1976-number-of-ways-to-arrive-at-destination/1976-number-of-ways-to-arrive-at-destination.cpp @@ -0,0 +1,58 @@ +class Solution { + +private: + int mod = 1e9 + 7; + +public: + int countPaths(int n, vector>& roads) { + + vector> adj[n+1]; + + for(auto itr : roads) + { + adj[itr[0]].push_back({itr[1], itr[2]}); + adj[itr[1]].push_back({itr[0], itr[2]}); + } + + vector dist(n, 1e18), ways(n, 0); + + priority_queue, vector> , greater> > pq; + + dist[0] = 0; + + ways[0] = 1; + + pq.push({dist[0], 0}); + + while(!pq.empty()) + { + long long dis = pq.top().first; + long long node = pq.top(). second; + + pq.pop(); + + for(auto itr : adj[node]) + { + long long adjNode = itr.first; + long long adjDis = itr.second; + + if((long long)adjDis + dis < dist[adjNode]) + { + dist[adjNode] = adjDis + dis; + + pq.push({dist[adjNode], adjNode}); + + ways[adjNode] = ways[node]; + } + else if(adjDis + dis == dist[adjNode]) + { + ways[adjNode] = (ways[adjNode] + ways[node]) % mod; + } + } + } + + if(ways[n-1] == 1e18) + return 0; + return ways[n-1] % mod; + } +}; \ No newline at end of file From 5486f349ab14b08463eb4c1c1b999bb197fedbf4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 May 2023 22:37:27 +0530 Subject: [PATCH 1019/3167] Create README - LeetHub --- 1547-minimum-cost-to-cut-a-stick/README.md | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1547-minimum-cost-to-cut-a-stick/README.md diff --git a/1547-minimum-cost-to-cut-a-stick/README.md b/1547-minimum-cost-to-cut-a-stick/README.md new file mode 100644 index 00000000..3cad0aef --- /dev/null +++ b/1547-minimum-cost-to-cut-a-stick/README.md @@ -0,0 +1,38 @@ +

1547. Minimum Cost to Cut a Stick

Hard


Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:

+ +

Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.

+ +

You should perform the cuts in order, you can change the order of the cuts as you wish.

+ +

The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.

+ +

Return the minimum total cost of the cuts.

+ +

 

+

Example 1:

+ +
Input: n = 7, cuts = [1,3,4,5]
+Output: 16
+Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
+
+The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20.
+Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).
+ +

Example 2:

+ +
Input: n = 9, cuts = [5,6,1,4,2]
+Output: 22
+Explanation: If you try the given cuts ordering the cost will be 25.
+There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 106
  • +
  • 1 <= cuts.length <= min(n - 1, 100)
  • +
  • 1 <= cuts[i] <= n - 1
  • +
  • All the integers in cuts array are distinct.
  • +
+
\ No newline at end of file From 8198125de9d0a296982b3a079c33ad0bbd68a5aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 May 2023 22:37:31 +0530 Subject: [PATCH 1020/3167] Time: 53 ms (89.37%), Space: 8.4 MB (95.97%) - LeetHub --- .../1547-minimum-cost-to-cut-a-stick.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1547-minimum-cost-to-cut-a-stick/1547-minimum-cost-to-cut-a-stick.cpp diff --git a/1547-minimum-cost-to-cut-a-stick/1547-minimum-cost-to-cut-a-stick.cpp b/1547-minimum-cost-to-cut-a-stick/1547-minimum-cost-to-cut-a-stick.cpp new file mode 100644 index 00000000..ec0fb908 --- /dev/null +++ b/1547-minimum-cost-to-cut-a-stick/1547-minimum-cost-to-cut-a-stick.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int dp[101][101]; + int solve(int start_stick, int end_stick, vector& cuts, int left, int right){ + if(left > right) return 0; + + if(dp[left][right] != -1) return dp[left][right]; + + int cost = 1e9; + + for(int i=left; i<=right; i++){ + int left_cost = solve(start_stick, cuts[i], cuts, left, i-1); + int right_cost = solve(cuts[i], end_stick, cuts, i+1, right); + int curr_cost = (end_stick - start_stick) + left_cost + right_cost; + cost = min(cost,curr_cost); + } + + return dp[left][right] = cost; + } + int minCost(int n, vector& cuts) { + memset(dp,-1,sizeof(dp)); + sort(cuts.begin(),cuts.end()); + return solve(0, n, cuts, 0, cuts.size()-1); + } +}; \ No newline at end of file From 20e6a2fc82713aaee86c079fbca02538d8bd22a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 May 2023 22:38:19 +0530 Subject: [PATCH 1021/3167] Create README - LeetHub --- 2631-group-by/README.md | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 2631-group-by/README.md diff --git a/2631-group-by/README.md b/2631-group-by/README.md new file mode 100644 index 00000000..04f5d874 --- /dev/null +++ b/2631-group-by/README.md @@ -0,0 +1,81 @@ +

2631. Group By

Medium


Write code that enhances all arrays such that you can call the array.groupBy(fn) method on any array and it will return a grouped version of the array.

+ +

grouped array is an object where each key is the output of fn(arr[i]) and each value is an array containing all items in the original array with that key.

+ +

The provided callback fn will accept an item in the array and return a string key.

+ +

The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.

+ +

Please solve it without lodash's _.groupBy function.

+ +

 

+

Example 1:

+ +
Input: 
+array = [
+  {"id":"1"},
+  {"id":"1"},
+  {"id":"2"}
+], 
+fn = function (item) { 
+  return item.id; 
+}
+Output: 
+{ 
+  "1": [{"id": "1"}, {"id": "1"}],   
+  "2": [{"id": "2"}] 
+}
+Explanation:
+Output is from array.groupBy(fn).
+The selector function gets the "id" out of each item in the array.
+There are two objects with an "id" of 1. Both of those objects are put in the first array.
+There is one object with an "id" of 2. That object is put in the second array.
+
+ +

Example 2:

+ +
Input: 
+array = [
+  [1, 2, 3],
+  [1, 3, 5],
+  [1, 5, 9]
+]
+fn = function (list) { 
+  return String(list[0]); 
+}
+Output: 
+{ 
+  "1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]] 
+}
+Explanation:
+The array can be of any type. In this case, the selector function defines the key as being the first element in the array. 
+All the arrays have 1 as their first element so they are grouped together.
+{
+  "1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
+}
+
+ +

Example 3:

+ +
Input: 
+array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+fn = function (n) { 
+  return String(n > 5);
+}
+Output:
+{
+  "true": [6, 7, 8, 9, 10],
+  "false": [1, 2, 3, 4, 5]
+}
+Explanation:
+The selector function splits the array by whether each number is greater than 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= array.length <= 105
  • +
  • fn returns a string
  • +
+
\ No newline at end of file From d04a73842d72aa9ca34cde1e6c73690131cd4ebb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 May 2023 22:38:19 +0530 Subject: [PATCH 1022/3167] Attach NOTES - LeetHub --- 2631-group-by/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2631-group-by/NOTES.md diff --git a/2631-group-by/NOTES.md b/2631-group-by/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2631-group-by/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5a11ad390b5143ffdcedf9eb44ff4a7573783f37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 18:33:52 +0530 Subject: [PATCH 1023/3167] Create README - LeetHub --- 1603-design-parking-system/README.md | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1603-design-parking-system/README.md diff --git a/1603-design-parking-system/README.md b/1603-design-parking-system/README.md new file mode 100644 index 00000000..3d8e5446 --- /dev/null +++ b/1603-design-parking-system/README.md @@ -0,0 +1,35 @@ +

1603. Design Parking System

Easy


Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

+ +

Implement the ParkingSystem class:

+ +
    +
  • ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
  • +
  • bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.
  • +
+ +

 

+

Example 1:

+ +
Input
+["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
+[[1, 1, 0], [1], [2], [3], [1]]
+Output
+[null, true, true, false, false]
+
+Explanation
+ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
+parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
+parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
+parkingSystem.addCar(3); // return false because there is no available slot for a small car
+parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= big, medium, small <= 1000
  • +
  • carType is 1, 2, or 3
  • +
  • At most 1000 calls will be made to addCar
  • +
+
\ No newline at end of file From 3a607ae115ca182269dac78d0f22fc8c76f7485e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 18:33:56 +0530 Subject: [PATCH 1024/3167] Time: 69 ms (29.49%), Space: 33.2 MB (28.39%) - LeetHub --- .../1603-design-parking-system.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1603-design-parking-system/1603-design-parking-system.cpp diff --git a/1603-design-parking-system/1603-design-parking-system.cpp b/1603-design-parking-system/1603-design-parking-system.cpp new file mode 100644 index 00000000..0815e1a0 --- /dev/null +++ b/1603-design-parking-system/1603-design-parking-system.cpp @@ -0,0 +1,31 @@ +class ParkingSystem { +public: + + unordered_map mp; + + ParkingSystem(int big, int medium, int small) { + + mp.insert({1, big}); + mp.insert({2, medium}); + mp.insert({3, small}); + + } + + bool addCar(int carType) { + + if(mp[carType] > 0) + { + --mp[carType]; + return true; + } + + return false; + + } +}; + +/** + * Your ParkingSystem object will be instantiated and called as such: + * ParkingSystem* obj = new ParkingSystem(big, medium, small); + * bool param_1 = obj->addCar(carType); + */ \ No newline at end of file From 399468747c1b605be1357c64a7b24c388ba561bb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 18:48:14 +0530 Subject: [PATCH 1025/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2618-check-if-object-instance-of-class/README.md diff --git a/2618-check-if-object-instance-of-class/README.md b/2618-check-if-object-instance-of-class/README.md new file mode 100644 index 00000000..a32bda5c --- /dev/null +++ b/2618-check-if-object-instance-of-class/README.md @@ -0,0 +1,37 @@ +

2618. Check if Object Instance of Class

Medium


Write a function that checks if a given value is an instance of a given class or superclass. For this problem, an object is considered an instance of a given class if that object has access to that class's methods.

+ +

There are no constraints on the data types that can be passed to the function. For example, the value or the class could be undefined.

+ +

 

+

Example 1:

+ +
Input: func = () => checkIfInstanceOf(new Date(), Date)
+Output: true
+Explanation: The object returned by the Date constructor is, by definition, an instance of Date.
+
+ +

Example 2:

+ +
Input: func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
+Output: true
+Explanation:
+class Animal {};
+class Dog extends Animal {};
+checkIfInstance(new Dog(), Animal); // true
+
+Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.
+ +

Example 3:

+ +
Input: func = () => checkIfInstanceOf(Date, Date)
+Output: false
+Explanation: A date constructor cannot logically be an instance of itself.
+
+ +

Example 4:

+ +
Input: func = () => checkIfInstanceOf(5, Number)
+Output: true
+Explanation: 5 is a Number. Note that the "instanceof" keyword would return false. However, it is still considered an instance of Number because it accesses the Number methods. For example "toFixed()".
+
+
\ No newline at end of file From 9656ac6d0a6070af9ee9f93690078f2d49ff1ebe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 18:48:14 +0530 Subject: [PATCH 1026/3167] Attach NOTES - LeetHub --- 2618-check-if-object-instance-of-class/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2618-check-if-object-instance-of-class/NOTES.md diff --git a/2618-check-if-object-instance-of-class/NOTES.md b/2618-check-if-object-instance-of-class/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2618-check-if-object-instance-of-class/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7ebbeba2f26207f5752de765e289af3d13bd0e07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 18:48:17 +0530 Subject: [PATCH 1027/3167] Time: 113 ms (36.14%), Space: 51.8 MB (74.81%) - LeetHub --- .../2618-check-if-object-instance-of-class.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2618-check-if-object-instance-of-class/2618-check-if-object-instance-of-class.js diff --git a/2618-check-if-object-instance-of-class/2618-check-if-object-instance-of-class.js b/2618-check-if-object-instance-of-class/2618-check-if-object-instance-of-class.js new file mode 100644 index 00000000..8969213f --- /dev/null +++ b/2618-check-if-object-instance-of-class/2618-check-if-object-instance-of-class.js @@ -0,0 +1,28 @@ +/** + * @param {any} obj + * @param {any} classFunction + * @return {boolean} + */ +var checkIfInstanceOf = function(obj, classFunction) { + + if(obj == null || obj == undefined || typeof classFunction !== 'function') + { + return false; + } + + let currPrototype = Object.getPrototypeOf(obj); + + while(currPrototype !== null) + { + if(currPrototype === classFunction.prototype) + { + return true; + } + currPrototype = Object.getPrototypeOf(currPrototype); + } + return false; +}; + +/** + * checkIfInstanceOf(new Date(), Date); // true + */ \ No newline at end of file From 6c1d3076c0978758e6ccf67a18b34b8556761fc6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 22:35:55 +0530 Subject: [PATCH 1028/3167] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md diff --git a/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md new file mode 100644 index 00000000..b1f25dce --- /dev/null +++ b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md @@ -0,0 +1,46 @@ +

1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance

Medium


There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.

+ +

Return the city with the smallest number of cities that are reachable through some path and whose distance is at most distanceThreshold, If there are multiple such cities, return the city with the greatest number.

+ +

Notice that the distance of a path connecting cities i and j is equal to the sum of the edges' weights along that path.

+ +

 

+

Example 1:

+ +
Input: n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
+Output: 3
+Explanation: The figure above describes the graph. 
+The neighboring cities at a distanceThreshold = 4 for each city are:
+City 0 -> [City 1, City 2] 
+City 1 -> [City 0, City 2, City 3] 
+City 2 -> [City 0, City 1, City 3] 
+City 3 -> [City 1, City 2] 
+Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.
+
+ +

Example 2:

+ +
Input: n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
+Output: 0
+Explanation: The figure above describes the graph. 
+The neighboring cities at a distanceThreshold = 2 for each city are:
+City 0 -> [City 1] 
+City 1 -> [City 0, City 4] 
+City 2 -> [City 3, City 4] 
+City 3 -> [City 2, City 4]
+City 4 -> [City 1, City 2, City 3] 
+The city 0 has 1 neighboring city at a distanceThreshold = 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 100
  • +
  • 1 <= edges.length <= n * (n - 1) / 2
  • +
  • edges[i].length == 3
  • +
  • 0 <= fromi < toi < n
  • +
  • 1 <= weighti, distanceThreshold <= 10^4
  • +
  • All pairs (fromi, toi) are distinct.
  • +
+
\ No newline at end of file From 1f88e0b595f79151b8cce2087b3f0174532f331e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 May 2023 22:35:58 +0530 Subject: [PATCH 1029/3167] Time: 105 ms (53.90%), Space: 12.7 MB (28.01%) - LeetHub --- ...r-of-neighbors-at-a-threshold-distance.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.cpp diff --git a/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.cpp b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.cpp new file mode 100644 index 00000000..871c7262 --- /dev/null +++ b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.cpp @@ -0,0 +1,65 @@ +class Solution { +public: + int findTheCity(int n, vector>& edges, int distanceThreshold) { + + vector> mat(n, vector(n, 1e9)); + + for(auto itr : edges) + { + int i = itr[0]; + int j = itr[1]; + int wt = itr[2]; + + mat[i][j] = wt; + mat[j][i] = wt; + } + + for(int k = 0; k < n; ++k) + { + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(i == j) + mat[i][j] = 0; + + mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]); + } + } + } + + // for(auto itr : mat) + // { + // for(auto x : itr) + // cout<> vp; + + for(int i = 0; i < n; ++i) + { + int cnt = 0; + for(int j = 0; j < n; ++j) + { + if(mat[i][j] <= distanceThreshold) + { + ++cnt; + } + } + vp.push_back({cnt, i}); + } + + sort(vp.begin(), vp.end(),[&](const auto &a, const auto &b){ + if(a.first == b.first) + return a.second > b.second; + return a.first < b.first; + }); + + // for(auto itr : vp) + // cout< Date: Tue, 30 May 2023 08:07:31 +0530 Subject: [PATCH 1030/3167] Create README - LeetHub --- 0705-design-hashset/README.md | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0705-design-hashset/README.md diff --git a/0705-design-hashset/README.md b/0705-design-hashset/README.md new file mode 100644 index 00000000..4c189349 --- /dev/null +++ b/0705-design-hashset/README.md @@ -0,0 +1,38 @@ +

705. Design HashSet

Easy


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

+ +

Implement MyHashSet class:

+ +
    +
  • void add(key) Inserts the value key into the HashSet.
  • +
  • bool contains(key) Returns whether the value key exists in the HashSet or not.
  • +
  • void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
+[[], [1], [2], [1], [3], [2], [2], [2], [2]]
+Output
+[null, null, null, true, false, null, true, null, false]
+
+Explanation
+MyHashSet myHashSet = new MyHashSet();
+myHashSet.add(1);      // set = [1]
+myHashSet.add(2);      // set = [1, 2]
+myHashSet.contains(1); // return True
+myHashSet.contains(3); // return False, (not found)
+myHashSet.add(2);      // set = [1, 2]
+myHashSet.contains(2); // return True
+myHashSet.remove(2);   // set = [1]
+myHashSet.contains(2); // return False, (already removed)
+ +

 

+

Constraints:

+ +
    +
  • 0 <= key <= 106
  • +
  • At most 104 calls will be made to add, remove, and contains.
  • +
+
\ No newline at end of file From c97e5539bdb9856112904966a4a0c42c841db04f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 May 2023 08:07:32 +0530 Subject: [PATCH 1031/3167] Attach NOTES - LeetHub --- 0705-design-hashset/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0705-design-hashset/NOTES.md diff --git a/0705-design-hashset/NOTES.md b/0705-design-hashset/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0705-design-hashset/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 21321b3895c50ef561788310cfb7179d34bd330e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 May 2023 08:07:37 +0530 Subject: [PATCH 1032/3167] Time: 113 ms (46.86%), Space: 41.8 MB (61.32%) - LeetHub --- 0705-design-hashset/0705-design-hashset.cpp | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0705-design-hashset/0705-design-hashset.cpp diff --git a/0705-design-hashset/0705-design-hashset.cpp b/0705-design-hashset/0705-design-hashset.cpp new file mode 100644 index 00000000..c76dbb4f --- /dev/null +++ b/0705-design-hashset/0705-design-hashset.cpp @@ -0,0 +1,29 @@ +class MyHashSet { +public: + + unordered_map mp; + + MyHashSet() { + + } + + void add(int key) { + ++mp[key]; + } + + void remove(int key) { + mp.erase(key); + } + + bool contains(int key) { + return mp[key] > 0; + } +}; + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet* obj = new MyHashSet(); + * obj->add(key); + * obj->remove(key); + * bool param_3 = obj->contains(key); + */ \ No newline at end of file From 8299b48a34abcfba61253c6ca70c6278d1122126 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 May 2023 08:08:11 +0530 Subject: [PATCH 1033/3167] Attach NOTES - LeetHub From c49246a7b778022197ee3eb7b62a3f48371f6a1f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 May 2023 08:08:40 +0530 Subject: [PATCH 1034/3167] Attach NOTES - LeetHub From 8fdd41d17c9925419b61a2a34ce037f2efd465e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 May 2023 23:24:41 +0530 Subject: [PATCH 1035/3167] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2693-call-function-with-custom-context/README.md diff --git a/2693-call-function-with-custom-context/README.md b/2693-call-function-with-custom-context/README.md new file mode 100644 index 00000000..87d051a7 --- /dev/null +++ b/2693-call-function-with-custom-context/README.md @@ -0,0 +1,50 @@ +

2693. Call Function with Custom Context

Medium


Enhance all functions to have the callPolyfill method. The method accepts an object obj as it's first parameter and any number of additional arguments. The obj becomes the this context for the function. The additional arguments are passed to the function (that the callPolyfill method belongs on).

+ +

For example if you had the function:

+ +
function tax(price, taxRate) {
+  const totalCost = price * (1 + taxRate);
+  console.log(`The cost of ${this.item} is ${totalCost}`);
+}
+
+ +

Calling this function like tax(10, 0.1) will log "The cost of undefined is 11". This is because the this context was not defined.

+ +

However, calling the function like tax.callPolyfill({item: "salad"}, 10, 0.1) will log "The cost of salad is 11". The this context was appropriately set, and the function logged an appropriate output.

+ +

Please solve this without using the built-in Function.call method.

+ +

 

+

Example 1:

+ +
Input:
+fn = function add(b) {
+  return this.a + b;
+}
+args = [{"a": 5}, 7]
+Output: 12
+Explanation:
+fn.callPolyfill({"a": 5}, 7); // 12
+callPolyfill sets the "this" context to {"a": 5}. 7 is passed as an argument.
+
+ +

Example 2:

+ +
Input: 
+fn = function tax(price, taxRate) { 
+ return `The cost of the ${this.item} is ${price * taxRate}`; 
+}
+args = [{"item": "burger"}, 10, 1,1]
+Output: "The cost of the burger is 11"
+Explanation: callPolyfill sets the "this" context to {"item": "burger"}. 10 and 1.1 are passed as additional arguments.
+
+ +

 

+

Constraints:

+ +
    +
  • typeof args[0] == 'object' and args[0] != null
  • +
  • 1 <= args.length <= 100
  • +
  • 2 <= JSON.stringify(args[0]).length <= 105
  • +
+
\ No newline at end of file From d864dd58b5a548fbfff2ec79bfc1ddfe463e7f1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 May 2023 23:24:45 +0530 Subject: [PATCH 1036/3167] Time: 67 ms (18.53%), Space: 42 MB (66.98%) - LeetHub --- .../2693-call-function-with-custom-context.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2693-call-function-with-custom-context/2693-call-function-with-custom-context.js diff --git a/2693-call-function-with-custom-context/2693-call-function-with-custom-context.js b/2693-call-function-with-custom-context/2693-call-function-with-custom-context.js new file mode 100644 index 00000000..48ee53f2 --- /dev/null +++ b/2693-call-function-with-custom-context/2693-call-function-with-custom-context.js @@ -0,0 +1,18 @@ +/** + * @param {Object} context + * @param {any[]} args + * @return {any} + */ +Function.prototype.callPolyfill = function(context, ...args) { + let a = Symbol(); + context[a] = this; + let res = context[a](...args); + delete context[a]; + + return res; +} + +/** + * function increment() { this.count++; return this.count; } + * increment.callPolyfill({count: 1}); // 2 + */ \ No newline at end of file From ecec1cb21287def3f4ec7c10b175b56d11867144 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 31 May 2023 19:44:41 +0530 Subject: [PATCH 1037/3167] Create README - LeetHub --- 1396-design-underground-system/README.md | 88 ++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 1396-design-underground-system/README.md diff --git a/1396-design-underground-system/README.md b/1396-design-underground-system/README.md new file mode 100644 index 00000000..552117d8 --- /dev/null +++ b/1396-design-underground-system/README.md @@ -0,0 +1,88 @@ +

1396. Design Underground System

Medium


An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.

+ +

Implement the UndergroundSystem class:

+ +
    +
  • void checkIn(int id, string stationName, int t) + +
      +
    • A customer with a card ID equal to id, checks in at the station stationName at time t.
    • +
    • A customer can only be checked into one place at a time.
    • +
    +
  • +
  • void checkOut(int id, string stationName, int t) +
      +
    • A customer with a card ID equal to id, checks out from the station stationName at time t.
    • +
    +
  • +
  • double getAverageTime(string startStation, string endStation) +
      +
    • Returns the average time it takes to travel from startStation to endStation.
    • +
    • The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation.
    • +
    • The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.
    • +
    • There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called.
    • +
    +
  • +
+ +

You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.

+ +

 

+

Example 1:

+ +
Input
+["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
+[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
+
+Output
+[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
+
+Explanation
+UndergroundSystem undergroundSystem = new UndergroundSystem();
+undergroundSystem.checkIn(45, "Leyton", 3);
+undergroundSystem.checkIn(32, "Paradise", 8);
+undergroundSystem.checkIn(27, "Leyton", 10);
+undergroundSystem.checkOut(45, "Waterloo", 15);  // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
+undergroundSystem.checkOut(27, "Waterloo", 20);  // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
+undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
+undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
+undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
+undergroundSystem.checkIn(10, "Leyton", 24);
+undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 11.00000
+undergroundSystem.checkOut(10, "Waterloo", 38);  // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
+undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
+
+ +

Example 2:

+ +
Input
+["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
+[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
+
+Output
+[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
+
+Explanation
+UndergroundSystem undergroundSystem = new UndergroundSystem();
+undergroundSystem.checkIn(10, "Leyton", 3);
+undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
+undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
+undergroundSystem.checkIn(5, "Leyton", 10);
+undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
+undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
+undergroundSystem.checkIn(2, "Leyton", 21);
+undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
+undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= id, t <= 106
  • +
  • 1 <= stationName.length, startStation.length, endStation.length <= 10
  • +
  • All strings consist of uppercase and lowercase English letters and digits.
  • +
  • There will be at most 2 * 104 calls in total to checkIn, checkOut, and getAverageTime.
  • +
  • Answers within 10-5 of the actual value will be accepted.
  • +
+
\ No newline at end of file From 845c2db403c3a6c979bd77080900e18033e4472f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 31 May 2023 19:44:41 +0530 Subject: [PATCH 1038/3167] Attach NOTES - LeetHub --- 1396-design-underground-system/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1396-design-underground-system/NOTES.md diff --git a/1396-design-underground-system/NOTES.md b/1396-design-underground-system/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1396-design-underground-system/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d4506d3b888bc842cb5bd794e2659766edb3b3c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 31 May 2023 19:44:45 +0530 Subject: [PATCH 1039/3167] Time: 170 ms (32.97%), Space: 57.2 MB (95.68%) - LeetHub --- .../1396-design-underground-system.cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 1396-design-underground-system/1396-design-underground-system.cpp diff --git a/1396-design-underground-system/1396-design-underground-system.cpp b/1396-design-underground-system/1396-design-underground-system.cpp new file mode 100644 index 00000000..ee8060bd --- /dev/null +++ b/1396-design-underground-system/1396-design-underground-system.cpp @@ -0,0 +1,59 @@ +class UndergroundSystem { + +private: + map> mp; + map, pair> links; + +public: + + UndergroundSystem() { + + } + + void checkIn(int id, string stationName, int t) { + mp.insert({id, {stationName, t}}); + } + + void checkOut(int id, string stationName, int t) { + + auto inTime = mp[id]; + + string checkInStationName = inTime.first; + double checkInStationTime = inTime.second; + + double currTime = t - checkInStationTime; + + if(links.find({checkInStationName,stationName}) != links.end()) + { + auto here = links[{checkInStationName,stationName}]; + + int cnt = here.first; + + double prevValue = here.second; + + links[{checkInStationName, stationName}]={cnt + 1, prevValue + currTime}; + } + else + { + links.insert({{checkInStationName, stationName},{1, currTime}}); + } + + mp.erase(id); + } + + double getAverageTime(string startStation, string endStation) { + + auto here = links[{startStation, endStation}]; + + return (double)(here.second/here.first); + + } +}; + +/** + * Your UndergroundSystem object will be instantiated and called as such: + * UndergroundSystem* obj = new UndergroundSystem(); + * obj->checkIn(id,stationName,t); + * obj->checkOut(id,stationName,t); + * double param_3 = obj->getAverageTime(startStation,endStation); + */ \ No newline at end of file From 7816cd7c6c4dfd91bf0fbf48e6e1fdb559f6eeef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Jun 2023 07:37:47 +0530 Subject: [PATCH 1040/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0145-binary-tree-postorder-traversal/README.md diff --git a/0145-binary-tree-postorder-traversal/README.md b/0145-binary-tree-postorder-traversal/README.md new file mode 100644 index 00000000..ed52ab88 --- /dev/null +++ b/0145-binary-tree-postorder-traversal/README.md @@ -0,0 +1,31 @@ +

145. Binary Tree Postorder Traversal

Easy


Given the root of a binary tree, return the postorder traversal of its nodes' values.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

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

 

+Follow up: Recursive solution is trivial, could you do it iteratively?
\ No newline at end of file From 213b8d8c6deb4765532cf62423be6350f9531b49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Jun 2023 07:37:47 +0530 Subject: [PATCH 1041/3167] Attach NOTES - LeetHub --- 0145-binary-tree-postorder-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0145-binary-tree-postorder-traversal/NOTES.md diff --git a/0145-binary-tree-postorder-traversal/NOTES.md b/0145-binary-tree-postorder-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0145-binary-tree-postorder-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 34b639e13e84461100de6262f614daa47e6d6cdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Jun 2023 07:37:51 +0530 Subject: [PATCH 1042/3167] Time: 0 ms (100.00%), Space: 8.3 MB (85.97%) - LeetHub --- .../0145-binary-tree-postorder-traversal.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.cpp diff --git a/0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.cpp b/0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.cpp new file mode 100644 index 00000000..e4ed9970 --- /dev/null +++ b/0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.cpp @@ -0,0 +1,60 @@ +/** + * 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 postorderTraversal(TreeNode* root) { + + vector ans; + + if(!root) + return ans; + + stack st; + + TreeNode* prev = root; + + while(prev != nullptr or !st.empty()) + { + if(prev) + { + st.push(prev); + prev = prev->left; + } + else + { + TreeNode* temp = st.top()->right; + + if(temp == nullptr) + { + temp = st.top(); + + st.pop(); + + ans.push_back(temp->val); + + while(!st.empty() and st.top()->right == temp) + { + temp = st.top(); + + st.pop(); + + ans.push_back(temp->val); + } + } + else + prev = temp; + } + } + + return ans; + } +}; \ No newline at end of file From 141c906d6c0152d4bce24a2e4ad2eb6ac54c4e67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Jun 2023 22:53:17 +0530 Subject: [PATCH 1043/3167] Create README - LeetHub --- 2695-array-wrapper/README.md | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2695-array-wrapper/README.md diff --git a/2695-array-wrapper/README.md b/2695-array-wrapper/README.md new file mode 100644 index 00000000..8d80d3c9 --- /dev/null +++ b/2695-array-wrapper/README.md @@ -0,0 +1,46 @@ +

2695. Array Wrapper

Easy


Create a class ArrayWrapper that accepts an array of integers in it's constructor. This class should have two features:

+ +
    +
  • When two instances of this class are added together with the + operator, the resulting value is the sum of all the elements in both arrays.
  • +
  • When the String() function is called on the instance, it will return a comma separated string surrounded by brackets. For example, [1,2,3].
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [[1,2],[3,4]], operation = "Add"
+Output: 10
+Explanation:
+const obj1 = new ArrayWrapper([1,2]);
+const obj2 = new ArrayWrapper([3,4]);
+obj1 + obj2; // 10
+
+ +

Example 2:

+ +
Input: nums = [[23,98,42,70]], operation = "String"
+Output: "[23,98,42,70]"
+Explanation:
+const obj = new ArrayWrapper([23,98,42,70]);
+String(obj); // "[23,98,42,70]"
+
+ +

Example 3:

+ +
Input: nums = [[],[]], operation = "Add"
+Output: 0
+Explanation:
+const obj1 = new ArrayWrapper([]);
+const obj2 = new ArrayWrapper([]);
+obj1 + obj2; // 0
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 1000
  • +
  • 0 <= nums[i] <= 1000
  • +
  • Note: nums is the array passed to the constructor
  • +
+
\ No newline at end of file From df94b3951cf89842b73afe10c73462d65ac37de3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Jun 2023 22:53:17 +0530 Subject: [PATCH 1044/3167] Attach NOTES - LeetHub --- 2695-array-wrapper/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2695-array-wrapper/NOTES.md diff --git a/2695-array-wrapper/NOTES.md b/2695-array-wrapper/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2695-array-wrapper/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4d4e30e983802e34e4bb8bfdacf5b5f478b09c3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:35:21 +0530 Subject: [PATCH 1045/3167] Create README - LeetHub --- 2101-detonate-the-maximum-bombs/README.md | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2101-detonate-the-maximum-bombs/README.md diff --git a/2101-detonate-the-maximum-bombs/README.md b/2101-detonate-the-maximum-bombs/README.md new file mode 100644 index 00000000..3cf611be --- /dev/null +++ b/2101-detonate-the-maximum-bombs/README.md @@ -0,0 +1,49 @@ +

2101. Detonate the Maximum Bombs

Medium


You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb.

+ +

The bombs are represented by a 0-indexed 2D integer array bombs where bombs[i] = [xi, yi, ri]. xi and yi denote the X-coordinate and Y-coordinate of the location of the ith bomb, whereas ri denotes the radius of its range.

+ +

You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. These bombs will further detonate the bombs that lie in their ranges.

+ +

Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb.

+ +

 

+

Example 1:

+ +
Input: bombs = [[2,1,3],[6,1,4]]
+Output: 2
+Explanation:
+The above figure shows the positions and ranges of the 2 bombs.
+If we detonate the left bomb, the right bomb will not be affected.
+But if we detonate the right bomb, both bombs will be detonated.
+So the maximum bombs that can be detonated is max(1, 2) = 2.
+
+ +

Example 2:

+ +
Input: bombs = [[1,1,5],[10,10,5]]
+Output: 1
+Explanation:
+Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.
+
+ +

Example 3:

+ +
Input: bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
+Output: 5
+Explanation:
+The best bomb to detonate is bomb 0 because:
+- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.
+- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.
+- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.
+Thus all 5 bombs are detonated.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= bombs.length <= 100
  • +
  • bombs[i].length == 3
  • +
  • 1 <= xi, yi, ri <= 105
  • +
+
\ No newline at end of file From 09267c2104f144c484d6456a10b6fc30149b81f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:35:25 +0530 Subject: [PATCH 1046/3167] Time: 93 ms (88.03%), Space: 14.6 MB (84.81%) - LeetHub --- .../2101-detonate-the-maximum-bombs.cpp | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 2101-detonate-the-maximum-bombs/2101-detonate-the-maximum-bombs.cpp diff --git a/2101-detonate-the-maximum-bombs/2101-detonate-the-maximum-bombs.cpp b/2101-detonate-the-maximum-bombs/2101-detonate-the-maximum-bombs.cpp new file mode 100644 index 00000000..d8a33911 --- /dev/null +++ b/2101-detonate-the-maximum-bombs/2101-detonate-the-maximum-bombs.cpp @@ -0,0 +1,67 @@ +#define ll long long int + +class Solution { + +private: + void dfs(int sv, vector adj[], vector& visited, ll& c) + { + visited[sv] = true; + + ++c; + + for(auto itr : adj[sv]) + { + if(!visited[itr]) + dfs(itr, adj, visited, c); + } + } + +public: + int maximumDetonation(vector>& bombs) { + + int n = bombs.size(); + + vector adj[n]; + + for(int i = 0; i < n; ++i) + { + ll x1 = bombs[i][0]; + ll y1 = bombs[i][1]; + ll rad = bombs[i][2]; + + for(int j = 0; j < n; ++j) + { + if(i != j) + { + ll x2 = bombs[j][0]; + ll y2 = bombs[j][1]; + + + ll x = abs(x1 - x2); + ll y = abs(y1 - y2); + + if((x*x) + (y*y) <= (rad*rad)) + { + adj[i].push_back(j); + } + } + } + } + + ll ans = 0; + + for(int i = 0; i < n; ++i) + { + vector visited(n, false); + + ll c = 0; + + dfs(i, adj, visited, c); + + ans = max(ans, c); + } + + return ans; + + } +}; \ No newline at end of file From c8345c419007cc8df83e55ec9e3835aa319927e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:35:53 +0530 Subject: [PATCH 1047/3167] Attach NOTES - LeetHub --- 2101-detonate-the-maximum-bombs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2101-detonate-the-maximum-bombs/NOTES.md diff --git a/2101-detonate-the-maximum-bombs/NOTES.md b/2101-detonate-the-maximum-bombs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2101-detonate-the-maximum-bombs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 26e5d01d145f83e55500fce7ef0d9d97a7373e62 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:35:56 +0530 Subject: [PATCH 1048/3167] Time: 93 ms (88.03%), Space: 14.6 MB (84.81%) - LeetHub From 7ae120cd6c01988f8c310c3870c84681d7d5f9ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:42:27 +0530 Subject: [PATCH 1049/3167] Create README - LeetHub --- 2648-generate-fibonacci-sequence/README.md | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2648-generate-fibonacci-sequence/README.md diff --git a/2648-generate-fibonacci-sequence/README.md b/2648-generate-fibonacci-sequence/README.md new file mode 100644 index 00000000..b2ad9203 --- /dev/null +++ b/2648-generate-fibonacci-sequence/README.md @@ -0,0 +1,34 @@ +

2648. Generate Fibonacci Sequence

Easy


Write a generator function that returns a generator object which yields the fibonacci sequence.

+ +

The fibonacci sequence is defined by the relation Xn = Xn-1 + Xn-2.

+ +

The first few numbers of the series are 0, 1, 1, 2, 3, 5, 8, 13.

+ +

 

+

Example 1:

+ +
Input: callCount = 5
+Output: [0,1,1,2,3]
+Explanation:
+const gen = fibGenerator();
+gen.next().value; // 0
+gen.next().value; // 1
+gen.next().value; // 1
+gen.next().value; // 2
+gen.next().value; // 3
+
+ +

Example 2:

+ +
Input: callCount = 0
+Output: []
+Explanation: gen.next() is never called so nothing is outputted
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= callCount <= 50
  • +
+
\ No newline at end of file From a6d80c0830b4b7af9b7da9287b31fec6da64948b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:42:30 +0530 Subject: [PATCH 1050/3167] Time: 49 ms (92.71%), Space: 42.6 MB (9.23%) - LeetHub --- .../2648-generate-fibonacci-sequence.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2648-generate-fibonacci-sequence/2648-generate-fibonacci-sequence.js diff --git a/2648-generate-fibonacci-sequence/2648-generate-fibonacci-sequence.js b/2648-generate-fibonacci-sequence/2648-generate-fibonacci-sequence.js new file mode 100644 index 00000000..9522c83c --- /dev/null +++ b/2648-generate-fibonacci-sequence/2648-generate-fibonacci-sequence.js @@ -0,0 +1,19 @@ +/** + * @return {Generator} + */ +var fibGenerator = function*() { + let a = 0; + let b = 1; + + while (true){ + yield a; + [a,b] = [b,a+b]; + } + +}; + +/** + * const gen = fibGenerator(); + * gen.next().value; // 0 + * gen.next().value; // 1 + */ \ No newline at end of file From e18a2852893a817fb356ca775a9a2f8124028f41 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:07:26 +0530 Subject: [PATCH 1051/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1376-time-needed-to-inform-all-employees/README.md diff --git a/1376-time-needed-to-inform-all-employees/README.md b/1376-time-needed-to-inform-all-employees/README.md new file mode 100644 index 00000000..878da7c5 --- /dev/null +++ b/1376-time-needed-to-inform-all-employees/README.md @@ -0,0 +1,41 @@ +

1376. Time Needed to Inform All Employees

Medium


A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.

+ +

Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.

+ +

The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.

+ +

The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).

+ +

Return the number of minutes needed to inform all the employees about the urgent news.

+ +

 

+

Example 1:

+ +
Input: n = 1, headID = 0, manager = [-1], informTime = [0]
+Output: 0
+Explanation: The head of the company is the only employee in the company.
+
+ +

Example 2:

+ +
Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
+Output: 1
+Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
+The tree structure of the employees in the company is shown.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 0 <= headID < n
  • +
  • manager.length == n
  • +
  • 0 <= manager[i] < n
  • +
  • manager[headID] == -1
  • +
  • informTime.length == n
  • +
  • 0 <= informTime[i] <= 1000
  • +
  • informTime[i] == 0 if employee i has no subordinates.
  • +
  • It is guaranteed that all the employees can be informed.
  • +
+
\ No newline at end of file From 941c7d96a18cb4cad6a25b124f79761be2cfb3e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:07:29 +0530 Subject: [PATCH 1052/3167] Time: 308 ms (75.33%), Space: 118.3 MB (63.61%) - LeetHub --- ...76-time-needed-to-inform-all-employees.cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 1376-time-needed-to-inform-all-employees/1376-time-needed-to-inform-all-employees.cpp diff --git a/1376-time-needed-to-inform-all-employees/1376-time-needed-to-inform-all-employees.cpp b/1376-time-needed-to-inform-all-employees/1376-time-needed-to-inform-all-employees.cpp new file mode 100644 index 00000000..1dee47a8 --- /dev/null +++ b/1376-time-needed-to-inform-all-employees/1376-time-needed-to-inform-all-employees.cpp @@ -0,0 +1,70 @@ +class Solution { + +private: + void dfs(int sv, vector> adj[], vector& visited, int& ans) + { + visited[sv] = true; + + for(auto itr : adj[sv]) + { + int child = itr.first; + int time = itr.second; + + if(!visited[child]) + { + ans += time; + dfs(child, adj, visited, ans); + } + } + } + +public: + int numOfMinutes(int n, int headID, vector& manager, vector& informTime) { + + vector> adj[n+1]; + + for(int i = 0; i < n; ++i) + { + if(manager[i] == -1) + continue; + + adj[manager[i]].push_back({i, informTime[manager[i]]}); + } + + int ans = 0; + + vector visited(n+1, false); + + // dfs(headID, adj, visited, ans); + + queue> q; + + q.push({headID ,0}); + + visited[headID] = true; + + while(!q.empty()) + { + int node = q.front().first; + int time = q.front().second; + + q.pop(); + + ans = max(ans, time); + + for(auto itr : adj[node]) + { + int child = itr.first; + int currTime = itr.second; + + if(!visited[child]) + { + visited[child] = true; + q.push({child, time + currTime}); + } + } + } + + return ans; + } +}; \ No newline at end of file From 69d038fe7c85c487759c96b70ac135812db70b0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:11:19 +0530 Subject: [PATCH 1053/3167] Attach NOTES - LeetHub --- 1376-time-needed-to-inform-all-employees/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1376-time-needed-to-inform-all-employees/NOTES.md diff --git a/1376-time-needed-to-inform-all-employees/NOTES.md b/1376-time-needed-to-inform-all-employees/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1376-time-needed-to-inform-all-employees/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9e8d45b0e91425c0cf504ad1ae2e9fb2d14a79ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:11:22 +0530 Subject: [PATCH 1054/3167] Time: 308 ms (75.33%), Space: 118.3 MB (63.61%) - LeetHub From 40991e0753c594f01d96d4a84f2117e15996254d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:14:46 +0530 Subject: [PATCH 1055/3167] Create README - LeetHub --- 2649-nested-array-generator/README.md | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2649-nested-array-generator/README.md diff --git a/2649-nested-array-generator/README.md b/2649-nested-array-generator/README.md new file mode 100644 index 00000000..73c2baee --- /dev/null +++ b/2649-nested-array-generator/README.md @@ -0,0 +1,37 @@ +

2649. Nested Array Generator

Medium


Given a multi-dimensional array of integers, return a generator object which yields integers in the same order as inorder traversal.

+ +

multi-dimensional array is a recursive data structure that contains both integers and other multi-dimensional arrays.

+ +

inorder traversal iterates over each array from left to right, yielding any integers it encounters or applying inorder traversal to any arrays it encounters.

+ +

 

+

Example 1:

+ +
Input: arr = [[[6]],[1,3],[]]
+Output: [6,1,3]
+Explanation:
+const generator = inorderTraversal(arr);
+generator.next().value; // 6
+generator.next().value; // 1
+generator.next().value; // 3
+generator.next().done; // true
+
+ +

Example 2:

+ +
Input: arr = []
+Output: []
+Explanation: There are no integers so the generator doesn't yield anything.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= arr.flat().length <= 105
  • +
  • 0 <= arr.flat()[i] <= 105
  • +
  • maxNestingDepth <= 105
  • +
+ +

 

+Can you solve this without creating a new flattened version of the array?
\ No newline at end of file From 852b017d1a8b198423c4fa3455103df292ede3ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:14:46 +0530 Subject: [PATCH 1056/3167] Attach NOTES - LeetHub --- 2649-nested-array-generator/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2649-nested-array-generator/NOTES.md diff --git a/2649-nested-array-generator/NOTES.md b/2649-nested-array-generator/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2649-nested-array-generator/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 887ff809e358c916bdbc60d7a972765f0522ac62 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:14:49 +0530 Subject: [PATCH 1057/3167] Time: 172 ms (78.04%), Space: 74.1 MB (50.62%) - LeetHub --- .../2649-nested-array-generator.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2649-nested-array-generator/2649-nested-array-generator.js diff --git a/2649-nested-array-generator/2649-nested-array-generator.js b/2649-nested-array-generator/2649-nested-array-generator.js new file mode 100644 index 00000000..ee262017 --- /dev/null +++ b/2649-nested-array-generator/2649-nested-array-generator.js @@ -0,0 +1,20 @@ +/** + * @param {Array} arr + * @return {Generator} + */ +var inorderTraversal = function*(arr) { + + for (let i = 0; i < arr.length; i++){ + + if (Array.isArray(arr[i]))yield* inorderTraversal(arr[i]) + + else yield arr[i] + + } +}; +/** + * const gen = inorderTraversal([1, [2, 3]]); + * gen.next().value; // 1 + * gen.next().value; // 2 + * gen.next().value; // 3 + */ \ No newline at end of file From 995ec878b8a57cc47830ec2547c99e24218033fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Jun 2023 10:01:03 +0530 Subject: [PATCH 1058/3167] Attach NOTES - LeetHub From 07d96fc5bda5d1f5c3c4ea8a61a9da51228ac017 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Jun 2023 10:01:07 +0530 Subject: [PATCH 1059/3167] Time: 30 ms (36.74%), Space: 13.6 MB (98.63%) - LeetHub --- .../0547-number-of-provinces.cpp | 159 ++++++++---------- 1 file changed, 73 insertions(+), 86 deletions(-) diff --git a/0547-number-of-provinces/0547-number-of-provinces.cpp b/0547-number-of-provinces/0547-number-of-provinces.cpp index af2f16de..c7f0c37e 100644 --- a/0547-number-of-provinces/0547-number-of-provinces.cpp +++ b/0547-number-of-provinces/0547-number-of-provinces.cpp @@ -1,88 +1,72 @@ -template -class DSU -{ -public: - int N; - vector rank, parent, size; - DSU(int n) - { - N = n; - rank.resize(n + 1, 0); - size.resize(n + 1, 1); - parent.resize(n + 1); - - for (T i = 0; i <= n; ++i) +class DSU{ + private: + vector rank, parent, size; + public: + DSU(int n) { - parent[i] = i; + rank.resize(n+1, 0); + parent.resize(n+1); + size.resize(n+1, 1); + + for(int i = 0; i <= n; ++i) + parent[i] = i; } - } - - template - X findParent(X u) - { - if (u == parent[u]) - return u; - return parent[u] = findParent(parent[u]); - } - - // Not be using unionByRank - // but yes for future reference - template - void unionByRank(X u, X v) - { - int nodeX = findParent(u); - int nodeY = findParent(v); - - if (nodeX == nodeY) - return; - - if (rank[nodeX] < rank[nodeY]) - { - parent[nodeX] = nodeY; - } - else if (rank[nodeY] < rank[nodeX]) + + int findPar(int u) { - parent[nodeY] = nodeX; + if(parent[u] == u) + return parent[u]; + return parent[u] = findPar(parent[u]); } - else + + void unionByRank(int u, int v) { - parent[nodeY] = nodeX; - ++rank[nodeX]; + int parU = findPar(u); + int parV = findPar(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 nodeX = findParent(u); - int nodeY = findParent(v); - - if (nodeX == nodeY) - return; - - if (size[nodeX] < size[nodeY]) + + void unionBySize(int u, int v) { - parent[nodeX] = nodeY; - size[nodeY] += size[nodeX]; + int parU = findPar(u); + int parV = findPar(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]; + } } - else + + bool isSame(int u, int v) { - parent[nodeY] = nodeX; - size[nodeX] += size[nodeY]; + return findPar(u) == findPar(v); } - } - - template - bool isSame(X u, X v) - { - return findParent(u) == findParent(v); - } }; -// DSU dsu(n); - -// dsu.unionByRank(u, v); -// dsu.unionBySize(u,v); -// dsu.isSame(u, v) bool - class Solution { public: @@ -90,24 +74,27 @@ class Solution { int n = isConnected.size(); int m = isConnected[0].size(); - int provinces = 0; - DSU dsu(n); - for(int i = 0; i< n; ++i) + DSU dsu(n); + + for(int i = 0; i < n; ++i) { - for(int j = 0; j Date: Mon, 5 Jun 2023 21:38:16 +0530 Subject: [PATCH 1060/3167] Create README - LeetHub --- 1232-check-if-it-is-a-straight-line/README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1232-check-if-it-is-a-straight-line/README.md diff --git a/1232-check-if-it-is-a-straight-line/README.md b/1232-check-if-it-is-a-straight-line/README.md new file mode 100644 index 00000000..832bb14e --- /dev/null +++ b/1232-check-if-it-is-a-straight-line/README.md @@ -0,0 +1,30 @@ +

1232. Check If It Is a Straight Line

Easy


You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.

+ +

 

+ +

 

+

Example 1:

+ +

+ +
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
+Output: true
+
+ +

Example 2:

+ +

+ +
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= coordinates.length <= 1000
  • +
  • coordinates[i].length == 2
  • +
  • -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
  • +
  • coordinates contains no duplicate point.
  • +
\ No newline at end of file From fd045a51c674f62639f60ce367e99a3847a880b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Jun 2023 21:38:20 +0530 Subject: [PATCH 1061/3167] Time: 11 ms (56.86%), Space: 10.1 MB (63.27%) - LeetHub --- .../1232-check-if-it-is-a-straight-line.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1232-check-if-it-is-a-straight-line/1232-check-if-it-is-a-straight-line.cpp diff --git a/1232-check-if-it-is-a-straight-line/1232-check-if-it-is-a-straight-line.cpp b/1232-check-if-it-is-a-straight-line/1232-check-if-it-is-a-straight-line.cpp new file mode 100644 index 00000000..239943ba --- /dev/null +++ b/1232-check-if-it-is-a-straight-line/1232-check-if-it-is-a-straight-line.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool checkStraightLine(vector>& coordinates) { + + int n = coordinates.size(); + + int x1 = coordinates[0][0]; + int y1 = coordinates[0][1]; + + int x2 = coordinates[1][0]; + int y2 = coordinates[1][1]; + + for(int i = 2; i < n; ++i) + { + int x = coordinates[i][0]; + int y = coordinates[i][1]; + + // y-y1/x-x1 = y-y2/x-x2 + + if((y-y1)*(x-x2) != (x-x1)*(y-y2)) + return false; + } + + return true; + + } +}; \ No newline at end of file From 5f00c3ed6d81bebb140a7137ee0436af14baf852 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:09:34 +0530 Subject: [PATCH 1062/3167] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1502-can-make-arithmetic-progression-from-sequence/README.md diff --git a/1502-can-make-arithmetic-progression-from-sequence/README.md b/1502-can-make-arithmetic-progression-from-sequence/README.md new file mode 100644 index 00000000..8f9e143f --- /dev/null +++ b/1502-can-make-arithmetic-progression-from-sequence/README.md @@ -0,0 +1,27 @@ +

1502. Can Make Arithmetic Progression From Sequence

Easy


A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

+ +

Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: arr = [3,5,1]
+Output: true
+Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
+
+ +

Example 2:

+ +
Input: arr = [1,2,4]
+Output: false
+Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= arr.length <= 1000
  • +
  • -106 <= arr[i] <= 106
  • +
+
\ No newline at end of file From 6828306ef29be5ba1c46196cb6547bf8051b3f91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:09:35 +0530 Subject: [PATCH 1063/3167] Attach NOTES - LeetHub --- 1502-can-make-arithmetic-progression-from-sequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1502-can-make-arithmetic-progression-from-sequence/NOTES.md diff --git a/1502-can-make-arithmetic-progression-from-sequence/NOTES.md b/1502-can-make-arithmetic-progression-from-sequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1502-can-make-arithmetic-progression-from-sequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 877020969d20bd6f2c94be24f8844d342cbc6198 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:09:38 +0530 Subject: [PATCH 1064/3167] Time: 9 ms (22.98%), Space: 9 MB (66.22%) - LeetHub --- ...e-arithmetic-progression-from-sequence.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1502-can-make-arithmetic-progression-from-sequence/1502-can-make-arithmetic-progression-from-sequence.cpp diff --git a/1502-can-make-arithmetic-progression-from-sequence/1502-can-make-arithmetic-progression-from-sequence.cpp b/1502-can-make-arithmetic-progression-from-sequence/1502-can-make-arithmetic-progression-from-sequence.cpp new file mode 100644 index 00000000..876efb1a --- /dev/null +++ b/1502-can-make-arithmetic-progression-from-sequence/1502-can-make-arithmetic-progression-from-sequence.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool canMakeArithmeticProgression(vector& arr) { + + int n = arr.size(); + + sort(arr.begin(), arr.end()); + + int diff = arr[1] - arr[0]; + + for(int i = 1; i < n; ++i) + { + if(arr[i] - arr[i-1] != diff) + return false; + } + + return true; + } +}; \ No newline at end of file From 1dcfdc460b11f236f9c3d09873bd8e5dbefc289b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:10:03 +0530 Subject: [PATCH 1065/3167] Attach NOTES - LeetHub From b68df883834b1b64f45e56220647c3fe77b0ea8d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:11:01 +0530 Subject: [PATCH 1066/3167] Attach NOTES - LeetHub From 5ff3e7a0a184ad02f3ca97aeaa160f37b8d13402 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:11:04 +0530 Subject: [PATCH 1067/3167] Time: 9 ms (22.98%), Space: 9 MB (66.22%) - LeetHub From 4ea937379d65c77c3235bdb06f19e6913e7a736e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Jun 2023 07:40:22 +0530 Subject: [PATCH 1068/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1318-minimum-flips-to-make-a-or-b-equal-to-c/README.md diff --git a/1318-minimum-flips-to-make-a-or-b-equal-to-c/README.md b/1318-minimum-flips-to-make-a-or-b-equal-to-c/README.md new file mode 100644 index 00000000..343f0c92 --- /dev/null +++ b/1318-minimum-flips-to-make-a-or-b-equal-to-c/README.md @@ -0,0 +1,32 @@ +

1318. Minimum Flips to Make a OR b Equal to c

Medium


Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
+Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

+ +

 

+

Example 1:

+ +

+ +
Input: a = 2, b = 6, c = 5
+Output: 3
+Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
+ +

Example 2:

+ +
Input: a = 4, b = 2, c = 7
+Output: 1
+
+ +

Example 3:

+ +
Input: a = 1, b = 2, c = 3
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= a <= 10^9
  • +
  • 1 <= b <= 10^9
  • +
  • 1 <= c <= 10^9
  • +
\ No newline at end of file From 3c6a4983ba93b1b27fce309951215bec67767737 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Jun 2023 07:40:22 +0530 Subject: [PATCH 1069/3167] Attach NOTES - LeetHub --- 1318-minimum-flips-to-make-a-or-b-equal-to-c/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1318-minimum-flips-to-make-a-or-b-equal-to-c/NOTES.md diff --git a/1318-minimum-flips-to-make-a-or-b-equal-to-c/NOTES.md b/1318-minimum-flips-to-make-a-or-b-equal-to-c/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1318-minimum-flips-to-make-a-or-b-equal-to-c/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f920eda086d1f129462bc9406c069d4f8be5bb46 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Jun 2023 07:40:25 +0530 Subject: [PATCH 1070/3167] Time: 0 ms (100.00%), Space: 6 MB (16.51%) - LeetHub --- ...inimum-flips-to-make-a-or-b-equal-to-c.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1318-minimum-flips-to-make-a-or-b-equal-to-c/1318-minimum-flips-to-make-a-or-b-equal-to-c.cpp diff --git a/1318-minimum-flips-to-make-a-or-b-equal-to-c/1318-minimum-flips-to-make-a-or-b-equal-to-c.cpp b/1318-minimum-flips-to-make-a-or-b-equal-to-c/1318-minimum-flips-to-make-a-or-b-equal-to-c.cpp new file mode 100644 index 00000000..7b0fa96e --- /dev/null +++ b/1318-minimum-flips-to-make-a-or-b-equal-to-c/1318-minimum-flips-to-make-a-or-b-equal-to-c.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int minFlips(int a, int b, int c) { + + int ai, bi, ci, cnt = 0; + + while(a or b or c) + { + ai = a & 1; + bi = b & 1; + ci = c & 1; + + if((ai | bi) != ci) + { + if(ai & bi) + cnt += 2; + else + ++cnt; + } + + a >>= 1; + b >>= 1; + c >>= 1; + } + + return cnt; + + } +}; + + \ No newline at end of file From 8692e594fa7e2f5499555737b1c811e850b9d4a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:41:17 +0530 Subject: [PATCH 1071/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1351-count-negative-numbers-in-a-sorted-matrix/README.md diff --git a/1351-count-negative-numbers-in-a-sorted-matrix/README.md b/1351-count-negative-numbers-in-a-sorted-matrix/README.md new file mode 100644 index 00000000..644b880a --- /dev/null +++ b/1351-count-negative-numbers-in-a-sorted-matrix/README.md @@ -0,0 +1,28 @@ +

1351. Count Negative Numbers in a Sorted Matrix

Easy


Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.

+ +

 

+

Example 1:

+ +
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
+Output: 8
+Explanation: There are 8 negatives number in the matrix.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 100
  • +
  • -100 <= grid[i][j] <= 100
  • +
+ +

 

+Follow up: Could you find an O(n + m) solution?
\ No newline at end of file From 4ff2e0686ab289b13c0152f6005e861430045776 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:41:21 +0530 Subject: [PATCH 1072/3167] Time: 15 ms (82.04%), Space: 10.4 MB (31.99%) - LeetHub --- ...nt-negative-numbers-in-a-sorted-matrix.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp diff --git a/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp b/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp new file mode 100644 index 00000000..591aaed2 --- /dev/null +++ b/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + int countNegatives(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + int end = m-1, cnt = 0; + + for(int i = 0; i < n; ++i) + { + if(grid[i][0] < 0) + { + cnt += m; + } + else if(grid[i][m-1] >= 0) + { + continue; + } + else + { + int low = 0, high = end; + + while(low <= high) + { + int mid = low + (high - low)/2; + + if(grid[i][mid] >= 0) + low = mid+1; + else + high = mid-1; + } + + cnt += m - low; + end = low; + } + } + + return cnt; + + } + +}; \ No newline at end of file From e3cf6af09f081ab86d738160d040320ca0dfd21d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:47:37 +0530 Subject: [PATCH 1073/3167] Attach NOTES - LeetHub --- 1351-count-negative-numbers-in-a-sorted-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1351-count-negative-numbers-in-a-sorted-matrix/NOTES.md diff --git a/1351-count-negative-numbers-in-a-sorted-matrix/NOTES.md b/1351-count-negative-numbers-in-a-sorted-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1351-count-negative-numbers-in-a-sorted-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 78bfd72a4d99f19bdd44307b7a75a76a897daf8c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:47:40 +0530 Subject: [PATCH 1074/3167] Time: 19 ms (43.59%), Space: 10.3 MB (99.08%) - LeetHub --- ...nt-negative-numbers-in-a-sorted-matrix.cpp | 35 +++++-------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp b/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp index 591aaed2..5629b207 100644 --- a/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp +++ b/1351-count-negative-numbers-in-a-sorted-matrix/1351-count-negative-numbers-in-a-sorted-matrix.cpp @@ -4,39 +4,22 @@ class Solution { int n = grid.size(), m = grid[0].size(); - int end = m-1, cnt = 0; + int i = n-1, j = 0; - for(int i = 0; i < n; ++i) + int negCnt = 0; + + while(i >= 0 and j < m) { - if(grid[i][0] < 0) - { - cnt += m; - } - else if(grid[i][m-1] >= 0) - { - continue; - } + if(grid[i][j] >= 0) + ++j; else { - int low = 0, high = end; - - while(low <= high) - { - int mid = low + (high - low)/2; - - if(grid[i][mid] >= 0) - low = mid+1; - else - high = mid-1; - } - - cnt += m - low; - end = low; + negCnt += m - j; + --i; } } - return cnt; + return negCnt; } - }; \ No newline at end of file From 7228efd6175e1d5784bbe51b452981a5bede476c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Jun 2023 07:39:39 +0530 Subject: [PATCH 1075/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0744-find-smallest-letter-greater-than-target/README.md diff --git a/0744-find-smallest-letter-greater-than-target/README.md b/0744-find-smallest-letter-greater-than-target/README.md new file mode 100644 index 00000000..e84b96c5 --- /dev/null +++ b/0744-find-smallest-letter-greater-than-target/README.md @@ -0,0 +1,37 @@ +

744. Find Smallest Letter Greater Than Target

Easy


You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.

+ +

Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.

+ +

 

+

Example 1:

+ +
Input: letters = ["c","f","j"], target = "a"
+Output: "c"
+Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
+
+ +

Example 2:

+ +
Input: letters = ["c","f","j"], target = "c"
+Output: "f"
+Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
+
+ +

Example 3:

+ +
Input: letters = ["x","x","y","y"], target = "z"
+Output: "x"
+Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= letters.length <= 104
  • +
  • letters[i] is a lowercase English letter.
  • +
  • letters is sorted in non-decreasing order.
  • +
  • letters contains at least two different characters.
  • +
  • target is a lowercase English letter.
  • +
+
\ No newline at end of file From a39d390697672644db92cb7d3539b7ae7ca462ae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Jun 2023 10:05:29 +0530 Subject: [PATCH 1076/3167] Create README - LeetHub --- 0031-next-permutation/README.md | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0031-next-permutation/README.md diff --git a/0031-next-permutation/README.md b/0031-next-permutation/README.md new file mode 100644 index 00000000..5d498653 --- /dev/null +++ b/0031-next-permutation/README.md @@ -0,0 +1,45 @@ +

31. Next Permutation

Medium


A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

+ +
    +
  • For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].
  • +
+ +

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

+ +
    +
  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • +
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • +
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
  • +
+ +

Given an array of integers nums, find the next permutation of nums.

+ +

The replacement must be in place and use only constant extra memory.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 100
  • +
+
\ No newline at end of file From 0275e1dd9e351ad1c3b3ca704344b6796d9b165d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Jun 2023 10:05:29 +0530 Subject: [PATCH 1077/3167] Attach NOTES - LeetHub --- 0031-next-permutation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0031-next-permutation/NOTES.md diff --git a/0031-next-permutation/NOTES.md b/0031-next-permutation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0031-next-permutation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 20ba859f3691ab8a058d37a3fc88753d1d9ee1f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Jun 2023 10:05:32 +0530 Subject: [PATCH 1078/3167] Time: 8 ms (28.65%), Space: 12.2 MB (23.74%) - LeetHub --- .../0031-next-permutation.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0031-next-permutation/0031-next-permutation.cpp diff --git a/0031-next-permutation/0031-next-permutation.cpp b/0031-next-permutation/0031-next-permutation.cpp new file mode 100644 index 00000000..3551b1e4 --- /dev/null +++ b/0031-next-permutation/0031-next-permutation.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + void nextPermutation(vector& nums) { + + int n = nums.size(); + + int idx = -1; + + for(int i = n-2; i >= 0; --i) + { + if(nums[i] < nums[i+1]) + { + idx = i; + break; + } + } + + if(idx == -1) + { + reverse(nums.begin(), nums.end()); + return; + } + + for(int i = n-1; i > idx; --i) + { + if(nums[i] > nums[idx]) + { + swap(nums[i], nums[idx]); + break; + } + } + + reverse(nums.begin() + idx + 1, nums.end()); + } +}; \ No newline at end of file From b2ad41863ebd2623a3b6d8b2d84e648b330e712a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Jun 2023 22:45:52 +0530 Subject: [PATCH 1079/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1802-maximum-value-at-a-given-index-in-a-bounded-array/README.md diff --git a/1802-maximum-value-at-a-given-index-in-a-bounded-array/README.md b/1802-maximum-value-at-a-given-index-in-a-bounded-array/README.md new file mode 100644 index 00000000..9cfd57cd --- /dev/null +++ b/1802-maximum-value-at-a-given-index-in-a-bounded-array/README.md @@ -0,0 +1,37 @@ +

1802. Maximum Value at a Given Index in a Bounded Array

Medium


You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:

+ +
    +
  • nums.length == n
  • +
  • nums[i] is a positive integer where 0 <= i < n.
  • +
  • abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1.
  • +
  • The sum of all the elements of nums does not exceed maxSum.
  • +
  • nums[index] is maximized.
  • +
+ +

Return nums[index] of the constructed array.

+ +

Note that abs(x) equals x if x >= 0, and -x otherwise.

+ +

 

+

Example 1:

+ +
Input: n = 4, index = 2,  maxSum = 6
+Output: 2
+Explanation: nums = [1,2,2,1] is one array that satisfies all the conditions.
+There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
+
+ +

Example 2:

+ +
Input: n = 6, index = 1,  maxSum = 10
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= maxSum <= 109
  • +
  • 0 <= index < n
  • +
+
\ No newline at end of file From 71357927114e51398008f3e56d09afc81d7d8f47 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Jun 2023 22:45:53 +0530 Subject: [PATCH 1080/3167] Attach NOTES - LeetHub --- 1802-maximum-value-at-a-given-index-in-a-bounded-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1802-maximum-value-at-a-given-index-in-a-bounded-array/NOTES.md diff --git a/1802-maximum-value-at-a-given-index-in-a-bounded-array/NOTES.md b/1802-maximum-value-at-a-given-index-in-a-bounded-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1802-maximum-value-at-a-given-index-in-a-bounded-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e9ea41a4c01bed66a9881d2867f20e59ec200862 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Jun 2023 22:45:56 +0530 Subject: [PATCH 1081/3167] Time: 4 ms (41.31%), Space: 5.9 MB (92.49%) - LeetHub --- ...ue-at-a-given-index-in-a-bounded-array.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1802-maximum-value-at-a-given-index-in-a-bounded-array/1802-maximum-value-at-a-given-index-in-a-bounded-array.cpp diff --git a/1802-maximum-value-at-a-given-index-in-a-bounded-array/1802-maximum-value-at-a-given-index-in-a-bounded-array.cpp b/1802-maximum-value-at-a-given-index-in-a-bounded-array/1802-maximum-value-at-a-given-index-in-a-bounded-array.cpp new file mode 100644 index 00000000..8e2264fb --- /dev/null +++ b/1802-maximum-value-at-a-given-index-in-a-bounded-array/1802-maximum-value-at-a-given-index-in-a-bounded-array.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + long long c(long long n){ + return (n*(n+1))/2; + } + int maxValue(int n, int index, int maxSum) { + long long l=1,r=maxSum; + long long ans=1; + while(l<=r){ + long long mid=(l+r)/2; + long long left = c(mid-1); + if(index>mid-1){ + left += (index-mid+1); + } + else left -= c(mid-1-index); + long long right = c(mid-1) ; + if((n-1-index)>(mid-1)){ + right += (n-1-index-(mid-1)); + } + else right -= c(mid-1-(n-1-index)); + if(left+right<=(maxSum-mid)){ + ans=mid; + l=mid+1; + } + else r=mid-1; + } + return ans; + } +}; \ No newline at end of file From 8bddfaac0f70fe4dd1081407ff2382f63987e9a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Jun 2023 22:44:17 +0530 Subject: [PATCH 1082/3167] Create README - LeetHub --- 1146-snapshot-array/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1146-snapshot-array/README.md diff --git a/1146-snapshot-array/README.md b/1146-snapshot-array/README.md new file mode 100644 index 00000000..b3ccb749 --- /dev/null +++ b/1146-snapshot-array/README.md @@ -0,0 +1,33 @@ +

1146. Snapshot Array

Medium


Implement a SnapshotArray that supports the following interface:

+ +
    +
  • SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.
  • +
  • void set(index, val) sets the element at the given index to be equal to val.
  • +
  • int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
  • +
  • int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id
  • +
+ +

 

+

Example 1:

+ +
Input: ["SnapshotArray","set","snap","set","get"]
+[[3],[0,5],[],[0,6],[0,0]]
+Output: [null,null,0,null,5]
+Explanation: 
+SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
+snapshotArr.set(0,5);  // Set array[0] = 5
+snapshotArr.snap();  // Take a snapshot, return snap_id = 0
+snapshotArr.set(0,6);
+snapshotArr.get(0,0);  // Get the value of array[0] with snap_id = 0, return 5
+ +

 

+

Constraints:

+ +
    +
  • 1 <= length <= 5 * 104
  • +
  • 0 <= index < length
  • +
  • 0 <= val <= 109
  • +
  • 0 <= snap_id < (the total number of times we call snap())
  • +
  • At most 5 * 104 calls will be made to set, snap, and get.
  • +
+
\ No newline at end of file From 313d5fdba25fc22f3412c15712f36020289003b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Jun 2023 22:44:21 +0530 Subject: [PATCH 1083/3167] Time: 593 ms (10.28%), Space: 185.3 MB (5.07%) - LeetHub --- 1146-snapshot-array/1146-snapshot-array.cpp | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1146-snapshot-array/1146-snapshot-array.cpp diff --git a/1146-snapshot-array/1146-snapshot-array.cpp b/1146-snapshot-array/1146-snapshot-array.cpp new file mode 100644 index 00000000..b8c78c47 --- /dev/null +++ b/1146-snapshot-array/1146-snapshot-array.cpp @@ -0,0 +1,40 @@ +class SnapshotArray { +public: + + map> mp; + int cnt = 0; + + SnapshotArray(int length) { + for(int i = 0; i < length; ++i) + { + map mp2; + mp2[0] = 0; + mp[i] = mp2; + } + } + + void set(int index, int val) { + mp[index][cnt] = val; + } + + int snap() { + ++cnt; + + return cnt-1; + } + + int get(int index, int snap_id) { + + auto here = mp[index].upper_bound(snap_id); + --here; + return here->second; + } +}; + +/** + * Your SnapshotArray object will be instantiated and called as such: + * SnapshotArray* obj = new SnapshotArray(length); + * obj->set(index,val); + * int param_2 = obj->snap(); + * int param_3 = obj->get(index,snap_id); + */ \ No newline at end of file From 0b053c0c35ae5217439183ce778b1b4c0b097a2b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Jun 2023 18:32:03 +0530 Subject: [PATCH 1084/3167] Create README - LeetHub --- 0228-summary-ranges/README.md | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0228-summary-ranges/README.md diff --git a/0228-summary-ranges/README.md b/0228-summary-ranges/README.md new file mode 100644 index 00000000..4200ad9f --- /dev/null +++ b/0228-summary-ranges/README.md @@ -0,0 +1,45 @@ +

228. Summary Ranges

Easy


You are given a sorted unique integer array nums.

+ +

A range [a,b] is the set of all integers from a to b (inclusive).

+ +

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

+ +

Each range [a,b] in the list should be output as:

+ +
    +
  • "a->b" if a != b
  • +
  • "a" if a == b
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [0,1,2,4,5,7]
+Output: ["0->2","4->5","7"]
+Explanation: The ranges are:
+[0,2] --> "0->2"
+[4,5] --> "4->5"
+[7,7] --> "7"
+
+ +

Example 2:

+ +
Input: nums = [0,2,3,4,6,8,9]
+Output: ["0","2->4","6","8->9"]
+Explanation: The ranges are:
+[0,0] --> "0"
+[2,4] --> "2->4"
+[6,6] --> "6"
+[8,9] --> "8->9"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 20
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • All the values of nums are unique.
  • +
  • nums is sorted in ascending order.
  • +
+
\ No newline at end of file From 1e22e0932ae579580c4de0c8c78bc214f106ab2f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Jun 2023 18:32:07 +0530 Subject: [PATCH 1085/3167] Time: 4 ms (34.63%), Space: 6.9 MB (57.22%) - LeetHub --- 0228-summary-ranges/0228-summary-ranges.cpp | 57 +++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 0228-summary-ranges/0228-summary-ranges.cpp diff --git a/0228-summary-ranges/0228-summary-ranges.cpp b/0228-summary-ranges/0228-summary-ranges.cpp new file mode 100644 index 00000000..f7704360 --- /dev/null +++ b/0228-summary-ranges/0228-summary-ranges.cpp @@ -0,0 +1,57 @@ +class Solution { +public: + vector summaryRanges(vector& nums) { + + int n = nums.size(); + + vector ans; + + if(n == 0) + return ans; + + int curr = nums[0], track = nums[0], prev = nums[0]; + + for(int i = 1; i < n; ++i) + { + if(nums[i] == track + 1) + { + ++track; + } + else + { + if(curr == prev) + ans.push_back(to_string(curr)); + else + { + string str = to_string(curr); + + str += "->" + to_string(prev); + + ans.push_back(str); + } + + curr = nums[i]; + + track = nums[i]; + + + } + + prev = nums[i]; + } + + if(curr == prev) + ans.push_back(to_string(curr)); + else + { + string str = to_string(curr); + + str += "->" + to_string(prev); + + ans.push_back(str); + } + + return ans; + + } +}; \ No newline at end of file From 8fb858d1ff9a897e6357ef1d216df8b85dac043b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Jun 2023 18:35:19 +0530 Subject: [PATCH 1086/3167] Attach NOTES - LeetHub --- 0228-summary-ranges/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0228-summary-ranges/NOTES.md diff --git a/0228-summary-ranges/NOTES.md b/0228-summary-ranges/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0228-summary-ranges/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 403c91d86068b063ad79f411855973311ccac603 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Jun 2023 18:35:22 +0530 Subject: [PATCH 1087/3167] Time: 4 ms (34.63%), Space: 6.9 MB (57.22%) - LeetHub From cfa47285d7a2ef873a1f484a532707e973c6809a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:02:16 +0530 Subject: [PATCH 1088/3167] Create README - LeetHub --- 2352-equal-row-and-column-pairs/README.md | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2352-equal-row-and-column-pairs/README.md diff --git a/2352-equal-row-and-column-pairs/README.md b/2352-equal-row-and-column-pairs/README.md new file mode 100644 index 00000000..54c26f69 --- /dev/null +++ b/2352-equal-row-and-column-pairs/README.md @@ -0,0 +1,32 @@ +

2352. Equal Row and Column Pairs

Medium


Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal.

+ +

A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).

+ +

 

+

Example 1:

+ +
Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
+Output: 1
+Explanation: There is 1 equal row and column pair:
+- (Row 2, Column 1): [2,7,7]
+
+ +

Example 2:

+ +
Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
+Output: 3
+Explanation: There are 3 equal row and column pairs:
+- (Row 0, Column 0): [3,1,2,2]
+- (Row 2, Column 2): [2,4,2,2]
+- (Row 3, Column 2): [2,4,2,2]
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 1 <= n <= 200
  • +
  • 1 <= grid[i][j] <= 105
  • +
+
\ No newline at end of file From 8ceff5314dea33287fb937f6ea5cd30e8d1ef8f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:02:19 +0530 Subject: [PATCH 1089/3167] Time: 105 ms (73.58%), Space: 28.3 MB (70.87%) - LeetHub --- .../2352-equal-row-and-column-pairs.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2352-equal-row-and-column-pairs/2352-equal-row-and-column-pairs.cpp diff --git a/2352-equal-row-and-column-pairs/2352-equal-row-and-column-pairs.cpp b/2352-equal-row-and-column-pairs/2352-equal-row-and-column-pairs.cpp new file mode 100644 index 00000000..5af26602 --- /dev/null +++ b/2352-equal-row-and-column-pairs/2352-equal-row-and-column-pairs.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int equalPairs(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + map, int> mp; + + vector> mat = grid; + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(i < j) + swap(grid[i][j], grid[j][i]); + } + ++mp[grid[i]]; + } + + for(int i = 0; i < n; ++i) + { + if(mp[mat[i]] > 0) + ans += mp[mat[i]]; + } + + return ans; + } +}; \ No newline at end of file From 74e9b6d30ba964304ad258f05654da6c6e734563 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:04:04 +0530 Subject: [PATCH 1090/3167] Attach NOTES - LeetHub --- 2352-equal-row-and-column-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2352-equal-row-and-column-pairs/NOTES.md diff --git a/2352-equal-row-and-column-pairs/NOTES.md b/2352-equal-row-and-column-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2352-equal-row-and-column-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f5e86d813c2461096fa61899b40752ce3981ab9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:04:08 +0530 Subject: [PATCH 1091/3167] Time: 105 ms (73.58%), Space: 28.3 MB (70.87%) - LeetHub From 193d3b5ef010a5b482518a588296516cd94d3bfa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:05:14 +0530 Subject: [PATCH 1092/3167] Attach NOTES - LeetHub From 6fc98bed759360b4e9b434c49eb651fdd463582c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 09:05:17 +0530 Subject: [PATCH 1093/3167] Time: 105 ms (73.58%), Space: 28.3 MB (70.87%) - LeetHub From d3998023917aa4e1c57b37ca7c1a472cc5c7eecd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:45:36 +0530 Subject: [PATCH 1094/3167] Create README - LeetHub --- 0025-reverse-nodes-in-k-group/README.md | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0025-reverse-nodes-in-k-group/README.md diff --git a/0025-reverse-nodes-in-k-group/README.md b/0025-reverse-nodes-in-k-group/README.md new file mode 100644 index 00000000..2c7afeca --- /dev/null +++ b/0025-reverse-nodes-in-k-group/README.md @@ -0,0 +1,31 @@ +

25. Reverse Nodes in k-Group

Hard


Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.

+ +

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.

+ +

You may not alter the values in the list's nodes, only nodes themselves may be changed.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is n.
  • +
  • 1 <= k <= n <= 5000
  • +
  • 0 <= Node.val <= 1000
  • +
+ +

 

+

Follow-up: Can you solve the problem in O(1) extra memory space?

+
\ No newline at end of file From 4b628749533a5c0b557091308be5450a253d8e2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:45:40 +0530 Subject: [PATCH 1095/3167] Time: 20 ms (42.39%), Space: 11.4 MB (52.77%) - LeetHub --- .../0025-reverse-nodes-in-k-group.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0025-reverse-nodes-in-k-group/0025-reverse-nodes-in-k-group.cpp diff --git a/0025-reverse-nodes-in-k-group/0025-reverse-nodes-in-k-group.cpp b/0025-reverse-nodes-in-k-group/0025-reverse-nodes-in-k-group.cpp new file mode 100644 index 00000000..403c05e4 --- /dev/null +++ b/0025-reverse-nodes-in-k-group/0025-reverse-nodes-in-k-group.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: + ListNode* reverseKGroup(ListNode* head, int k) { + + if(!head or k == 1) + return head; + + ListNode* dummy = new ListNode(0); + + dummy->next = head; + + ListNode* prev = dummy, *nex = dummy, *curr = dummy; + + int cnt = 0; + + while(head) + { + ++cnt; + head = head->next; + } + + while(cnt >= k) + { + curr = prev->next; + nex = curr->next; + + for(int i = 1; i < k; ++i) + { + curr->next = nex->next; + nex->next = prev->next; + prev->next = nex; + nex = curr->next; + } + prev = curr; + cnt -= k; + } + + return dummy->next; + } +}; \ No newline at end of file From 02c73c4da7bf0db042dacfb961905b4d366b7dca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:47:17 +0530 Subject: [PATCH 1096/3167] Attach NOTES - LeetHub --- 0025-reverse-nodes-in-k-group/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0025-reverse-nodes-in-k-group/NOTES.md diff --git a/0025-reverse-nodes-in-k-group/NOTES.md b/0025-reverse-nodes-in-k-group/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0025-reverse-nodes-in-k-group/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e630814c14ac6482ba72aa210c8a917c3445c147 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:47:20 +0530 Subject: [PATCH 1097/3167] Time: 20 ms (42.39%), Space: 11.4 MB (52.77%) - LeetHub From f460c9444374005bdb69cf55286a68065454e9d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Jun 2023 21:37:03 +0530 Subject: [PATCH 1098/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0530-minimum-absolute-difference-in-bst/README.md diff --git a/0530-minimum-absolute-difference-in-bst/README.md b/0530-minimum-absolute-difference-in-bst/README.md new file mode 100644 index 00000000..652088ce --- /dev/null +++ b/0530-minimum-absolute-difference-in-bst/README.md @@ -0,0 +1,26 @@ +

530. Minimum Absolute Difference in BST

Easy


Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: root = [1,0,48,null,null,12,49]
+Output: 1
+
+ +

 

+

Constraints:

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

 

+

Note: This question is the same as 783: https://leetcode.com/problems/minimum-distance-between-bst-nodes/

+
\ No newline at end of file From 4a27d3fa755e3a75f8df7d15ba11e7244334585f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Jun 2023 21:37:07 +0530 Subject: [PATCH 1099/3167] Time: 15 ms (87.59%), Space: 25.2 MB (76.48%) - LeetHub --- ...530-minimum-absolute-difference-in-bst.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0530-minimum-absolute-difference-in-bst/0530-minimum-absolute-difference-in-bst.cpp diff --git a/0530-minimum-absolute-difference-in-bst/0530-minimum-absolute-difference-in-bst.cpp b/0530-minimum-absolute-difference-in-bst/0530-minimum-absolute-difference-in-bst.cpp new file mode 100644 index 00000000..7a9d4d49 --- /dev/null +++ b/0530-minimum-absolute-difference-in-bst/0530-minimum-absolute-difference-in-bst.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 { + +private: + int helper(TreeNode* root, int& mini, int& val) + { + if(root->left) + helper(root->left,mini, val); + + if(val >= 0) + mini = min(mini, root->val - val); + + val = root->val; + + if(root->right) + helper(root->right, mini , val); + + return mini; + } + +public: + int getMinimumDifference(TreeNode* root) { + + int mini = INT_MAX,val = -1; + + return helper(root, mini, val); + + } +}; \ No newline at end of file From 06fea3e4a433f597440a3bf329e421bbe51f7261 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Jun 2023 08:41:36 +0530 Subject: [PATCH 1100/3167] Attach NOTES - LeetHub From 415a33a2d5f357f1a5f585bc655761e92ab0b81f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Jun 2023 08:41:39 +0530 Subject: [PATCH 1101/3167] Time: 31 ms (64.89%), Space: 16.1 MB (47.00%) - LeetHub --- .../0005-longest-palindromic-substring.cpp | 63 ++++++++++--------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp b/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp index d32c36fc..c47bc3c6 100644 --- a/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp +++ b/0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp @@ -4,44 +4,47 @@ class Solution { int n = s.size(); - vector> dp(n+1, vector(n+1, 0)); + int maxi = 0; string ans; - int maxLength = 0; - - for(int diff = 0; diff < n; ++diff) - { - for(int i = 0, j = i+diff; i < n and j < n; ++i, ++j) - { - if(i == j) - dp[i][j] = 1; - else if(diff == 1) + function helper = [=](int start, int end){ + + while( start >= 0 and end < n) + { + if(s[start] == s[end]) { - if(s[i] == s[j]) - { - dp[i][j] = 2; - } + --start, ++end; } - else - { - if(s[i] == s[j]) - { - if(dp[i+1][j-1]) - { - dp[i][j] = dp[i+1][j-1] + 2; - } - } - } - - int len = j - i + 1; - - if(dp[i][j] > maxLength) - ans = s.substr(i,len); - + else + break; + } + + return s.substr(start+1, end-start-1); + + }; + + for(int i = 0; i < n; ++i) + { + string oddCase = helper(i,i); + + if(oddCase.length() > maxi) + { + maxi = oddCase.length(); + ans = oddCase; + } + + string evenCase = helper(i,i+1); + + if(evenCase.length() > maxi) + { + maxi = evenCase.length(); + ans = evenCase; } } + return ans; + } }; \ No newline at end of file From efe9db727c00d1441f8a3b5919051e0c8e74769d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Jun 2023 08:49:16 +0530 Subject: [PATCH 1102/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1161-maximum-level-sum-of-a-binary-tree/README.md diff --git a/1161-maximum-level-sum-of-a-binary-tree/README.md b/1161-maximum-level-sum-of-a-binary-tree/README.md new file mode 100644 index 00000000..a172e9a4 --- /dev/null +++ b/1161-maximum-level-sum-of-a-binary-tree/README.md @@ -0,0 +1,30 @@ +

1161. Maximum Level Sum of a Binary Tree

Medium


Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.

+ +

Return the smallest level x such that the sum of all the values of nodes at level x is maximal.

+ +

 

+

Example 1:

+ +
Input: root = [1,7,0,7,-8,null,null]
+Output: 2
+Explanation: 
+Level 1 sum = 1.
+Level 2 sum = 7 + 0 = 7.
+Level 3 sum = 7 + -8 = -1.
+So we return the level with the maximum sum which is level 2.
+
+ +

Example 2:

+ +
Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
+Output: 2
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -105 <= Node.val <= 105
  • +
+
\ No newline at end of file From 0c9524fc2f2b99bf6def342abea410c79cb12f20 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Jun 2023 08:49:20 +0530 Subject: [PATCH 1103/3167] Time: 194 ms (84.85%), Space: 107.2 MB (59.25%) - LeetHub --- ...161-maximum-level-sum-of-a-binary-tree.cpp | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 1161-maximum-level-sum-of-a-binary-tree/1161-maximum-level-sum-of-a-binary-tree.cpp diff --git a/1161-maximum-level-sum-of-a-binary-tree/1161-maximum-level-sum-of-a-binary-tree.cpp b/1161-maximum-level-sum-of-a-binary-tree/1161-maximum-level-sum-of-a-binary-tree.cpp new file mode 100644 index 00000000..d85f70ba --- /dev/null +++ b/1161-maximum-level-sum-of-a-binary-tree/1161-maximum-level-sum-of-a-binary-tree.cpp @@ -0,0 +1,54 @@ +/** + * 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 maxLevelSum(TreeNode* root) { + + queue q; + + int lvl = 0, ans = 0, maxLvlSum = -1e9; + + q.push(root); + + while(!q.empty()) + { + lvl += 1; + + int size = q.size(); + + int sum = 0; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + + q.pop(); + + if(curr->left) + q.push(curr->left); + + if(curr->right) + q.push(curr->right); + + sum += curr->val; + } + + if(sum > maxLvlSum) + { + ans = lvl; + maxLvlSum = sum; + } + } + + return ans; + } +}; \ No newline at end of file From 82341ac0fe2ac7f5f416e65951088502f2223aa9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Jun 2023 22:27:15 +0530 Subject: [PATCH 1104/3167] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1569-number-of-ways-to-reorder-array-to-get-same-bst/README.md diff --git a/1569-number-of-ways-to-reorder-array-to-get-same-bst/README.md b/1569-number-of-ways-to-reorder-array-to-get-same-bst/README.md new file mode 100644 index 00000000..93cf7d10 --- /dev/null +++ b/1569-number-of-ways-to-reorder-array-to-get-same-bst/README.md @@ -0,0 +1,46 @@ +

1569. Number of Ways to Reorder Array to Get Same BST

Hard


Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.

+ +
    +
  • For example, given nums = [2,1,3], we will have 2 as the root, 1 as a left child, and 3 as a right child. The array [2,3,1] also yields the same BST but [3,2,1] yields a different BST.
  • +
+ +

Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.

+ +

Since the answer may be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: nums = [2,1,3]
+Output: 1
+Explanation: We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST.
+
+ +

Example 2:

+ +
Input: nums = [3,4,5,1,2]
+Output: 5
+Explanation: The following 5 arrays will yield the same BST: 
+[3,1,2,4,5]
+[3,1,4,2,5]
+[3,1,4,5,2]
+[3,4,1,2,5]
+[3,4,1,5,2]
+
+ +

Example 3:

+ +
Input: nums = [1,2,3]
+Output: 0
+Explanation: There are no other orderings of nums that will yield the same BST.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= nums.length
  • +
  • All integers in nums are distinct.
  • +
+
\ No newline at end of file From a7f12b697ded207f3b82d4efd920d001055566f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Jun 2023 22:27:16 +0530 Subject: [PATCH 1105/3167] Attach NOTES - LeetHub --- 1569-number-of-ways-to-reorder-array-to-get-same-bst/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1569-number-of-ways-to-reorder-array-to-get-same-bst/NOTES.md diff --git a/1569-number-of-ways-to-reorder-array-to-get-same-bst/NOTES.md b/1569-number-of-ways-to-reorder-array-to-get-same-bst/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1569-number-of-ways-to-reorder-array-to-get-same-bst/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6fcdfeb2d8a9d1b8b63f78852dcd9f7fe6d25c4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Jun 2023 22:27:19 +0530 Subject: [PATCH 1106/3167] Time: 381 ms (38.41%), Space: 172 MB (17.22%) - LeetHub --- ...-ways-to-reorder-array-to-get-same-bst.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1569-number-of-ways-to-reorder-array-to-get-same-bst/1569-number-of-ways-to-reorder-array-to-get-same-bst.cpp diff --git a/1569-number-of-ways-to-reorder-array-to-get-same-bst/1569-number-of-ways-to-reorder-array-to-get-same-bst.cpp b/1569-number-of-ways-to-reorder-array-to-get-same-bst/1569-number-of-ways-to-reorder-array-to-get-same-bst.cpp new file mode 100644 index 00000000..ac145f0a --- /dev/null +++ b/1569-number-of-ways-to-reorder-array-to-get-same-bst/1569-number-of-ways-to-reorder-array-to-get-same-bst.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector < vector < long long > > pascal; + int MOD = 1e9 + 7; + + int numOfWays(vector& nums) { + int N = nums.size(); + pascal = vector < vector < long long > > (N + 1); + for (int i = 0; i < N + 1; i ++){ + pascal[i] = vector < long long > (i + 1, 1); + for (int j = 1; j < i; j ++){ + pascal[i][j] = (pascal[i - 1][j] + pascal[i - 1][j - 1])%MOD; + } + } + return dfs(nums) - 1; + } + + int dfs(vector < int > nums){ + if (nums.size() <= 2) + return 1; + vector < int > left, right; + for (int i = 1; i < int(nums.size()); i ++){ + if (nums[i] < nums[0]) + left.push_back(nums[i]); + else + right.push_back(nums[i]); + } + return (((dfs(left) * pascal[nums.size() - 1][left.size()]) % MOD) * dfs(right))%MOD; + } +}; \ No newline at end of file From 4810bc018b6dd00754b6f8ac60a8960095c4c199 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 00:15:02 +0530 Subject: [PATCH 1107/3167] Create README - LeetHub --- 1187-make-array-strictly-increasing/README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1187-make-array-strictly-increasing/README.md diff --git a/1187-make-array-strictly-increasing/README.md b/1187-make-array-strictly-increasing/README.md new file mode 100644 index 00000000..de398023 --- /dev/null +++ b/1187-make-array-strictly-increasing/README.md @@ -0,0 +1,36 @@ +

1187. Make Array Strictly Increasing

Hard


Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.

+ +

In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].

+ +

If there is no way to make arr1 strictly increasing, return -1.

+ +

 

+

Example 1:

+ +
Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
+Output: 1
+Explanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].
+
+ +

Example 2:

+ +
Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
+Output: 2
+Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].
+
+ +

Example 3:

+ +
Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
+Output: -1
+Explanation: You can't make arr1 strictly increasing.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr1.length, arr2.length <= 2000
  • +
  • 0 <= arr1[i], arr2[i] <= 10^9
  • +
+ +

 

\ No newline at end of file From d013706a83de8e18fedafb2933e20ff25e73b3bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 00:15:05 +0530 Subject: [PATCH 1108/3167] Time: 1103 ms (6.98%), Space: 78.1 MB (15.12%) - LeetHub --- .../1187-make-array-strictly-increasing.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1187-make-array-strictly-increasing/1187-make-array-strictly-increasing.cpp diff --git a/1187-make-array-strictly-increasing/1187-make-array-strictly-increasing.cpp b/1187-make-array-strictly-increasing/1187-make-array-strictly-increasing.cpp new file mode 100644 index 00000000..c6944efa --- /dev/null +++ b/1187-make-array-strictly-increasing/1187-make-array-strictly-increasing.cpp @@ -0,0 +1,46 @@ +#define ll long long int + +class Solution { + +private: + ll helper(int idx, int prev, int n, vector& arr1, vector& arr2, map,int>& dp) + { + if(idx == n) + return 0; + + if(dp.find({idx, prev}) != dp.end()) + return dp[{idx, prev}]; + + ll ans = INT_MAX; + + ll ind = upper_bound(arr2.begin(), arr2.end(), prev) - arr2.begin(); + + if(arr1[idx] > prev) + { + ans = min(ans, helper(idx+1, arr1[idx], n, arr1, arr2, dp)); + } + + if(ind < arr2.size()) + { + ans = min(ans, 1 + helper(idx+1, arr2[ind], n, arr1, arr2, dp)); + } + + + return dp[{idx, prev}] = ans; + } + +public: + int makeArrayIncreasing(vector& arr1, vector& arr2) { + + sort(arr2.begin(), arr2.end()); + + map, int> dp; + + int n = arr1.size(); + + int ans = helper(0, INT_MIN,n, arr1, arr2, dp); + + return (ans == INT_MAX ? -1 : ans); + + } +}; \ No newline at end of file From 10587ad69e209ff256d31ef6fa7358a9c4c47b6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:35:48 +0530 Subject: [PATCH 1109/3167] Attach NOTES - LeetHub --- 2742-painting-the-walls/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2742-painting-the-walls/NOTES.md diff --git a/2742-painting-the-walls/NOTES.md b/2742-painting-the-walls/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2742-painting-the-walls/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bd79776b8a67730441ae7d966c454a3f87142fc1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:35:48 +0530 Subject: [PATCH 1110/3167] Create README - LeetHub --- 2742-painting-the-walls/README.md | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2742-painting-the-walls/README.md diff --git a/2742-painting-the-walls/README.md b/2742-painting-the-walls/README.md new file mode 100644 index 00000000..cf18a4cb --- /dev/null +++ b/2742-painting-the-walls/README.md @@ -0,0 +1,34 @@ +

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 From 703dce5f09d11816a118a9601cf62f57bf8ec994 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:39:16 +0530 Subject: [PATCH 1111/3167] Create README - LeetHub From 8f6d47d7c515ac01389be45dae0259a5927cb8c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:39:17 +0530 Subject: [PATCH 1112/3167] Attach NOTES - LeetHub From d8ed2e047dad51a5b3c0b10b4190b61a605fc953 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:39:21 +0530 Subject: [PATCH 1113/3167] Time: 1365 ms (25.00%), Space: 350.7 MB (12.50%) - LeetHub --- .../2742-painting-the-walls.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2742-painting-the-walls/2742-painting-the-walls.cpp diff --git a/2742-painting-the-walls/2742-painting-the-walls.cpp b/2742-painting-the-walls/2742-painting-the-walls.cpp new file mode 100644 index 00000000..7c95a47b --- /dev/null +++ b/2742-painting-the-walls/2742-painting-the-walls.cpp @@ -0,0 +1,30 @@ +class Solution { + +private: + int helper(int idx, int walls, vector& cost, vector& time, vector>& dp) + { + if(walls <= 0) + return 0; + + if(idx >= cost.size()) + return INT_MAX/2; + + if(dp[idx][walls] != -1) + return dp[idx][walls]; + + int notTake = helper(idx+1, walls, cost, time, dp); + int take = cost[idx] + helper(idx+1, walls - time[idx] - 1, cost, time, dp); + + return dp[idx][walls] = min(take, notTake); + } +public: + int paintWalls(vector& cost, vector& time) { + + int n = cost.size(); + + vector> dp(501, vector(501, -1)); + + return helper(0, n, cost, time, dp); + + } +}; \ No newline at end of file From dc7194d6e2df47456bbe6480296403f4a0a7ea14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:44:58 +0530 Subject: [PATCH 1114/3167] Create README - LeetHub --- 2741-special-permutations/README.md | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2741-special-permutations/README.md diff --git a/2741-special-permutations/README.md b/2741-special-permutations/README.md new file mode 100644 index 00000000..cffc2930 --- /dev/null +++ b/2741-special-permutations/README.md @@ -0,0 +1,31 @@ +

2741. Special Permutations

Medium


You are given a 0-indexed integer array nums containing n distinct positive integers. A permutation of nums is called special if:

+ +
    +
  • For all indexes 0 <= i < n - 1, either nums[i] % nums[i+1] == 0 or nums[i+1] % nums[i] == 0.
  • +
+ +

Return the total number of special permutations. As the answer could be large, return it modulo 10+ 7.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,6]
+Output: 2
+Explanation: [3,6,2] and [2,6,3] are the two special permutations of nums.
+
+ +

Example 2:

+ +
Input: nums = [1,4,3]
+Output: 2
+Explanation: [3,1,4] and [4,1,3] are the two special permutations of nums.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 14
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From e53f44482f6ff82c38231040bc61e60f3cf06d0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:44:59 +0530 Subject: [PATCH 1115/3167] Attach NOTES - LeetHub --- 2741-special-permutations/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2741-special-permutations/NOTES.md diff --git a/2741-special-permutations/NOTES.md b/2741-special-permutations/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2741-special-permutations/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b070f18755e42863aaa344cd11508f5b3e690c71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:45:03 +0530 Subject: [PATCH 1116/3167] Time: 431 ms (100.00%), Space: 309.3 MB (12.50%) - LeetHub --- .../2741-special-permutations.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2741-special-permutations/2741-special-permutations.cpp diff --git a/2741-special-permutations/2741-special-permutations.cpp b/2741-special-permutations/2741-special-permutations.cpp new file mode 100644 index 00000000..d236c5bc --- /dev/null +++ b/2741-special-permutations/2741-special-permutations.cpp @@ -0,0 +1,42 @@ +class Solution { + +private: + + int mod = 1e9 + 7; + + int helper(int idx, int prev, int mask, int n, vector& nums, vector>& dp) + { + if(idx == n) + return 1; + + if(dp[prev+1][mask] != -1) + return dp[prev+1][mask]; + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + if(mask & (1 << i)) + continue; + + if(prev == -1 or nums[i] % nums[prev] == 0 or nums[prev] % nums[i] == 0) + { + ans += helper(idx+1, i, mask | (1 << i), n, nums, dp); + ans %= mod; + } + } + + return dp[prev+1][mask] = ans; + } + +public: + int specialPerm(vector& nums) { + + int n = nums.size(); + + vector> dp(n+1, vector(1 << 14 + 1, -1)); + + return helper(0, -1, 0, n, nums, dp); + + } +}; \ No newline at end of file From 760585c9790d0f2006f97560739475f926cce3f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 21:20:42 +0530 Subject: [PATCH 1117/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2328-number-of-increasing-paths-in-a-grid/README.md diff --git a/2328-number-of-increasing-paths-in-a-grid/README.md b/2328-number-of-increasing-paths-in-a-grid/README.md new file mode 100644 index 00000000..39f07a06 --- /dev/null +++ b/2328-number-of-increasing-paths-in-a-grid/README.md @@ -0,0 +1,39 @@ +

2328. Number of Increasing Paths in a Grid

Hard


You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.

+ +

Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7.

+ +

Two paths are considered different if they do not have exactly the same sequence of visited cells.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,1],[3,4]]
+Output: 8
+Explanation: The strictly increasing paths are:
+- Paths with length 1: [1], [1], [3], [4].
+- Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
+- Paths with length 3: [1 -> 3 -> 4].
+The total number of paths is 4 + 3 + 1 = 8.
+
+ +

Example 2:

+ +
Input: grid = [[1],[2]]
+Output: 3
+Explanation: The strictly increasing paths are:
+- Paths with length 1: [1], [2].
+- Paths with length 2: [1 -> 2].
+The total number of paths is 2 + 1 = 3.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 1000
  • +
  • 1 <= m * n <= 105
  • +
  • 1 <= grid[i][j] <= 105
  • +
+
\ No newline at end of file From 7afe311cfa1e7c22ce1256870866b763a672375e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 21:20:46 +0530 Subject: [PATCH 1118/3167] Time: 345 ms (31.41%), Space: 57.5 MB (24.65%) - LeetHub --- ...8-number-of-increasing-paths-in-a-grid.cpp | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 2328-number-of-increasing-paths-in-a-grid/2328-number-of-increasing-paths-in-a-grid.cpp diff --git a/2328-number-of-increasing-paths-in-a-grid/2328-number-of-increasing-paths-in-a-grid.cpp b/2328-number-of-increasing-paths-in-a-grid/2328-number-of-increasing-paths-in-a-grid.cpp new file mode 100644 index 00000000..084282cc --- /dev/null +++ b/2328-number-of-increasing-paths-in-a-grid/2328-number-of-increasing-paths-in-a-grid.cpp @@ -0,0 +1,54 @@ +class Solution { + +private: + int mod = 1e9 + 7; + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1, 0}; + + int helper(int i, int j, int n, int m, vector>& grid, vector>& dp) + { + if(dp[i][j] != -1) + return dp[i][j]; + + int ans = 1; + + for(int k = 0; k < 4; ++k) + { + int x = dx[k] + i; + int y = dy[k] + j; + + if(x >= 0 and y >= 0 and x < n and y < m and grid[x][y] > grid[i][j]) + { + ans += helper(x, y, n, m, grid, dp); + ans %= mod; + } + } + + return dp[i][j] = ans; + + } + +public: + int countPaths(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + vector> dp(n+1, vector(m+1, -1)); + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + cnt += helper(i, j, n, m, grid, dp); + cnt %= mod; + } + } + + return cnt % mod; + + } +}; \ No newline at end of file From 8d91f6725f852cac92654e8c34817faafd27038b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 21:22:33 +0530 Subject: [PATCH 1119/3167] Attach NOTES - LeetHub --- 2328-number-of-increasing-paths-in-a-grid/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2328-number-of-increasing-paths-in-a-grid/NOTES.md diff --git a/2328-number-of-increasing-paths-in-a-grid/NOTES.md b/2328-number-of-increasing-paths-in-a-grid/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2328-number-of-increasing-paths-in-a-grid/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 489cf624af3bec198f22743283a0ca7bfa902250 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 21:23:01 +0530 Subject: [PATCH 1120/3167] Attach NOTES - LeetHub From b5a28d70cbeb4c287c3d4398f40d78761bb65596 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Jun 2023 21:23:04 +0530 Subject: [PATCH 1121/3167] Time: 345 ms (31.41%), Space: 57.5 MB (24.65%) - LeetHub From 8cb3ae1a1cb7b1a0166beaaa052c20424423b75e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:02:55 +0530 Subject: [PATCH 1122/3167] Create README - LeetHub --- 1732-find-the-highest-altitude/README.md | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1732-find-the-highest-altitude/README.md diff --git a/1732-find-the-highest-altitude/README.md b/1732-find-the-highest-altitude/README.md new file mode 100644 index 00000000..663a3de3 --- /dev/null +++ b/1732-find-the-highest-altitude/README.md @@ -0,0 +1,28 @@ +

1732. Find the Highest Altitude

Easy


There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.

+ +

You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point.

+ +

 

+

Example 1:

+ +
Input: gain = [-5,1,5,0,-7]
+Output: 1
+Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
+
+ +

Example 2:

+ +
Input: gain = [-4,-3,-2,-1,4,3,2]
+Output: 0
+Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • n == gain.length
  • +
  • 1 <= n <= 100
  • +
  • -100 <= gain[i] <= 100
  • +
+
\ No newline at end of file From 163554b94c04d54aee32d8adfd8c7f95730e2f1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:02:59 +0530 Subject: [PATCH 1123/3167] Time: 6 ms (28.46%), Space: 8.1 MB (8.24%) - LeetHub --- .../1732-find-the-highest-altitude.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1732-find-the-highest-altitude/1732-find-the-highest-altitude.cpp diff --git a/1732-find-the-highest-altitude/1732-find-the-highest-altitude.cpp b/1732-find-the-highest-altitude/1732-find-the-highest-altitude.cpp new file mode 100644 index 00000000..fbdc0a29 --- /dev/null +++ b/1732-find-the-highest-altitude/1732-find-the-highest-altitude.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int largestAltitude(vector& gain) { + + int sum = 0, ans = 0; + + for(auto itr : gain) + { + sum += itr; + ans = max(ans, sum); + } + + return ans; + + } +}; \ No newline at end of file From 5abdaff83697b081edc9ffc5fd7b62834c4763c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:03:21 +0530 Subject: [PATCH 1124/3167] Attach NOTES - LeetHub --- 1732-find-the-highest-altitude/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1732-find-the-highest-altitude/NOTES.md diff --git a/1732-find-the-highest-altitude/NOTES.md b/1732-find-the-highest-altitude/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1732-find-the-highest-altitude/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 94d6a656dd0247e75fda729d7b06790738a6b914 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:03:24 +0530 Subject: [PATCH 1125/3167] Time: 6 ms (28.46%), Space: 8.1 MB (8.24%) - LeetHub From 8e0f30af5d0dbe3bb136372b9a9209a2932d34d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Jun 2023 23:45:34 +0530 Subject: [PATCH 1126/3167] Create README - LeetHub --- 2090-k-radius-subarray-averages/README.md | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2090-k-radius-subarray-averages/README.md diff --git a/2090-k-radius-subarray-averages/README.md b/2090-k-radius-subarray-averages/README.md new file mode 100644 index 00000000..53f12651 --- /dev/null +++ b/2090-k-radius-subarray-averages/README.md @@ -0,0 +1,52 @@ +

2090. K Radius Subarray Averages

Medium


You are given a 0-indexed array nums of n integers, and an integer k.

+ +

The k-radius average for a subarray of nums centered at some index i with the radius k is the average of all elements in nums between the indices i - k and i + k (inclusive). If there are less than k elements before or after the index i, then the k-radius average is -1.

+ +

Build and return an array avgs of length n where avgs[i] is the k-radius average for the subarray centered at index i.

+ +

The average of x elements is the sum of the x elements divided by x, using integer division. The integer division truncates toward zero, which means losing its fractional part.

+ +
    +
  • For example, the average of four elements 2, 3, 1, and 5 is (2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75, which truncates to 2.
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [7,4,3,9,1,8,5,2,6], k = 3
+Output: [-1,-1,-1,5,4,4,-1,-1,-1]
+Explanation:
+- avg[0], avg[1], and avg[2] are -1 because there are less than k elements before each index.
+- The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37.
+  Using integer division, avg[3] = 37 / 7 = 5.
+- For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4.
+- For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4.
+- avg[6], avg[7], and avg[8] are -1 because there are less than k elements after each index.
+
+ +

Example 2:

+ +
Input: nums = [100000], k = 0
+Output: [100000]
+Explanation:
+- The sum of the subarray centered at index 0 with radius 0 is: 100000.
+  avg[0] = 100000 / 1 = 100000.
+
+ +

Example 3:

+ +
Input: nums = [8], k = 100000
+Output: [-1]
+Explanation: 
+- avg[0] is -1 because there are less than k elements before and after index 0.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 105
  • +
  • 0 <= nums[i], k <= 105
  • +
+
\ No newline at end of file From ac3cd88ff3bc74e6a8e25e82b83ad726b26d0b1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Jun 2023 23:45:38 +0530 Subject: [PATCH 1127/3167] Time: 259 ms (56.74%), Space: 129.9 MB (79.40%) - LeetHub --- .../2090-k-radius-subarray-averages.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2090-k-radius-subarray-averages/2090-k-radius-subarray-averages.cpp diff --git a/2090-k-radius-subarray-averages/2090-k-radius-subarray-averages.cpp b/2090-k-radius-subarray-averages/2090-k-radius-subarray-averages.cpp new file mode 100644 index 00000000..38568f27 --- /dev/null +++ b/2090-k-radius-subarray-averages/2090-k-radius-subarray-averages.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector getAverages(vector& nums, int k) { + + int win = 2 * k + 1; + + int i = 0, j = 0, n = nums.size(); + + vector ans(n, -1); + + long long sum = 0; + + while(j < n) + { + sum += nums[j]; + + if(j - i + 1 == win) + { + ans[j-k] = sum/win; + + sum -= nums[i++]; + } + ++j; + } + + return ans; + } +}; \ No newline at end of file From 40d899e354765ff69c1d342bea96e953045722cb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Jun 2023 23:46:45 +0530 Subject: [PATCH 1128/3167] Attach NOTES - LeetHub --- 2090-k-radius-subarray-averages/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2090-k-radius-subarray-averages/NOTES.md diff --git a/2090-k-radius-subarray-averages/NOTES.md b/2090-k-radius-subarray-averages/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2090-k-radius-subarray-averages/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fcd7320da7608ef1947580b3e6d3caac36fec2ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Jun 2023 23:46:48 +0530 Subject: [PATCH 1129/3167] Time: 259 ms (56.74%), Space: 129.9 MB (79.40%) - LeetHub From 010e8f0cacee461df270a55975df927ab6ed34a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Jun 2023 23:36:12 +0530 Subject: [PATCH 1130/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2448-minimum-cost-to-make-array-equal/README.md diff --git a/2448-minimum-cost-to-make-array-equal/README.md b/2448-minimum-cost-to-make-array-equal/README.md new file mode 100644 index 00000000..053f9dce --- /dev/null +++ b/2448-minimum-cost-to-make-array-equal/README.md @@ -0,0 +1,41 @@ +

2448. Minimum Cost to Make Array Equal

Hard


You are given two 0-indexed arrays nums and cost consisting each of n positive integers.

+ +

You can do the following operation any number of times:

+ +
    +
  • Increase or decrease any element of the array nums by 1.
  • +
+ +

The cost of doing one operation on the ith element is cost[i].

+ +

Return the minimum total cost such that all the elements of the array nums become equal.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,5,2], cost = [2,3,1,14]
+Output: 8
+Explanation: We can make all the elements equal to 2 in the following way:
+- Increase the 0th element one time. The cost is 2.
+- Decrease the 1st element one time. The cost is 3.
+- Decrease the 2nd element three times. The cost is 1 + 1 + 1 = 3.
+The total cost is 2 + 3 + 3 = 8.
+It can be shown that we cannot make the array equal with a smaller cost.
+
+ +

Example 2:

+ +
Input: nums = [2,2,2,2,2], cost = [4,2,8,1,3]
+Output: 0
+Explanation: All the elements are already equal, so no operations are needed.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length == cost.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= nums[i], cost[i] <= 106
  • +
+
\ No newline at end of file From bf89cde58c8c987a1c7f477059b995ee1538684a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Jun 2023 23:36:16 +0530 Subject: [PATCH 1131/3167] Time: 103 ms (69.34%), Space: 41.7 MB (36.86%) - LeetHub --- .../2448-minimum-cost-to-make-array-equal.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2448-minimum-cost-to-make-array-equal/2448-minimum-cost-to-make-array-equal.cpp diff --git a/2448-minimum-cost-to-make-array-equal/2448-minimum-cost-to-make-array-equal.cpp b/2448-minimum-cost-to-make-array-equal/2448-minimum-cost-to-make-array-equal.cpp new file mode 100644 index 00000000..a6f7c0c5 --- /dev/null +++ b/2448-minimum-cost-to-make-array-equal/2448-minimum-cost-to-make-array-equal.cpp @@ -0,0 +1,42 @@ +#define ll long long int + +class Solution { +public: + long long minCost(vector& nums, vector& cost) { + + int n = nums.size(); + + vector> vp; + + for(int i = 0; i < n; ++i) + { + vp.push_back({nums[i], cost[i]}); + } + + sort(vp.begin(), vp.end()); + + ll median, sum = 0, tot = 0; + + for(auto itr : vp) + { + sum += itr.second; + } + + ll here = 0, ans = 0; + + ll i = 0; + + while(i < n and here < (sum + 1) / 2) + { + here += vp[i].second; + median = vp[i++].first; + } + + for(int i = 0; i < n; ++i) + { + ans += (abs(nums[i] - median) * cost[i]); + } + + return ans; + } +}; \ No newline at end of file From 6942b0c3cdc547f1e8f3c54b312ad89478825892 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Jun 2023 20:24:06 +0530 Subject: [PATCH 1132/3167] Attach NOTES - LeetHub From dc61ef7916e6ecb23a000dd108a70718a119a7f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Jun 2023 20:24:09 +0530 Subject: [PATCH 1133/3167] Time: 324 ms (5.34%), Space: 108.4 MB (5.73%) - LeetHub --- ...uy-and-sell-stock-with-transaction-fee.cpp | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp index 94e64e4d..ed8980a0 100644 --- a/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp +++ b/0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.cpp @@ -1,22 +1,32 @@ class Solution { -public: + +private: + int helper(int idx, bool buy, int n, vector& prices, int fee, vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][buy] != -1) + return dp[idx][buy]; + + int take = INT_MIN, take2 = INT_MIN; + + if(!buy) + take = max(-prices[idx] + helper(idx+1, !buy, n, prices, fee, dp), helper(idx+1, buy, n, prices, fee, dp)); + else + take2 = max(prices[idx] - fee + helper(idx+1, !buy, n, prices, fee, dp), helper(idx+1, buy, n, prices, fee, dp)); + + return dp[idx][buy] = max({take, take2}); + } +public: int maxProfit(vector& prices, int fee) { int n = prices.size(); - vector curr(2,0), ahead(2,0); - - for(int i = n-1; i>=0; --i) - { - for(int buy = 1; buy>=0; --buy) - { - if(buy == 1) - curr[buy] = max(-prices[i] + ahead[0], ahead[1]); - else - curr[buy] = max(prices[i] - fee + ahead[1], ahead[0]); - } - ahead = curr; - } - return ahead[1]; + + vector> dp(n, vector(2, -1)); + + return helper(0, 0, n, prices, fee, dp); + } }; \ No newline at end of file From 8b2cf9f6dd25a4375a24b0d8761e7fe97382010c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Jun 2023 20:29:18 +0530 Subject: [PATCH 1134/3167] Create README - LeetHub --- 1027-longest-arithmetic-subsequence/README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1027-longest-arithmetic-subsequence/README.md diff --git a/1027-longest-arithmetic-subsequence/README.md b/1027-longest-arithmetic-subsequence/README.md new file mode 100644 index 00000000..b24aa52d --- /dev/null +++ b/1027-longest-arithmetic-subsequence/README.md @@ -0,0 +1,39 @@ +

1027. Longest Arithmetic Subsequence

Medium


Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.

+ +

Note that:

+ +
    +
  • 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.
  • +
  • A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [3,6,9,12]
+Output: 4
+Explanation:  The whole array is an arithmetic sequence with steps of length = 3.
+
+ +

Example 2:

+ +
Input: nums = [9,4,7,2,10]
+Output: 3
+Explanation:  The longest arithmetic subsequence is [4,7,10].
+
+ +

Example 3:

+ +
Input: nums = [20,1,15,3,10,5,8]
+Output: 4
+Explanation:  The longest arithmetic subsequence is [20,15,10,5].
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 1000
  • +
  • 0 <= nums[i] <= 500
  • +
+
\ No newline at end of file From 28290f270e3b3bc1822b386309ae592cb20fc481 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Jun 2023 20:29:21 +0530 Subject: [PATCH 1135/3167] Time: 2128 ms (35.64%), Space: 242.5 MB (42.25%) - LeetHub --- .../1027-longest-arithmetic-subsequence.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1027-longest-arithmetic-subsequence/1027-longest-arithmetic-subsequence.cpp diff --git a/1027-longest-arithmetic-subsequence/1027-longest-arithmetic-subsequence.cpp b/1027-longest-arithmetic-subsequence/1027-longest-arithmetic-subsequence.cpp new file mode 100644 index 00000000..e6fd8d76 --- /dev/null +++ b/1027-longest-arithmetic-subsequence/1027-longest-arithmetic-subsequence.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + int longestArithSeqLength(vector& nums) { + + int n = nums.size(); + int ans = 0; + + if(n <= 2) + return n; + + unordered_map dp[n+1]; + + for(int i=1;i Date: Fri, 23 Jun 2023 20:30:19 +0530 Subject: [PATCH 1136/3167] Create README - LeetHub --- 1027-longest-arithmetic-subsequence/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/1027-longest-arithmetic-subsequence/README.md b/1027-longest-arithmetic-subsequence/README.md index b24aa52d..3a4eae55 100644 --- a/1027-longest-arithmetic-subsequence/README.md +++ b/1027-longest-arithmetic-subsequence/README.md @@ -10,24 +10,24 @@

 

Example 1:

-
Input: nums = [3,6,9,12]
+
Input: nums = [3,6,9,12]
 Output: 4
 Explanation:  The whole array is an arithmetic sequence with steps of length = 3.
-
+

Example 2:

-
Input: nums = [9,4,7,2,10]
+
Input: nums = [9,4,7,2,10]
 Output: 3
 Explanation:  The longest arithmetic subsequence is [4,7,10].
-
+

Example 3:

-
Input: nums = [20,1,15,3,10,5,8]
+
Input: nums = [20,1,15,3,10,5,8]
 Output: 4
 Explanation:  The longest arithmetic subsequence is [20,15,10,5].
-
+

 

Constraints:

From d06b66219d0cf8c720ed42ecc46aa1bd5302d847 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Jun 2023 22:15:39 +0530 Subject: [PATCH 1137/3167] Create README - LeetHub --- 0956-tallest-billboard/README.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0956-tallest-billboard/README.md diff --git a/0956-tallest-billboard/README.md b/0956-tallest-billboard/README.md new file mode 100644 index 00000000..7cd40f31 --- /dev/null +++ b/0956-tallest-billboard/README.md @@ -0,0 +1,37 @@ +

956. Tallest Billboard

Hard


You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.

+ +

You are given a collection of rods that can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.

+ +

Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.

+ +

 

+

Example 1:

+ +
Input: rods = [1,2,3,6]
+Output: 6
+Explanation: We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.
+
+ +

Example 2:

+ +
Input: rods = [1,2,3,4,5,6]
+Output: 10
+Explanation: We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.
+
+ +

Example 3:

+ +
Input: rods = [1,2]
+Output: 0
+Explanation: The billboard cannot be supported, so we return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= rods.length <= 20
  • +
  • 1 <= rods[i] <= 1000
  • +
  • sum(rods[i]) <= 5000
  • +
+
\ No newline at end of file From ebb1659a8c6733e8156a77750e9e265d79340d1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Jun 2023 22:15:42 +0530 Subject: [PATCH 1138/3167] Time: 1102 ms (14.10%), Space: 216 MB (6.61%) - LeetHub --- .../0956-tallest-billboard.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0956-tallest-billboard/0956-tallest-billboard.cpp diff --git a/0956-tallest-billboard/0956-tallest-billboard.cpp b/0956-tallest-billboard/0956-tallest-billboard.cpp new file mode 100644 index 00000000..13b1211d --- /dev/null +++ b/0956-tallest-billboard/0956-tallest-billboard.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int helper(int s1,int i,vector&r, vector>& dp){ + + if(i>=r.size()){ + if(s1)return INT_MIN; + return 0; + } + + if(dp[i].count(s1)){ + return dp[i][s1]; + } + + int ans = helper(s1,i+1,r,dp); + + ans = max(ans,r[i] + helper(s1-r[i],i+1,r,dp)); + + ans = max(ans,helper(s1+r[i],i+1,r,dp)); + + return dp[i][s1]=ans; + } + int tallestBillboard(vector& r) { + int n=r.size(); + + vector> dp(n+1); + + return helper(0,0,r,dp); + } +}; \ No newline at end of file From a1d4fc4667c0db27126c06fcadb30d0f24c50e68 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Jun 2023 23:15:31 +0530 Subject: [PATCH 1139/3167] Attach NOTES - LeetHub --- 1575-count-all-possible-routes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1575-count-all-possible-routes/NOTES.md diff --git a/1575-count-all-possible-routes/NOTES.md b/1575-count-all-possible-routes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1575-count-all-possible-routes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6eb40707194aae3aa9fad892c0651bcdb8fb957e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Jun 2023 23:15:32 +0530 Subject: [PATCH 1140/3167] Create README - LeetHub --- 1575-count-all-possible-routes/README.md | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1575-count-all-possible-routes/README.md diff --git a/1575-count-all-possible-routes/README.md b/1575-count-all-possible-routes/README.md new file mode 100644 index 00000000..458260c8 --- /dev/null +++ b/1575-count-all-possible-routes/README.md @@ -0,0 +1,50 @@ +

1575. Count All Possible Routes

Hard


You are given an array of distinct positive integers locations where locations[i] represents the position of city i. You are also given integers start, finish and fuel representing the starting city, ending city, and the initial amount of fuel you have, respectively.

+ +

At each step, if you are at city i, you can pick any city j such that j != i and 0 <= j < locations.length and move to city j. Moving from city i to city j reduces the amount of fuel you have by |locations[i] - locations[j]|. Please notice that |x| denotes the absolute value of x.

+ +

Notice that fuel cannot become negative at any point in time, and that you are allowed to visit any city more than once (including start and finish).

+ +

Return the count of all possible routes from start to finish. Since the answer may be too large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
+Output: 4
+Explanation: The following are all possible routes, each uses 5 units of fuel:
+1 -> 3
+1 -> 2 -> 3
+1 -> 4 -> 3
+1 -> 4 -> 2 -> 3
+
+ +

Example 2:

+ +
Input: locations = [4,3,1], start = 1, finish = 0, fuel = 6
+Output: 5
+Explanation: The following are all possible routes:
+1 -> 0, used fuel = 1
+1 -> 2 -> 0, used fuel = 5
+1 -> 2 -> 1 -> 0, used fuel = 5
+1 -> 0 -> 1 -> 0, used fuel = 3
+1 -> 0 -> 1 -> 0 -> 1 -> 0, used fuel = 5
+
+ +

Example 3:

+ +
Input: locations = [5,2,1], start = 0, finish = 2, fuel = 3
+Output: 0
+Explanation: It is impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= locations.length <= 100
  • +
  • 1 <= locations[i] <= 109
  • +
  • All integers in locations are distinct.
  • +
  • 0 <= start, finish < locations.length
  • +
  • 1 <= fuel <= 200
  • +
+
\ No newline at end of file From a0747ab2162880fe490d7fca5de4a7baa82b2db5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 25 Jun 2023 23:15:35 +0530 Subject: [PATCH 1141/3167] Time: 312 ms (18.51%), Space: 11.9 MB (42.54%) - LeetHub --- .../1575-count-all-possible-routes.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1575-count-all-possible-routes/1575-count-all-possible-routes.cpp diff --git a/1575-count-all-possible-routes/1575-count-all-possible-routes.cpp b/1575-count-all-possible-routes/1575-count-all-possible-routes.cpp new file mode 100644 index 00000000..3bdbcdac --- /dev/null +++ b/1575-count-all-possible-routes/1575-count-all-possible-routes.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int mod=1e9+7; + + int helper(vector>&dp,vector&locations,int current,int &finish,int fuel, int n){ + + if(fuel<0) + return 0; + + if(dp[current][fuel]!=-1) + return dp[current][fuel]; + + int ans=0; + + if(current==finish) + ans++; + + for(int next=0;next& locations, int start, int finish, int fuel) { + + int n=locations.size(); + + vector>dp(n,vector(fuel+1,-1)); + + return helper(dp,locations,start,finish,fuel,n); + } +}; \ No newline at end of file From 58492f72492557fb843afb9cc25d3ae2cf857d56 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:25:43 +0530 Subject: [PATCH 1142/3167] Create README - LeetHub --- 2462-total-cost-to-hire-k-workers/README.md | 50 +++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2462-total-cost-to-hire-k-workers/README.md diff --git a/2462-total-cost-to-hire-k-workers/README.md b/2462-total-cost-to-hire-k-workers/README.md new file mode 100644 index 00000000..91a9d501 --- /dev/null +++ b/2462-total-cost-to-hire-k-workers/README.md @@ -0,0 +1,50 @@ +

2462. Total Cost to Hire K Workers

Medium


You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker.

+ +

You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules:

+ +
    +
  • You will run k sessions and hire exactly one worker in each session.
  • +
  • In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index. +
      +
    • For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].
    • +
    • In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.
    • +
    +
  • +
  • If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.
  • +
  • A worker can only be chosen once.
  • +
+ +

Return the total cost to hire exactly k workers.

+ +

 

+

Example 1:

+ +
Input: costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
+Output: 11
+Explanation: We hire 3 workers in total. The total cost is initially 0.
+- In the first hiring round we choose the worker from [17,12,10,2,7,2,11,20,8]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
+- In the second hiring round we choose the worker from [17,12,10,7,2,11,20,8]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
+- In the third hiring round we choose the worker from [17,12,10,7,11,20,8]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
+The total hiring cost is 11.
+
+ +

Example 2:

+ +
Input: costs = [1,2,4,1], k = 3, candidates = 3
+Output: 4
+Explanation: We hire 3 workers in total. The total cost is initially 0.
+- In the first hiring round we choose the worker from [1,2,4,1]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
+- In the second hiring round we choose the worker from [2,4,1]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
+- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [2,4]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
+The total hiring cost is 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= costs.length <= 105
  • +
  • 1 <= costs[i] <= 105
  • +
  • 1 <= k, candidates <= costs.length
  • +
+
\ No newline at end of file From 1af421fac87ff71badded7320bfd06ed313881b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:25:44 +0530 Subject: [PATCH 1143/3167] Attach NOTES - LeetHub --- 2462-total-cost-to-hire-k-workers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2462-total-cost-to-hire-k-workers/NOTES.md diff --git a/2462-total-cost-to-hire-k-workers/NOTES.md b/2462-total-cost-to-hire-k-workers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2462-total-cost-to-hire-k-workers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 28d74d5b677ddb9aa88f595753137368a69fa601 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:25:48 +0530 Subject: [PATCH 1144/3167] Time: 201 ms (74.02%), Space: 72.8 MB (61.12%) - LeetHub --- .../2462-total-cost-to-hire-k-workers.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2462-total-cost-to-hire-k-workers/2462-total-cost-to-hire-k-workers.cpp diff --git a/2462-total-cost-to-hire-k-workers/2462-total-cost-to-hire-k-workers.cpp b/2462-total-cost-to-hire-k-workers/2462-total-cost-to-hire-k-workers.cpp new file mode 100644 index 00000000..b494c152 --- /dev/null +++ b/2462-total-cost-to-hire-k-workers/2462-total-cost-to-hire-k-workers.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + long long totalCost(vector& costs, int k, int candidates) { + + long long ans=0; + priority_queue,greater> pq1,pq2; + + int cnt = 0; + int i=0; + int j=costs.size()-1; + + while(cnt=i) pq2.push(costs[j--]); + + int cost1 = pq1.size()>0?pq1.top():INT_MAX; + int cost2 = pq2.size()>0?pq2.top():INT_MAX; + + if(cost1<=cost2){ + + ans += cost1; + pq1.pop(); + } + else{ + + ans += cost2; + pq2.pop(); + } + + cnt++; + } + + return ans; + + } +}; \ No newline at end of file From c519bb9a94f307909fc70b4cd1705f4bd68ab6de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Jun 2023 22:11:09 +0530 Subject: [PATCH 1145/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0373-find-k-pairs-with-smallest-sums/README.md diff --git a/0373-find-k-pairs-with-smallest-sums/README.md b/0373-find-k-pairs-with-smallest-sums/README.md new file mode 100644 index 00000000..97846ec1 --- /dev/null +++ b/0373-find-k-pairs-with-smallest-sums/README.md @@ -0,0 +1,38 @@ +

373. Find K Pairs with Smallest Sums

Medium


You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.

+ +

Define a pair (u, v) which consists of one element from the first array and one element from the second array.

+ +

Return the k pairs (u1, v1), (u2, v2), ..., (uk, vk) with the smallest sums.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
+Output: [[1,2],[1,4],[1,6]]
+Explanation: The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
+
+ +

Example 2:

+ +
Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
+Output: [[1,1],[1,1]]
+Explanation: The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
+
+ +

Example 3:

+ +
Input: nums1 = [1,2], nums2 = [3], k = 3
+Output: [[1,3],[2,3]]
+Explanation: All possible pairs are returned from the sequence: [1,3],[2,3]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 105
  • +
  • -109 <= nums1[i], nums2[i] <= 109
  • +
  • nums1 and nums2 both are sorted in ascending order.
  • +
  • 1 <= k <= 104
  • +
+
\ No newline at end of file From 4bc8f3e925652bbceb2479ee86fb9a3317066d2e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Jun 2023 22:11:12 +0530 Subject: [PATCH 1146/3167] Time: 387 ms (39.48%), Space: 110.1 MB (61.74%) - LeetHub --- .../0373-find-k-pairs-with-smallest-sums.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0373-find-k-pairs-with-smallest-sums/0373-find-k-pairs-with-smallest-sums.cpp diff --git a/0373-find-k-pairs-with-smallest-sums/0373-find-k-pairs-with-smallest-sums.cpp b/0373-find-k-pairs-with-smallest-sums/0373-find-k-pairs-with-smallest-sums.cpp new file mode 100644 index 00000000..29d83f55 --- /dev/null +++ b/0373-find-k-pairs-with-smallest-sums/0373-find-k-pairs-with-smallest-sums.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + vector> kSmallestPairs(vector& nums1, vector& nums2, int k) { + + int n = nums1.size(); + int m = nums2.size(); + + priority_queue> > pq; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + int sum = nums1[i] + nums2[j]; + + if(pq.size() < k) + { + pq.push({sum, {nums1[i], nums2[j]}}); + } + else if(sum < pq.top().first) + { + pq.pop(); + pq.push({sum, {nums1[i], nums2[j]}}); + } + else + break; + } + } + + vector > ans; + + while(!pq.empty()) + { + int a = pq.top().second.first; + int b = pq.top().second.second; + + ans.push_back({a, b}); + + pq.pop(); + } + + return ans; + + } +}; \ No newline at end of file From 27b5ae91bc543ab02c5790a9158e6b464048cc1d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:20:38 +0530 Subject: [PATCH 1147/3167] Create README - LeetHub --- 1514-path-with-maximum-probability/README.md | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1514-path-with-maximum-probability/README.md diff --git a/1514-path-with-maximum-probability/README.md b/1514-path-with-maximum-probability/README.md new file mode 100644 index 00000000..01914192 --- /dev/null +++ b/1514-path-with-maximum-probability/README.md @@ -0,0 +1,47 @@ +

1514. Path with Maximum Probability

Medium


You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].

+ +

Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.

+ +

If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.

+ +

 

+

Example 1:

+ +

+ +
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
+Output: 0.25000
+Explanation: There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
+
+ +

Example 2:

+ +

+ +
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
+Output: 0.30000
+
+ +

Example 3:

+ +

+ +
Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
+Output: 0.00000
+Explanation: There is no path between 0 and 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 10^4
  • +
  • 0 <= start, end < n
  • +
  • start != end
  • +
  • 0 <= a, b < n
  • +
  • a != b
  • +
  • 0 <= succProb.length == edges.length <= 2*10^4
  • +
  • 0 <= succProb[i] <= 1
  • +
  • There is at most one edge between every two nodes.
  • +
+
\ No newline at end of file From 0a6431ea34d9926ee56f85e2cca8e3f4c1265257 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:20:38 +0530 Subject: [PATCH 1148/3167] Attach NOTES - LeetHub --- 1514-path-with-maximum-probability/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1514-path-with-maximum-probability/NOTES.md diff --git a/1514-path-with-maximum-probability/NOTES.md b/1514-path-with-maximum-probability/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1514-path-with-maximum-probability/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f2d7df938449743ed60b71ce9fd667f41d562de1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:20:41 +0530 Subject: [PATCH 1149/3167] Time: 207 ms (77.82%), Space: 64.4 MB (96.29%) - LeetHub --- .../1514-path-with-maximum-probability.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1514-path-with-maximum-probability/1514-path-with-maximum-probability.cpp diff --git a/1514-path-with-maximum-probability/1514-path-with-maximum-probability.cpp b/1514-path-with-maximum-probability/1514-path-with-maximum-probability.cpp new file mode 100644 index 00000000..2c1b7ccb --- /dev/null +++ b/1514-path-with-maximum-probability/1514-path-with-maximum-probability.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + double maxProbability(int n, vector>& edges, vector& succProb, int start, int end) { + + vector> adj[n]; + + for(int i = 0; i < edges.size(); ++i) + { + int u = edges[i][0]; + int v = edges[i][1]; + + adj[u].push_back({v, succProb[i]}); + adj[v].push_back({u , succProb[i]}); + } + + queue q; + + vector dist(n, 0.0); + + dist[start] = 1.0; + + q.push(start); + + while(!q.empty()) + { + int node = q.front(); + + q.pop(); + + for(auto itr : adj[node]) + { + int x = itr.first; + double prob = itr.second; + + double newProb = dist[node] * prob; + + if(newProb > dist[x]) + { + dist[x] = newProb; + q.push(x); + } + } + } + + return dist[end]; + + } +}; \ No newline at end of file From 694924dda24ac52167eeaa85d66db41eda41c06d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:21:10 +0530 Subject: [PATCH 1150/3167] Attach NOTES - LeetHub From 51f8db22a96b7fe4e523d385b9e86312c4b2ab7f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:21:15 +0530 Subject: [PATCH 1151/3167] Time: 207 ms (77.82%), Space: 64.4 MB (96.29%) - LeetHub From e5cb854ddf2cd90f6242e06e513186c10ec9eacc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:24:26 +0530 Subject: [PATCH 1152/3167] Attach NOTES - LeetHub From 4ecfd34f038fb4b9a1aa56d45803eb9be94a55cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Jun 2023 00:19:49 +0530 Subject: [PATCH 1153/3167] Create README - LeetHub --- 0864-shortest-path-to-get-all-keys/README.md | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0864-shortest-path-to-get-all-keys/README.md diff --git a/0864-shortest-path-to-get-all-keys/README.md b/0864-shortest-path-to-get-all-keys/README.md new file mode 100644 index 00000000..e49301b3 --- /dev/null +++ b/0864-shortest-path-to-get-all-keys/README.md @@ -0,0 +1,51 @@ +

864. Shortest Path to Get All Keys

Hard


You are given an m x n grid grid where:

+ +
    +
  • '.' is an empty cell.
  • +
  • '#' is a wall.
  • +
  • '@' is the starting point.
  • +
  • Lowercase letters represent keys.
  • +
  • Uppercase letters represent locks.
  • +
+ +

You start at the starting point and one move consists of walking one space in one of the four cardinal directions. You cannot walk outside the grid, or walk into a wall.

+ +

If you walk over a key, you can pick it up and you cannot walk over a lock unless you have its corresponding key.

+ +

For some 1 <= k <= 6, there is exactly one lowercase and one uppercase letter of the first k letters of the English alphabet in the grid. This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet.

+ +

Return the lowest number of moves to acquire all keys. If it is impossible, return -1.

+ +

 

+

Example 1:

+ +
Input: grid = ["@.a..","###.#","b.A.B"]
+Output: 8
+Explanation: Note that the goal is to obtain all the keys not to open all the locks.
+
+ +

Example 2:

+ +
Input: grid = ["@..aA","..B#.","....b"]
+Output: 6
+
+ +

Example 3:

+ +
Input: grid = ["@Aa"]
+Output: -1
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 30
  • +
  • grid[i][j] is either an English letter, '.', '#', or '@'.
  • +
  • The number of keys in the grid is in the range [1, 6].
  • +
  • Each key in the grid is unique.
  • +
  • Each key in the grid has a matching lock.
  • +
+
\ No newline at end of file From 2c60371fc39634c95a2aac754096f6edd084c26c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Jun 2023 00:19:53 +0530 Subject: [PATCH 1154/3167] Time: 31 ms (89.08%), Space: 9.9 MB (92.15%) - LeetHub --- .../0864-shortest-path-to-get-all-keys.cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 0864-shortest-path-to-get-all-keys/0864-shortest-path-to-get-all-keys.cpp diff --git a/0864-shortest-path-to-get-all-keys/0864-shortest-path-to-get-all-keys.cpp b/0864-shortest-path-to-get-all-keys/0864-shortest-path-to-get-all-keys.cpp new file mode 100644 index 00000000..c482cecd --- /dev/null +++ b/0864-shortest-path-to-get-all-keys/0864-shortest-path-to-get-all-keys.cpp @@ -0,0 +1,70 @@ +class Solution { +public: + int shortestPathAllKeys(vector& grid) { + + int m=grid.size(), n=m?grid[0].size():0; + + if(!m || !n) return 0; + + int path=0, K=0; + + + vector dirs={0,-1,0,1,0}; + + vector>> visited(m,vector>(n,vector(64,0))); + + queue> q; + + for(int i=0;i='A' && grid[i][j]<='F') K++; //total alpha number + } + } + while(!q.empty()) + { + int size=q.size(); + + for(int i=0;i=m || y<0 || y>=n || grid[x][y]=='#') continue; + + if(grid[x][y]>='a' && grid[x][y]<='f'){ + + k=carry|(1<<(grid[x][y]-'a')); + } + else if(grid[x][y]>='A' && grid[x][y]<='F'){ + + if(!(carry & (1<<(grid[x][y]-'A')))) continue; + } + if(!visited[x][y][k]){ + visited[x][y][k]=1; + q.push({x*n+y,k}); + } + } + } + path++; + } + return -1; +} +}; \ No newline at end of file From 902eb6130131d4db59ec4ed0871f7d370634358b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Jun 2023 22:17:47 +0530 Subject: [PATCH 1155/3167] Create README - LeetHub --- .../README.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1970-last-day-where-you-can-still-cross/README.md diff --git a/1970-last-day-where-you-can-still-cross/README.md b/1970-last-day-where-you-can-still-cross/README.md new file mode 100644 index 00000000..24619fe9 --- /dev/null +++ b/1970-last-day-where-you-can-still-cross/README.md @@ -0,0 +1,45 @@ +

1970. Last Day Where You Can Still Cross

Hard


There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matrix, respectively.

+ +

Initially on day 0, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-based 2D array cells, where cells[i] = [ri, ci] represents that on the ith day, the cell on the rith row and cith column (1-based coordinates) will be covered with water (i.e., changed to 1).

+ +

You want to find the last day that it is possible to walk from the top to the bottom by only walking on land cells. You can start from any cell in the top row and end at any cell in the bottom row. You can only travel in the four cardinal directions (left, right, up, and down).

+ +

Return the last day where it is possible to walk from the top to the bottom by only walking on land cells.

+ +

 

+

Example 1:

+ +
Input: row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]
+Output: 2
+Explanation: The above image depicts how the matrix changes each day starting from day 0.
+The last day where it is possible to cross from top to bottom is on day 2.
+
+ +

Example 2:

+ +
Input: row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]
+Output: 1
+Explanation: The above image depicts how the matrix changes each day starting from day 0.
+The last day where it is possible to cross from top to bottom is on day 1.
+
+ +

Example 3:

+ +
Input: row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]
+Output: 3
+Explanation: The above image depicts how the matrix changes each day starting from day 0.
+The last day where it is possible to cross from top to bottom is on day 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= row, col <= 2 * 104
  • +
  • 4 <= row * col <= 2 * 104
  • +
  • cells.length == row * col
  • +
  • 1 <= ri <= row
  • +
  • 1 <= ci <= col
  • +
  • All the values of cells are unique.
  • +
+
\ No newline at end of file From 338c2d7c4d73b988f17d5d5078d3fd52410ef76d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 30 Jun 2023 22:17:53 +0530 Subject: [PATCH 1156/3167] Time: 894 ms (18.01%), Space: 240.3 MB (13.06%) - LeetHub --- ...970-last-day-where-you-can-still-cross.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1970-last-day-where-you-can-still-cross/1970-last-day-where-you-can-still-cross.cpp diff --git a/1970-last-day-where-you-can-still-cross/1970-last-day-where-you-can-still-cross.cpp b/1970-last-day-where-you-can-still-cross/1970-last-day-where-you-can-still-cross.cpp new file mode 100644 index 00000000..6a61a643 --- /dev/null +++ b/1970-last-day-where-you-can-still-cross/1970-last-day-where-you-can-still-cross.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + int n,m; + int d[5] = {0,1,0,-1,0}; + bool dfs(vector>&vis,vector>&v,int i,int j){ + vis[i][j]=1; + if(i==n-1)return true; + bool ans = false; + for(int k=0;k<4;k++){ + int ni = i + d[k]; + int nj = j + d[k+1]; + if(ni>=0 && nj>=0 && ni>& cell) { + int l=0,r=cell.size()-1; + n = row; + m = col; + int day=0; + while(l<=r){ + int mid = (l+r)/2; + vector>v(row,vector(col,0)); + for(int i=0;i<=mid;i++){ + v[cell[i][0]-1][cell[i][1]-1]=1; + } + vector>vis(row,vector(col,0)); + bool ans=false; + for(int i=0;i Date: Sat, 1 Jul 2023 20:07:20 +0530 Subject: [PATCH 1157/3167] Create README - LeetHub --- 0287-find-the-duplicate-number/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0287-find-the-duplicate-number/README.md diff --git a/0287-find-the-duplicate-number/README.md b/0287-find-the-duplicate-number/README.md new file mode 100644 index 00000000..9394e105 --- /dev/null +++ b/0287-find-the-duplicate-number/README.md @@ -0,0 +1,37 @@ +

287. Find the Duplicate Number

Medium


Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

+ +

There is only one repeated number in nums, return this repeated number.

+ +

You must solve the problem without modifying the array nums and uses only constant extra space.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • nums.length == n + 1
  • +
  • 1 <= nums[i] <= n
  • +
  • All the integers in nums appear only once except for precisely one integer which appears two or more times.
  • +
+ +

 

+

Follow up:

+ +
    +
  • How can we prove that at least one duplicate number must exist in nums?
  • +
  • Can you solve the problem in linear runtime complexity?
  • +
+
\ No newline at end of file From 0609e5b9794e248a82b632549976eef13489e272 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:07:20 +0530 Subject: [PATCH 1158/3167] Attach NOTES - LeetHub --- 0287-find-the-duplicate-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0287-find-the-duplicate-number/NOTES.md diff --git a/0287-find-the-duplicate-number/NOTES.md b/0287-find-the-duplicate-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0287-find-the-duplicate-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7defd2b425a992fb9b13e68b802f0fcdbb64919c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:07:24 +0530 Subject: [PATCH 1159/3167] Time: 112 ms (87.81%), Space: 61.1 MB (79.37%) - LeetHub --- .../0287-find-the-duplicate-number.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0287-find-the-duplicate-number/0287-find-the-duplicate-number.cpp diff --git a/0287-find-the-duplicate-number/0287-find-the-duplicate-number.cpp b/0287-find-the-duplicate-number/0287-find-the-duplicate-number.cpp new file mode 100644 index 00000000..7876c293 --- /dev/null +++ b/0287-find-the-duplicate-number/0287-find-the-duplicate-number.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int findDuplicate(vector& nums) { + + int fast = nums[0]; + int slow = nums[0]; + + do{ + fast = nums[nums[fast]]; + slow = nums[slow]; + } while(fast != slow); + + fast = nums[0]; + + while(fast != slow) + { + fast = nums[fast]; + slow = nums[slow]; + } + + return fast; + + } +}; \ No newline at end of file From feafaf986b59072d825b6195817e3fec6e7f1826 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:23:26 +0530 Subject: [PATCH 1160/3167] Create README - LeetHub --- 0075-sort-colors/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0075-sort-colors/README.md diff --git a/0075-sort-colors/README.md b/0075-sort-colors/README.md new file mode 100644 index 00000000..19eb0c70 --- /dev/null +++ b/0075-sort-colors/README.md @@ -0,0 +1,31 @@ +

75. Sort Colors

Medium


Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

+ +

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

+ +

You must solve this problem without using the library's sort function.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 300
  • +
  • nums[i] is either 0, 1, or 2.
  • +
+ +

 

+

Follow up: Could you come up with a one-pass algorithm using only constant extra space?

+
\ No newline at end of file From 274cf2f045123478879b6cb3c720861889f8fc4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:23:29 +0530 Subject: [PATCH 1161/3167] Time: 6 ms (24.09%), Space: 8.2 MB (83.21%) - LeetHub --- 0075-sort-colors/0075-sort-colors.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0075-sort-colors/0075-sort-colors.cpp diff --git a/0075-sort-colors/0075-sort-colors.cpp b/0075-sort-colors/0075-sort-colors.cpp new file mode 100644 index 00000000..5c62fdff --- /dev/null +++ b/0075-sort-colors/0075-sort-colors.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + void sortColors(vector& nums) { + + int n = nums.size(); + + int zero = 0; + + int start = 0, end = n-1; + + while(start <= end) + { + if(nums[start] == 0) + { + swap(nums[zero++],nums[start++]); + } + else if(nums[start] == 2) + { + swap(nums[start], nums[end--]); + } + else + start++; + } + + } +}; \ No newline at end of file From f8a45fee28fdab30663114ec386e70d039834924 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:27:45 +0530 Subject: [PATCH 1162/3167] Attach NOTES - LeetHub --- 0026-remove-duplicates-from-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0026-remove-duplicates-from-sorted-array/NOTES.md diff --git a/0026-remove-duplicates-from-sorted-array/NOTES.md b/0026-remove-duplicates-from-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0026-remove-duplicates-from-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 69f227b47a3d74cc25a4ae63e6fdaa7818d85052 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:27:48 +0530 Subject: [PATCH 1163/3167] Time: 4 ms (97.93%), Space: 18.4 MB (28.90%) - LeetHub --- ...26-remove-duplicates-from-sorted-array.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp diff --git a/0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp b/0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp new file mode 100644 index 00000000..4187bcf0 --- /dev/null +++ b/0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int removeDuplicates(vector& nums) { + + int n = nums.size(); + + int cnt = 0; + + for(int i = 1; i < n; ++i) + { + if(nums[i] == nums[i-1]) + { + ++cnt; + } + else + nums[i-cnt] = nums[i]; + } + + return n - cnt; + + } +}; \ No newline at end of file From c3ddf8fb99de2a08920ed2530a016c93718ee07b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:35:44 +0530 Subject: [PATCH 1164/3167] Attach NOTES - LeetHub --- 0073-set-matrix-zeroes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0073-set-matrix-zeroes/NOTES.md diff --git a/0073-set-matrix-zeroes/NOTES.md b/0073-set-matrix-zeroes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0073-set-matrix-zeroes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 17f9d30348e22bda578e8560d93ac23df6ab246c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:35:47 +0530 Subject: [PATCH 1165/3167] Time: 16 ms (71.41%), Space: 13.4 MB (26.96%) - LeetHub --- .../0073-set-matrix-zeroes.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0073-set-matrix-zeroes/0073-set-matrix-zeroes.cpp diff --git a/0073-set-matrix-zeroes/0073-set-matrix-zeroes.cpp b/0073-set-matrix-zeroes/0073-set-matrix-zeroes.cpp new file mode 100644 index 00000000..37244de9 --- /dev/null +++ b/0073-set-matrix-zeroes/0073-set-matrix-zeroes.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + void setZeroes(vector>& matrix) { + + int n = matrix.size(), 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] == 0) + { + 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] = 0; + } + } + + } +}; \ No newline at end of file From da3e3f788941d474509ca5465db1deb42aa4ccbd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 23:34:32 +0530 Subject: [PATCH 1166/3167] Create README - LeetHub --- 2305-fair-distribution-of-cookies/README.md | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2305-fair-distribution-of-cookies/README.md diff --git a/2305-fair-distribution-of-cookies/README.md b/2305-fair-distribution-of-cookies/README.md new file mode 100644 index 00000000..f3d14dcf --- /dev/null +++ b/2305-fair-distribution-of-cookies/README.md @@ -0,0 +1,39 @@ +

2305. Fair Distribution of Cookies

Medium


You are given an integer array cookies, where cookies[i] denotes the number of cookies in the ith bag. You are also given an integer k that denotes the number of children to distribute all the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.

+ +

The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.

+ +

Return the minimum unfairness of all distributions.

+ +

 

+

Example 1:

+ +
Input: cookies = [8,15,10,20,8], k = 2
+Output: 31
+Explanation: One optimal distribution is [8,15,8] and [10,20]
+- The 1st child receives [8,15,8] which has a total of 8 + 15 + 8 = 31 cookies.
+- The 2nd child receives [10,20] which has a total of 10 + 20 = 30 cookies.
+The unfairness of the distribution is max(31,30) = 31.
+It can be shown that there is no distribution with an unfairness less than 31.
+
+ +

Example 2:

+ +
Input: cookies = [6,1,3,2,2,4,1,2], k = 3
+Output: 7
+Explanation: One optimal distribution is [6,1], [3,2,2], and [4,1,2]
+- The 1st child receives [6,1] which has a total of 6 + 1 = 7 cookies.
+- The 2nd child receives [3,2,2] which has a total of 3 + 2 + 2 = 7 cookies.
+- The 3rd child receives [4,1,2] which has a total of 4 + 1 + 2 = 7 cookies.
+The unfairness of the distribution is max(7,7,7) = 7.
+It can be shown that there is no distribution with an unfairness less than 7.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= cookies.length <= 8
  • +
  • 1 <= cookies[i] <= 105
  • +
  • 2 <= k <= cookies.length
  • +
+
\ No newline at end of file From 7ff1e21375f5c27c0610b8dec56fddda16105fe0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jul 2023 23:34:32 +0530 Subject: [PATCH 1167/3167] Attach NOTES - LeetHub --- 2305-fair-distribution-of-cookies/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2305-fair-distribution-of-cookies/NOTES.md diff --git a/2305-fair-distribution-of-cookies/NOTES.md b/2305-fair-distribution-of-cookies/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2305-fair-distribution-of-cookies/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f0e26c006d63e4c91d506d606c1efbb117179ff9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 11:24:18 +0530 Subject: [PATCH 1168/3167] Create README - LeetHub --- 6911-continuous-subarrays/README.md | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 6911-continuous-subarrays/README.md diff --git a/6911-continuous-subarrays/README.md b/6911-continuous-subarrays/README.md new file mode 100644 index 00000000..4afbbdcf --- /dev/null +++ b/6911-continuous-subarrays/README.md @@ -0,0 +1,45 @@ +

6911. Continuous Subarrays

Medium


You are given a 0-indexed integer array nums. A subarray of nums is called continuous if:

+ +
    +
  • Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2.
  • +
+ +

Return the total number of continuous subarrays.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [5,4,2,4]
+Output: 8
+Explanation: 
+Continuous subarray of size 1: [5], [4], [2], [4].
+Continuous subarray of size 2: [5,4], [4,2], [2,4].
+Continuous subarray of size 3: [4,2,4].
+Thereare no subarrys of size 4.
+Total continuous subarrays = 4 + 3 + 1 = 8.
+It can be shown that there are no more continuous subarrays.
+
+ +

 

+ +

Example 2:

+ +
Input: nums = [1,2,3]
+Output: 6
+Explanation: 
+Continuous subarray of size 1: [1], [2], [3].
+Continuous subarray of size 2: [1,2], [2,3].
+Continuous subarray of size 3: [1,2,3].
+Total continuous subarrays = 3 + 2 + 1 = 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 99438826d6202c3428aaa6fae6d5d732008dc369 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 11:24:21 +0530 Subject: [PATCH 1169/3167] Time: 374 ms (38.46%), Space: 149.9 MB (7.69%) - LeetHub --- .../6911-continuous-subarrays.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 6911-continuous-subarrays/6911-continuous-subarrays.cpp diff --git a/6911-continuous-subarrays/6911-continuous-subarrays.cpp b/6911-continuous-subarrays/6911-continuous-subarrays.cpp new file mode 100644 index 00000000..49146e41 --- /dev/null +++ b/6911-continuous-subarrays/6911-continuous-subarrays.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long continuousSubarrays(vector& nums) { + + int n = nums.size(); + + long long int ans = 0, j = 0; + + multiset st; + + for(int i = 0; i < n; ++i) + { + st.insert(nums[i]); + + while(*st.rbegin() - *st.begin() > 2) + { + auto it = st.find(nums[j]); + st.erase(it); + ++j; + } + + ans += (i - j + 1); + } + + return ans; + + } +}; \ No newline at end of file From 3541c28b566167dd652d6e6e93ebe5b8c214099e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 11:24:37 +0530 Subject: [PATCH 1170/3167] Attach NOTES - LeetHub --- 6911-continuous-subarrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 6911-continuous-subarrays/NOTES.md diff --git a/6911-continuous-subarrays/NOTES.md b/6911-continuous-subarrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/6911-continuous-subarrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 732ae98385a29e4be519d6c97801d5093cbec358 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 11:24:41 +0530 Subject: [PATCH 1171/3167] Time: 374 ms (38.46%), Space: 149.9 MB (7.69%) - LeetHub From 4cbbf43bc50445491fe73eb7bc06df04b46ee80c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 11:25:23 +0530 Subject: [PATCH 1172/3167] Attach NOTES - LeetHub From 240799f381e144fdf09d412dfb552c0f5f0d50dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 11:25:26 +0530 Subject: [PATCH 1173/3167] Time: 374 ms (38.46%), Space: 149.9 MB (7.69%) - LeetHub From 4cfdc3d373fb1c3320d26f82c1638b3d04cc9d05 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:12:40 +0530 Subject: [PATCH 1174/3167] Create README - LeetHub --- 0283-move-zeroes/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0283-move-zeroes/README.md diff --git a/0283-move-zeroes/README.md b/0283-move-zeroes/README.md new file mode 100644 index 00000000..4867dc0c --- /dev/null +++ b/0283-move-zeroes/README.md @@ -0,0 +1,22 @@ +

283. Move Zeroes

Easy


Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

+ +

Note that you must do this in-place without making a copy of the array.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
+ +

 

+Follow up: Could you minimize the total number of operations done?
\ No newline at end of file From 72617152341af6e6be91133f44512085a533bbc7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:12:43 +0530 Subject: [PATCH 1175/3167] Time: 25 ms (67.12%), Space: 19.1 MB (97.99%) - LeetHub --- 0283-move-zeroes/0283-move-zeroes.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 0283-move-zeroes/0283-move-zeroes.cpp diff --git a/0283-move-zeroes/0283-move-zeroes.cpp b/0283-move-zeroes/0283-move-zeroes.cpp new file mode 100644 index 00000000..6f52d9f3 --- /dev/null +++ b/0283-move-zeroes/0283-move-zeroes.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + void moveZeroes(vector& nums) { + + int n = nums.size(); + + int start = 0; + + for(int i = 0; i < n; ++i) + { + if(nums[i] != 0) + { + swap(nums[i], nums[start++]); + } + } + + } +}; \ No newline at end of file From 3e54cfbbbd31d6829c0583d4158e7fd38f1fd7bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:13:32 +0530 Subject: [PATCH 1176/3167] Attach NOTES - LeetHub --- 0283-move-zeroes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0283-move-zeroes/NOTES.md diff --git a/0283-move-zeroes/NOTES.md b/0283-move-zeroes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0283-move-zeroes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 45911283a3797c7b049c1a0073b6311253d1764b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:13:36 +0530 Subject: [PATCH 1177/3167] Time: 25 ms (67.12%), Space: 19.1 MB (97.99%) - LeetHub From 931a184e36a4f71a1e50227d0cf9b56c16d6d4bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:18:17 +0530 Subject: [PATCH 1178/3167] Attach NOTES - LeetHub --- 0121-best-time-to-buy-and-sell-stock/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0121-best-time-to-buy-and-sell-stock/NOTES.md diff --git a/0121-best-time-to-buy-and-sell-stock/NOTES.md b/0121-best-time-to-buy-and-sell-stock/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0121-best-time-to-buy-and-sell-stock/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ee9282ad9ce6e481d0b15ceb8c53a31daddf18a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:18:22 +0530 Subject: [PATCH 1179/3167] Time: 156 ms (30.54%), Space: 93.3 MB (29.61%) - LeetHub --- .../0121-best-time-to-buy-and-sell-stock.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp b/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp index 412cf328..b0b77e4f 100644 --- a/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp +++ b/0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp @@ -2,16 +2,17 @@ class Solution { public: int maxProfit(vector& prices) { - int n = prices.size(); - int mini = prices[0]; + int minPrice = INT_MAX, n = prices.size(); + int ans = 0; for(int i = 0; i < n; ++i) { - mini = min(mini, prices[i]); - ans = max(ans, prices[i] - mini); + minPrice = min(minPrice, prices[i]); + ans = max(ans, prices[i] - minPrice); } return ans; + } }; \ No newline at end of file From cba82d1cb8ed2547d674d24797507f41b829b187 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 21:29:44 +0530 Subject: [PATCH 1180/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1601-maximum-number-of-achievable-transfer-requests/README.md diff --git a/1601-maximum-number-of-achievable-transfer-requests/README.md b/1601-maximum-number-of-achievable-transfer-requests/README.md new file mode 100644 index 00000000..1752990c --- /dev/null +++ b/1601-maximum-number-of-achievable-transfer-requests/README.md @@ -0,0 +1,49 @@ +

1601. Maximum Number of Achievable Transfer Requests

Hard


We have n buildings numbered from 0 to n - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.

+ +

You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.

+ +

All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2, there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2.

+ +

Return the maximum number of achievable requests.

+ +

 

+

Example 1:

+ +
Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
+Output: 5
+Explantion: Let's see the requests:
+From building 0 we have employees x and y and both want to move to building 1.
+From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.
+From building 2 we have employee z and they want to move to building 0.
+From building 3 we have employee c and they want to move to building 4.
+From building 4 we don't have any requests.
+We can achieve the requests of users x and b by swapping their places.
+We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
+
+ +

Example 2:

+ +
Input: n = 3, requests = [[0,0],[1,2],[2,1]]
+Output: 3
+Explantion: Let's see the requests:
+From building 0 we have employee x and they want to stay in the same building 0.
+From building 1 we have employee y and they want to move to building 2.
+From building 2 we have employee z and they want to move to building 1.
+We can achieve all the requests. 
+ +

Example 3:

+ +
Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
+Output: 4
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 20
  • +
  • 1 <= requests.length <= 16
  • +
  • requests[i].length == 2
  • +
  • 0 <= fromi, toi < n
  • +
+
\ No newline at end of file From e4b361b95a03fa5b5abae3f73a535f5c0dd00437 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jul 2023 21:29:48 +0530 Subject: [PATCH 1181/3167] Time: 97 ms (85.29%), Space: 8.7 MB (75.49%) - LeetHub --- ...number-of-achievable-transfer-requests.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1601-maximum-number-of-achievable-transfer-requests/1601-maximum-number-of-achievable-transfer-requests.cpp diff --git a/1601-maximum-number-of-achievable-transfer-requests/1601-maximum-number-of-achievable-transfer-requests.cpp b/1601-maximum-number-of-achievable-transfer-requests/1601-maximum-number-of-achievable-transfer-requests.cpp new file mode 100644 index 00000000..5a6ae294 --- /dev/null +++ b/1601-maximum-number-of-achievable-transfer-requests/1601-maximum-number-of-achievable-transfer-requests.cpp @@ -0,0 +1,41 @@ +class Solution { + +private: + void helper(int idx, int n, int cur, vector>& requests, vector& dp, int& ans) + { + if(idx == n) + { + for(auto itr : dp) + { + if(itr != 0) + return; + } + + ans = max(ans, cur); + return; + } + + helper(idx+1, n, cur, requests, dp , ans); + + --dp[requests[idx][0]]; + ++dp[requests[idx][1]]; + + helper(idx+1, n ,cur+1, requests, dp, ans); + + ++dp[requests[idx][0]]; + --dp[requests[idx][1]]; + } + +public: + int maximumRequests(int n, vector>& requests) { + + int ans = 0, sz = requests.size(); + + vector dp(n, 0); + + helper(0, sz, 0, requests,dp, ans); + + return ans; + + } +}; \ No newline at end of file From 055b7504fbafc4ed56ba67744bae41bd0768f5bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:18:50 +0530 Subject: [PATCH 1182/3167] Create README - LeetHub --- 0001-two-sum/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0001-two-sum/README.md diff --git a/0001-two-sum/README.md b/0001-two-sum/README.md new file mode 100644 index 00000000..140e3104 --- /dev/null +++ b/0001-two-sum/README.md @@ -0,0 +1,38 @@ +

1. Two Sum

Easy


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

+ +

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

+ +

You can return the answer in any order.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 104
  • +
  • -109 <= nums[i] <= 109
  • +
  • -109 <= target <= 109
  • +
  • Only one valid answer exists.
  • +
+ +

 

+Follow-up: Can you come up with an algorithm that is less than O(n2time complexity?
\ No newline at end of file From a30f14ec44ff6599944691168d11ce0eb5cfe8e7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:18:51 +0530 Subject: [PATCH 1183/3167] Attach NOTES - LeetHub --- 0001-two-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0001-two-sum/NOTES.md diff --git a/0001-two-sum/NOTES.md b/0001-two-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0001-two-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From eb465dec1f576dac6cf2adf30c61b3a2e763ef4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:18:55 +0530 Subject: [PATCH 1184/3167] Time: 11 ms (84.45%), Space: 11.9 MB (10.77%) - LeetHub --- 0001-two-sum/0001-two-sum.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0001-two-sum/0001-two-sum.cpp diff --git a/0001-two-sum/0001-two-sum.cpp b/0001-two-sum/0001-two-sum.cpp new file mode 100644 index 00000000..6f51642c --- /dev/null +++ b/0001-two-sum/0001-two-sum.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + + int n = nums.size(); + + unordered_map mp; + + for(int i = 0; i < n; ++i) + mp[nums[i]] = i; + + for(int i = 0; i < n; ++i) + { + if(mp.find(target-nums[i]) != mp.end()) + { + if(i != mp[target - nums[i]]) + return {i, mp[target - nums[i]]}; + } + } + + return {-1,-1}; + + } +}; \ No newline at end of file From d16f2831c7b75487abdeb29dc73dcc769beff54c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:20:23 +0530 Subject: [PATCH 1185/3167] Attach NOTES - LeetHub From 3b5c73334cd1d6451053ee4122556922a65e06fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:20:27 +0530 Subject: [PATCH 1186/3167] Time: 12 ms (80.94%), Space: 10.7 MB (50.85%) - LeetHub --- 0001-two-sum/0001-two-sum.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/0001-two-sum/0001-two-sum.cpp b/0001-two-sum/0001-two-sum.cpp index 6f51642c..78b71a2f 100644 --- a/0001-two-sum/0001-two-sum.cpp +++ b/0001-two-sum/0001-two-sum.cpp @@ -6,19 +6,16 @@ class Solution { unordered_map mp; - for(int i = 0; i < n; ++i) - mp[nums[i]] = i; - for(int i = 0; i < n; ++i) { - if(mp.find(target-nums[i]) != mp.end()) + if(mp.find(target - nums[i]) != mp.end()) { - if(i != mp[target - nums[i]]) - return {i, mp[target - nums[i]]}; + return {i, mp[target - nums[i]]}; } + mp[nums[i]] = i; } - return {-1,-1}; + return {}; } }; \ No newline at end of file From 823604da4ee4b990ea558f656b74eab6608a3ef5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:28:28 +0530 Subject: [PATCH 1187/3167] Attach NOTES - LeetHub From 5774b4b23b60d6eac156fc39d8d96d9f7b60a008 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:28:32 +0530 Subject: [PATCH 1188/3167] Time: 11 ms (45.88%), Space: 23.5 MB (6.65%) - LeetHub --- ...122-best-time-to-buy-and-sell-stock-ii.cpp | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp index 78e7b9c0..f58ba5a2 100644 --- a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp +++ b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp @@ -1,23 +1,38 @@ class Solution { -public: - int maxProfit(vector& prices) { +private: + int helper(int idx, int buy, int n, vector& prices, vector>& dp) + { + if(idx == n) + return 0; - int n = prices.size(); + if(dp[idx][buy] != -1) + return dp[idx][buy]; - int aheadBuy, aheadNotBuy, currBuy, currNotBuy; - aheadBuy = aheadNotBuy = 0; + int take = 0, notTake = 0, notTake2 = 0, take2 = 0; - for(int i = n-1; i>=0; --i) + if(buy) { - currBuy = max(-prices[i] + aheadNotBuy, aheadBuy); - - currNotBuy = max(prices[i] + aheadBuy, aheadNotBuy); - - aheadBuy = currBuy; - aheadNotBuy = currNotBuy; + take = -prices[idx] + helper(idx+1, 0, n, prices, dp); + notTake = helper(idx+1, buy, n, prices, dp); } + else + { + take2 = prices[idx] + helper(idx+1, 1, n, prices, dp); + notTake2 = helper(idx+1, buy, n, prices, dp); + } + + return dp[idx][buy] = max({take, notTake, take2, notTake2}); + + } +public: + int maxProfit(vector& prices) { + + int n = prices.size(); + + vector> dp(n, vector(2, -1)); + + return helper(0, 1, n, prices,dp); - return aheadBuy; } }; \ No newline at end of file From b3e6dd6a0fbe911845d6ab0cf119994591885eb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:38:56 +0530 Subject: [PATCH 1189/3167] Attach NOTES - LeetHub From e3e1eac3a92036e325f9bf7cc5f22ff1e7d248f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 07:38:59 +0530 Subject: [PATCH 1190/3167] Time: 7 ms (75.00%), Space: 13.1 MB (37.68%) - LeetHub --- ...122-best-time-to-buy-and-sell-stock-ii.cpp | 39 ++++++------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp index f58ba5a2..c40923dc 100644 --- a/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp +++ b/0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.cpp @@ -1,38 +1,21 @@ class Solution { - -private: - int helper(int idx, int buy, int n, vector& prices, vector>& dp) - { - if(idx == n) - return 0; - - if(dp[idx][buy] != -1) - return dp[idx][buy]; - - int take = 0, notTake = 0, notTake2 = 0, take2 = 0; - - if(buy) - { - take = -prices[idx] + helper(idx+1, 0, n, prices, dp); - notTake = helper(idx+1, buy, n, prices, dp); - } - else - { - take2 = prices[idx] + helper(idx+1, 1, n, prices, dp); - notTake2 = helper(idx+1, buy, n, prices, dp); - } - - return dp[idx][buy] = max({take, notTake, take2, notTake2}); - - } public: int maxProfit(vector& prices) { int n = prices.size(); - vector> dp(n, vector(2, -1)); + int currBuy, currNotBuy, aheadBuy = 0, aheadNotBuy = 0; + + for(int i = n-1; i >=0 ; --i) + { + currBuy = max(-prices[i] + aheadNotBuy, aheadBuy); + currNotBuy = max(prices[i] + aheadBuy, aheadNotBuy); + + aheadBuy = currBuy; + aheadNotBuy = currNotBuy; + } - return helper(0, 1, n, prices,dp); + return aheadBuy; } }; \ No newline at end of file From 4d3322e839b756c7a1b8b504ccdc8125c8d1dcf6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:00:19 +0530 Subject: [PATCH 1191/3167] Attach NOTES - LeetHub From 69aa90cc107065931d75a9a5ccf42e0200cbb9b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:00:23 +0530 Subject: [PATCH 1192/3167] Time: 55 ms (76.92%), Space: 30 MB (87.84%) - LeetHub --- .../0974-subarray-sums-divisible-by-k.cpp | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp b/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp index a18694dc..429752aa 100644 --- a/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp +++ b/0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp @@ -2,20 +2,22 @@ class Solution { public: int subarraysDivByK(vector& nums, int k) { - int sum = 0, n = nums.size(); - vector cnt(k,0); + int n = nums.size(), sum = 0; - for(auto itr : nums) - { - sum += (itr % k + k) % k; - ++cnt[sum%k]; - } + vector v(k,0); - int res = cnt[0]; - for(auto itr : cnt) + for(int i = 0; i < n; ++i) { - res += (itr * (itr - 1))/2; + sum += (nums[i]%k + k)%k; + ++v[sum%k]; } - return res; + + int ans = v[0]; + + for(auto itr : v) + ans += (itr * (itr-1))/2; + + return ans; + } }; \ No newline at end of file From d119bd76ff029b5163368eb9d00b659bf607d8b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 09:07:07 +0530 Subject: [PATCH 1193/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0442-find-all-duplicates-in-an-array/README.md diff --git a/0442-find-all-duplicates-in-an-array/README.md b/0442-find-all-duplicates-in-an-array/README.md new file mode 100644 index 00000000..1aaf2301 --- /dev/null +++ b/0442-find-all-duplicates-in-an-array/README.md @@ -0,0 +1,25 @@ +

442. Find All Duplicates in an Array

Medium


Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.

+ +

You must write an algorithm that runs in O(n) time and uses only constant extra space.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= nums[i] <= n
  • +
  • Each element in nums appears once or twice.
  • +
+
\ No newline at end of file From 4c8b4f74c3c1d36c5ed27e5b5b4acf27e4bb857b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 09:07:11 +0530 Subject: [PATCH 1194/3167] Time: 56 ms (91.40%), Space: 33.5 MB (66.14%) - LeetHub --- .../0442-find-all-duplicates-in-an-array.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.cpp diff --git a/0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.cpp b/0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.cpp new file mode 100644 index 00000000..0912efd0 --- /dev/null +++ b/0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector findDuplicates(vector& nums) { + + int n = nums.size(); + + vector res ; + + for(int i = 0; i < n; ++i) + { + if(nums[abs(nums[i])-1] < 0) + { + res.push_back(abs(nums[i])); + } + else + { + nums[abs(nums[i])-1] = - nums[abs(nums[i])-1]; + } + } + + return res; + + } +}; From 3c7a50739d659001595ae834bde5c2f0d7c39d56 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 20:18:24 +0530 Subject: [PATCH 1195/3167] Attach NOTES - LeetHub --- 0011-container-with-most-water/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0011-container-with-most-water/NOTES.md diff --git a/0011-container-with-most-water/NOTES.md b/0011-container-with-most-water/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0011-container-with-most-water/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7887cc21bcc6fa689d1c2348cd9a3ef23998a4fd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 20:18:27 +0530 Subject: [PATCH 1196/3167] Time: 84 ms (98.19%), Space: 58.9 MB (99.61%) - LeetHub --- .../0011-container-with-most-water.cpp | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/0011-container-with-most-water/0011-container-with-most-water.cpp b/0011-container-with-most-water/0011-container-with-most-water.cpp index e5ce0f2b..d7ba1774 100644 --- a/0011-container-with-most-water/0011-container-with-most-water.cpp +++ b/0011-container-with-most-water/0011-container-with-most-water.cpp @@ -2,23 +2,27 @@ class Solution { public: int maxArea(vector& height) { - int left = 0, right = height.size()-1; - int maxArea = 0; + int start = 0, end = height.size()-1; - while(left < right) + int ans = 0; + + while(start < end) { - int width = right - left; - int h = min(height[left],height[right]); + int width = end - start; + + int area = min(height[start], height[end]) * width; - maxArea = max(maxArea, width*h); + ans = max(ans, area); - if(height[left] < height[right]) - ++left; - else if(height[left] > height[right]) - --right; + if(height[start] < height[end]) + ++start; + else if(height[end] < height[start]) + --end; else - ++left , --right; + ++start, --end; } - return maxArea; + + return ans; + } }; \ No newline at end of file From 69902638cb50f20476c941c80217fa4d27317401 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 21:09:02 +0530 Subject: [PATCH 1197/3167] Create README - LeetHub --- 0859-buddy-strings/README.md | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0859-buddy-strings/README.md diff --git a/0859-buddy-strings/README.md b/0859-buddy-strings/README.md new file mode 100644 index 00000000..09c932fb --- /dev/null +++ b/0859-buddy-strings/README.md @@ -0,0 +1,38 @@ +

859. Buddy Strings

Easy


Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.

+ +

Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].

+ +
    +
  • For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
  • +
+ +

 

+

Example 1:

+ +
Input: s = "ab", goal = "ba"
+Output: true
+Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
+
+ +

Example 2:

+ +
Input: s = "ab", goal = "ab"
+Output: false
+Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
+
+ +

Example 3:

+ +
Input: s = "aa", goal = "aa"
+Output: true
+Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length, goal.length <= 2 * 104
  • +
  • s and goal consist of lowercase letters.
  • +
+
\ No newline at end of file From 6dad8819d01019be0864be2917b20d8e87671504 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 21:09:03 +0530 Subject: [PATCH 1198/3167] Attach NOTES - LeetHub --- 0859-buddy-strings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0859-buddy-strings/NOTES.md diff --git a/0859-buddy-strings/NOTES.md b/0859-buddy-strings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0859-buddy-strings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c04f1dfb276fcc72223a95b8de2979ca58adf3cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 21:09:06 +0530 Subject: [PATCH 1199/3167] Time: 11 ms (11.19%), Space: 6.9 MB (85.37%) - LeetHub --- 0859-buddy-strings/0859-buddy-strings.cpp | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0859-buddy-strings/0859-buddy-strings.cpp diff --git a/0859-buddy-strings/0859-buddy-strings.cpp b/0859-buddy-strings/0859-buddy-strings.cpp new file mode 100644 index 00000000..c42ee33a --- /dev/null +++ b/0859-buddy-strings/0859-buddy-strings.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + bool buddyStrings(string s, string goal) { + + int n = s.size(), m = goal.size(); + + if(n != m) + return false; + + int cnt = 0, have = 0; + + unordered_map mp, mp2; + + for(int i = 0; i < n; ++i) + { + if(s[i] != goal[i]) + ++cnt; + + ++mp[s[i]]; + + ++mp2[goal[i]]; + + have = max(have, mp[s[i]]); + } + + for(auto itr : mp) + { + if(mp[itr.first] != mp2[itr.first]) + return false; + } + + return cnt == 2 or (cnt == 0 and have > 1); + + } +}; \ No newline at end of file From 809c0c065effa4b1e2f7570936b1c1270d911be9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 21:10:04 +0530 Subject: [PATCH 1200/3167] Attach NOTES - LeetHub From 840a777372ac0b25e92d2b177b0eddd06d3bb690 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jul 2023 21:10:07 +0530 Subject: [PATCH 1201/3167] Time: 11 ms (11.19%), Space: 6.9 MB (85.37%) - LeetHub From e85b69f1a0c046c4537d0264fcfa6806f9036d35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jul 2023 23:17:23 +0530 Subject: [PATCH 1202/3167] Create README - LeetHub --- 0137-single-number-ii/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0137-single-number-ii/README.md diff --git a/0137-single-number-ii/README.md b/0137-single-number-ii/README.md new file mode 100644 index 00000000..72a3e4fe --- /dev/null +++ b/0137-single-number-ii/README.md @@ -0,0 +1,21 @@ +

137. Single Number II

Medium


Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it.

+ +

You must implement a solution with a linear runtime complexity and use only constant extra space.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • Each element in nums appears exactly three times except for one element which appears once.
  • +
+
\ No newline at end of file From 98941b62c05a6d87153e3175cfa4642134f7c693 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jul 2023 23:17:27 +0530 Subject: [PATCH 1203/3167] Time: 3 ms (98.52%), Space: 9.4 MB (72.62%) - LeetHub --- 0137-single-number-ii/0137-single-number-ii.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0137-single-number-ii/0137-single-number-ii.cpp diff --git a/0137-single-number-ii/0137-single-number-ii.cpp b/0137-single-number-ii/0137-single-number-ii.cpp new file mode 100644 index 00000000..a173bb54 --- /dev/null +++ b/0137-single-number-ii/0137-single-number-ii.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int singleNumber(vector& nums) { + + int ones = 0, twos = 0; + + for(auto itr : nums) + { + ones = (ones ^ itr) & (~twos); + twos = (twos ^ itr) & (~ones); + } + + return ones; + + } +}; \ No newline at end of file From 3c3e61cc90aba84abfd532c5edc4de822abbad3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jul 2023 22:08:16 +0530 Subject: [PATCH 1204/3167] Attach NOTES - LeetHub --- 1493-longest-subarray-of-1s-after-deleting-one-element/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1493-longest-subarray-of-1s-after-deleting-one-element/NOTES.md diff --git a/1493-longest-subarray-of-1s-after-deleting-one-element/NOTES.md b/1493-longest-subarray-of-1s-after-deleting-one-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1493-longest-subarray-of-1s-after-deleting-one-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 92d744b82778d4a7369d886cddf0a3d14f395f66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jul 2023 22:08:20 +0530 Subject: [PATCH 1205/3167] Time: 34 ms (95.98%), Space: 36.4 MB (98.32%) - LeetHub --- ...array-of-1s-after-deleting-one-element.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1493-longest-subarray-of-1s-after-deleting-one-element/1493-longest-subarray-of-1s-after-deleting-one-element.cpp diff --git a/1493-longest-subarray-of-1s-after-deleting-one-element/1493-longest-subarray-of-1s-after-deleting-one-element.cpp b/1493-longest-subarray-of-1s-after-deleting-one-element/1493-longest-subarray-of-1s-after-deleting-one-element.cpp new file mode 100644 index 00000000..ed1b879c --- /dev/null +++ b/1493-longest-subarray-of-1s-after-deleting-one-element/1493-longest-subarray-of-1s-after-deleting-one-element.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int longestSubarray(vector& nums) { + + int i = 0, j = 0, n = nums.size(); + + int ans = 0, zero = 0; + + while(j < n) + { + if(nums[j] == 0) + ++zero; + + while(zero > 1){ + if(nums[i] == 0) + --zero; + ++i; + } + + ans = max(ans, j-i); + + ++j; + } + + return ans; + + } +}; \ No newline at end of file From 84d3eb430773e0455bed676ac658a227053b658e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jul 2023 22:15:09 +0530 Subject: [PATCH 1206/3167] Create README - LeetHub --- 0209-minimum-size-subarray-sum/README.md | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0209-minimum-size-subarray-sum/README.md diff --git a/0209-minimum-size-subarray-sum/README.md b/0209-minimum-size-subarray-sum/README.md new file mode 100644 index 00000000..a87e5a79 --- /dev/null +++ b/0209-minimum-size-subarray-sum/README.md @@ -0,0 +1,33 @@ +

209. Minimum Size Subarray Sum

Medium


Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

+ +

 

+

Example 1:

+ +
Input: target = 7, nums = [2,3,1,2,4,3]
+Output: 2
+Explanation: The subarray [4,3] has the minimal length under the problem constraint.
+
+ +

Example 2:

+ +
Input: target = 4, nums = [1,4,4]
+Output: 1
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= target <= 109
  • +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 104
  • +
+ +

 

+Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).
\ No newline at end of file From 57720b4d0d72cd5b2542f2af09b4fe7bb92fc297 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jul 2023 22:15:10 +0530 Subject: [PATCH 1207/3167] Attach NOTES - LeetHub --- 0209-minimum-size-subarray-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0209-minimum-size-subarray-sum/NOTES.md diff --git a/0209-minimum-size-subarray-sum/NOTES.md b/0209-minimum-size-subarray-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0209-minimum-size-subarray-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 140d04b0a2a0b38e322d725e7ac5d9ff84db6573 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jul 2023 22:15:14 +0530 Subject: [PATCH 1208/3167] Time: 41 ms (70.46%), Space: 28.2 MB (71.68%) - LeetHub --- .../0209-minimum-size-subarray-sum.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.cpp diff --git a/0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.cpp b/0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.cpp new file mode 100644 index 00000000..4950b0fa --- /dev/null +++ b/0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minSubArrayLen(int target, vector& nums) { + + int i = 0, j = 0, n = nums.size(); + + int sum = 0, ans = INT_MAX; + + while(j < n) + { + sum += nums[j]; + + while(sum >= target) + { + ans = min(ans, j - i + 1); + + sum -= nums[i++]; + } + + ++j; + } + + return (ans == INT_MAX ? 0 : ans); + + } +}; \ No newline at end of file From 4a8fe8324cca9ff5ae76fb24e13fd10cff600096 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jul 2023 08:20:41 +0530 Subject: [PATCH 1209/3167] Attach NOTES - LeetHub --- 0081-search-in-rotated-sorted-array-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0081-search-in-rotated-sorted-array-ii/NOTES.md diff --git a/0081-search-in-rotated-sorted-array-ii/NOTES.md b/0081-search-in-rotated-sorted-array-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0081-search-in-rotated-sorted-array-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4eb5b9a1cf3b4e245b0638976104cbdbbd94481e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jul 2023 08:20:45 +0530 Subject: [PATCH 1210/3167] Time: 7 ms (61.60%), Space: 13.9 MB (90.31%) - LeetHub --- ...0081-search-in-rotated-sorted-array-ii.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp diff --git a/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp b/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp new file mode 100644 index 00000000..f86435f6 --- /dev/null +++ b/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + bool search(vector& nums, int target) { + + int low = 0, high = nums.size()-1; + + while(low <= high) + { + int mid = (low + high) >> 1; + + if(nums[mid] == target) + return true; + + if((nums[low] == nums[mid]) and (nums[mid] == nums[high])) + { + ++low; + --high; + } + else if(nums[low] <= nums[mid]) + { + if(nums[low] <= target and target < nums[mid]) + high = mid-1; + else + low = mid+1; + } + else + { + if(nums[mid] < target and target <= nums[high]) + low = mid+1; + else + high = mid-1; + } + } + + return false; + + } +}; \ No newline at end of file From d87b95e635c8999ee90e5224f3f757d8e783c9f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:04:48 +0530 Subject: [PATCH 1211/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2024-maximize-the-confusion-of-an-exam/README.md diff --git a/2024-maximize-the-confusion-of-an-exam/README.md b/2024-maximize-the-confusion-of-an-exam/README.md new file mode 100644 index 00000000..33a9157c --- /dev/null +++ b/2024-maximize-the-confusion-of-an-exam/README.md @@ -0,0 +1,47 @@ +

2024. Maximize the Confusion of an Exam

Medium


A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row).

+ +

You are given a string answerKey, where answerKey[i] is the original answer to the ith question. In addition, you are given an integer k, the maximum number of times you may perform the following operation:

+ +
    +
  • Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F').
  • +
+ +

Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.

+ +

 

+

Example 1:

+ +
Input: answerKey = "TTFF", k = 2
+Output: 4
+Explanation: We can replace both the 'F's with 'T's to make answerKey = "TTTT".
+There are four consecutive 'T's.
+
+ +

Example 2:

+ +
Input: answerKey = "TFFT", k = 1
+Output: 3
+Explanation: We can replace the first 'T' with an 'F' to make answerKey = "FFFT".
+Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF".
+In both cases, there are three consecutive 'F's.
+
+ +

Example 3:

+ +
Input: answerKey = "TTFTTFTT", k = 1
+Output: 5
+Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT"
+Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT". 
+In both cases, there are five consecutive 'T's.
+
+ +

 

+

Constraints:

+ +
    +
  • n == answerKey.length
  • +
  • 1 <= n <= 5 * 104
  • +
  • answerKey[i] is either 'T' or 'F'
  • +
  • 1 <= k <= n
  • +
+
\ No newline at end of file From 39242d5235cb120a8678380d68b643a49d96bd6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:04:48 +0530 Subject: [PATCH 1212/3167] Attach NOTES - LeetHub --- 2024-maximize-the-confusion-of-an-exam/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2024-maximize-the-confusion-of-an-exam/NOTES.md diff --git a/2024-maximize-the-confusion-of-an-exam/NOTES.md b/2024-maximize-the-confusion-of-an-exam/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2024-maximize-the-confusion-of-an-exam/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 31e6119cadb679c6b6f82c58df2d5ecd53e35654 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:04:52 +0530 Subject: [PATCH 1213/3167] Time: 24 ms (83.37%), Space: 10.1 MB (59.24%) - LeetHub --- ...2024-maximize-the-confusion-of-an-exam.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2024-maximize-the-confusion-of-an-exam/2024-maximize-the-confusion-of-an-exam.cpp diff --git a/2024-maximize-the-confusion-of-an-exam/2024-maximize-the-confusion-of-an-exam.cpp b/2024-maximize-the-confusion-of-an-exam/2024-maximize-the-confusion-of-an-exam.cpp new file mode 100644 index 00000000..6f15163f --- /dev/null +++ b/2024-maximize-the-confusion-of-an-exam/2024-maximize-the-confusion-of-an-exam.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int maxConsecutiveAnswers(string answerKey, int k) { + + int i = 0, j = 0, n = answerKey.size(); + + int t = 0, f = 0, ans = 0; + + while(j < n) + { + if(answerKey[j] == 'T') + ++t; + if(answerKey[j] == 'F') + ++f; + + while(t > k and f > k) + { + if(answerKey[i] == 'T') + --t; + else + --f; + ++i; + } + + ans = max(ans, j - i + 1); + + ++j; + } + + return ans; + + } +}; \ No newline at end of file From 69a2251bf0d0215328a4ff6e8aa69d207d7578bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:06:36 +0530 Subject: [PATCH 1214/3167] Attach NOTES - LeetHub From 767c1e9f2e22f41e0255d71b4d656503c9d8cda3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:06:40 +0530 Subject: [PATCH 1215/3167] Time: 24 ms (83.37%), Space: 10.1 MB (59.24%) - LeetHub From f382f8e63fbfefc0dedf6aee8b5f94b6e9c9d0be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:07:15 +0530 Subject: [PATCH 1216/3167] Attach NOTES - LeetHub From 38f38d1a6254bbce54aba50cf15312420c992fef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:07:44 +0530 Subject: [PATCH 1217/3167] Attach NOTES - LeetHub From c9973cfb7cd712eaffa7e56fdafacdb0bd3a5d7c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:08:02 +0530 Subject: [PATCH 1218/3167] Attach NOTES - LeetHub From dda319099226e6ad7a5ad58e9eb0cb0d47f59f22 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:08:05 +0530 Subject: [PATCH 1219/3167] Time: 24 ms (83.37%), Space: 10.1 MB (59.24%) - LeetHub From 92cd77d14b522defda6ba54790be72debadc3d49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 08:01:09 +0530 Subject: [PATCH 1220/3167] Create README - LeetHub --- Find triplets with zero sum - GFG/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Find triplets with zero sum - GFG/README.md diff --git a/Find triplets with zero sum - GFG/README.md b/Find triplets with zero sum - GFG/README.md new file mode 100644 index 00000000..f9e99296 --- /dev/null +++ b/Find triplets with zero sum - GFG/README.md @@ -0,0 +1,14 @@ +# Find triplets with zero sum +## Easy +

Given an array arr[] of n integers. Check whether it contains a triplet that sums up to zero. 

+

Note: Return 1, if there is at least one triplet following the condition else return 0.

+

Example 1:

+
Input: n = 5, arr[] = {0, -1, 2, -3, 1}
+Output: 1
+Explanation: 0, -1 and 1 forms a triplet
+with sum equal to 0.
+

Example 2:

+
Input: n = 3, arr[] = {1, 2, 3}
+Output: 0
+Explanation: No triplet with zero sum exists. 
+


Your Task:
You don't need to read input or print anything. Your task is to complete the boolean function findTriplets() which takes the array arr[] and the size of the array (n) as inputs and print 1 if the function returns true else print 0 if the function returns false. 

Expected Time Complexity: O(n2)
Expected Auxiliary Space: O(1)

Constrains:
1 <= n <= 104

-106 <= Ai <= 106

\ No newline at end of file From 53b29e3507a5b8939d3a10bce368777895ce1a7c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jul 2023 08:01:10 +0530 Subject: [PATCH 1221/3167] Added solution - LeetHub --- .../find-triplets-with-zero-sum.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Find triplets with zero sum - GFG/find-triplets-with-zero-sum.java diff --git a/Find triplets with zero sum - GFG/find-triplets-with-zero-sum.java b/Find triplets with zero sum - GFG/find-triplets-with-zero-sum.java new file mode 100644 index 00000000..ba976361 --- /dev/null +++ b/Find triplets with zero sum - GFG/find-triplets-with-zero-sum.java @@ -0,0 +1,58 @@ +//{ Driver Code Starts +import java.util.*; +class Triplets{ + 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 Date: Sun, 9 Jul 2023 00:33:08 +0530 Subject: [PATCH 1222/3167] Create README - LeetHub --- 2551-put-marbles-in-bags/README.md | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2551-put-marbles-in-bags/README.md diff --git a/2551-put-marbles-in-bags/README.md b/2551-put-marbles-in-bags/README.md new file mode 100644 index 00000000..ba6efec6 --- /dev/null +++ b/2551-put-marbles-in-bags/README.md @@ -0,0 +1,41 @@ +

2551. Put Marbles in Bags

Hard


You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k.

+ +

Divide the marbles into the k bags according to the following rules:

+ +
    +
  • No bag is empty.
  • +
  • If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag.
  • +
  • If a bag consists of all the marbles with an index from i to j inclusively, then the cost of the bag is weights[i] + weights[j].
  • +
+ +

The score after distributing the marbles is the sum of the costs of all the k bags.

+ +

Return the difference between the maximum and minimum scores among marble distributions.

+ +

 

+

Example 1:

+ +
Input: weights = [1,3,5,1], k = 2
+Output: 4
+Explanation: 
+The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. 
+The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. 
+Thus, we return their difference 10 - 6 = 4.
+
+ +

Example 2:

+ +
Input: weights = [1, 3], k = 2
+Output: 0
+Explanation: The only distribution possible is [1],[3]. 
+Since both the maximal and minimal score are the same, we return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= weights.length <= 105
  • +
  • 1 <= weights[i] <= 109
  • +
+
\ No newline at end of file From 565db473012ff4305b52ad5d38731abc42ed497c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jul 2023 00:33:08 +0530 Subject: [PATCH 1223/3167] Attach NOTES - LeetHub --- 2551-put-marbles-in-bags/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2551-put-marbles-in-bags/NOTES.md diff --git a/2551-put-marbles-in-bags/NOTES.md b/2551-put-marbles-in-bags/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2551-put-marbles-in-bags/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e9e5f02cdf4cc88b21c0c57568424bb0d6acbf4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jul 2023 22:50:48 +0530 Subject: [PATCH 1224/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2272-substring-with-largest-variance/README.md diff --git a/2272-substring-with-largest-variance/README.md b/2272-substring-with-largest-variance/README.md new file mode 100644 index 00000000..d8050e50 --- /dev/null +++ b/2272-substring-with-largest-variance/README.md @@ -0,0 +1,36 @@ +

2272. Substring With Largest Variance

Hard


The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not be the same.

+ +

Given a string s consisting of lowercase English letters only, return the largest variance possible among all substrings of s.

+ +

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

+ +

 

+

Example 1:

+ +
Input: s = "aababbb"
+Output: 3
+Explanation:
+All possible variances along with their respective substrings are listed below:
+- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
+- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
+- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
+- Variance 3 for substring "babbb".
+Since the largest possible variance is 3, we return it.
+
+ +

Example 2:

+ +
Input: s = "abcde"
+Output: 0
+Explanation:
+No letter occurs more than once in s, so the variance of every substring is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From 89af989b812aa961c00aea330a5910839d4f4939 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jul 2023 22:50:49 +0530 Subject: [PATCH 1225/3167] Attach NOTES - LeetHub --- 2272-substring-with-largest-variance/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2272-substring-with-largest-variance/NOTES.md diff --git a/2272-substring-with-largest-variance/NOTES.md b/2272-substring-with-largest-variance/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2272-substring-with-largest-variance/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 96dfc10cbfb0bd20772905b4d2a1f31f747ae29e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jul 2023 22:50:52 +0530 Subject: [PATCH 1226/3167] Time: 454 ms (18.87%), Space: 6.8 MB (96.23%) - LeetHub --- .../2272-substring-with-largest-variance.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2272-substring-with-largest-variance/2272-substring-with-largest-variance.cpp diff --git a/2272-substring-with-largest-variance/2272-substring-with-largest-variance.cpp b/2272-substring-with-largest-variance/2272-substring-with-largest-variance.cpp new file mode 100644 index 00000000..1b3624fb --- /dev/null +++ b/2272-substring-with-largest-variance/2272-substring-with-largest-variance.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int largestVariance(string s) { + int ans = 0; + vector freq(26); + for(auto& c:s){ + freq[c-'a']++; + } + for(char ch1='a';ch1<='z';ch1++){ + for(char ch2='a';ch2<='z';ch2++){ + if(ch1==ch2 or !freq[ch1-'a'] or !freq[ch2-'a']){ + continue; + } + for(int rev=1;rev<=2;rev++){ + int cnt1 = 0,cnt2 = 0; + for(auto& c:s){ + cnt1 += c==ch1; + cnt2 += c==ch2; + if(cnt10 and cnt2>0){ + ans = max(ans,cnt1-cnt2); + } + } + reverse(s.begin(),s.end()); + } + } + } + return ans; + } +}; \ No newline at end of file From 3cfbb2966bc2d601cbf715c6bfd8c26086bad20d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jul 2023 23:19:07 +0530 Subject: [PATCH 1227/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Find kth element of spiral matrix - GFG/README.md diff --git a/Find kth element of spiral matrix - GFG/README.md b/Find kth element of spiral matrix - GFG/README.md new file mode 100644 index 00000000..a3977203 --- /dev/null +++ b/Find kth element of spiral matrix - GFG/README.md @@ -0,0 +1,26 @@ +# Find kth element of spiral matrix +## Medium +

Given a matrix with n rows and m columns. Your task is to find the kth element which is obtained while traversing the matrix spirally. You need to complete the method findK which takes four arguments the first argument is the matrix A and the next two arguments will be n and m denoting the size of the matrix A and then the forth argument is an integer k. The function will return the kth element obtained while traversing the matrix spirally.

+

Example 1:

+
Input:
+n = 4, m = 4, k = 10
+A[][] = {{1  2  3  4},
+         {5  6  7  8},
+         {9  10 11 12},
{13 14 15 16}} +Output: +13 +Explanation:
+The spiral order of matrix will look like 1->2->3->4->8->12->16->15->14->13->9->5->6->7->11->10. So the 10th element in this order is 13.
+

Example 2:

+
Input:
+n = 3, m = 3, k = 4
+A[][] = {{1 2 3},
+         {4 5 6},
+         {7 8 9}}
+Output:
+6
+Explanation:
+The spiral order of matrix will look like 1->2->3->6->9->8->7->4->5. So the 4th element in this order is 6.
+

Your Task:
You only need to implement the given function findK(). Do not read input, instead use the arguments given in the function. Return the K'th element obtained by traversing matrix spirally.

+

Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)

+

Constraints:
1<=n,m<=103
1<=k<=n*m

\ No newline at end of file From 7a3858fa35dd907a56ab751e6f723cb0f36cad34 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jul 2023 23:19:08 +0530 Subject: [PATCH 1228/3167] Added solution - LeetHub --- .../find-kth-element-of-spiral-matrix.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Find kth element of spiral matrix - GFG/find-kth-element-of-spiral-matrix.java diff --git a/Find kth element of spiral matrix - GFG/find-kth-element-of-spiral-matrix.java b/Find kth element of spiral matrix - GFG/find-kth-element-of-spiral-matrix.java new file mode 100644 index 00000000..3f1fbb07 --- /dev/null +++ b/Find kth element of spiral matrix - GFG/find-kth-element-of-spiral-matrix.java @@ -0,0 +1,74 @@ +//{ Driver Code Starts +import java.util.*; + +class Find_Given_Element_Of_Spiral_Matrix +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while(t > 0) + { + int n = sc.nextInt(); + int m = sc.nextInt(); + int k = sc.nextInt(); + int arr[][] = new int[1000][1000]; + for(int i=0; i v = new ArrayList<>(); + + while(top <= bottom && left <= right) + { + for(int i = left; i <= right; ++i) + { + v.add(A[top][i]); + } + ++top; + + for(int i = top; i <= bottom; ++i) + { + v.add(A[i][right]); + } + --right; + + for(int i = right; i >= left; --i) + { + v.add(A[bottom][i]); + } + --bottom; + for(int i = bottom; i >= top; --i) + { + v.add(A[i][left]); + } + ++left; + + } + + // for(int i = 0; i < v.size(); ++i) + // System.out.println(v.get(i)); + + return v.get(k-1); + } +} \ No newline at end of file From bfb19267d043de5d670f38d7dc6e943282251128 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 02:31:13 +0530 Subject: [PATCH 1229/3167] Attach NOTES - LeetHub --- 0863-all-nodes-distance-k-in-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0863-all-nodes-distance-k-in-binary-tree/NOTES.md diff --git a/0863-all-nodes-distance-k-in-binary-tree/NOTES.md b/0863-all-nodes-distance-k-in-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0863-all-nodes-distance-k-in-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2f9c008c904b7f8c04fb38b004893d7fd9705a0e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 02:31:17 +0530 Subject: [PATCH 1230/3167] Time: 0 ms (100.00%), Space: 15.1 MB (14.77%) - LeetHub --- ...63-all-nodes-distance-k-in-binary-tree.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0863-all-nodes-distance-k-in-binary-tree/0863-all-nodes-distance-k-in-binary-tree.cpp diff --git a/0863-all-nodes-distance-k-in-binary-tree/0863-all-nodes-distance-k-in-binary-tree.cpp b/0863-all-nodes-distance-k-in-binary-tree/0863-all-nodes-distance-k-in-binary-tree.cpp new file mode 100644 index 00000000..41ad77e8 --- /dev/null +++ b/0863-all-nodes-distance-k-in-binary-tree/0863-all-nodes-distance-k-in-binary-tree.cpp @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { + void dfs(TreeNode* root, unordered_map& m) { + if(!root) return; + if(root->left) { + m.insert({root->left, root}); + } + if(root->right) { + m.insert({root->right, root}); + } + dfs(root->left, m); + dfs(root->right, m); + } +public: + vector distanceK(TreeNode* root, TreeNode* target, int k) { + unordered_map m; //{current, parent} + queue q; + dfs(root, m); + q.push(target); + vector res; + unordered_map visited; + while(!q.empty()) { + int len = q.size(); + while(len--) { + TreeNode* curr = q.front(); q.pop(); + if(!k) res.push_back(curr->val); + visited[curr] = true; + if(curr->left && !visited[curr->left]) { + q.push(curr->left); + } + if(curr->right && !visited[curr->right]) { + q.push(curr->right); + } + if(m.find(curr) != m.end() && visited[m[curr]] == false) { + q.push(m[curr]); + } + } + k--; + if(k < 0) break; + } + return res; + } +}; \ No newline at end of file From 583676c1e2404a5c0cdcfdd80a664966194e69ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:37:35 +0530 Subject: [PATCH 1231/3167] Attach NOTES - LeetHub --- 0075-sort-colors/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0075-sort-colors/NOTES.md diff --git a/0075-sort-colors/NOTES.md b/0075-sort-colors/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0075-sort-colors/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 799c936cf9b42b8d3a8b8f791fabfa08288601ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:37:38 +0530 Subject: [PATCH 1232/3167] Time: 0 ms (100.00%), Space: 8.2 MB (81.59%) - LeetHub --- 0075-sort-colors/0075-sort-colors.cpp | 28 +++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/0075-sort-colors/0075-sort-colors.cpp b/0075-sort-colors/0075-sort-colors.cpp index 5c62fdff..96ed566c 100644 --- a/0075-sort-colors/0075-sort-colors.cpp +++ b/0075-sort-colors/0075-sort-colors.cpp @@ -1,26 +1,24 @@ class Solution { public: void sortColors(vector& nums) { - - int n = nums.size(); - int zero = 0; + int start = 0, end = nums.size()-1; - int start = 0, end = n-1; + int zero = 0; while(start <= end) { - if(nums[start] == 0) - { - swap(nums[zero++],nums[start++]); - } - else if(nums[start] == 2) - { - swap(nums[start], nums[end--]); - } - else - start++; + if(nums[start] == 2) + { + swap(nums[start], nums[end]); + --end; + } + else if(nums[start] == 0) + { + swap(nums[zero++], nums[start++]); + } + else + ++start; } - } }; \ No newline at end of file From 75d02fcdab846d12949c5dea2edd581797e9eff8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:43:12 +0530 Subject: [PATCH 1233/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sort an array of 0s, 1s and 2s - GFG/README.md diff --git a/Sort an array of 0s, 1s and 2s - GFG/README.md b/Sort an array of 0s, 1s and 2s - GFG/README.md new file mode 100644 index 00000000..1f27b04e --- /dev/null +++ b/Sort an array of 0s, 1s and 2s - GFG/README.md @@ -0,0 +1,40 @@ +# Sort an array of 0s, 1s and 2s +## Easy +

Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.

+ +


+Example 1:

+ +
Input: 
+N = 5
+arr[]= {0 2 1 2 0}
+Output:
+0 0 1 2 2
+Explanation:
+0s 1s and 2s are segregated 
+into ascending order.
+ +

Example 2:

+ +
Input: 
+N = 3
+arr[] = {0 1 0}
+Output:
+0 0 1
+Explanation:
+0s 1s and 2s are segregated 
+into ascending order.
+ +


+Your Task:
+You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 <= N <= 10^6
+0 <= A[i] <= 2

+
\ No newline at end of file From 9dd61fcfd17e1ab0e74ef5185119bde9e3db1815 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:43:13 +0530 Subject: [PATCH 1234/3167] Added solution - LeetHub --- .../sort-an-array-of-0s-1s-and-2s.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Sort an array of 0s, 1s and 2s - GFG/sort-an-array-of-0s-1s-and-2s.java diff --git a/Sort an array of 0s, 1s and 2s - GFG/sort-an-array-of-0s-1s-and-2s.java b/Sort an array of 0s, 1s and 2s - GFG/sort-an-array-of-0s-1s-and-2s.java new file mode 100644 index 00000000..8b6d5f39 --- /dev/null +++ b/Sort an array of 0s, 1s and 2s - GFG/sort-an-array-of-0s-1s-and-2s.java @@ -0,0 +1,71 @@ +//{ Driver Code Starts +//Initial template for Java + +import java.io.*; +import java.util.*; + + +// } Driver Code Ends +//User function template for Java + +class Solution +{ + + public static void swap(int a[], int i, int j) + { + int temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + + + public static void sort012(int a[], int n) + { + // code here + + int start = 0, end = n-1; + int zero = 0 ; + + while(start <= end) + { + if(a[start] == 2) + { + swap(a, start, end--); + } + else if(a[start] == 0) + { + swap(a, start++, zero++); + } + else + ++start; + } + } +} + +//{ Driver Code Starts. + +class GFG { + + public static void main (String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(br.readLine().trim()); //Inputting the testcases + while(t-->0){ + int n = Integer.parseInt(br.readLine().trim()); + int arr[] = new int[n]; + String inputLine[] = br.readLine().trim().split(" "); + for(int i=0; i Date: Wed, 12 Jul 2023 22:59:19 +0530 Subject: [PATCH 1235/3167] Create README - LeetHub --- Power Of Numbers - GFG/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Power Of Numbers - GFG/README.md diff --git a/Power Of Numbers - GFG/README.md b/Power Of Numbers - GFG/README.md new file mode 100644 index 00000000..3585c161 --- /dev/null +++ b/Power Of Numbers - GFG/README.md @@ -0,0 +1,17 @@ +# Power Of Numbers +## Medium +

Given a number and its reverse. Find that number raised to the power of its own reverse.
Note: As answers can be very large, print the result modulo 109 + 7.

+

Example 1:

+
Input:
+N = 2, R = 2
+Output: 4
+Explanation: The reverse of 2 is 2 and after raising power of 2 by 2 we get 4 which gives remainder as 4 when divided by 1000000007.
+

Example 2:

+
Input:
+N = 12, R = 21
+Output: 864354781
+Explanation: The reverse of 12 is 21and 1221 when divided by 1000000007 gives remainder as 864354781.
+

Your Task:
You don't need to read input or print anything. You just need to complete the function pow() that takes two parameters N and R denoting the input number and its reverse and returns power of (N to R)mod(109 + 7).

+

Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(LogN).

+

Constraints:
1 <= N <= 109

+

 

\ No newline at end of file From a408f46d3031252f650fb12b56b81dd6df1a41a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 22:59:20 +0530 Subject: [PATCH 1236/3167] Added solution - LeetHub --- Power Of Numbers - GFG/power-of-numbers.java | 93 ++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Power Of Numbers - GFG/power-of-numbers.java diff --git a/Power Of Numbers - GFG/power-of-numbers.java b/Power Of Numbers - GFG/power-of-numbers.java new file mode 100644 index 00000000..604d85f3 --- /dev/null +++ b/Power Of Numbers - GFG/power-of-numbers.java @@ -0,0 +1,93 @@ +//{ Driver Code Starts +//Initial Template for Java + +/*package whatever //do not write package name here */ + +import java.io.*; +import java.util.*; +class Main { + + // compute reverse of a number + public static long rev(long n) + { + long rev_num = 0; + while(n > 0) + { + rev_num = rev_num*10 + n%10; + n = n/10; + } + return rev_num; + } + + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + + //testcases + int T=sc.nextInt(); + while(T-->0) + { + Solution obj=new Solution(); + + int N; + + + //input N + N=sc.nextInt(); + int R=0; + + + // reverse the given number n + R=(int)rev(N); + + //power of the number to it's reverse + long ans=(long)obj.power(N,R); + System.out.println(ans); + + + } + + } +} + + +// } Driver Code Ends + + +//User function Template for Java + + +class Solution +{ + + long mod = 1000000007; + + long expo(long x, long y) + { + long res = 1; + + while(y > 0) + { + if(y % 2 == 1) + { + res = (res * x) % mod; + } + + x = (x* x) % mod; + + y >>= 1; + } + + return res; + } + + long power(int N,int R) + { + //Your code here + + long ans = expo(N, R); + + return ans; + + } + +} From ff6fa40073ffbca0f9d47116ed3f73ac3b9498a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 23:53:10 +0530 Subject: [PATCH 1237/3167] Attach NOTES - LeetHub From 3979d11cecaa33cb34ed6c12584d661677d2b1bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jul 2023 23:53:13 +0530 Subject: [PATCH 1238/3167] Time: 235 ms (26.45%), Space: 58.8 MB (40.01%) - LeetHub From 2ab0c20c3cb619371714f5991693d8ada14f0b92 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jul 2023 23:03:23 +0530 Subject: [PATCH 1239/3167] Create README - LeetHub --- Unique Number of Occurrences - GFG/README.md | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Unique Number of Occurrences - GFG/README.md diff --git a/Unique Number of Occurrences - GFG/README.md b/Unique Number of Occurrences - GFG/README.md new file mode 100644 index 00000000..0ca8666c --- /dev/null +++ b/Unique Number of Occurrences - GFG/README.md @@ -0,0 +1,24 @@ +# Unique Number of Occurrences +## Easy +

Given an array arr of N integers, the task is to check whether the frequency of the elements in the array is unique or not. Or in other words, there are no two distinct numbers in array with equal frequency. If all the frequency is unique then return true, else return false.

+

Example 1:

+
Input:
+N = 5
arr = [1, 1, 2, 5, 5] +Output: +false +Explanation:
The array contains 2 (1’s), 1 (2’s) and 2 (5’s), since the number of frequency of 1 and 5 are the same i.e. 2 times. Therefore, this array does not satisfy the condition. +
+

Example 2:

+
Input:
+N = 10
arr = [2, 2, 5, 10, 1, 2, 10, 5, 10, 2] +Output: +true +Explanation: +Number of 1’s -> 1 +Number of 2’s -> 4 +Number of 5’s -> 2 +Number of 10’s -> 3. +Since, the number of occurrences of elements present in the array is unique. Therefore, this array satisfy the condition.
+

Your task:
You don't need to read input or print anything. Your task is to complete the function isFrequencyUnique() which take integer N and array arr of size N as arguments, and returns a boolean.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

+

Constraints:
1 <= N <=105
-109 <= arr[i] <= 109

\ No newline at end of file From c879e66d7ac01ee5f815ba541f08aa2ba3a9d2e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jul 2023 23:03:23 +0530 Subject: [PATCH 1240/3167] Added solution - LeetHub --- .../unique-number-of-occurrences.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Unique Number of Occurrences - GFG/unique-number-of-occurrences.java diff --git a/Unique Number of Occurrences - GFG/unique-number-of-occurrences.java b/Unique Number of Occurrences - GFG/unique-number-of-occurrences.java new file mode 100644 index 00000000..1579aa58 --- /dev/null +++ b/Unique Number of Occurrences - GFG/unique-number-of-occurrences.java @@ -0,0 +1,89 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + + +class IntArray +{ + public static int[] input(BufferedReader br, int n) throws IOException + { + String[] s = br.readLine().trim().split(" "); + int[] a = new int[n]; + for(int i = 0; i < n; i++) + a[i] = Integer.parseInt(s[i]); + + return a; + } + + public static void print(int[] a) + { + for(int e : a) + System.out.print(e + " "); + System.out.println(); + } + + public static void print(ArrayList a) + { + for(int e : a) + System.out.print(e + " "); + System.out.println(); + } +} + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t; + t = Integer.parseInt(br.readLine()); + while(t-- > 0){ + + int n; + n = Integer.parseInt(br.readLine()); + + + int[] arr = IntArray.input(br, n); + + Solution obj = new Solution(); + boolean res = obj.isFrequencyUnique(n, arr); + + int _result_val = (res) ? 1 : 0; + System.out.println(_result_val); + } + } +} + +// } Driver Code Ends + + + +class Solution { + public static boolean isFrequencyUnique(int n, int[] arr) { + // code here + + HashMap mp = new HashMap<>(); + HashSet st = new HashSet<>(); + + 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); + } + } + + for(int freq : mp.values()) + { + if(st.contains(freq)) + return false; + else + st.add(freq); + } + + return true; + } +} + From 5e44e482b3be9b2dbe6ec2a3c9d1ee689fa8af83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jul 2023 23:51:45 +0530 Subject: [PATCH 1241/3167] Attach NOTES - LeetHub From 9383712cf8002d5bb241327d4f21cb2e3981f80e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jul 2023 23:51:48 +0530 Subject: [PATCH 1242/3167] Time: 34 ms (14.51%), Space: 13.7 MB (68.39%) - LeetHub From d102b907e591038b5db6cf184f7a04652ed34fdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:27:48 +0530 Subject: [PATCH 1243/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Implement two stacks in an array - GFG/README.md diff --git a/Implement two stacks in an array - GFG/README.md b/Implement two stacks in an array - GFG/README.md new file mode 100644 index 00000000..ddbf8c9c --- /dev/null +++ b/Implement two stacks in an array - GFG/README.md @@ -0,0 +1,36 @@ +# Implement two stacks in an array +## Easy +

Your task is to implement  2 stacks in one array efficiently. You need to implement 4 methods.
push1 : pushes element into first stack.
push2 : pushes element into second stack.
pop1 : pops element from first stack and returns the popped element. If first stack is empty, it should return -1.
pop2 : pops element from second stack and returns the popped element. If second stack is empty, it should return -1.

+

Example 1:

+
Input:
+push1(2)
+push1(3)
+push2(4)
+pop1()
+pop2()
+pop2()
+Output:
+3 4 -1
+Explanation:
+push1(2) the stack1 will be {2}
+push1(3) the stack1 will be {2,3}
+push2(4) the stack2 will be {4}
+pop1()   the poped element will be 3 from stack1 and stack1 will be {2}
+pop2()   the poped element will be 4 from stack2 and now stack2 is empty
+pop2()   the stack2 is now empty hence returned -1.
+

Example 2:

+
Input:
+push1(1)
+push2(2)
pop1() +push1(3) +pop1() +pop1() +Output: +3 1 -1 +Explanation: +push1(1) the stack1 will be {1} +push2(2) the stack2 will be {2}
pop1() the poped element will be 1 from stack1 and stack1 will be empty
push1(3) the stack1 will be {3} +pop1()   the poped element will be 3 from stack1 and stack1 will be empty
pop1()  the stack1 is now empty hence returned -1.
+

Your Task:
You don't need to read input or print anything. You are required to complete the 4 methods push1, push2 which takes one argument an integer 'x' to be pushed into stack one and two and pop1, pop2 which returns the integer poped out from stack one and two. If no integer is present in the stack return -1.

+

Expected Time Complexity: O(1) for all the four methods.
Expected Auxiliary Space: O(1) for all the four methods.

+

Constraints:
1 <= Number of queries <= 104
1 <= Number of elements in the stack
<= 100
The sum of elements in both the stacks < size of the given array

\ No newline at end of file From 98b635698dd2ede8897b417109ac5d260cb4096e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:27:48 +0530 Subject: [PATCH 1244/3167] Added solution - LeetHub --- .../implement-two-stacks-in-an-array.cpp | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.cpp diff --git a/Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.cpp b/Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.cpp new file mode 100644 index 00000000..c0c0fe42 --- /dev/null +++ b/Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.cpp @@ -0,0 +1,106 @@ +//{ Driver Code Starts +#include + +using namespace std; + + +// } Driver Code Ends + + +class twoStacks +{ + int *arr; + int size; + int top1, top2; + public: + + twoStacks(int n=100) + { + size = n; + arr = new int[n]; + top1 = -1; + top2 = size; + } + + //Function to push an integer into the stack1. + void push1(int x) + { + if(top1 + 1 < top2) + arr[++top1] = x; + } + + //Function to push an integer into the stack2. + void push2(int x) + { + if(top2-1 > top1) + arr[--top2] = x; + } + + //Function to remove an element from top of the stack1. + int pop1() + { + if(top1 < 0) + return -1; + + int val = arr[top1]; + --top1; + + return val; + + } + + //Function to remove an element from top of the stack2. + int pop2() + { + if(top2>=size) + return -1; + + int val = arr[top2]; + ++top2; + + return val; + } +}; + + + +//{ Driver Code Starts. + +int main() +{ + + int T; + cin>>T; + while(T--) + { + twoStacks *sq = new twoStacks(); + + int Q; + cin>>Q; + while(Q--){ + int stack_no; + cin>>stack_no; + int QueryType=0; + cin>>QueryType; + + if(QueryType==1) + { + int a; + cin>>a; + if(stack_no ==1) + sq->push1(a); + else if(stack_no==2) + sq->push2(a); + }else if(QueryType==2){ + if(stack_no==1) + cout<pop1()<<" "; + else if(stack_no==2) + cout<pop2()<<" "; + + } + } + cout< Date: Fri, 14 Jul 2023 23:30:11 +0530 Subject: [PATCH 1245/3167] Create README - LeetHub From d3fcda04ea93ec1399e227c86b7de33f4bf12a33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:30:12 +0530 Subject: [PATCH 1246/3167] Added solution - LeetHub --- .../implement-two-stacks-in-an-array.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.java diff --git a/Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.java b/Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.java new file mode 100644 index 00000000..2137335e --- /dev/null +++ b/Implement two stacks in an array - GFG/implement-two-stacks-in-an-array.java @@ -0,0 +1,97 @@ +//{ Driver Code Starts +import java.util.*; + +class TwoStack +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int T = sc.nextInt(); + while(T>0) + { + twoStacks g = new twoStacks(); + int Q = sc.nextInt(); + while(Q>0) + { + int stack_no = sc.nextInt(); + int QueryType = sc.nextInt(); + + + if(QueryType == 1) + { + int a = sc.nextInt(); + if(stack_no == 1) + g.push1(a); + else if(stack_no ==2) + g.push2(a); + }else if(QueryType == 2) + { + if(stack_no==1) + System.out.print(g.pop1()+" "); + else if(stack_no==2) + System.out.print(g.pop2()+" "); + } + + Q--; + } + System.out.println(); + T--; + } + } +} + + +// } Driver Code Ends + + + +class twoStacks +{ + int arr[]; + int size; + int top1, top2; + twoStacks() + { + size = 100; + arr = new int[100]; + top1 = -1; + top2 = size; + } + //Function to push an integer into the stack1. + void push1(int x) + { + if(top1 + 1 < top2) + { + arr[++top1] = x; + } + } + //Function to push an integer into the stack2. + void push2(int x) + { + if(top2 - 1 > top1) + arr[--top2] = x; + } + //Function to remove an element from top of the stack1. + int pop1() + { + if(top1 <= -1) + return -1; + + int val = arr[top1]; + --top1; + + return val; + } + //Function to remove an element from top of the stack2. + int pop2() + { + if(top2 >= size) + return -1; + + int val = arr[top2]; + ++top2; + + return val; + } +} + From 458706c6e9d8df23343d6f0207e995baeb5d31f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 15 Jul 2023 00:54:38 +0530 Subject: [PATCH 1247/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1218-longest-arithmetic-subsequence-of-given-difference/README.md diff --git a/1218-longest-arithmetic-subsequence-of-given-difference/README.md b/1218-longest-arithmetic-subsequence-of-given-difference/README.md new file mode 100644 index 00000000..6fbe49c9 --- /dev/null +++ b/1218-longest-arithmetic-subsequence-of-given-difference/README.md @@ -0,0 +1,33 @@ +

1218. Longest Arithmetic Subsequence of Given Difference

Medium


Given an integer array arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference.

+ +

A subsequence is a sequence that can be derived from arr by deleting some or no elements without changing the order of the remaining elements.

+ +

 

+

Example 1:

+ +
Input: arr = [1,2,3,4], difference = 1
+Output: 4
+Explanation: The longest arithmetic subsequence is [1,2,3,4].
+ +

Example 2:

+ +
Input: arr = [1,3,5,7], difference = 1
+Output: 1
+Explanation: The longest arithmetic subsequence is any single element.
+
+ +

Example 3:

+ +
Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2
+Output: 4
+Explanation: The longest arithmetic subsequence is [7,5,3,1].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 105
  • +
  • -104 <= arr[i], difference <= 104
  • +
+
\ No newline at end of file From a6efc772dbc04fbacd5e795f7d1b983e0fdad616 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 15 Jul 2023 00:54:42 +0530 Subject: [PATCH 1248/3167] Time: 164 ms (58.04%), Space: 54.8 MB (92.60%) - LeetHub --- ...hmetic-subsequence-of-given-difference.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1218-longest-arithmetic-subsequence-of-given-difference/1218-longest-arithmetic-subsequence-of-given-difference.cpp diff --git a/1218-longest-arithmetic-subsequence-of-given-difference/1218-longest-arithmetic-subsequence-of-given-difference.cpp b/1218-longest-arithmetic-subsequence-of-given-difference/1218-longest-arithmetic-subsequence-of-given-difference.cpp new file mode 100644 index 00000000..e21c8d06 --- /dev/null +++ b/1218-longest-arithmetic-subsequence-of-given-difference/1218-longest-arithmetic-subsequence-of-given-difference.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int longestSubsequence(vector& arr, int difference) { + + unordered_map dp; + + int n = arr.size(); + + int ans = 1; + + for(int i = 0; i < n; ++i) + { + int num = arr[i]; + + if(dp.find(num-difference) != dp.end()) + dp[num] = dp[num-difference] + 1; + + else + dp[num] = 1; + + ans = max(ans, dp[num]); + } + + return ans; + + } +}; \ No newline at end of file From 32d18292fb610d9ed9115ecb4ede70fd5bddff3d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:30:01 +0530 Subject: [PATCH 1249/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1751-maximum-number-of-events-that-can-be-attended-ii/README.md diff --git a/1751-maximum-number-of-events-that-can-be-attended-ii/README.md b/1751-maximum-number-of-events-that-can-be-attended-ii/README.md new file mode 100644 index 00000000..58dd30a9 --- /dev/null +++ b/1751-maximum-number-of-events-that-can-be-attended-ii/README.md @@ -0,0 +1,42 @@ +

1751. Maximum Number of Events That Can Be Attended II

Hard


You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend.

+ +

You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.

+ +

Return the maximum sum of values that you can receive by attending events.

+ +

 

+

Example 1:

+ +

+ +
Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
+Output: 7
+Explanation: Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.
+ +

Example 2:

+ +

+ +
Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
+Output: 10
+Explanation: Choose event 2 for a total value of 10.
+Notice that you cannot attend any other event as they overlap, and that you do not have to attend k events.
+ +

Example 3:

+ +

+ +
Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
+Output: 9
+Explanation: Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= events.length
  • +
  • 1 <= k * events.length <= 106
  • +
  • 1 <= startDayi <= endDayi <= 109
  • +
  • 1 <= valuei <= 106
  • +
+
\ No newline at end of file From 8a9a42eef307861b0f4e788a5172f2d4e3dfb1a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:30:05 +0530 Subject: [PATCH 1250/3167] Time: 304 ms (88.31%), Space: 72.7 MB (74.72%) - LeetHub --- ...mber-of-events-that-can-be-attended-ii.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1751-maximum-number-of-events-that-can-be-attended-ii/1751-maximum-number-of-events-that-can-be-attended-ii.cpp diff --git a/1751-maximum-number-of-events-that-can-be-attended-ii/1751-maximum-number-of-events-that-can-be-attended-ii.cpp b/1751-maximum-number-of-events-that-can-be-attended-ii/1751-maximum-number-of-events-that-can-be-attended-ii.cpp new file mode 100644 index 00000000..24a3725e --- /dev/null +++ b/1751-maximum-number-of-events-that-can-be-attended-ii/1751-maximum-number-of-events-that-can-be-attended-ii.cpp @@ -0,0 +1,42 @@ +class Solution { + +private: + int helper(int idx, int k, int n, vector>& events, vector>& dp) + { + if(idx >= n) + return 0; + + if(dp[idx][k] != -1) + return dp[idx][k]; + + int ans = helper(idx+1, k , n, events, dp); + + if(k) + { + int pos = n; + for(int i = idx+1; i < n; ++i) + { + if(events[i][0] > events[idx][1]) + { + pos = i; + break; + } + } + ans = max(ans , events[idx][2] + helper(pos, k-1, n, events, dp)); + } + + return dp[idx][k] = ans; + } +public: + int maxValue(vector>& events, int k) { + + int n = events.size(); + + sort(events.begin(), events.end()); + + vector> dp(n+1, vector(k+1,-1)); + + return helper(0, k, n, events, dp); + } +}; + \ No newline at end of file From efa363bbe32913e339f4855f5a691624bc1ca493 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Jul 2023 21:47:50 +0530 Subject: [PATCH 1251/3167] Create README - LeetHub --- Queue Reversal - GFG/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Queue Reversal - GFG/README.md diff --git a/Queue Reversal - GFG/README.md b/Queue Reversal - GFG/README.md new file mode 100644 index 00000000..a8960d12 --- /dev/null +++ b/Queue Reversal - GFG/README.md @@ -0,0 +1,23 @@ +# Queue Reversal +## Easy +

Given a Queue Q containing N elements. The task is to reverse the Queue. Your task is to complete the function rev(), that reverses the N elements of the queue.

+

Example 1:

+
Input:
+6
+4 3 1 10 2 6
+Output: 
+6 2 10 1 3 4
+Explanation: 
+After reversing the given elements of the queue , the resultant queue will be 6 2 10 1 3 4.
+
+

Example 2:

+
Input:
+4
+4 3 2 1 
+Output: 
+1 2 3 4
+Explanation: 
+After reversing the given elements of the queue , the resultant queue will be 1 2 3 4.
+

Your Task: You need to complete the function rev that takes a queue as parameter and returns the reversed queue. The printing is done automatically by the driver code.

+

Expected Time Complexity : O(n)
Expected Auxilliary Space : O(n)

+

Constraints:
1 ≤ N ≤ 105
1 ≤ elements of Queue ≤ 105

\ No newline at end of file From f5d9f06ab02a9023f7e1320f5b3440262a3d7410 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Jul 2023 21:47:50 +0530 Subject: [PATCH 1252/3167] Added solution - LeetHub --- Queue Reversal - GFG/queue-reversal.cpp | 65 +++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Queue Reversal - GFG/queue-reversal.cpp diff --git a/Queue Reversal - GFG/queue-reversal.cpp b/Queue Reversal - GFG/queue-reversal.cpp new file mode 100644 index 00000000..50acb6ab --- /dev/null +++ b/Queue Reversal - GFG/queue-reversal.cpp @@ -0,0 +1,65 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +//function Template for C++ + +//Function to reverse the queue. +class Solution +{ + public: + queue rev(queue q) + { + // add code here. + + // fifo + + stack st; + + while(!q.empty()) + { + st.push(q.front()); + q.pop(); + } + + while(!st.empty()) + { + q.push(st.top()); + st.pop(); + } + + return q; + + } +}; + + +//{ Driver Code Starts. +int main() +{ + int test; + cin>>test; + while(test--) + { + queue q; + int n, var; + cin>>n; + while(n--) + { + cin>>var; + q.push(var); + } + Solution ob; + queue a=ob.rev(q); + while(!a.empty()) + { + cout< Date: Sun, 16 Jul 2023 23:57:26 +0530 Subject: [PATCH 1253/3167] Create README - LeetHub --- 1125-smallest-sufficient-team/README.md | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1125-smallest-sufficient-team/README.md diff --git a/1125-smallest-sufficient-team/README.md b/1125-smallest-sufficient-team/README.md new file mode 100644 index 00000000..ea414a9c --- /dev/null +++ b/1125-smallest-sufficient-team/README.md @@ -0,0 +1,37 @@ +

1125. Smallest Sufficient Team

Hard


In a project, you have a list of required skills req_skills, and a list of people. The ith person people[i] contains a list of skills that the person has.

+ +

Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person.

+ +
    +
  • For example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
  • +
+ +

Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.

+ +

It is guaranteed an answer exists.

+ +

 

+

Example 1:

+
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
+Output: [0,2]
+

Example 2:

+
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
+Output: [1,2]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= req_skills.length <= 16
  • +
  • 1 <= req_skills[i].length <= 16
  • +
  • req_skills[i] consists of lowercase English letters.
  • +
  • All the strings of req_skills are unique.
  • +
  • 1 <= people.length <= 60
  • +
  • 0 <= people[i].length <= 16
  • +
  • 1 <= people[i][j].length <= 16
  • +
  • people[i][j] consists of lowercase English letters.
  • +
  • All the strings of people[i] are unique.
  • +
  • Every skill in people[i] is a skill in req_skills.
  • +
  • It is guaranteed a sufficient team exists.
  • +
+
\ No newline at end of file From 7c77b3c812ec33d2f9c3747effd590b431a44919 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Jul 2023 23:57:26 +0530 Subject: [PATCH 1254/3167] Attach NOTES - LeetHub --- 1125-smallest-sufficient-team/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1125-smallest-sufficient-team/NOTES.md diff --git a/1125-smallest-sufficient-team/NOTES.md b/1125-smallest-sufficient-team/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1125-smallest-sufficient-team/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a5006b06b198cee98b97bbb3c221d446f6ae1099 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 16 Jul 2023 23:57:29 +0530 Subject: [PATCH 1255/3167] Time: 70 ms (81.93%), Space: 48.2 MB (35.16%) - LeetHub --- .../1125-smallest-sufficient-team.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 1125-smallest-sufficient-team/1125-smallest-sufficient-team.cpp diff --git a/1125-smallest-sufficient-team/1125-smallest-sufficient-team.cpp b/1125-smallest-sufficient-team/1125-smallest-sufficient-team.cpp new file mode 100644 index 00000000..799d66d1 --- /dev/null +++ b/1125-smallest-sufficient-team/1125-smallest-sufficient-team.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + +vectorres; + +void helper(int i,vector&people_skill,int m,int mask,vector&ans,vector>&dp) +{ + if(i == people_skill.size()) + { + if(mask == ((1< 0) dp[i][mask] = ans.size(); +} + + + vector smallestSufficientTeam(vector& req_skills, vector>& people) { + + int n = people.size(); + int m = req_skills.size(); + + unordered_mapmpp; + + for(int i = 0;ipeople_skill; + + for(auto it : people) + { + int mask = 0; + for(int j = 0; j < it.size(); ++j) + { + if(mpp.count(it[j])) mask |= mpp[it[j]]; + } + people_skill.push_back(mask); + } + + vector> dp(n, vector((1<ans; + + helper(0,people_skill,m,0,ans,dp); + return res; + } +}; \ No newline at end of file From c98ecd5a251a7da2d66307bc8644736c2bf4f3de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:37:09 +0530 Subject: [PATCH 1256/3167] Create README - LeetHub --- 0445-add-two-numbers-ii/README.md | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0445-add-two-numbers-ii/README.md diff --git a/0445-add-two-numbers-ii/README.md b/0445-add-two-numbers-ii/README.md new file mode 100644 index 00000000..ee38ba81 --- /dev/null +++ b/0445-add-two-numbers-ii/README.md @@ -0,0 +1,35 @@ +

445. Add Two Numbers II

Medium


You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

+ +

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

+ +

 

+

Example 1:

+ +
Input: l1 = [7,2,4,3], l2 = [5,6,4]
+Output: [7,8,0,7]
+
+ +

Example 2:

+ +
Input: l1 = [2,4,3], l2 = [5,6,4]
+Output: [8,0,7]
+
+ +

Example 3:

+ +
Input: l1 = [0], l2 = [0]
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in each linked list is in the range [1, 100].
  • +
  • 0 <= Node.val <= 9
  • +
  • It is guaranteed that the list represents a number that does not have leading zeros.
  • +
+ +

 

+

Follow up: Could you solve it without reversing the input lists?

+
\ No newline at end of file From 420425a137d8a5d7f5703d2a5a1e1f484706838a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:37:13 +0530 Subject: [PATCH 1257/3167] Time: 39 ms (64.55%), Space: 71.1 MB (74.64%) - LeetHub --- .../0445-add-two-numbers-ii.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp diff --git a/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp b/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp new file mode 100644 index 00000000..4936c2bc --- /dev/null +++ b/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp @@ -0,0 +1,77 @@ +/** + * 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 { + +private: + ListNode* reverseList(ListNode *head) + { + ListNode* prev = nullptr; + + while(head) + { + ListNode* temp = head->next; + head->next = prev; + prev = head; + head = temp; + } + return prev; + } + +public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { + + l1 = reverseList(l1); + l2 = reverseList(l2); + + ListNode* dummy = new ListNode(0); + ListNode* ptr = dummy; + + int carry = 0; + + while(l1 or l2) + { + int sum = carry; + + if(l1) + { + sum += l1->val; + l1 = l1->next; + + } + if(l2) + { + sum += l2->val; + l2 = l2->next; + } + + + carry = sum/10; + + ListNode* cur = new ListNode(sum % 10); + + ptr->next = cur; + ptr = ptr->next; + } + + if(carry) + { + ListNode* cur = new ListNode(carry); + + ptr->next = cur; + ptr = ptr->next; + } + + ListNode* ans = reverseList(dummy->next); + + return ans; + + } +}; \ No newline at end of file From c392a5ad8c75f057efc5b539642d6f1c6772b3d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:39:28 +0530 Subject: [PATCH 1258/3167] Attach NOTES - LeetHub --- 0445-add-two-numbers-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0445-add-two-numbers-ii/NOTES.md diff --git a/0445-add-two-numbers-ii/NOTES.md b/0445-add-two-numbers-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0445-add-two-numbers-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 68e9e2653d5f0d3cca3bf5d8a9b328b0125a6cc4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:39:31 +0530 Subject: [PATCH 1259/3167] Time: 39 ms (64.55%), Space: 71.1 MB (74.64%) - LeetHub From 20edb482dc1a9bff1c73daa83320d82a10e283d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:39:32 +0530 Subject: [PATCH 1260/3167] Attach NOTES - LeetHub From da17ca205e11abdf32f8294f5f9be9d15d2283ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:39:35 +0530 Subject: [PATCH 1261/3167] Time: 39 ms (64.55%), Space: 71.1 MB (74.64%) - LeetHub From ff3d03a55185d922fbaec8932ff50338321548b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:30:44 +0530 Subject: [PATCH 1262/3167] Attach NOTES - LeetHub From 84ca432888c4b20f5e5b417d974fde224c67ede6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:30:48 +0530 Subject: [PATCH 1263/3167] Time: 39 ms (64.55%), Space: 71.1 MB (74.64%) - LeetHub From f212d006fe193e647f62ded903addd862b9db6b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:53:40 +0530 Subject: [PATCH 1264/3167] Attach NOTES - LeetHub From e62f47dcf8fc131618dfeaf6016377fe3177cf51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:53:43 +0530 Subject: [PATCH 1265/3167] Time: 39 ms (64.55%), Space: 71.1 MB (74.64%) - LeetHub From dfc79d5a717fbeb69c790f3f3b4d3f268945f663 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:55:01 +0530 Subject: [PATCH 1266/3167] Attach NOTES - LeetHub From b8ab877bb29fc96b6818ba08a23a3d500536d126 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:55:05 +0530 Subject: [PATCH 1267/3167] Time: 39 ms (64.55%), Space: 71.1 MB (74.64%) - LeetHub From c184d6d837a096f98e5cf48651ad8a04b534a5c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:59:35 +0530 Subject: [PATCH 1268/3167] Attach NOTES - LeetHub From a00435bc8ce5cf11a910ff1ddb9a40f3002478bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:59:39 +0530 Subject: [PATCH 1269/3167] Time: 47 ms (31.01%), Space: 73.9 MB (7.74%) - LeetHub --- .../0445-add-two-numbers-ii.cpp | 72 ++++++++----------- 1 file changed, 29 insertions(+), 43 deletions(-) diff --git a/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp b/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp index 4936c2bc..3d81442a 100644 --- a/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp +++ b/0445-add-two-numbers-ii/0445-add-two-numbers-ii.cpp @@ -9,69 +9,55 @@ * }; */ class Solution { - -private: - ListNode* reverseList(ListNode *head) - { - ListNode* prev = nullptr; +public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { - while(head) + stack st1; + stack st2; + + while(l1) { - ListNode* temp = head->next; - head->next = prev; - prev = head; - head = temp; + st1.push(l1->val); + l1 = l1->next; + } + + while(l2) + { + st2.push(l2->val); + l2 = l2->next; } - return prev; - } - -public: - ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { - - l1 = reverseList(l1); - l2 = reverseList(l2); ListNode* dummy = new ListNode(0); ListNode* ptr = dummy; int carry = 0; - while(l1 or l2) + while(!st1.empty() or !st2.empty()) { - int sum = carry; + int a = 0, b = 0; - if(l1) + if(!st1.empty()) { - sum += l1->val; - l1 = l1->next; - + a = st1.top(); + st1.pop(); } - if(l2) + if(!st2.empty()) { - sum += l2->val; - l2 = l2->next; + b = st2.top(); + st2.pop(); } - - carry = sum/10; + int sum = (a + b + ptr->val); - ListNode* cur = new ListNode(sum % 10); + ptr->val = sum % 10; + + ListNode* cur = new ListNode(sum/10); - ptr->next = cur; - ptr = ptr->next; - } - - if(carry) - { - ListNode* cur = new ListNode(carry); + cur->next = ptr; - ptr->next = cur; - ptr = ptr->next; + ptr = cur; } - ListNode* ans = reverseList(dummy->next); - - return ans; - + return (ptr->val == 0 ? ptr->next : ptr); } }; \ No newline at end of file From f4a042f2a40942e4d984a5a8066c611d5349b38e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:00:36 +0530 Subject: [PATCH 1270/3167] Attach NOTES - LeetHub From 4de48dd143d724d96844020ec47a40c41dcfff3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:00:39 +0530 Subject: [PATCH 1271/3167] Time: 47 ms (31.01%), Space: 73.9 MB (7.74%) - LeetHub From d4e77b28ac898b98f06484a3fc2468fa712b6457 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jul 2023 21:49:01 +0530 Subject: [PATCH 1272/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 First non-repeating character in a stream - GFG/README.md diff --git a/First non-repeating character in a stream - GFG/README.md b/First non-repeating character in a stream - GFG/README.md new file mode 100644 index 00000000..71923c68 --- /dev/null +++ b/First non-repeating character in a stream - GFG/README.md @@ -0,0 +1,25 @@ +# First non-repeating character in a stream +## Medium +

Given an input stream A of n characters consisting only of lower case alphabets. While reading characters from the stream, you have to tell which character has appeared only once in the stream upto that point. If there are many characters that have appeared only once, you have to tell which one of them was the first one to appear. If there is no such character then append '#' to the answer.
 

+

Example 1:

+
Input: A = "aabc"
+Output: "a#bb"
+Explanation: For every character first non
+repeating character is as follow-
+"a" - first non-repeating character is 'a'
+"aa" - no non-repeating character so '#'
+"aab" - first non-repeating character is 'b'
+"aabc" - first non-repeating character is 'b'
+
+

Example 2:

+
Input: A = "zz"
+Output: "z#"
+Explanation: For every character first non
+repeating character is as follow-
+"z" - first non-repeating character is 'z'
+"zz" - no non-repeating character so '#'
+
+

 

+

Your Task:
You don't need to read or print anything. Your task is to complete the function FirstNonRepeating() which takes A as input parameter and returns a string after processing the input stream.

 

+

Expected Time Complexity: O(n)
Expected Space Complexity: O(n)

 

+

Constraints:
1 <= n <= 105

\ No newline at end of file From 9b8ac4caeb77ebdcccb2bbf5dd6cfddc248f49b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jul 2023 21:49:01 +0530 Subject: [PATCH 1273/3167] Added solution - LeetHub --- ...rst-nonrepeating-character-in-a-stream.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 First non-repeating character in a stream - GFG/first-nonrepeating-character-in-a-stream.cpp diff --git a/First non-repeating character in a stream - GFG/first-nonrepeating-character-in-a-stream.cpp b/First non-repeating character in a stream - GFG/first-nonrepeating-character-in-a-stream.cpp new file mode 100644 index 00000000..78d118e1 --- /dev/null +++ b/First non-repeating character in a stream - GFG/first-nonrepeating-character-in-a-stream.cpp @@ -0,0 +1,65 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution { + public: + string FirstNonRepeating(string A){ + // Code here + + int n = A.size(); + + vector v(26,0), first(26,n+1); + + string ans; + + for(int i = 0; i < n; ++i) + { + ++v[A[i] - 'a']; + + if(first[A[i] - 'a'] == n+1) + first[A[i] - 'a'] = i; + + bool ok = false; + int mini = n+1; + char ch = '#'; + + for(int j = 0; j < 26; ++j) + { + if(v[j] == 1) + { + ok = true; + if(first[j] < mini) + { + mini = first[j]; + ch = A[mini]; + } + } + } + + if(ok) + ans += ch; + else + ans += ch; + } + + return ans; + } + +}; + +//{ Driver Code Starts. +int main(){ + int tc; + cin >> tc; + while(tc--){ + string A; + cin >> A; + Solution obj; + string ans = obj.FirstNonRepeating(A); + cout << ans << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From d0a74eb7aa218340443119284767680bcfa5b22c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jul 2023 22:40:53 +0530 Subject: [PATCH 1274/3167] Create README - LeetHub --- Longest Repeating Subsequence - GFG/README.md | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Longest Repeating Subsequence - GFG/README.md diff --git a/Longest Repeating Subsequence - GFG/README.md b/Longest Repeating Subsequence - GFG/README.md new file mode 100644 index 00000000..d1c44f65 --- /dev/null +++ b/Longest Repeating Subsequence - GFG/README.md @@ -0,0 +1,80 @@ +# Longest Repeating Subsequence +## Medium +

Given string str, find the length of the longest repeating subsequence such that it can be found twice in the given string.

+ +

The two identified subsequences A and B can use the same ith character from string str if and only if that ith character has different indices in A and B. For example, A = "xax" and B = "xax" then the index of first "x" must be different in the original string for A and B.

+ +

Example 1:

+ +
Input:
+str = "axxzxy"
+Output: 2
+Explanation:
+The given array with indexes looks like
+a x x z x y 
+0 1 2 3 4 5
+
+The longest subsequence is "xx". 
+It appears twice as explained below.
+
+subsequence A
+x x
+0 1  <-- index of subsequence A
+------
+1 2  <-- index of str 
+
+
+subsequence B
+x x
+0 1  <-- index of subsequence B
+------
+2 4  <-- index of str 
+
+We are able to use character 'x' 
+(at index 2 in str) in both subsequences
+as it appears on index 1 in subsequence A 
+and index 0 in subsequence B.
+ +

Example 2:

+ +
Input:
+str = "axxxy"
+Output: 2
+Explanation:
+The given array with indexes looks like
+a x x x y 
+0 1 2 3 4
+
+The longest subsequence is "xx". 
+It appears twice as explained below.
+
+subsequence A
+x x
+0 1  <-- index of subsequence A
+------
+1 2  <-- index of str 
+
+
+subsequence B
+x x
+0 1  <-- index of subsequence B
+------
+2 3  <-- index of str 
+
+We are able to use character 'x' 
+(at index 2 in str) in both subsequences
+as it appears on index 1 in subsequence A 
+and index 0 in subsequence B.
+ +


+Your Task:
+You don't need to read or print anything. Your task is to complete the LongestRepeatingSubsequence() which takes str as input parameter and returns the length of the longest repeating subsequnece.

+ +


+Expected Time Complexity: O(n2)
+Expected Space Complexity: O(n2)

+ +


+Constraints:
+1 <= |str| <= 103

+
\ No newline at end of file From 7d9b214ea593a84aa05e6ea3326f343cef65c52c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jul 2023 22:40:54 +0530 Subject: [PATCH 1275/3167] Added solution - LeetHub --- .../longest-repeating-subsequence.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp diff --git a/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp b/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp new file mode 100644 index 00000000..c823a3a3 --- /dev/null +++ b/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp @@ -0,0 +1,53 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution { + + private: + int helper(int i, int j, int n, string& str1, string& str2, vector>& dp) + { + if(i >= n or j >= n) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int take = 0; + + if(str1[i] == str2[j] and i != j) + take = 1 + helper(i+1, j+1, n, str1, str2, dp); + + int notTake = max(helper(i+1, j ,n, str1, str2, dp), helper(i, j+1, n, str1, str2, dp)); + + dp[i][j] = max(take, notTake); + } + + public: + int LongestRepeatingSubsequence(string str){ + // Code here + + int n = str.size(); + + vector> dp(n+1, vector(n+1, -1)); + + return helper(0, 0, n, str, str, dp); + } + +}; + +//{ Driver Code Starts. +int main(){ + int tc; + cin >> tc; + while(tc--){ + string str; + cin >> str; + Solution obj; + int ans = obj.LongestRepeatingSubsequence(str); + cout << ans << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From 9026033c530bf1f68a5ea1419d7bab0b1f53d9c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jul 2023 22:43:02 +0530 Subject: [PATCH 1276/3167] Create README - LeetHub From b22000baac0b4420680f808ac85b3611af98ecd5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jul 2023 22:43:03 +0530 Subject: [PATCH 1277/3167] Added solution - LeetHub --- .../longest-repeating-subsequence.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp b/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp index c823a3a3..27f2c8dc 100644 --- a/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp +++ b/Longest Repeating Subsequence - GFG/longest-repeating-subsequence.cpp @@ -30,9 +30,24 @@ class Solution { int n = str.size(); - vector> dp(n+1, vector(n+1, -1)); + vector> dp(n+1, vector(n+1, 0)); - return helper(0, 0, n, str, str, dp); + for(int i = n-1; i >= 0; --i) + { + for(int j = n-1; j >= 0; --j) + { + int take = 0; + + if(str[i] == str[j] and i != j) + take = 1 + dp[i+1][j+1]; + + int notTake = max(dp[i+1][j], dp[i][j+1]); + + dp[i][j] = max(take, notTake); + } + } + + return dp[0][0]; } }; From 3b87807058bd74e8357f281c0e989c642a6691a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jul 2023 00:19:14 +0530 Subject: [PATCH 1278/3167] Attach NOTES - LeetHub From dda875a3a323c6902ba50aa89a972edba1eb36ec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jul 2023 00:19:17 +0530 Subject: [PATCH 1279/3167] Time: 542 ms (24.05%), Space: 183 MB (5.65%) - LeetHub --- 0146-lru-cache/0146-lru-cache.cpp | 59 ++++++++++++++++++------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/0146-lru-cache/0146-lru-cache.cpp b/0146-lru-cache/0146-lru-cache.cpp index b04c62f3..714960b4 100644 --- a/0146-lru-cache/0146-lru-cache.cpp +++ b/0146-lru-cache/0146-lru-cache.cpp @@ -3,36 +3,40 @@ class LRUCache { class Node{ public: - int key, val; - Node* prev, *next; - Node(int k, int v) - { - key = k; - val = v; - } + int key, val; + Node *prev, *next; + + Node(int k, int v) + { + this->key = k; + this->val = v; + } }; int cap; Node* head = new Node(-1,-1); Node* tail = new Node(-1,-1); - unordered_map mp; + unordered_map mp; void addNode(Node* ptr) { Node* temp = head->next; head->next = ptr; - ptr->prev = head; ptr->next = temp; + ptr->prev = head; temp->prev = ptr; } - void deleteNode(Node *ptr) + void deleteNode(Node* ptr) { - Node* ptrPrev = ptr->prev; - Node* ptrNext = ptr->next; - ptrPrev->next = ptrNext; + Node *ptrPrev = ptr->prev; + Node *ptrNext = ptr->next; + ptrNext->prev = ptrPrev; + ptrPrev->next = ptrNext; + + delete(ptr); } LRUCache(int capacity) { @@ -42,35 +46,42 @@ class LRUCache { } int get(int key) { + if(mp.find(key) != mp.end()) { - Node* resultNode = mp[key]; - int res = resultNode->val; + Node* existing = mp[key]; + int value = existing->val; mp.erase(key); - deleteNode(resultNode); - addNode(resultNode); + deleteNode(existing); + addNode(new Node(key, value)); mp[key] = head->next; - return res; + return value; } + return -1; + } void put(int key, int value) { + if(mp.find(key) != mp.end()) { - Node* existingNode = mp[key]; + Node* existing = mp[key]; mp.erase(key); - deleteNode(existingNode); + deleteNode(existing); } - if(mp.size() == cap) + + if(cap == mp.size()) { - Node* recentNode = tail->prev; - mp.erase(recentNode->key); - deleteNode(recentNode); + Node* existing = tail->prev; + mp.erase(existing->key); + deleteNode(existing); } addNode(new Node(key, value)); + mp[key] = head->next; + } }; From aa00cad728e1392ea33e382009c07ce2b034603d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jul 2023 22:32:12 +0530 Subject: [PATCH 1280/3167] Create README - LeetHub --- .../README.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Longest Palindromic Subsequence - GFG/README.md diff --git a/Longest Palindromic Subsequence - GFG/README.md b/Longest Palindromic Subsequence - GFG/README.md new file mode 100644 index 00000000..90467274 --- /dev/null +++ b/Longest Palindromic Subsequence - GFG/README.md @@ -0,0 +1,20 @@ +# Longest Palindromic Subsequence +## Medium +

Given a String, find the longest palindromic subsequence.

+

Example 1:

+
Input:
+S = "bbabcbcab"
+Output: 7
+Explanation: Subsequence "babcbab" is the
+longest subsequence which is also a palindrome.
+
+

Example 2:

+
Input: 
+S = "abcd"
+Output: 1
+Explanation: "a", "b", "c" and "d" are
+palindromic and all have a length 1.
+
+


Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.

+

Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).

+

Constraints:
1 ≤ |S| ≤ 1000

\ No newline at end of file From 6d62fe12e2aff478f9997b5d08ec3243e1b6637d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jul 2023 22:42:47 +0530 Subject: [PATCH 1281/3167] Create README - LeetHub From bf915f2bf984dfcc446def0a0a0cc1ba692e03d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jul 2023 22:42:48 +0530 Subject: [PATCH 1282/3167] Added solution - LeetHub --- .../longest-palindromic-subsequence.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Longest Palindromic Subsequence - GFG/longest-palindromic-subsequence.java diff --git a/Longest Palindromic Subsequence - GFG/longest-palindromic-subsequence.java b/Longest Palindromic Subsequence - GFG/longest-palindromic-subsequence.java new file mode 100644 index 00000000..9060a828 --- /dev/null +++ b/Longest Palindromic Subsequence - GFG/longest-palindromic-subsequence.java @@ -0,0 +1,52 @@ +//{ Driver Code Starts +//Initial Template for Java + +import java.io.*; +import java.util.*; + +class GfG +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while(t-->0) + { + String s = sc.next(); + Solution obj = new Solution(); + System.out.println(obj.longestPalinSubseq(s)); + } + + } +} +// } Driver Code Ends + + +//User function Template for Java + +class Solution +{ + public int longestPalinSubseq(String S) + { + //code here + + int n = S.length(); + + String rev = new StringBuilder(S).reverse().toString(); + + int[][] dp = new int[n+1][n+1]; + + for(int i = n-1; i >= 0; --i) + { + for(int j = n-1; j >= 0; --j) + { + if(S.charAt(i) == rev.charAt(j)) + dp[i][j] = 1 + dp[i+1][j+1]; + else + dp[i][j] = Math.max(dp[i+1][j], dp[i][j+1]); + } + } + + return dp[0][0]; + } +} \ No newline at end of file From df5330f0f7085ed657df76227f5094ea5dfe1ddf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 00:22:49 +0530 Subject: [PATCH 1283/3167] Create README - LeetHub --- 0435-non-overlapping-intervals/README.md | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0435-non-overlapping-intervals/README.md diff --git a/0435-non-overlapping-intervals/README.md b/0435-non-overlapping-intervals/README.md new file mode 100644 index 00000000..c05f8684 --- /dev/null +++ b/0435-non-overlapping-intervals/README.md @@ -0,0 +1,33 @@ +

435. Non-overlapping Intervals

Medium


Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

+ +

 

+

Example 1:

+ +
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
+Output: 1
+Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
+
+ +

Example 2:

+ +
Input: intervals = [[1,2],[1,2],[1,2]]
+Output: 2
+Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
+
+ +

Example 3:

+ +
Input: intervals = [[1,2],[2,3]]
+Output: 0
+Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= intervals.length <= 105
  • +
  • intervals[i].length == 2
  • +
  • -5 * 104 <= starti < endi <= 5 * 104
  • +
+
\ No newline at end of file From 9010b30b22e23cbf9188a5949b588b46d1bd4668 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 00:22:49 +0530 Subject: [PATCH 1284/3167] Attach NOTES - LeetHub --- 0435-non-overlapping-intervals/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0435-non-overlapping-intervals/NOTES.md diff --git a/0435-non-overlapping-intervals/NOTES.md b/0435-non-overlapping-intervals/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0435-non-overlapping-intervals/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 881dfe5ec5231415984775a5b03e54fcd6bd9975 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 00:22:53 +0530 Subject: [PATCH 1285/3167] Time: 454 ms (85.96%), Space: 89.8 MB (58.83%) - LeetHub --- .../0435-non-overlapping-intervals.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0435-non-overlapping-intervals/0435-non-overlapping-intervals.cpp diff --git a/0435-non-overlapping-intervals/0435-non-overlapping-intervals.cpp b/0435-non-overlapping-intervals/0435-non-overlapping-intervals.cpp new file mode 100644 index 00000000..d7e1179b --- /dev/null +++ b/0435-non-overlapping-intervals/0435-non-overlapping-intervals.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int eraseOverlapIntervals(vector>& intervals) { + + int n = intervals.size(); + + sort(intervals.begin(), intervals.end(),[&](const auto &a, const auto & b){ + return a[1] < b[1]; + }); + + int cnt = 1; + + int prev = intervals[0][1]; + + for(int i = 1; i < n; ++i) + { + if(intervals[i][0] >= prev) + { + ++cnt; + prev = intervals[i][1]; + } + } + + return n - cnt; + + + } +}; \ No newline at end of file From 62eaa10c7d78a9e2d67f4bf0d8913ef72f122847 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 21:54:46 +0530 Subject: [PATCH 1286/3167] Create README - LeetHub --- Non Repeating Character - GFG/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Non Repeating Character - GFG/README.md diff --git a/Non Repeating Character - GFG/README.md b/Non Repeating Character - GFG/README.md new file mode 100644 index 00000000..f54a1bab --- /dev/null +++ b/Non Repeating Character - GFG/README.md @@ -0,0 +1,22 @@ +# Non Repeating Character +## Easy +

Given a string S consisting of lowercase Latin Letters. Return the first non-repeating character in S. If there is no non-repeating character, return '$'.

+

Example 1:

+
Input:
+S = hello
+Output: h
+Explanation: In the given string, the
+first character which is non-repeating
+is h, as it appears first and there is
+no other 'h' in the string.
+

Example 2:

+
Input:
+S = zxvczbtxyzvy
+Output: c
+Explanation: In the given string, 'c' is
+the character which is non-repeating. 
+
+

Your Task:
You only need to complete the function nonrepeatingCharacter() that takes string S as a parameter and returns the character. If there is no non-repeating character then return '$' .

+

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Note: N = |S|

+

Constraints:
1 <= N <= 105

+

 

\ No newline at end of file From 3d513ab6e34291dec2bf41870a08f2d8c54d1f26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 21:54:47 +0530 Subject: [PATCH 1287/3167] Added solution - LeetHub --- .../non-repeating-character.cpp | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Non Repeating Character - GFG/non-repeating-character.cpp diff --git a/Non Repeating Character - GFG/non-repeating-character.cpp b/Non Repeating Character - GFG/non-repeating-character.cpp new file mode 100644 index 00000000..7798d9c9 --- /dev/null +++ b/Non Repeating Character - GFG/non-repeating-character.cpp @@ -0,0 +1,73 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution +{ + public: + //Function to find the first non-repeating character in a string. + char nonrepeatingCharacter(string S) + { + //Your code here + + int n = S.size(); + + vector freq(26, 0); + map mp; + + for(int i = 0; i < n; ++i) + { + ++freq[S[i]-'a']; + + mp[S[i]] = i; + } + + char ch = '#'; + + int idx = INT_MAX; + + for(int i = 0; i < n; ++i) + { + if(freq[S[i] - 'a'] == 1) + { + idx = min(idx, mp[S[i]]); + } + } + + if(idx == INT_MAX) + return '$'; + return S[idx]; + } + +}; + +//{ Driver Code Starts. + +int main() { + + int T; + cin >> T; + + while(T--) + { + + string S; + cin >> S; + Solution obj; + char ans = obj.nonrepeatingCharacter(S); + + if(ans != '$') + cout << ans; + else cout << "-1"; + + cout << endl; + + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file From db5c822291d6f547557f1d7ecdbcd02a67bad9b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:10:23 +0530 Subject: [PATCH 1288/3167] Attach NOTES - LeetHub From 6bad8dec441d09c3ccdcb125dd721d7147d129f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:10:26 +0530 Subject: [PATCH 1289/3167] Time: 16 ms (59.10%), Space: 17.8 MB (24.17%) - LeetHub From a2c9eab73f0d09ba5daf0ab1a1d2ba8ae45549f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jul 2023 09:02:56 +0530 Subject: [PATCH 1290/3167] Attach NOTES - LeetHub --- 0300-longest-increasing-subsequence/NOTES.md | 31 +------------------- 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/0300-longest-increasing-subsequence/NOTES.md b/0300-longest-increasing-subsequence/NOTES.md index fcc3fdab..38c1374a 100644 --- a/0300-longest-increasing-subsequence/NOTES.md +++ b/0300-longest-increasing-subsequence/NOTES.md @@ -1,30 +1 @@ -vector dp(n+1,1),hash(n); -int ans = 1, lastIndex = 0; -for(int i= 0; i nums[j] and dp[j] + 1 > dp[i]) -{ -dp[i] = dp[j] + 1; -hash[i] = j; -} -} -if(dp[i] > ans) -{ -ans = dp[i]; -lastIndex = i; -} -} -vector lis; -lis.push_back(nums[lastIndex]); -while(hash[lastIndex] != lastIndex) -{ -lastIndex = hash[lastIndex]; -lis.push_back(nums[lastIndex]); -} -reverse(lis.begin(),lis.end()); -for(auto itr : lis) -cout< Date: Fri, 21 Jul 2023 09:03:00 +0530 Subject: [PATCH 1291/3167] Time: 1371 ms (23.71%), Space: 292.3 MB (8.16%) - LeetHub --- .../0300-longest-increasing-subsequence.cpp | 58 ++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp index ad538d86..dbb79fcc 100644 --- a/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp +++ b/0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp @@ -1,43 +1,35 @@ class Solution { -public: - int lengthOfLIS(vector& nums) { - - int n = nums.size(); - vector dp(n+1,1),hash(n); - - int ans = 1, lastIndex = 0; - for(int i= 0; i& nums, vector>& dp) { - hash[i] = i; - for(int j = 0; j nums[j] and dp[j] + 1 > dp[i]) - { - dp[i] = dp[j] + 1; - hash[i] = j; - } + return dp[idx][prev+1]; } - if(dp[i] > ans) - { - ans = dp[i]; - lastIndex = i; - } - } + + int notTake = helper(idx+1, prev, n, nums, dp); + + int take = 0; + + if(prev == -1 or nums[idx] > nums[prev]) + take = 1 + helper(idx+1, idx, n, nums, dp); + + return dp[idx][prev+1] = max(take, notTake); + } + + +public: + int lengthOfLIS(vector& nums) { - vector lis; - lis.push_back(nums[lastIndex]); - while(hash[lastIndex] != lastIndex) - { - lastIndex = hash[lastIndex]; - lis.push_back(nums[lastIndex]); - } + int n = nums.size(); - reverse(lis.begin(),lis.end()); - for(auto itr : lis) - cout<> dp(n+1, vector(n+1, -1)); - return ans; + return helper(0, -1, n, nums, dp); } }; \ No newline at end of file From ea3cd36cbaa3f2e3c08bb05e0f8bbb4b3fec5097 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jul 2023 21:56:13 +0530 Subject: [PATCH 1292/3167] Attach NOTES - LeetHub --- 0673-number-of-longest-increasing-subsequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0673-number-of-longest-increasing-subsequence/NOTES.md diff --git a/0673-number-of-longest-increasing-subsequence/NOTES.md b/0673-number-of-longest-increasing-subsequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0673-number-of-longest-increasing-subsequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e1289ea1ac75d2dbba71ed6ccfdba5f18bd2d861 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jul 2023 21:56:17 +0530 Subject: [PATCH 1293/3167] Time: 215 ms (48.76%), Space: 13.1 MB (66.65%) - LeetHub --- ...mber-of-longest-increasing-subsequence.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp b/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp index 18f2c851..414af113 100644 --- a/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp +++ b/0673-number-of-longest-increasing-subsequence/0673-number-of-longest-increasing-subsequence.cpp @@ -3,29 +3,37 @@ class Solution { int findNumberOfLIS(vector& nums) { int n = nums.size(); - vector dp(n,1), cnt(n,1); - int maxi = 1,ans = 0; - for(int i = 0; i dp(n, 1) , cnt(n , 1); + + int maxi = 0, ans = 0; + + for(int i = 0; i < n; ++i) { - for(int j = 0; j nums[j] and dp[j] + 1 > dp[i]) { - dp[i] = dp[j] + 1; + dp[i] = 1 + dp[j]; cnt[i] = cnt[j]; } + else if(nums[i] > nums[j] and dp[j] + 1 == dp[i]) + { cnt[i] += cnt[j]; + } } - maxi = max(dp[i],maxi); + + maxi = max(maxi, dp[i]); } - for(int i = 0; i Date: Sat, 22 Jul 2023 23:07:44 +0530 Subject: [PATCH 1294/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0688-knight-probability-in-chessboard/README.md diff --git a/0688-knight-probability-in-chessboard/README.md b/0688-knight-probability-in-chessboard/README.md new file mode 100644 index 00000000..66bda063 --- /dev/null +++ b/0688-knight-probability-in-chessboard/README.md @@ -0,0 +1,35 @@ +

688. Knight Probability in Chessboard

Medium


On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).

+ +

A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.

+ +

Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

+ +

The knight continues moving until it has made exactly k moves or has moved off the chessboard.

+ +

Return the probability that the knight remains on the board after it has stopped moving.

+ +

 

+

Example 1:

+ +
Input: n = 3, k = 2, row = 0, column = 0
+Output: 0.06250
+Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
+From each of those positions, there are also two moves that will keep the knight on the board.
+The total probability the knight stays on the board is 0.0625.
+
+ +

Example 2:

+ +
Input: n = 1, k = 0, row = 0, column = 0
+Output: 1.00000
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 25
  • +
  • 0 <= k <= 100
  • +
  • 0 <= row, column <= n - 1
  • +
+
\ No newline at end of file From 17b1322a4c3bf0cd6d8e6d7ad7b8e8782fe6d68c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:07:47 +0530 Subject: [PATCH 1295/3167] Time: 21 ms (56.61%), Space: 9.1 MB (41.76%) - LeetHub --- .../0688-knight-probability-in-chessboard.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0688-knight-probability-in-chessboard/0688-knight-probability-in-chessboard.cpp diff --git a/0688-knight-probability-in-chessboard/0688-knight-probability-in-chessboard.cpp b/0688-knight-probability-in-chessboard/0688-knight-probability-in-chessboard.cpp new file mode 100644 index 00000000..ce550e79 --- /dev/null +++ b/0688-knight-probability-in-chessboard/0688-knight-probability-in-chessboard.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + double knightProbability(int n, int k, int row, int column) { + + if(n == 1 and k == 1) + return 0.0; + + vector> curr(n, vector(n,0.0)); + + curr[row][column] = 1.0; + + for(int moves = 1; moves <= k; ++moves) + { + vector> next(n, vector(n,0.0)); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(curr[i][j] != 0) + { + int dx[8]={ 2, 1, -1, -2, -2, -1, 1, 2 }; + int dy[8]={ 1, 2, 2, 1, -1, -2, -2, -1 }; + + for(int k = 0; k < 8; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(newx >= 0 and newx < n and newy >= 0 and newy < n) + { + next[newx][newy] += curr[i][j]/8.0; + } + } + } + } + } + + curr = next; + } + + double ans = 0.0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + ans += curr[i][j]; + } + + return ans; + } +}; \ No newline at end of file From 6210fea45973a1e5f45b361b21de8678c4e21cd1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jul 2023 23:39:05 +0530 Subject: [PATCH 1296/3167] Create README - LeetHub --- 0894-all-possible-full-binary-trees/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0894-all-possible-full-binary-trees/README.md diff --git a/0894-all-possible-full-binary-trees/README.md b/0894-all-possible-full-binary-trees/README.md new file mode 100644 index 00000000..6a00abbe --- /dev/null +++ b/0894-all-possible-full-binary-trees/README.md @@ -0,0 +1,26 @@ +

894. All Possible Full Binary Trees

Medium


Given an integer n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0.

+ +

Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.

+ +

A full binary tree is a binary tree where each node has exactly 0 or 2 children.

+ +

 

+

Example 1:

+ +
Input: n = 7
+Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
+
+ +

Example 2:

+ +
Input: n = 3
+Output: [[0,0,0]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 20
  • +
+
\ No newline at end of file From 7401fc7827ca92b5fa49b963ede1b15d09fcfee9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jul 2023 22:08:10 +0530 Subject: [PATCH 1297/3167] Create README - LeetHub --- Right View of Binary Tree - GFG/README.md | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Right View of Binary Tree - GFG/README.md diff --git a/Right View of Binary Tree - GFG/README.md b/Right View of Binary Tree - GFG/README.md new file mode 100644 index 00000000..b73e9ea4 --- /dev/null +++ b/Right View of Binary Tree - GFG/README.md @@ -0,0 +1,44 @@ +# Right View of Binary Tree +## Easy +

Given a Binary Tree, find Right view of it. Right view of a Binary Tree is set of nodes visible when tree is viewed from right side.

+ +

Right view of following tree is 1 3 7 8.

+ +

          1
+       /     \
+     2        3
+   /   \      /    \
+  4     5   6    7
+    \
+     8

+ +

Example 1:

+ +
Input:
+       1
+    /    \
+   3      2
+Output: 1 2
+
+ +

Example 2:

+ +
Input:
+     10
+    /   \
+  20     30
+ /   \
+40  60 
+Output: 10 30 60
+
+ +

Your Task:
+Just complete the function rightView() that takes node as parameter and returns the right view as a list. 

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(Height of the Tree).

+ +

Constraints:
+1 ≤ Number of nodes ≤ 105
+0 ≤ Data of a node ≤ 105

+
\ No newline at end of file From bdd7479422278e2399af39a617ff0314127971b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jul 2023 22:08:11 +0530 Subject: [PATCH 1298/3167] Added solution - LeetHub --- .../right-view-of-binary-tree.cpp | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 Right View of Binary Tree - GFG/right-view-of-binary-tree.cpp diff --git a/Right View of Binary Tree - GFG/right-view-of-binary-tree.cpp b/Right View of Binary Tree - GFG/right-view-of-binary-tree.cpp new file mode 100644 index 00000000..76622afc --- /dev/null +++ b/Right View of Binary Tree - GFG/right-view-of-binary-tree.cpp @@ -0,0 +1,165 @@ +//{ Driver Code Starts +#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; +} + + +// } Driver Code Ends +/* A binary tree node has data, pointer to left child + and a pointer to right child +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; */ + +// Should return right view of tree +class Solution +{ + private: + void helper(Node* root, int& maxLevel, int level, vector& ans) + { + if(!root) + return; + + if(level > maxLevel) + { + ans.push_back(root->data); + maxLevel = level; + } + + helper(root->right, maxLevel, level + 1, ans); + helper(root->left, maxLevel, level + 1, ans); + } + + public: + //Function to return list containing elements of right view of binary tree. + vector rightView(Node *root) + { + // Your Code here + + int maxLevel = -1; + + vector ans; + + helper(root, maxLevel, 0, ans); + + return ans; + } +}; + + + +//{ Driver Code Starts. + +// 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; +} + + +int main() { + int t; + string tc; + getline(cin,tc); + t=stoi(tc); + while(t--) + { + string s; + getline(cin,s); + Node* root = buildTree(s); + + Solution ob; + vector vec = ob.rightView(root); + for(int x : vec){ + cout< Date: Mon, 24 Jul 2023 22:45:19 +0530 Subject: [PATCH 1299/3167] Attach NOTES - LeetHub From 7f00950bd4790d1e687b2936bb96b9be41a83341 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jul 2023 22:45:22 +0530 Subject: [PATCH 1300/3167] Time: 2 ms (45.63%), Space: 5.9 MB (62.65%) - LeetHub From 39af249ac99babbd2a02934b42dcac7afa447705 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:21:57 +0530 Subject: [PATCH 1301/3167] Create README - LeetHub --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Level order traversal in spiral form - GFG/README.md diff --git a/Level order traversal in spiral form - GFG/README.md b/Level order traversal in spiral form - GFG/README.md new file mode 100644 index 00000000..e58fd3f4 --- /dev/null +++ b/Level order traversal in spiral form - GFG/README.md @@ -0,0 +1,24 @@ +# Level order traversal in spiral form +## Easy +

Given a binary tree and the task is to find the spiral order traversal of the tree.

+

Spiral order Traversal mean: Starting from level 0 for root node, for all the even levels we print the node's value from right to left and for all the odd levels we print the node's value from left to right. 

+

For below tree, function should return 1, 2, 3, 4, 5, 6, 7.


 

 

+

Example 1:

+
Input:
+      1
+    /   \
+   3     2
+Output:1 3 2
+
+
+

Example 2:

+
Input:
+           10
+         /     \
+        20     30
+      /    \
+    40     60
+Output: 10 20 30 60 40 
+
+

Your Task:
The task is to complete the function findSpiral() which takes root node as input parameter and returns the elements in spiral form of level order traversal as a list. The newline is automatically appended by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

+

Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 105

\ No newline at end of file From 744f5cc78003febb39e2a6bae6f1c476892bc0f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:21:58 +0530 Subject: [PATCH 1302/3167] Added solution - LeetHub --- .../level-order-traversal-in-spiral-form.java | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 Level order traversal in spiral form - GFG/level-order-traversal-in-spiral-form.java diff --git a/Level order traversal in spiral form - GFG/level-order-traversal-in-spiral-form.java b/Level order traversal in spiral form - GFG/level-order-traversal-in-spiral-form.java new file mode 100644 index 00000000..7afd39e1 --- /dev/null +++ b/Level order traversal in spiral form - GFG/level-order-traversal-in-spiral-form.java @@ -0,0 +1,191 @@ +//{ Driver Code Starts +//Initial Template for Java + + + +//Initial Template for Java + +//Contributed by Sudarshan Sharma +import java.util.LinkedList; +import java.util.Queue; +import java.io.*; +import java.util.*; + +class Node{ + int data; + Node left; + Node right; + Node(int data){ + this.data = data; + left=null; + right=null; + } +} + + +class GfG { + + static Node buildTree(String str){ + + if(str.length()==0 || str.charAt(0)=='N'){ + return null; + } + + String ip[] = str.split(" "); + // Create the root of the tree + Node root = new Node(Integer.parseInt(ip[0])); + // Push the root to the queue + + Queue queue = new LinkedList<>(); + + queue.add(root); + // Starting from the second element + + int i = 1; + while(queue.size()>0 && i < ip.length) { + + // Get and remove the front of the queue + Node currNode = queue.peek(); + queue.remove(); + + // Get the current node's value from the string + String currVal = ip[i]; + + // If the left child is not null + if(!currVal.equals("N")) { + + // Create the left child for the current node + currNode.left = new Node(Integer.parseInt(currVal)); + // Push it to the queue + queue.add(currNode.left); + } + + // For the right child + i++; + if(i >= ip.length) + break; + + currVal = ip[i]; + + // If the right child is not null + if(!currVal.equals("N")) { + + // Create the right child for the current node + currNode.right = new Node(Integer.parseInt(currVal)); + + // Push it to the queue + queue.add(currNode.right); + } + i++; + } + + return root; + } + void inOrder(Node node) { + if (node == null) { + return; + } + + inOrder(node.left); + System.out.print(node.data + " "); + + inOrder(node.right); + } + + public static void main (String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int t=Integer.parseInt(br.readLine()); + + while(t-- > 0){ + String s = br.readLine(); + Node root = buildTree(s); + Spiral g = new Spiral(); + ArrayList result = g.findSpiral(root); + for(int value : result) + System.out.print(value + " "); + System.out.println(); + + } + } +} + + + + +// } Driver Code Ends + + +//User function Template for Java + + +/* +// A Binary Tree node +class Node +{ + int data; + Node left, right; + + Node(int item) + { + data = item; + left = right = null; + } +} +*/ + +class Spiral +{ + //Function to return a list containing the level order + //traversal in spiral form. + ArrayList findSpiral(Node root) + { + // Your code here + + ArrayList arr = new ArrayList<>(); + + if(root == null) + return arr; + + Queue q = new LinkedList<>(); + + q.add(root); + + int cnt = 0; + + while(!q.isEmpty()) + { + int size = q.size(); + + ArrayList level = new ArrayList<>(); + + for(int i = 0; i < size; ++i) + { + Node cur = q.poll(); + + level.add(cur.data); + + if(cur.left != null) + q.add(cur.left); + if(cur.right != null) + q.add(cur.right); + } + + if(cnt % 2 == 0) + { + for(int i = size-1; i >= 0; --i) + arr.add(level.get(i)); + } + else + { + for(int i = 0; i < size; ++i) + arr.add(level.get(i)); + } + + ++cnt; + } + + return arr; + + } +} \ No newline at end of file From 1813e02c5bfb866d9c4336b1a151fd3fb16682a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:29:38 +0530 Subject: [PATCH 1303/3167] Create README - LeetHub --- 0852-peak-index-in-a-mountain-array/README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0852-peak-index-in-a-mountain-array/README.md diff --git a/0852-peak-index-in-a-mountain-array/README.md b/0852-peak-index-in-a-mountain-array/README.md new file mode 100644 index 00000000..e6e7ee11 --- /dev/null +++ b/0852-peak-index-in-a-mountain-array/README.md @@ -0,0 +1,44 @@ +

852. Peak Index in a Mountain Array

Medium


An array arr a mountain if the following properties hold:

+ +
    +
  • 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 arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].

+ +

You must solve it in O(log(arr.length)) time complexity.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: arr = [0,2,1,0]
+Output: 1
+
+ +

Example 3:

+ +
Input: arr = [0,10,5,2]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= arr.length <= 105
  • +
  • 0 <= arr[i] <= 106
  • +
  • arr is guaranteed to be a mountain array.
  • +
+
\ No newline at end of file From f10d62387c578c8640f0db98af4789aec1e7d08f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:29:39 +0530 Subject: [PATCH 1304/3167] Attach NOTES - LeetHub --- 0852-peak-index-in-a-mountain-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0852-peak-index-in-a-mountain-array/NOTES.md diff --git a/0852-peak-index-in-a-mountain-array/NOTES.md b/0852-peak-index-in-a-mountain-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0852-peak-index-in-a-mountain-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 86cdfc0c0fdcef8a061fdf0ba811787715ea6c66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:29:42 +0530 Subject: [PATCH 1305/3167] Time: 129 ms (47.91%), Space: 59.6 MB (40.39%) - LeetHub --- .../0852-peak-index-in-a-mountain-array.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp diff --git a/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp b/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp new file mode 100644 index 00000000..0b7b5372 --- /dev/null +++ b/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int peakIndexInMountainArray(vector& arr) { + + int n = arr.size(); + + for(int i = 1; i < n-1; ++i) + { + if(arr[i] > arr[i-1] and arr[i] > arr[i+1]) + return i; + } + + return -1; + + } +}; \ No newline at end of file From 4c6f4cc675323d560a711a703cddef6d78b416bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:33:27 +0530 Subject: [PATCH 1306/3167] Attach NOTES - LeetHub From 271160394e2b6b8fe13c9a0f2c8162989c3209e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:33:30 +0530 Subject: [PATCH 1307/3167] Time: 129 ms (47.91%), Space: 59.6 MB (40.39%) - LeetHub From b36274dbf160974fa2511343eafa260d61dbbc75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:35:49 +0530 Subject: [PATCH 1308/3167] Attach NOTES - LeetHub From 80d510ad85a290955353e7ed28045721949fc812 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:35:54 +0530 Subject: [PATCH 1309/3167] Time: 124 ms (67.11%), Space: 59.6 MB (40.39%) - LeetHub --- .../0852-peak-index-in-a-mountain-array.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp b/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp index 0b7b5372..369ec8de 100644 --- a/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp +++ b/0852-peak-index-in-a-mountain-array/0852-peak-index-in-a-mountain-array.cpp @@ -4,13 +4,19 @@ class Solution { int n = arr.size(); - for(int i = 1; i < n-1; ++i) + int start = 0, end = n-1; + + while(start <= end) { - if(arr[i] > arr[i-1] and arr[i] > arr[i+1]) - return i; + int mid = (start + end) >> 1; + + if(arr[mid] < arr[mid+1]) + start = mid+1; + else + end = mid-1; } - return -1; + return start; } }; \ No newline at end of file From 9f27046fd01af189d645e4234d8ac36f41354586 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jul 2023 23:32:47 +0530 Subject: [PATCH 1310/3167] Create README - LeetHub --- Kth Ancestor in a Tree - GFG/README.md | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Kth Ancestor in a Tree - GFG/README.md diff --git a/Kth Ancestor in a Tree - GFG/README.md b/Kth Ancestor in a Tree - GFG/README.md new file mode 100644 index 00000000..3ac85373 --- /dev/null +++ b/Kth Ancestor in a Tree - GFG/README.md @@ -0,0 +1,30 @@ +# Kth Ancestor in a Tree +## Medium +

Given a binary tree of size  N, a node, and a positive integer k., Your task is to complete the function kthAncestor(), the function should return the kth ancestor of the given node in the binary tree. If there does not exist any such ancestor then return -1.
Note: It is guaranteed that the node exists in the tree.

+

Example 1:

+

+
+Input:
+K = 2 Node = 4
+Output: 1
+Explanation:
+Since, K is 2 and node is 4, so we
+first need to locate the node and
+look k times its ancestors.
+Here in this Case node 4 has 1 as his
+2nd Ancestor aka the Root of the tree.
+

Example 2:

+
Input:
+k=1 
+node=3
+      1
+    /   \
+    2     3
+
+Output:
+1
+Explanation:
+K=1 and node=3 ,Kth ancestor of node 3 is 1.
+

Your Task:
You are asked to complete the function kthAncestor() which accepts root of the tree, k and node as input parameters, and returns the kth ancestor of Node which contains node as its value.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

+

Constraints:
1<=N<=105
1<= K <= 100
1 <= Node.data <= N

\ No newline at end of file From 994b0c555c681f5eea3a4dd9d2e986d02f8c2d3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jul 2023 23:32:48 +0530 Subject: [PATCH 1311/3167] Added solution - LeetHub --- .../kth-ancestor-in-a-tree.cpp | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 Kth Ancestor in a Tree - GFG/kth-ancestor-in-a-tree.cpp diff --git a/Kth Ancestor in a Tree - GFG/kth-ancestor-in-a-tree.cpp b/Kth Ancestor in a Tree - GFG/kth-ancestor-in-a-tree.cpp new file mode 100644 index 00000000..5d60c091 --- /dev/null +++ b/Kth Ancestor in a Tree - GFG/kth-ancestor-in-a-tree.cpp @@ -0,0 +1,152 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; +}; +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} +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; +} +int kthAncestor(Node *root, int k, int node); + +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + int k , node; + scanf("%d ",&k); + scanf("%d ",&node); + string s; + getline(cin,s); + Node* root = buildTree(s); + cout<data == node) + return 0; + + int left = helper(root->left, k , node, ans); + int right = helper(root->right, k, node, ans); + + if(left != -1 and right == -1) + { + if(left + 1 == k) + ans = root->data; + return left + 1; + } + + if(left == -1 and right != -1) + { + if(right + 1 == k) + ans = root->data; + return right + 1; + } + + return -1; +} + +int kthAncestor(Node *root, int k, int node) +{ + // Code here + int ans = -1; + + helper(root, k , node, ans); + + return ans; +} From e51bd7642bd897298d404656f7d1766a0fde878a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jul 2023 00:09:51 +0530 Subject: [PATCH 1312/3167] Create README - LeetHub --- .../README.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 1870-minimum-speed-to-arrive-on-time/README.md diff --git a/1870-minimum-speed-to-arrive-on-time/README.md b/1870-minimum-speed-to-arrive-on-time/README.md new file mode 100644 index 00000000..49497e4c --- /dev/null +++ b/1870-minimum-speed-to-arrive-on-time/README.md @@ -0,0 +1,53 @@ +

1870. Minimum Speed to Arrive on Time

Medium


You are given a floating-point number hour, representing the amount of time you have to reach the office. To commute to the office, you must take n trains in sequential order. You are also given an integer array dist of length n, where dist[i] describes the distance (in kilometers) of the ith train ride.

+ +

Each train can only depart at an integer hour, so you may need to wait in between each train ride.

+ +
    +
  • For example, if the 1st train ride takes 1.5 hours, you must wait for an additional 0.5 hours before you can depart on the 2nd train ride at the 2 hour mark.
  • +
+ +

Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, or -1 if it is impossible to be on time.

+ +

Tests are generated such that the answer will not exceed 107 and hour will have at most two digits after the decimal point.

+ +

 

+

Example 1:

+ +
Input: dist = [1,3,2], hour = 6
+Output: 1
+Explanation: At speed 1:
+- The first train ride takes 1/1 = 1 hour.
+- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.
+- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.
+- You will arrive at exactly the 6 hour mark.
+
+ +

Example 2:

+ +
Input: dist = [1,3,2], hour = 2.7
+Output: 3
+Explanation: At speed 3:
+- The first train ride takes 1/3 = 0.33333 hours.
+- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.
+- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.
+- You will arrive at the 2.66667 hour mark.
+
+ +

Example 3:

+ +
Input: dist = [1,3,2], hour = 1.9
+Output: -1
+Explanation: It is impossible because the earliest the third train can depart is at the 2 hour mark.
+
+ +

 

+

Constraints:

+ +
    +
  • n == dist.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= dist[i] <= 105
  • +
  • 1 <= hour <= 109
  • +
  • There will be at most two digits after the decimal point in hour.
  • +
+
\ No newline at end of file From 8663087fb44f07d2861c5c7760f0044ef15c20f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jul 2023 00:09:51 +0530 Subject: [PATCH 1313/3167] Attach NOTES - LeetHub --- 1870-minimum-speed-to-arrive-on-time/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1870-minimum-speed-to-arrive-on-time/NOTES.md diff --git a/1870-minimum-speed-to-arrive-on-time/NOTES.md b/1870-minimum-speed-to-arrive-on-time/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1870-minimum-speed-to-arrive-on-time/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fa5fea68cda7fd2f4957a0b0bb3086baa7d8d0ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jul 2023 00:09:54 +0530 Subject: [PATCH 1314/3167] Time: 385 ms (35.39%), Space: 101.4 MB (94.10%) - LeetHub --- .../1870-minimum-speed-to-arrive-on-time.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1870-minimum-speed-to-arrive-on-time/1870-minimum-speed-to-arrive-on-time.cpp diff --git a/1870-minimum-speed-to-arrive-on-time/1870-minimum-speed-to-arrive-on-time.cpp b/1870-minimum-speed-to-arrive-on-time/1870-minimum-speed-to-arrive-on-time.cpp new file mode 100644 index 00000000..856e8dbb --- /dev/null +++ b/1870-minimum-speed-to-arrive-on-time/1870-minimum-speed-to-arrive-on-time.cpp @@ -0,0 +1,32 @@ +class Solution +{ + bool canReachInTime(const vector& dist, const double hour, int speed) + { + double time = 0; + for (int i = 0; i < dist.size() - 1; ++i) + time += ((dist[i] + speed - 1) / speed); + + time += ((double)dist.back()) / speed; + return time <= hour; + } + +public: + int minSpeedOnTime(vector& dist, double hour) + { + int N = dist.size(); + if (hour <= (double)(N - 1)) + return -1; + + int lo = 1, hi = 1e7, mi; + while (lo < hi) + { + mi = (lo + hi) / 2; + if (canReachInTime(dist, hour, mi)) + hi = mi; + else + lo = mi + 1; + } + + return hi; + } +}; \ No newline at end of file From fa4d601a07ea8f3825892f5999f6c4e359e7d25f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jul 2023 22:51:10 +0530 Subject: [PATCH 1315/3167] Attach NOTES - LeetHub --- 2141-maximum-running-time-of-n-computers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2141-maximum-running-time-of-n-computers/NOTES.md diff --git a/2141-maximum-running-time-of-n-computers/NOTES.md b/2141-maximum-running-time-of-n-computers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2141-maximum-running-time-of-n-computers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0789f0e54ba15538281fa7764247853bddcb5f42 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jul 2023 22:51:14 +0530 Subject: [PATCH 1316/3167] Time: 117 ms (99.64%), Space: 55.7 MB (98.56%) - LeetHub --- ...41-maximum-running-time-of-n-computers.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2141-maximum-running-time-of-n-computers/2141-maximum-running-time-of-n-computers.cpp diff --git a/2141-maximum-running-time-of-n-computers/2141-maximum-running-time-of-n-computers.cpp b/2141-maximum-running-time-of-n-computers/2141-maximum-running-time-of-n-computers.cpp new file mode 100644 index 00000000..a6e3efc9 --- /dev/null +++ b/2141-maximum-running-time-of-n-computers/2141-maximum-running-time-of-n-computers.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + bool isPossible(vector& batteries, long long time, int computers){ + long long totTime = time*computers; + + for(long long bTime : batteries) + totTime -= min(time, bTime); + + return (totTime <= 0); + } + long long maxRunTime(int n, vector& batteries) { + long long low = 0, high = 0; + int si = batteries.size(); + + for(int i = 0; i < si; i++){ + high += batteries[i]; + } + + long long ans = 0; + while(low <= high){ + + long long mid = low + (high-low)/2; + + if(isPossible(batteries, mid, n)){ + ans = mid; + low = mid+1; + } + else{ + high = mid-1; + } + } + + return ans; + } + +}; \ No newline at end of file From f7623d771fba9f8a0b15b0983ce1da5bd439dc1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jul 2023 21:04:35 +0530 Subject: [PATCH 1317/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Lowest Common Ancestor in a BST - GFG/README.md diff --git a/Lowest Common Ancestor in a BST - GFG/README.md b/Lowest Common Ancestor in a BST - GFG/README.md new file mode 100644 index 00000000..2d3883ae --- /dev/null +++ b/Lowest Common Ancestor in a BST - GFG/README.md @@ -0,0 +1,26 @@ +# Lowest Common Ancestor in a BST +## Easy +

Given a Binary Search Tree (with all values unique) and two node values n1 and n2 (n1!=n2). Find the Lowest Common Ancestors of the two nodes in the BST.

+

Example 1:

+
Input:
+              5
+            /   \
+          4      6
+         /        \
+        3          7
+                    \
+                     8
+n1 = 7, n2 = 8
+Output: 7
+
+

Example 2:

+
Input:
+     2
+   /   \
+  1     3
+n1 = 1, n2 = 3
+Output: 2
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function LCA() which takes the root Node of the BST and two integer values n1 and n2 as inputs and returns the Lowest Common Ancestor of the Nodes with values n1 and n2 in the given BST. 

+

Expected Time Complexity: O(Height of the BST).
Expected Auxiliary Space: O(Height of the BST).

+

Constraints:
1 <= N <= 104

\ No newline at end of file From ded691e644d37fba2c162bca7d3d971f5a312012 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jul 2023 21:04:36 +0530 Subject: [PATCH 1318/3167] Added solution - LeetHub --- .../lowest-common-ancestor-in-a-bst.cpp | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Lowest Common Ancestor in a BST - GFG/lowest-common-ancestor-in-a-bst.cpp diff --git a/Lowest Common Ancestor in a BST - GFG/lowest-common-ancestor-in-a-bst.cpp b/Lowest Common Ancestor in a BST - GFG/lowest-common-ancestor-in-a-bst.cpp new file mode 100644 index 00000000..b0ce9934 --- /dev/null +++ b/Lowest Common Ancestor in a BST - GFG/lowest-common-ancestor-in-a-bst.cpp @@ -0,0 +1,129 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +// Function to Build Tree + + +// } Driver Code Ends +//Function to find the lowest common ancestor in a BST. +class Solution{ + public: + Node* LCA(Node *root, int n1, int n2) + { + // code here + + if(!root or root->data == n1 or root->data == n2) + return root; + + Node* left = LCA(root->left, n1, n2); + Node* right = LCA(root->right, n1, n2); + + if(!left and !right) + return nullptr; + else if(!left) + return right; + else if(!right) + return left; + return root; + } + +}; + + +//{ Driver Code Starts. + +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; +} + + +int main() +{ + + int t; + scanf("%d ",&t); + while(t--) + { + string s; + int l , h; + getline(cin,s); + scanf("%d ",&l); + scanf("%d ",&h); + Node* root = buildTree(s); + Solution sol; + int ans = sol.LCA(root, l, h)->data; + cout< Date: Fri, 28 Jul 2023 22:29:36 +0530 Subject: [PATCH 1319/3167] Create README - LeetHub --- 0486-predict-the-winner/README.md | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0486-predict-the-winner/README.md diff --git a/0486-predict-the-winner/README.md b/0486-predict-the-winner/README.md new file mode 100644 index 00000000..42b757b5 --- /dev/null +++ b/0486-predict-the-winner/README.md @@ -0,0 +1,33 @@ +

486. Predict the Winner

Medium


You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2.

+ +

Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array.

+ +

Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.

+ +

 

+

Example 1:

+ +
Input: nums = [1,5,2]
+Output: false
+Explanation: Initially, player 1 can choose between 1 and 2. 
+If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). 
+So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. 
+Hence, player 1 will never be the winner and you need to return false.
+
+ +

Example 2:

+ +
Input: nums = [1,5,233,7]
+Output: true
+Explanation: Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
+Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 20
  • +
  • 0 <= nums[i] <= 107
  • +
+
\ No newline at end of file From 03c64be649e5afd9e2153bf4fa5357237f3f9849 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jul 2023 22:29:39 +0530 Subject: [PATCH 1320/3167] Time: 4 ms (63.90%), Space: 7.8 MB (17.26%) - LeetHub --- .../0486-predict-the-winner.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0486-predict-the-winner/0486-predict-the-winner.cpp diff --git a/0486-predict-the-winner/0486-predict-the-winner.cpp b/0486-predict-the-winner/0486-predict-the-winner.cpp new file mode 100644 index 00000000..9a5f4f9c --- /dev/null +++ b/0486-predict-the-winner/0486-predict-the-winner.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + + + int helper(int i, int j, vector& nums, vector>& dp) + { + if(i == j) + return nums[i]; + if(dp[i][j] != -1) + return dp[i][j]; + int start = nums[i] - helper(i+1, j, nums, dp); + int end = nums[j] - helper(i,j-1,nums, dp); + + return dp[i][j] = max(start, end); + } + + bool PredictTheWinner(vector& nums) { + + int n = nums.size(); + vector > dp(n, vector(n,-1)); + return helper(0 , n-1, nums, dp) >= 0; + + } +}; \ No newline at end of file From 471490fabff9117ed64822c43c0b958eb0a8bcdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jul 2023 23:07:23 +0530 Subject: [PATCH 1321/3167] Create README - LeetHub --- 0808-soup-servings/README.md | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0808-soup-servings/README.md diff --git a/0808-soup-servings/README.md b/0808-soup-servings/README.md new file mode 100644 index 00000000..d4e862af --- /dev/null +++ b/0808-soup-servings/README.md @@ -0,0 +1,39 @@ +

808. Soup Servings

Medium


There are two types of soup: type A and type B. Initially, we have n ml of each type of soup. There are four kinds of operations:

+ +
    +
  1. Serve 100 ml of soup A and 0 ml of soup B,
  2. +
  3. Serve 75 ml of soup A and 25 ml of soup B,
  4. +
  5. Serve 50 ml of soup A and 50 ml of soup B, and
  6. +
  7. Serve 25 ml of soup A and 75 ml of soup B.
  8. +
+ +

When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as possible. We stop once we no longer have some quantity of both types of soup.

+ +

Note that we do not have an operation where all 100 ml's of soup B are used first.

+ +

Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time. Answers within 10-5 of the actual answer will be accepted.

+ +

 

+

Example 1:

+ +
Input: n = 50
+Output: 0.62500
+Explanation: If we choose the first two operations, A will become empty first.
+For the third operation, A and B will become empty at the same time.
+For the fourth operation, B will become empty first.
+So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
+
+ +

Example 2:

+ +
Input: n = 100
+Output: 0.71875
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 109
  • +
+
\ No newline at end of file From 67908745e4d6f6e5b25df6ec867029f3938334d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jul 2023 11:15:22 +0530 Subject: [PATCH 1322/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Strongly connected component (Tarjans's Algo) - GFG/README.md diff --git a/Strongly connected component (Tarjans's Algo) - GFG/README.md b/Strongly connected component (Tarjans's Algo) - GFG/README.md new file mode 100644 index 00000000..a8379078 --- /dev/null +++ b/Strongly connected component (Tarjans's Algo) - GFG/README.md @@ -0,0 +1,25 @@ +# Strongly connected component (Tarjans's Algo) +## Hard +

Given a Directed Graph with V vertices and E edges, Find the members of strongly connected components in the graph.

+


Example 1:

+
Input:
+
+Output:
+0 1 2,3,4,
+Explanation:
+
+We can clearly see that there are 3 Strongly
+Connected Components in the Graph as mentioned
+in the Output.
+
+

Example 2:

+
Input:
+
+Output:
+0 1 2,
+Explanation:
+All of the nodes are connected to each other.
+So, there's only one SCC as shown.
+


Your Task:
You don't need to read input or print anything. Your task is to complete the function tarjans() which takes the number of vertices V and adjacency list of the graph as input parameters and returns a list of list of integers denoting the members of strongly connected components in the given graph.
Note: A single strongly connected component must be represented in the form of a list if integers sorted in the ascending order. The resulting list should consist of a list of all SCCs which must be sorted in a way such that a lexicographically smaller list of integers appears first.

+


Expected Time Complexity: O(V + E).
Expected Auxiliary Space: O(V).

+


Constraints:
1
105
1
105
0
u, v N-1

\ No newline at end of file From ca25785ce31b5017c689fd67a3081fc74a127d5d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jul 2023 11:15:23 +0530 Subject: [PATCH 1323/3167] Added solution - LeetHub --- ...ngly-connected-component-tarjanss-algo.cpp | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Strongly connected component (Tarjans's Algo) - GFG/strongly-connected-component-tarjanss-algo.cpp diff --git a/Strongly connected component (Tarjans's Algo) - GFG/strongly-connected-component-tarjanss-algo.cpp b/Strongly connected component (Tarjans's Algo) - GFG/strongly-connected-component-tarjanss-algo.cpp new file mode 100644 index 00000000..bcc9af87 --- /dev/null +++ b/Strongly connected component (Tarjans's Algo) - GFG/strongly-connected-component-tarjanss-algo.cpp @@ -0,0 +1,139 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +//User function template for C++ + +class Solution +{ + public: + //Function to return a list of lists of integers denoting the members + //of strongly connected components in the given graph. + + void dfs(int sv, vector adj[], vector& tin, vector& low, vector& inStack, stack&st, vector>& res) + { + static int timer = 0; + + tin[sv] = low[sv] = timer; + + ++timer; + + st.push(sv); + + inStack[sv] = true; + + for(auto itr : adj[sv]) + { + if(tin[itr] == -1) + { + dfs(itr, adj, tin, low, inStack, st, res); + low[sv] = min(low[sv], low[itr]); + } + else if(inStack[itr]) + { + // if node is visited and + // if node is present in stack that means it is a back edge and if it is not present in stack + // it is cross edge and we will do not consider cross edges. + + low[sv] = min(low[sv], tin[itr]); + } + } + + // if low[sv] == tin[sv] that means it is head node of the strong connected commonent + + if(low[sv] == tin[sv]) + { + vector scc; + + while(st.top() != sv) + { + scc.push_back(st.top()); + inStack[st.top()] = false; + st.pop(); + } + + scc.push_back(sv); + inStack[st.top()] = false; + st.pop(); + + sort(scc.begin(), scc.end()); + + res.push_back(scc); + } + + } + + vector> TarjansAlgorithm(vector adj[], int n) + { + vector> res; + + vector tin(n,-1), low(n,-1); + + vector inStack(n, false); + + stack st; + + for(int i = 0; i < n; ++i) + { + if(tin[i] == -1) + dfs(i, adj, tin, low, inStack, st, res); + } + + sort(res.begin(), res.end()); + + return res; + } + + vector> tarjans(int V, vector adj[]) + { + //code here + + return TarjansAlgorithm(adj, V); + } +}; + +//{ 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> ptr = obj.tarjans(V, adj); + + for(int i=0; i Date: Sun, 30 Jul 2023 21:46:00 +0530 Subject: [PATCH 1324/3167] Create README - LeetHub --- 0152-maximum-product-subarray/README.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0152-maximum-product-subarray/README.md diff --git a/0152-maximum-product-subarray/README.md b/0152-maximum-product-subarray/README.md new file mode 100644 index 00000000..4742c655 --- /dev/null +++ b/0152-maximum-product-subarray/README.md @@ -0,0 +1,28 @@ +

152. Maximum Product Subarray

Medium


Given an integer array nums, find a subarray that has the largest product, and return the product.

+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,-2,4]
+Output: 6
+Explanation: [2,3] has the largest product 6.
+
+ +

Example 2:

+ +
Input: nums = [-2,0,-1]
+Output: 0
+Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 2 * 104
  • +
  • -10 <= nums[i] <= 10
  • +
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  • +
+
\ No newline at end of file From 6b178149d2a1a539670938bea363d4e1718f49e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jul 2023 21:46:04 +0530 Subject: [PATCH 1325/3167] Time: 4 ms (86.56%), Space: 13.7 MB (59.85%) - LeetHub --- .../0152-maximum-product-subarray.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0152-maximum-product-subarray/0152-maximum-product-subarray.cpp diff --git a/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp b/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp new file mode 100644 index 00000000..cac945f3 --- /dev/null +++ b/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int maxProduct(vector& nums) { + + int n = nums.size(); + + int max_end = nums[0]; + int min_end = nums[0]; + int overall_max = nums[0]; + + for(int i = 1; i < n; ++i) + { + int temp = max_end; + + max_end = max({nums[i], max_end * nums[i], min_end * nums[i]}); + min_end = min({nums[i], temp * nums[i], min_end * nums[i]}); + + overall_max = max({overall_max, max_end, min_end}); + } + + return overall_max; + + } +}; \ No newline at end of file From ed942de9b6123ce5008eefda2cfd2e87419bb8bb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jul 2023 21:47:47 +0530 Subject: [PATCH 1326/3167] Attach NOTES - LeetHub --- 0152-maximum-product-subarray/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0152-maximum-product-subarray/NOTES.md diff --git a/0152-maximum-product-subarray/NOTES.md b/0152-maximum-product-subarray/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0152-maximum-product-subarray/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f517769dce59851c526b3f07329b6382821c5dea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jul 2023 21:47:50 +0530 Subject: [PATCH 1327/3167] Time: 5 ms (77.79%), Space: 13.7 MB (97.47%) - LeetHub --- .../0152-maximum-product-subarray.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp b/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp index cac945f3..5833f4c2 100644 --- a/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp +++ b/0152-maximum-product-subarray/0152-maximum-product-subarray.cpp @@ -4,21 +4,23 @@ class Solution { int n = nums.size(); - int max_end = nums[0]; - int min_end = nums[0]; - int overall_max = nums[0]; + int overall_max = INT_MIN; - for(int i = 1; i < n; ++i) + int pref = 1, suff = 1; + + for(int i = 0; i < n; ++i) { - int temp = max_end; + if(pref == 0) + pref = 1; + if(suff == 0) + suff = 1; - max_end = max({nums[i], max_end * nums[i], min_end * nums[i]}); - min_end = min({nums[i], temp * nums[i], min_end * nums[i]}); + pref *= nums[i]; + suff *= nums[n-i-1]; - overall_max = max({overall_max, max_end, min_end}); + overall_max = max({overall_max, pref, suff}); } return overall_max; - } }; \ No newline at end of file From 1c6c1c75fdb49df8ef9f5a6445c8f0d7ac4b695b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 03:15:23 +0530 Subject: [PATCH 1328/3167] Create README - LeetHub --- 0664-strange-printer/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0664-strange-printer/README.md diff --git a/0664-strange-printer/README.md b/0664-strange-printer/README.md new file mode 100644 index 00000000..0a0cb354 --- /dev/null +++ b/0664-strange-printer/README.md @@ -0,0 +1,32 @@ +

664. Strange Printer

Hard


There is a strange printer with the following two special properties:

+ +
    +
  • The printer can only print a sequence of the same character each time.
  • +
  • At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
  • +
+ +

Given a string s, return the minimum number of turns the printer needed to print it.

+ +

 

+

Example 1:

+ +
Input: s = "aaabbb"
+Output: 2
+Explanation: Print "aaa" first and then print "bbb".
+
+ +

Example 2:

+ +
Input: s = "aba"
+Output: 2
+Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From 71738803876faa4fe0e0da2e92a1b4dbd4c50314 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 03:15:27 +0530 Subject: [PATCH 1329/3167] Time: 16 ms (96.26%), Space: 6.1 MB (98.13%) - LeetHub --- 0664-strange-printer/0664-strange-printer.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0664-strange-printer/0664-strange-printer.cpp diff --git a/0664-strange-printer/0664-strange-printer.cpp b/0664-strange-printer/0664-strange-printer.cpp new file mode 100644 index 00000000..42000946 --- /dev/null +++ b/0664-strange-printer/0664-strange-printer.cpp @@ -0,0 +1,30 @@ + +class Solution +{ +private: + int f[100][100]; + +private: + int dfs(const std::string& s, int l, int r) + { + if (l > r) return 0; + if (f[l][r]) return f[l][r]; + f[l][r] = dfs(s, l, r - 1) + 1; + for (int i = l; i < r; ++i) + { + if (s[i] == s[r]) + { + f[l][r] = std::min(f[l][r], dfs(s, l, i) + dfs(s, i + 1, r - 1)); + } + } + return f[l][r]; + } + +public: + int strangePrinter(std::string s) + { + memset(f, 0, sizeof(f)); + int len = (int)s.size(); + return dfs(s, 0, len - 1); + } +}; \ No newline at end of file From fc5efffb369244ed14df0aaa7119dd51ae571464 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:14:39 +0530 Subject: [PATCH 1330/3167] Create README - LeetHub --- BFS of graph - GFG/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 BFS of graph - GFG/README.md diff --git a/BFS of graph - GFG/README.md b/BFS of graph - GFG/README.md new file mode 100644 index 00000000..aad23504 --- /dev/null +++ b/BFS of graph - GFG/README.md @@ -0,0 +1,26 @@ +# BFS of graph +## Easy +

Given a directed graph. The task is to do Breadth First Traversal of this graph starting from 0.
Note: One can move from node u to node v only if there's an edge from u to v. Find the BFS traversal of the graph starting from the 0th vertex,
from left to right according to the input graph. Also, you should only take nodes directly or indirectly connected from Node 0 in consideration.

+


Example 1:

+
Input:
+
+Output: 0 1 2 3 4
+Explanation: 
+0 is connected to 1 , 2 , 3.
+2 is connected to 4.
+so starting from 0, it will go to 1 then 2
+then 3. After this 2 to 4, thus bfs will be
+0 1 2 3 4.
+
+

Example 2:

+
Input:
+
+Output: 0 1 2
+Explanation:
+0 is connected to 1 , 2.
+so starting from 0, it will go to 1 then 2,
+thus bfs will be 0 1 2. 
+
+


Your task:
You dont need to read input or print anything. Your task is to complete the function bfsOfGraph() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns
 a list containing the BFS traversal of the graph starting from the 0th vertex from left to right.

+


Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)

+


Constraints:
1 ≤ V, E ≤ 104

\ No newline at end of file From a2768ab6d000789965fca6b3149e3894f2783853 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:34:22 +0530 Subject: [PATCH 1331/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0712-minimum-ascii-delete-sum-for-two-strings/README.md diff --git a/0712-minimum-ascii-delete-sum-for-two-strings/README.md b/0712-minimum-ascii-delete-sum-for-two-strings/README.md new file mode 100644 index 00000000..4bc11e8d --- /dev/null +++ b/0712-minimum-ascii-delete-sum-for-two-strings/README.md @@ -0,0 +1,31 @@ +

712. Minimum ASCII Delete Sum for Two Strings

Medium


Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal.

+ +

 

+

Example 1:

+ +
Input: s1 = "sea", s2 = "eat"
+Output: 231
+Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
+Deleting "t" from "eat" adds 116 to the sum.
+At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
+
+ +

Example 2:

+ +
Input: s1 = "delete", s2 = "leet"
+Output: 403
+Explanation: Deleting "dee" from "delete" to turn the string into "let",
+adds 100[d] + 101[e] + 101[e] to the sum.
+Deleting "e" from "leet" adds 101[e] to the sum.
+At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
+If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s1.length, s2.length <= 1000
  • +
  • s1 and s2 consist of lowercase English letters.
  • +
+
\ No newline at end of file From 284ef0de2ad6715ae369f1dec75008b061abb811 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:34:26 +0530 Subject: [PATCH 1332/3167] Time: 49 ms (63.58%), Space: 15.3 MB (41.16%) - LeetHub --- ...nimum-ascii-delete-sum-for-two-strings.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 0712-minimum-ascii-delete-sum-for-two-strings/0712-minimum-ascii-delete-sum-for-two-strings.cpp diff --git a/0712-minimum-ascii-delete-sum-for-two-strings/0712-minimum-ascii-delete-sum-for-two-strings.cpp b/0712-minimum-ascii-delete-sum-for-two-strings/0712-minimum-ascii-delete-sum-for-two-strings.cpp new file mode 100644 index 00000000..679cb5b9 --- /dev/null +++ b/0712-minimum-ascii-delete-sum-for-two-strings/0712-minimum-ascii-delete-sum-for-two-strings.cpp @@ -0,0 +1,53 @@ +class Solution { + +private: + int helper(int i, int j, int n, int m, string& s1, string& s2, vector>& dp) + { + if(i == n and j == m) + return 0; + if(i == n) + { + int deleteS2 = 0; + + for(int k = j; k < m; ++k) + { + deleteS2 += s2[k]; + } + + return deleteS2; + } + + if(j == m) + { + int deleteS1 = 0; + + for(int k = i; k < n; ++k) + { + deleteS1 += s1[k]; + } + + return deleteS1; + } + + if(dp[i][j] != -1) + return dp[i][j]; + + if(s1[i] == s2[j]) + return dp[i][j] = helper(i+1, j+1, n, m, s1, s2, dp); + else + { + return dp[i][j] = min(s1[i] + helper(i+1, j, n, m, s1, s2, dp), s2[j] + helper(i, j+1, n, m, s1, s2, dp)); + } + } + +public: + int minimumDeleteSum(string s1, string s2) { + + int n = s1.size(), m = s2.size(); + + vector> dp(n+1, vector(m+1, -1)); + + return helper(0, 0, n, m, s1, s2, dp); + + } +}; \ No newline at end of file From 84ed8a21c5013ab65d4962004cde6971a4eaacc1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:35:25 +0530 Subject: [PATCH 1333/3167] Attach NOTES - LeetHub --- 0712-minimum-ascii-delete-sum-for-two-strings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0712-minimum-ascii-delete-sum-for-two-strings/NOTES.md diff --git a/0712-minimum-ascii-delete-sum-for-two-strings/NOTES.md b/0712-minimum-ascii-delete-sum-for-two-strings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0712-minimum-ascii-delete-sum-for-two-strings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 62213406989b394eb4b9b56f7428f7564bdfa718 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:35:28 +0530 Subject: [PATCH 1334/3167] Time: 49 ms (63.58%), Space: 15.3 MB (41.16%) - LeetHub From 0e94226aed49270fef5bf90f507a5b60d08a87a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:33:14 +0530 Subject: [PATCH 1335/3167] Create README - LeetHub --- DFS of Graph - GFG/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 DFS of Graph - GFG/README.md diff --git a/DFS of Graph - GFG/README.md b/DFS of Graph - GFG/README.md new file mode 100644 index 00000000..5921428f --- /dev/null +++ b/DFS of Graph - GFG/README.md @@ -0,0 +1,33 @@ +# DFS of Graph +## Easy +

You are given a connected undirected graph. Perform a Depth First Traversal of the graph.
Note: Use a recursive approach to find the DFS traversal of the graph starting from the 0th vertex from left to right according to the graph.

+


Example 1:

+
Input: V = 5 , adj = [[2,3,1] , [0], [0,4], [0], [2]]
+
+Output: 0 2 4 3 1
+Explanation: 
+0 is connected to 2, 3, 1.
+1 is connected to 0.
+2 is connected to 0 and 4.
+3 is connected to 0.
+4 is connected to 2.
+so starting from 0, it will go to 2 then 4,
+and then 3 and 1.
+Thus dfs will be 0 2 4 3 1.
+
+

Example 2:

+
Input: V = 4, adj = [[1,3], [2,0], [1], [0]]
+
+Output: 0 1 2 3
+Explanation:
+0 is connected to 1 , 3.
+1 is connected to 0, 2. 
+2 is connected to 1.
+3 is connected to 0. 
+so starting from 0, it will go to 1 then 2
+then back to 0 then 0 to 3
+thus dfs will be 0 1 2 3. 
+
+


Your task:
You don't need to read input or print anything. Your task is to complete the function dfsOfGraph() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns a list containing the DFS traversal of the graph starting from the 0th vertex from left to right according to the graph.

+


Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)

+


Constraints:
1 ≤ V, E ≤ 104

\ No newline at end of file From 158826ea2cb60291f20d5d3925e6767dd61d0c33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:33:14 +0530 Subject: [PATCH 1336/3167] Added solution - LeetHub --- DFS of Graph - GFG/dfs-of-graph.java | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 DFS of Graph - GFG/dfs-of-graph.java diff --git a/DFS of Graph - GFG/dfs-of-graph.java b/DFS of Graph - GFG/dfs-of-graph.java new file mode 100644 index 00000000..38473f8f --- /dev/null +++ b/DFS of Graph - GFG/dfs-of-graph.java @@ -0,0 +1,67 @@ +//{ Driver Code Starts +// Initial Template for Java +import java.util.*; +import java.lang.*; +import java.io.*; +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = + new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine().trim()); + while (T-- > 0) { + String[] s = br.readLine().trim().split(" "); + int V = Integer.parseInt(s[0]); + int E = Integer.parseInt(s[1]); + ArrayList> adj = + new ArrayList>(); + for (int i = 0; i < V; i++) adj.add(new ArrayList()); + for (int i = 0; i < E; i++) { + String[] S = br.readLine().trim().split(" "); + int u = Integer.parseInt(S[0]); + int v = Integer.parseInt(S[1]); + adj.get(u).add(v); + adj.get(v).add(u); + } + Solution obj = new Solution(); + ArrayList ans = obj.dfsOfGraph(V, adj); + for (int i = 0; i < ans.size(); i++) + System.out.print(ans.get(i) + " "); + System.out.println(); + } + } +} + +// } Driver Code Ends + + +class Solution { + // Function to return a list containing the DFS traversal of the graph. + + void dfs(int sv, ArrayList> adj, boolean visited[], ArrayList arr) + { + arr.add(sv); + + visited[sv] = true; + + for(int i = 0; i < adj.get(sv).size(); ++i) + { + int val = adj.get(sv).get(i); + if(!visited[val]) + { + dfs(val, adj, visited, arr); + } + } + } + + public ArrayList dfsOfGraph(int V, ArrayList> adj) { + // Code here + + ArrayList arr = new ArrayList(); + + boolean[] visited = new boolean[V+1]; + + dfs(0, adj, visited, arr); + + return arr; + } +} \ No newline at end of file From 6a7c013c6d204691404695d39a40d4e7481368c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:06:17 +0530 Subject: [PATCH 1337/3167] Attach NOTES - LeetHub --- 0077-combinations/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0077-combinations/NOTES.md diff --git a/0077-combinations/NOTES.md b/0077-combinations/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0077-combinations/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 51bec79a678ca46d4f6ec4c2192300c8d98f8897 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:06:20 +0530 Subject: [PATCH 1338/3167] Time: 374 ms (17.89%), Space: 164.1 MB (10.97%) - LeetHub --- 0077-combinations/0077-combinations.cpp | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0077-combinations/0077-combinations.cpp diff --git a/0077-combinations/0077-combinations.cpp b/0077-combinations/0077-combinations.cpp new file mode 100644 index 00000000..0ae513c5 --- /dev/null +++ b/0077-combinations/0077-combinations.cpp @@ -0,0 +1,32 @@ +class Solution { + +private: + void helper(int idx, int n, int k, vector ds, vector>& ans) + { + if(ds.size() == k) + { + ans.push_back(ds); + return; + } + + for(int i = idx; i <= n; ++i) + { + ds.push_back(i); + helper(i+1, n, k, ds, ans); + ds.pop_back(); + } + } + +public: + vector> combine(int n, int k) { + + vector ds; + + vector> ans; + + helper(1, n, k, ds, ans); + + return ans; + + } +}; \ No newline at end of file From 8b5a2eda60614e45010b115c73190c718e318df8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:33:48 +0530 Subject: [PATCH 1339/3167] Create README - LeetHub --- 0139-word-break/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0139-word-break/README.md diff --git a/0139-word-break/README.md b/0139-word-break/README.md new file mode 100644 index 00000000..2b78319b --- /dev/null +++ b/0139-word-break/README.md @@ -0,0 +1,37 @@ +

139. Word Break

Medium


Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

+ +

Note that the same word in the dictionary may be reused multiple times in the segmentation.

+ +

 

+

Example 1:

+ +
Input: s = "leetcode", wordDict = ["leet","code"]
+Output: true
+Explanation: Return true because "leetcode" can be segmented as "leet code".
+
+ +

Example 2:

+ +
Input: s = "applepenapple", wordDict = ["apple","pen"]
+Output: true
+Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
+Note that you are allowed to reuse a dictionary word.
+
+ +

Example 3:

+ +
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 300
  • +
  • 1 <= wordDict.length <= 1000
  • +
  • 1 <= wordDict[i].length <= 20
  • +
  • s and wordDict[i] consist of only lowercase English letters.
  • +
  • All the strings of wordDict are unique.
  • +
+
\ No newline at end of file From 2b4c493b5b24b6b5976e220d5878118a2d2feab6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:33:52 +0530 Subject: [PATCH 1340/3167] Time: 3 ms (91.68%), Space: 8.1 MB (77.31%) - LeetHub --- 0139-word-break/0139-word-break.cpp | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0139-word-break/0139-word-break.cpp diff --git a/0139-word-break/0139-word-break.cpp b/0139-word-break/0139-word-break.cpp new file mode 100644 index 00000000..920d5126 --- /dev/null +++ b/0139-word-break/0139-word-break.cpp @@ -0,0 +1,43 @@ +class Solution { + +private: + bool helper(int idx, int n, string& str, unordered_set& st, vector& dp) + { + if(idx == n) + return true; + + if(dp[idx] != -1) + return dp[idx]; + + string temp = ""; + + for(int i = idx; i < n; ++i) + { + temp += str[i]; + + if(st.find(temp) != st.end()) + { + if(helper(i+1, n, str, st, dp)) + return dp[idx] = true; + } + } + + return dp[idx] = false; + } + +public: + bool wordBreak(string s, vector& wordDict) { + + int n = s.size(); + + vector dp(n+1 , -1); + + unordered_set st; + + for(auto itr : wordDict) + st.insert(itr); + + return helper(0, n, s, st, dp); + + } +}; \ No newline at end of file From 9f24ec23b906ff3bb35cad6e98397c639622379e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:55:55 +0530 Subject: [PATCH 1341/3167] Attach NOTES - LeetHub --- 0139-word-break/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0139-word-break/NOTES.md diff --git a/0139-word-break/NOTES.md b/0139-word-break/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0139-word-break/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6e46708499d546723540b2dd1b4787691cf5c394 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:56:34 +0530 Subject: [PATCH 1342/3167] Attach NOTES - LeetHub From 2dd04a5c848d8ad0ed8372b3f13bb1362fc27a95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:56:37 +0530 Subject: [PATCH 1343/3167] Time: 7 ms (74.20%), Space: 8.1 MB (77.31%) - LeetHub --- 0139-word-break/0139-word-break.cpp | 52 ++++++++++++----------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/0139-word-break/0139-word-break.cpp b/0139-word-break/0139-word-break.cpp index 920d5126..d0663251 100644 --- a/0139-word-break/0139-word-break.cpp +++ b/0139-word-break/0139-word-break.cpp @@ -1,43 +1,35 @@ class Solution { - -private: - bool helper(int idx, int n, string& str, unordered_set& st, vector& dp) - { - if(idx == n) - return true; - - if(dp[idx] != -1) - return dp[idx]; - - string temp = ""; - - for(int i = idx; i < n; ++i) - { - temp += str[i]; - - if(st.find(temp) != st.end()) - { - if(helper(i+1, n, str, st, dp)) - return dp[idx] = true; - } - } - - return dp[idx] = false; - } - public: bool wordBreak(string s, vector& wordDict) { int n = s.size(); - vector dp(n+1 , -1); + int mod = 1e9 + 7; + + vector dp(n+1, 0); unordered_set st; - for(auto itr : wordDict) + for(auto& itr : wordDict) st.insert(itr); - return helper(0, n, s, st, dp); - + for(int i = 0; i < n; ++i) + { + string temp; + for(int j = i; j < n; ++j) + { + temp += s[j]; + + if(st.find(temp) != st.end()) + { + if(i > 0) + dp[j] = ((dp[j] % mod + dp[i-1]) % mod) % mod; + else + dp[j] = 1; + } + } + } + + return dp[n-1]; } }; \ No newline at end of file From 94a40ea1d1ac0deb23eb95b6bb49e2cd6dd71ec5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 01:14:56 +0530 Subject: [PATCH 1344/3167] Attach NOTES - LeetHub From 1dede66ed03d1f38417d4d1461827d910c4dcb43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 01:14:59 +0530 Subject: [PATCH 1345/3167] Time: 8 ms (68.91%), Space: 8.2 MB (77.15%) - LeetHub --- 0139-word-break/0139-word-break.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/0139-word-break/0139-word-break.cpp b/0139-word-break/0139-word-break.cpp index d0663251..8ab459af 100644 --- a/0139-word-break/0139-word-break.cpp +++ b/0139-word-break/0139-word-break.cpp @@ -23,7 +23,10 @@ class Solution { if(st.find(temp) != st.end()) { if(i > 0) - dp[j] = ((dp[j] % mod + dp[i-1]) % mod) % mod; + { + if(!dp[j]) + dp[j] = dp[i-1]; + } else dp[j] = 1; } From 4c6c01ba56f57296cb7b2fb20fa434b86120433e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:01:08 +0530 Subject: [PATCH 1346/3167] Create README - LeetHub --- 0046-permutations/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0046-permutations/README.md diff --git a/0046-permutations/README.md b/0046-permutations/README.md new file mode 100644 index 00000000..25218c6e --- /dev/null +++ b/0046-permutations/README.md @@ -0,0 +1,22 @@ +

46. Permutations

Medium


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 6
  • +
  • -10 <= nums[i] <= 10
  • +
  • All the integers of nums are unique.
  • +
+
\ No newline at end of file From 9a6565e56b62aea72aaa750d4de30de475585e92 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:01:08 +0530 Subject: [PATCH 1347/3167] Attach NOTES - LeetHub --- 0046-permutations/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0046-permutations/NOTES.md diff --git a/0046-permutations/NOTES.md b/0046-permutations/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0046-permutations/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4cef49720ff21df0bf3f2e69dabb09d9fb87e928 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:01:12 +0530 Subject: [PATCH 1348/3167] Time: 5 ms (33.26%), Space: 7.6 MB (60.28%) - LeetHub --- 0046-permutations/0046-permutations.cpp | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0046-permutations/0046-permutations.cpp diff --git a/0046-permutations/0046-permutations.cpp b/0046-permutations/0046-permutations.cpp new file mode 100644 index 00000000..9e8d9636 --- /dev/null +++ b/0046-permutations/0046-permutations.cpp @@ -0,0 +1,37 @@ +class Solution { + +private: + void helper(int idx, vector& ds, vector& freq, vector& nums, vector>& ans) + { + if(ds.size() == nums.size()) + { + ans.push_back(ds); + return; + } + + for(int i = 0; i < nums.size(); ++i) + { + if(!freq[i]) + { + ds.push_back(nums[i]); + freq[i] = 1; + helper(i+1, ds, freq, nums, ans); + freq[i] = 0; + ds.pop_back(); + } + } + } + +public: + vector> permute(vector& nums) { + + vector> ans; + + vector freq(nums.size(),0), ds; + + helper(0, ds, freq, nums, ans); + + return ans; + + } +}; \ No newline at end of file From 0a5828431ff9848f3f74903fb8670fb7f934f7bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:25:18 +0530 Subject: [PATCH 1349/3167] Attach NOTES - LeetHub From 0f561df8bcfb817d5a69a9c6d9c768d3d3826b4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:25:21 +0530 Subject: [PATCH 1350/3167] Time: 0 ms (100.00%), Space: 7.8 MB (46.70%) - LeetHub --- 0046-permutations/0046-permutations.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/0046-permutations/0046-permutations.cpp b/0046-permutations/0046-permutations.cpp index 9e8d9636..f051254a 100644 --- a/0046-permutations/0046-permutations.cpp +++ b/0046-permutations/0046-permutations.cpp @@ -1,7 +1,7 @@ class Solution { private: - void helper(int idx, vector& ds, vector& freq, vector& nums, vector>& ans) + void helper(vector& ds, vector& freq, vector& nums, vector>& ans) { if(ds.size() == nums.size()) { @@ -15,7 +15,7 @@ class Solution { { ds.push_back(nums[i]); freq[i] = 1; - helper(i+1, ds, freq, nums, ans); + helper(ds, freq, nums, ans); freq[i] = 0; ds.pop_back(); } @@ -29,7 +29,7 @@ class Solution { vector freq(nums.size(),0), ds; - helper(0, ds, freq, nums, ans); + helper(ds, freq, nums, ans); return ans; From 5bcaf749af908a4689d1db48b8ef794466f4a433 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:28:15 +0530 Subject: [PATCH 1351/3167] Attach NOTES - LeetHub From 78fc5f6be10517f94e50e2f9c3e55c77de4c0f12 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:28:18 +0530 Subject: [PATCH 1352/3167] Time: 2 ms (68.70%), Space: 7.5 MB (82.67%) - LeetHub --- 0046-permutations/0046-permutations.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/0046-permutations/0046-permutations.cpp b/0046-permutations/0046-permutations.cpp index f051254a..2fd910bd 100644 --- a/0046-permutations/0046-permutations.cpp +++ b/0046-permutations/0046-permutations.cpp @@ -1,24 +1,19 @@ class Solution { private: - void helper(vector& ds, vector& freq, vector& nums, vector>& ans) + void helper(int idx, vector& nums, vector>& ans) { - if(ds.size() == nums.size()) + if(idx == nums.size()) { - ans.push_back(ds); + ans.push_back(nums); return; } - for(int i = 0; i < nums.size(); ++i) + for(int i = idx; i < nums.size(); ++i) { - if(!freq[i]) - { - ds.push_back(nums[i]); - freq[i] = 1; - helper(ds, freq, nums, ans); - freq[i] = 0; - ds.pop_back(); - } + swap(nums[idx], nums[i]); + helper(idx+1, nums, ans); + swap(nums[idx], nums[i]); } } @@ -27,9 +22,7 @@ class Solution { vector> ans; - vector freq(nums.size(),0), ds; - - helper(ds, freq, nums, ans); + helper(0, nums, ans); return ans; From 1b67455030e66c2eed13c295b9047307ab739040 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:34:55 +0530 Subject: [PATCH 1353/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Shortest Source to Destination Path - GFG/README.md diff --git a/Shortest Source to Destination Path - GFG/README.md b/Shortest Source to Destination Path - GFG/README.md new file mode 100644 index 00000000..c003d229 --- /dev/null +++ b/Shortest Source to Destination Path - GFG/README.md @@ -0,0 +1,28 @@ +# Shortest Source to Destination Path +## Medium +

Given a 2D binary matrix A(0-based index) of dimensions NxM. Find the minimum number of steps required to reach from (0,0) to (X, Y).
Note: You can only move left, right, up and down, and only through cells that contain 1.

+

Example 1:

+
Input:
+N=3, M=4
+A=[[1,0,0,0], 
+   [1,1,0,1],
[0,1,1,1]] +X=2, Y=3 +Output: +5 +Explanation: +The shortest path is as follows: +(0,0)->(1,0)->(1,1)->(2,1)->(2,2)->(2,3).
+

Example 2:

+
Input:
+N=3, M=4
+A=[[1,1,1,1],
+   [0,0,0,1],
[0,0,0,1]] +X=0, Y=3 +Output: +3 +Explanation: +The shortest path is as follows: +(0,0)->(0,1)->(0,2)->(0,3).
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function shortestDistance() which takes the integer N, M, X, Y, and the 2D binary matrix A as input parameters and returns the minimum number of steps required to go from (0,0) to (X, Y).If it is impossible to go from (0,0) to (X, Y),then function returns -1. If value of the cell (0,0) is 0 (i.e  A[0][0]=0) then return -1.

+

Expected Time Complexity:O(N*M)
Expected Auxillary Space:O(N*M)

+

Constraints:
1 <= N,M <= 250
0 <= X < N
0 <= Y < M
0 <= A[i][j] <= 1

\ No newline at end of file From 43538f98f3ad58c768ba67739f14bdbcef219266 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 08:34:56 +0530 Subject: [PATCH 1354/3167] Added solution - LeetHub --- .../shortest-source-to-destination-path.cpp | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.cpp diff --git a/Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.cpp b/Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.cpp new file mode 100644 index 00000000..fc61f512 --- /dev/null +++ b/Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.cpp @@ -0,0 +1,73 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + int shortestDistance(int N, int M, vector> A, int X, int Y) { + // code here + + vector> visited(N, vector(M,false)); + + queue> > q; + + + vector dx = {-1, 0, 0, 1}; + vector dy = {0, -1, +1, 0}; + + q.push({0,{0, 0}}); + + visited[0][0] = true; + + while(!q.empty()) + { + auto curr = q.front(); + q.pop(); + + int step = curr.first; + int x = curr.second.first; + int y = curr.second.second; + + if(x == X and y == Y) + { + return step; + } + + for(int i = 0; i < 4; ++i) + { + int newx = x + dx[i]; + int newy = y + dy[i]; + + if(newx >= 0 and newy >= 0 and newx < N and newy < M and !visited[newx][newy] and A[newx][newy] == 1) + { + visited[newx][newy] = true; + q.push({step+1,{newx,newy}}); + } + } + } + + return -1; + } +}; + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int N, M, x, y; + cin >> N >> M; + vector> v(N, vector(M)); + for (int i = 0; i < N; i++) + for (int j = 0; j < M; j++) cin >> v[i][j]; + cin >> x >> y; + Solution ob; + cout << ob.shortestDistance(N, M, v, x, y) << "\n"; + } +} +// } Driver Code Ends \ No newline at end of file From 8c93eba2b9b233680c33c7a061f0e40bd8cb1c08 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 09:47:58 +0530 Subject: [PATCH 1355/3167] Create README - LeetHub --- Shortest Source to Destination Path - GFG/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Shortest Source to Destination Path - GFG/README.md b/Shortest Source to Destination Path - GFG/README.md index c003d229..4aef80d6 100644 --- a/Shortest Source to Destination Path - GFG/README.md +++ b/Shortest Source to Destination Path - GFG/README.md @@ -2,7 +2,7 @@ ## Medium

Given a 2D binary matrix A(0-based index) of dimensions NxM. Find the minimum number of steps required to reach from (0,0) to (X, Y).
Note: You can only move left, right, up and down, and only through cells that contain 1.

Example 1:

-
Input:
+
Input:
 N=3, M=4
 A=[[1,0,0,0], 
    [1,1,0,1],
[0,1,1,1]] @@ -11,9 +11,9 @@ X=2, Y=3 5 Explanation: The shortest path is as follows: -(0,0)->(1,0)->(1,1)->(2,1)->(2,2)->(2,3).
+(0,0)->(1,0)->(1,1)->(2,1)->(2,2)->(2,3).

Example 2:

-
Input:
+
Input:
 N=3, M=4
 A=[[1,1,1,1],
    [0,0,0,1],
[0,0,0,1]] @@ -22,7 +22,7 @@ X=0, Y=3 3 Explanation: The shortest path is as follows: -(0,0)->(0,1)->(0,2)->(0,3).
+(0,0)->(0,1)->(0,2)->(0,3).

Your Task:
You don't need to read input or print anything. Your task is to complete the function shortestDistance() which takes the integer N, M, X, Y, and the 2D binary matrix A as input parameters and returns the minimum number of steps required to go from (0,0) to (X, Y).If it is impossible to go from (0,0) to (X, Y),then function returns -1. If value of the cell (0,0) is 0 (i.e  A[0][0]=0) then return -1.

Expected Time Complexity:O(N*M)
Expected Auxillary Space:O(N*M)

Constraints:
1 <= N,M <= 250
0 <= X < N
0 <= Y < M
0 <= A[i][j] <= 1

\ No newline at end of file From ef31ed1c1b7e93b2b774039eb2ab3cabeec23a2e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 2 Aug 2023 09:47:59 +0530 Subject: [PATCH 1356/3167] Added solution - LeetHub --- .../shortest-source-to-destination-path.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.java diff --git a/Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.java b/Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.java new file mode 100644 index 00000000..097c1f65 --- /dev/null +++ b/Shortest Source to Destination Path - GFG/shortest-source-to-destination-path.java @@ -0,0 +1,78 @@ +//{ Driver Code Starts +// Initial Template for Java + +import java.io.*; +import java.util.*; +class GFG { + public static void main(String args[]) throws IOException { + BufferedReader read = + new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(read.readLine()); + while (t-- > 0) { + int N, M, x, y; + String S[] = read.readLine().split(" "); + N = Integer.parseInt(S[0]); + M = Integer.parseInt(S[1]); + int a[][] = new int[N][M]; + for (int i = 0; i < N; i++) { + String s[] = read.readLine().split(" "); + for (int j = 0; j < M; j++) a[i][j] = Integer.parseInt(s[j]); + } + String s1[] = read.readLine().split(" "); + x = Integer.parseInt(s1[0]); + y = Integer.parseInt(s1[1]); + Solution ob = new Solution(); + System.out.println(ob.shortestDistance(N, M, a, x, y)); + } + } +} +// } Driver Code Ends + + +// User function Template for Java + +class Solution { + int shortestDistance(int N, int M, int A[][], int X, int Y) { + // code here + + boolean[][] visited = new boolean[N][M]; + + Queue q = new LinkedList<>(); + + q.add(new int[]{0, 0, 0}); + + visited[0][0] = true; + + int[] dx = {-1, 0, 0, +1}; + int[] dy = {0, -1, +1, 0}; + + while(!q.isEmpty()) + { + int curr[] = q.poll(); + + int step = curr[0]; + int x = curr[1]; + int y = curr[2]; + + // System.out.println(x + " " + step + " " + y); + + if(X == x && Y == y) + return step; + + for(int i = 0; i < 4; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(newx >= 0 && newy >= 0 && newx < N && newy < M && !visited[newx][newy] && A[newx][newy] == 1) + { + // System.out.println(newx + " " + newy); + visited[newx][newy] = true; + q.add(new int[]{step+1, newx, newy}); + } + } + + } + return -1; + } +}; \ No newline at end of file From 7103baea64201cacd664df17f4b4d90ad03778ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 20:48:06 +0530 Subject: [PATCH 1357/3167] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Shortest path in Directed Acyclic Graph - GFG/README.md diff --git a/Shortest path in Directed Acyclic Graph - GFG/README.md b/Shortest path in Directed Acyclic Graph - GFG/README.md new file mode 100644 index 00000000..bff5298e --- /dev/null +++ b/Shortest path in Directed Acyclic Graph - GFG/README.md @@ -0,0 +1,21 @@ +# Shortest path in Directed Acyclic Graph +## Medium +

Given a Directed Acyclic Graph of N vertices from 0 to N-1 and a 2D Integer array(or vector) edges[ ][ ] of length M, where there is a directed edge from edge[i][0] to edge[i][1] with a distance of edge[i][2] for all i.

+

Find the shortest path from src(0) vertex to all the vertices and if it is impossible to reach any vertex, then return -1 for that vertex.

+

Example1:

+
Input:
+N = 4, M = 2
+edge = [[0,1,2],[0,2,1]
+Output:
+0 2 1 -1
Explanation:
Shortest path from 0 to 1 is 0->1 with edge weight 2. 
Shortest path from 0 to 2 is 0->2 with edge weight 1.
There is no way we can reach 3, so it's -1 for 3.
+

Example2:

+
Input:
+N = 6, M = 7
+edge = [[0,1,2],[0,4,1],[4,5,4],[4,2,2],[1,2,3],[2,3,6],[5,3,1]]
+Output:
+0 2 3 6 1 5
Explanation:
Shortest path from 0 to 1 is 0->1 with edge weight 2.
Shortest path from 0 to 2 is 0->4->2 with edge weight 1+2=3.
Shortest path from 0 to 3 is 0->4->5->3 with edge weight 1+4+1=6.
Shortest path from 0 to 4 is 0->4 with edge weight 1.
Shortest path from 0 to 5 is 0->4->5 with edge weight 1+4=5.
+

Your Task:

+

You don't need to print or input anything. Complete the function shortest path() which takes an integer N as number of vertices, an integer M as number of edges and a 2D Integer array(or vector) edges as the input parameters and returns an integer array(or vector), denoting the list of distance from src to all nodes.

+

Expected Time Complexity: O(N+M), where N is the number of nodes and M is edges
Expected Space Complexity: O(N)

+

Constraint:
1 <= N,M <= 100
0 <= edgei,0,edgei,1 < n
0 <= 
edgei,2 <=105

+

 

\ No newline at end of file From cb39ab64abca1a6bc09c0caa1711e5dab423e3f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 20:48:06 +0530 Subject: [PATCH 1358/3167] Added solution - LeetHub --- ...hortest-path-in-directed-acyclic-graph.cpp | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Shortest path in Directed Acyclic Graph - GFG/shortest-path-in-directed-acyclic-graph.cpp diff --git a/Shortest path in Directed Acyclic Graph - GFG/shortest-path-in-directed-acyclic-graph.cpp b/Shortest path in Directed Acyclic Graph - GFG/shortest-path-in-directed-acyclic-graph.cpp new file mode 100644 index 00000000..6564fa7f --- /dev/null +++ b/Shortest path in Directed Acyclic Graph - GFG/shortest-path-in-directed-acyclic-graph.cpp @@ -0,0 +1,92 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ +class Solution { + public: + vector shortestPath(int N,int M, vector>& edges){ + // code here + + vector> adj[N+1]; + + for(auto& itr : edges) + { + int u = itr[0]; + int v = itr[1]; + int dist = itr[2]; + + adj[u].push_back({v, dist}); + } + + priority_queue, vector>, greater> > pq; + + vector distance(N, 1e9); + + pq.push({0, 0}); + + distance[0] = 0; + + while(!pq.empty()) + { + auto curr = pq.top(); + + pq.pop(); + + int node = curr.second; + int currDist = curr.first; + + for(auto itr : adj[node]) + { + int childNode = itr.first; + int childDist = itr.second; + + if(currDist + childDist < distance[childNode]) + { + distance[childNode] = currDist + childDist; + pq.push({distance[childNode], childNode}); + } + } + } + + for(int i = 0; i < N; ++i) + { + if(distance[i] == 1e9) + distance[i] = -1; + } + + return distance; + + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + vector> edges; + for(int i=0; i temp; + for(int j=0; j<3; ++j){ + int x; cin>>x; + temp.push_back(x); + } + edges.push_back(temp); + } + Solution obj; + vector res = obj.shortestPath(n, m, edges); + for (auto x : res) { + cout << x << " "; + } + cout << "\n"; + } +} + +// } Driver Code Ends \ No newline at end of file From 8f00c2e3a449f7fc35e42f51a24f44c7b8cedea8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 22:33:51 +0530 Subject: [PATCH 1359/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0017-letter-combinations-of-a-phone-number/README.md diff --git a/0017-letter-combinations-of-a-phone-number/README.md b/0017-letter-combinations-of-a-phone-number/README.md new file mode 100644 index 00000000..81e0b1fb --- /dev/null +++ b/0017-letter-combinations-of-a-phone-number/README.md @@ -0,0 +1,31 @@ +

17. Letter Combinations of a Phone Number

Medium


Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

+ +

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

+ +

 

+

Example 1:

+ +
Input: digits = "23"
+Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
+
+ +

Example 2:

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

Example 3:

+ +
Input: digits = "2"
+Output: ["a","b","c"]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= digits.length <= 4
  • +
  • digits[i] is a digit in the range ['2', '9'].
  • +
+
\ No newline at end of file From ea619cef967e2805e18fec7ee3791de54bfb4567 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 22:33:52 +0530 Subject: [PATCH 1360/3167] Attach NOTES - LeetHub --- 0017-letter-combinations-of-a-phone-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0017-letter-combinations-of-a-phone-number/NOTES.md diff --git a/0017-letter-combinations-of-a-phone-number/NOTES.md b/0017-letter-combinations-of-a-phone-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0017-letter-combinations-of-a-phone-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f029f80a6ef9a84397aa1e696224affe70952450 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 22:33:55 +0530 Subject: [PATCH 1361/3167] Time: 0 ms (100.00%), Space: 6.7 MB (18.80%) - LeetHub --- ...-letter-combinations-of-a-phone-number.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0017-letter-combinations-of-a-phone-number/0017-letter-combinations-of-a-phone-number.cpp diff --git a/0017-letter-combinations-of-a-phone-number/0017-letter-combinations-of-a-phone-number.cpp b/0017-letter-combinations-of-a-phone-number/0017-letter-combinations-of-a-phone-number.cpp new file mode 100644 index 00000000..c5e92c43 --- /dev/null +++ b/0017-letter-combinations-of-a-phone-number/0017-letter-combinations-of-a-phone-number.cpp @@ -0,0 +1,47 @@ +class Solution { + +private: + void helper(int idx ,string ds, string& digits, map>& mp, vector& ans) + { + if(idx == digits.size()) + { + if(!ds.empty()) + ans.push_back(ds); + return; + } + + + vector curr = mp[digits[idx]]; + + for(int j = 0; j < curr.size(); ++j) + { + ds.push_back(curr[j]); + helper(idx+1, ds, digits, mp, ans); + ds.pop_back(); + } + + + } + +public: + vector letterCombinations(string digits) { + + map> mp; + + mp['2'] = {'a','b','c'}; + mp['3'] = {'d','e','f'}; + mp['4'] = {'g','h','i'}; + mp['5'] = {'j','k','l'}; + mp['6'] = {'m','n','o'}; + mp['7'] = {'p','q','r','s'}; + mp['8'] = {'t','u','v'}; + mp['9'] = {'w','x','y','z'}; + + vector ans; + + helper(0, "",digits, mp, ans); + + return ans; + + } +}; \ No newline at end of file From 5bc30f6e22d020a04c91cc24724264e7127d757f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 22:41:30 +0530 Subject: [PATCH 1362/3167] Attach NOTES - LeetHub From e154afdcc277b99cfd253537b8ff7fd203bb1d4e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 3 Aug 2023 22:41:34 +0530 Subject: [PATCH 1363/3167] Time: 0 ms (100.00%), Space: 6.7 MB (18.80%) - LeetHub From b1ecb09d7d2ebad79eec0033d37efd562b0e2958 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 4 Aug 2023 23:32:49 +0530 Subject: [PATCH 1364/3167] Create README - LeetHub --- Reverse a Stack - GFG/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Reverse a Stack - GFG/README.md diff --git a/Reverse a Stack - GFG/README.md b/Reverse a Stack - GFG/README.md new file mode 100644 index 00000000..1b0419ff --- /dev/null +++ b/Reverse a Stack - GFG/README.md @@ -0,0 +1,18 @@ +# Reverse a Stack +## Medium +

You are given a stack St. You have to reverse the stack using recursion.

+

Example 1:

+
Input:
+St = {3,2,1,7,6}
+Output:
+{6,7,1,2,3}
Explanation:
Input stack after reversing will look like the stack in the output.
+

Example 2:

+
Input:
+St = {4,3,9,6}
+Output:
+{6,9,3,4}
Explanation:
Input stack after reversing will look like the stack in the output.
+
+

Your Task:

+

You don't need to read input or print anything. Your task is to complete the function Reverse() which takes the stack St as input and reverses the given stack.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

+

Constraints:
1 <= size of the stack <= 104

-109 <= Each element of the stack <= 109
Sum of N over all test cases doesn't exceeds 106
Array may contain duplicate elements. 

\ No newline at end of file From 0eda943de8f516c2ff10fa2ff4296c66b15894fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 4 Aug 2023 23:32:50 +0530 Subject: [PATCH 1365/3167] Added solution - LeetHub --- Reverse a Stack - GFG/reverse-a-stack.cpp | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Reverse a Stack - GFG/reverse-a-stack.cpp diff --git a/Reverse a Stack - GFG/reverse-a-stack.cpp b/Reverse a Stack - GFG/reverse-a-stack.cpp new file mode 100644 index 00000000..40daaa86 --- /dev/null +++ b/Reverse a Stack - GFG/reverse-a-stack.cpp @@ -0,0 +1,72 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +//User function Template for C++ + +class Solution{ + +private: + void insert(stack& St, int ele) + { + if(St.empty()) + { + St.push(ele); + return; + } + + int cur = St.top(); + St.pop(); + insert(St, ele); + St.push(cur); + } + +public: + void Reverse(stack &St){ + + if(St.empty()) + return; + + int ele = St.top(); + St.pop(); + Reverse(St); + insert(St, ele); + } +}; + +//{ Driver Code Starts. + + +int main(){ + int T; + cin>>T; + while(T--){ + int N; + cin>>N; + stack St; + for(int i=0;i>x; + St.push(x); + } + Solution ob; + ob.Reverse(St); + vectorres; + while(St.size()) + { + res.push_back(St.top()); + St.pop(); + } + for(int i = res.size()-1;i>=0;i--) + { + cout< Date: Fri, 4 Aug 2023 23:40:52 +0530 Subject: [PATCH 1366/3167] Attach NOTES - LeetHub From c114daf5b290942b3cad6071a21ffe084458acc9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 4 Aug 2023 23:40:55 +0530 Subject: [PATCH 1367/3167] Time: 7 ms (73.42%), Space: 8.6 MB (72.71%) - LeetHub --- 0139-word-break/0139-word-break.cpp | 55 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/0139-word-break/0139-word-break.cpp b/0139-word-break/0139-word-break.cpp index 8ab459af..1aa51f03 100644 --- a/0139-word-break/0139-word-break.cpp +++ b/0139-word-break/0139-word-break.cpp @@ -1,38 +1,37 @@ class Solution { + +private: + bool helper(int idx, int n, string& s, unordered_set& used, vector& dp) + { + if(idx == n) + return true; + + if(dp[idx] != -1) + return dp[idx]; + + string temp; + for(int i = idx; i < n; ++i) + { + temp += s[i]; + + if(used.find(temp) != used.end()) + { + if(helper(i+1, n, s, used, dp)) + return dp[idx] = true; + } + } + return dp[idx] = false; + } public: bool wordBreak(string s, vector& wordDict) { - int n = s.size(); - - int mod = 1e9 + 7; + unordered_set used(wordDict.begin(), wordDict.end()); - vector dp(n+1, 0); + int n = s.size(); - unordered_set st; + vector dp(n+1, -1); - for(auto& itr : wordDict) - st.insert(itr); + return helper(0, n, s, used, dp); - for(int i = 0; i < n; ++i) - { - string temp; - for(int j = i; j < n; ++j) - { - temp += s[j]; - - if(st.find(temp) != st.end()) - { - if(i > 0) - { - if(!dp[j]) - dp[j] = dp[i-1]; - } - else - dp[j] = 1; - } - } - } - - return dp[n-1]; } }; \ No newline at end of file From 495a9203e7cd75cd4832505fa0baf8748bdc6e9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 4 Aug 2023 23:49:47 +0530 Subject: [PATCH 1368/3167] Attach NOTES - LeetHub From 8ba49f96b6061e160ea9382a192e6720b44c9094 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 4 Aug 2023 23:49:50 +0530 Subject: [PATCH 1369/3167] Time: 4 ms (84.76%), Space: 8.2 MB (74.89%) - LeetHub --- 0139-word-break/0139-word-break.cpp | 48 +++++++++++++---------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/0139-word-break/0139-word-break.cpp b/0139-word-break/0139-word-break.cpp index 1aa51f03..acc5ffee 100644 --- a/0139-word-break/0139-word-break.cpp +++ b/0139-word-break/0139-word-break.cpp @@ -1,27 +1,4 @@ class Solution { - -private: - bool helper(int idx, int n, string& s, unordered_set& used, vector& dp) - { - if(idx == n) - return true; - - if(dp[idx] != -1) - return dp[idx]; - - string temp; - for(int i = idx; i < n; ++i) - { - temp += s[i]; - - if(used.find(temp) != used.end()) - { - if(helper(i+1, n, s, used, dp)) - return dp[idx] = true; - } - } - return dp[idx] = false; - } public: bool wordBreak(string s, vector& wordDict) { @@ -29,9 +6,28 @@ class Solution { int n = s.size(); - vector dp(n+1, -1); - - return helper(0, n, s, used, dp); + vector dp(n+1, 0); + for(int i = 0; i < n; ++i) + { + string temp; + for(int j = i; j < n; ++j) + { + temp += s[j]; + + if(used.find(temp) != used.end()) + { + if(i > 0) + { + if(dp[i-1]) + dp[j] = 1; + } + else + dp[j] = 1; + } + } + } + + return dp[n-1]; } }; \ No newline at end of file From 2f799b9b142d2e8e608c07dd771fd07679af12ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 08:08:09 +0530 Subject: [PATCH 1370/3167] Create README - LeetHub --- .../README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Chocolate Distribution Problem - GFG/README.md diff --git a/Chocolate Distribution Problem - GFG/README.md b/Chocolate Distribution Problem - GFG/README.md new file mode 100644 index 00000000..9682977c --- /dev/null +++ b/Chocolate Distribution Problem - GFG/README.md @@ -0,0 +1,19 @@ +# Chocolate Distribution Problem +## Easy +

Given an array A[ ] of positive integers of size N, where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are M students, the task is to distribute chocolate packets among M students such that :
1. Each student gets exactly one packet.
2. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum.

+

Example 1:

+
Input:
+N = 8, M = 5
+A = {3, 4, 1, 9, 56, 7, 9, 12}
+Output: 6
+Explanation: The minimum difference between maximum chocolates and minimum chocolates is 9 - 3 = 6 by choosing following M packets :{3, 4, 9, 7, 9}.
+
+

Example 2:

+
Input:
+N = 7, M = 3
+A = {7, 3, 2, 4, 9, 12, 56}
+Output: 2
+Explanation: The minimum difference between maximum chocolates and minimum chocolates is 4 - 2 = 2 by choosing following M packets :{3, 2, 4}.
+

Your Task:
You don't need to take any input or print anything. Your task is to complete the function findMinDiff() which takes array A[ ], N and M as input parameters and returns the minimum possible difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student.

+

Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(1)

+

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 105
1 ≤ Ai ≤ 109
1 ≤ M ≤ N

\ No newline at end of file From b360d78bb7568466970c75aa2cbdb3c87f9cb7a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 08:08:09 +0530 Subject: [PATCH 1371/3167] Added solution - LeetHub --- .../chocolate-distribution-problem.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Chocolate Distribution Problem - GFG/chocolate-distribution-problem.cpp diff --git a/Chocolate Distribution Problem - GFG/chocolate-distribution-problem.cpp b/Chocolate Distribution Problem - GFG/chocolate-distribution-problem.cpp new file mode 100644 index 00000000..b16c3333 --- /dev/null +++ b/Chocolate Distribution Problem - GFG/chocolate-distribution-problem.cpp @@ -0,0 +1,57 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution{ + public: + long long findMinDiff(vector a, long long n, long long m){ + //code + sort(a.begin(),a.end()); + + int i = 0, j = 0, ans = a[a.size()-1]; + + list l; + + while(j < n) + { + l.push_back(a[j]); + + if(l.size() == m) + { + ans = min(ans, l.back() - l.front()); + + l.pop_front(); + ++i; + } + ++j; + } + + return ans; + } +}; + +//{ Driver Code Starts. +int main() { + long long t; + cin>>t; + while(t--) + { + long long n; + cin>>n; + vector a; + long long x; + for(long long i=0;i>x; + a.push_back(x); + } + + long long m; + cin>>m; + Solution ob; + cout< Date: Sat, 5 Aug 2023 23:06:30 +0530 Subject: [PATCH 1372/3167] Attach NOTES - LeetHub --- 0095-unique-binary-search-trees-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0095-unique-binary-search-trees-ii/NOTES.md diff --git a/0095-unique-binary-search-trees-ii/NOTES.md b/0095-unique-binary-search-trees-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0095-unique-binary-search-trees-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 06d76c1b4b5119b058776f9754406832a9442076 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 23:06:33 +0530 Subject: [PATCH 1373/3167] Time: 11 ms (96.04%), Space: 15.7 MB (69.55%) - LeetHub --- .../0095-unique-binary-search-trees-ii.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 0095-unique-binary-search-trees-ii/0095-unique-binary-search-trees-ii.cpp diff --git a/0095-unique-binary-search-trees-ii/0095-unique-binary-search-trees-ii.cpp b/0095-unique-binary-search-trees-ii/0095-unique-binary-search-trees-ii.cpp new file mode 100644 index 00000000..68d2715e --- /dev/null +++ b/0095-unique-binary-search-trees-ii/0095-unique-binary-search-trees-ii.cpp @@ -0,0 +1,58 @@ +/** + * 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: + vector helper(int start, int end) + { + vector ans; + + if(start > end) + { + ans.push_back(nullptr); + return ans; + } + + if(start == end) + { + ans.push_back(new TreeNode(start)); + return ans; + } + + for(int i = start; i <= end; ++i) + { + vector leftPossibleSubtrees = helper(start, i-1); + vector rightPossibleSubtrees = helper(i+1, end); + + + for(auto rLeft : leftPossibleSubtrees) + { + for(auto rRight : rightPossibleSubtrees) + { + TreeNode* root = new TreeNode(i); + root->left = rLeft; + root->right = rRight; + ans.push_back(root); + } + } + } + + return ans; + } + +public: + vector generateTrees(int n) { + + return helper(1, n); + + } +}; \ No newline at end of file From 0880806ee87f37a10e1b33449225b67d7830d26f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 23:12:31 +0530 Subject: [PATCH 1374/3167] Create README - LeetHub --- 0096-unique-binary-search-trees/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0096-unique-binary-search-trees/README.md diff --git a/0096-unique-binary-search-trees/README.md b/0096-unique-binary-search-trees/README.md new file mode 100644 index 00000000..92b6a309 --- /dev/null +++ b/0096-unique-binary-search-trees/README.md @@ -0,0 +1,22 @@ +

96. Unique Binary Search Trees

Medium


Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 5
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 19
  • +
+
\ No newline at end of file From 491628291810436f2b06067bf9eaf8fc68829ec4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 23:12:35 +0530 Subject: [PATCH 1375/3167] Time: 2037 ms (12.12%), Space: 5.9 MB (73.08%) - LeetHub --- .../0096-unique-binary-search-trees.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0096-unique-binary-search-trees/0096-unique-binary-search-trees.cpp diff --git a/0096-unique-binary-search-trees/0096-unique-binary-search-trees.cpp b/0096-unique-binary-search-trees/0096-unique-binary-search-trees.cpp new file mode 100644 index 00000000..9260f8ab --- /dev/null +++ b/0096-unique-binary-search-trees/0096-unique-binary-search-trees.cpp @@ -0,0 +1,27 @@ +class Solution { + +private: + int helper(int n) + { + if(n <= 1) + return 1; + + int ans = 0; + + for(int i = 1; i <= n; ++i) + { + int leftPossibleSubtrees = helper(i-1); + int rightPossibleSubtrees = helper(n-i); + + ans += (leftPossibleSubtrees * rightPossibleSubtrees); + } + return ans; + } + +public: + int numTrees(int n) { + + return helper(n); + + } +}; \ No newline at end of file From 11f4f32fa580e249bc31361855ffeb43f0a9f1c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 23:12:59 +0530 Subject: [PATCH 1376/3167] Attach NOTES - LeetHub --- 0096-unique-binary-search-trees/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0096-unique-binary-search-trees/NOTES.md diff --git a/0096-unique-binary-search-trees/NOTES.md b/0096-unique-binary-search-trees/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0096-unique-binary-search-trees/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 646dbc25b955db5c6b249452332eacf9e2311606 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 5 Aug 2023 23:13:02 +0530 Subject: [PATCH 1377/3167] Time: 2037 ms (12.12%), Space: 5.9 MB (73.08%) - LeetHub From 302a6ad2ba40c4d955148ce62cee7ebaf10e719e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 6 Aug 2023 22:31:46 +0530 Subject: [PATCH 1378/3167] Create README - LeetHub --- String Permutations - GFG/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 String Permutations - GFG/README.md diff --git a/String Permutations - GFG/README.md b/String Permutations - GFG/README.md new file mode 100644 index 00000000..48d5e630 --- /dev/null +++ b/String Permutations - GFG/README.md @@ -0,0 +1,18 @@ +# String Permutations +## Easy +

Given a string S. The task is to find all permutations (need not be different) of a given string.

+

Example 1:

+
Input:
+S = AAA
+Output: AAA AAA AAA AAA AAA AAA
Explanation: There are total 6 permutations, as given in the output.
+
+

Example 2:

+
Input:
+S = ABSG
+Output: ABGS ABSG AGBS AGSB ASBG ASGB
+BAGS BASG BGAS BGSA BSAG BSGA GABS
+GASB GBAS GBSA GSAB GSBA SABG SAGB
+SBAG SBGA SGAB SGBA
Explanation: There are total 24 permutations, as given in the output.
+

Your Task:
This is a function problem. You only need to complete the function permutation that takes S as parameter and returns the list of permutations in lexicographically increasing order. The newline is automatically added by driver code.

+

Constraints:
1 ≤ size of string ≤ 5

+

Expected Time Complexity: O(N * N!), N = length of string.
Expected Auxiliary Space: O(1)

\ No newline at end of file From f2bdc1a4fad58739df759ac07d2fde0744058106 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 6 Aug 2023 22:31:46 +0530 Subject: [PATCH 1379/3167] Added solution - LeetHub --- .../string-permutations.cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 String Permutations - GFG/string-permutations.cpp diff --git a/String Permutations - GFG/string-permutations.cpp b/String Permutations - GFG/string-permutations.cpp new file mode 100644 index 00000000..2e57d4d2 --- /dev/null +++ b/String Permutations - GFG/string-permutations.cpp @@ -0,0 +1,62 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution{ + + private: + void helper(int idx, string S, vector& ans) + { + if(idx == S.size()) + { + ans.push_back(S); + return; + } + + for(int i = idx; i < S.size(); ++i) + { + swap(S[idx], S[i]); + helper(idx+1, S, ans); + swap(S[idx], S[i]); + } + } + + public: + //Complete this function + + vector permutation(string S) + { + //Your code here + + vector ans; + + helper(0, S, ans); + + sort(ans.begin(), ans.end()); + + return ans; + } +}; + +//{ Driver Code Starts. + +int main() +{ + int T; + cin>>T; + while(T--) + { + string S; + cin>>S; + Solution ob; + vector vec = ob.permutation(S); + for(string s : vec){ + cout< Date: Sun, 6 Aug 2023 23:53:49 +0530 Subject: [PATCH 1380/3167] Create README - LeetHub --- 0920-number-of-music-playlists/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0920-number-of-music-playlists/README.md diff --git a/0920-number-of-music-playlists/README.md b/0920-number-of-music-playlists/README.md new file mode 100644 index 00000000..6d344821 --- /dev/null +++ b/0920-number-of-music-playlists/README.md @@ -0,0 +1,37 @@ +

920. Number of Music Playlists

Hard


Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that:

+ +
    +
  • Every song is played at least once.
  • +
  • A song can only be played again only if k other songs have been played.
  • +
+ +

Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 109 + 7.

+

 

+

Example 1:

+ +
Input: n = 3, goal = 3, k = 1
+Output: 6
+Explanation: There are 6 possible playlists: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1].
+
+ +

Example 2:

+ +
Input: n = 2, goal = 3, k = 0
+Output: 6
+Explanation: There are 6 possible playlists: [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], and [1, 2, 2].
+
+ +

Example 3:

+ +
Input: n = 2, goal = 3, k = 1
+Output: 2
+Explanation: There are 2 possible playlists: [1, 2, 1] and [2, 1, 2].
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= k < n <= goal <= 100
  • +
+
\ No newline at end of file From 2fbaeeb10da8e23de3e129d909e2045a46359fcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 6 Aug 2023 23:53:49 +0530 Subject: [PATCH 1381/3167] Attach NOTES - LeetHub --- 0920-number-of-music-playlists/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0920-number-of-music-playlists/NOTES.md diff --git a/0920-number-of-music-playlists/NOTES.md b/0920-number-of-music-playlists/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0920-number-of-music-playlists/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 784bd84564b7f780fc6e22dbe4e74b3818248fc4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 6 Aug 2023 23:53:53 +0530 Subject: [PATCH 1382/3167] Time: 8 ms (35.59%), Space: 8 MB (50.00%) - LeetHub --- .../0920-number-of-music-playlists.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0920-number-of-music-playlists/0920-number-of-music-playlists.cpp diff --git a/0920-number-of-music-playlists/0920-number-of-music-playlists.cpp b/0920-number-of-music-playlists/0920-number-of-music-playlists.cpp new file mode 100644 index 00000000..61bc1a30 --- /dev/null +++ b/0920-number-of-music-playlists/0920-number-of-music-playlists.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int mod = 1e9 + 7; + + long long int helper(int n, int k, int goal, vector< vector< int>> &dp) + { + if(n==0 and goal==0) + return 1; + + if(n==0 || goal==0) + return 0; + + if(dp[n][goal]!=-1) + return dp[n][goal]; + + long long int a = (helper(n-1, k, goal-1, dp)*n); + + long long int b = (helper(n, k, goal-1, dp)*(max(n-k, 0))); + + return dp[n][goal] = (a+b)%mod; + } + int numMusicPlaylists(int n, int goal, int k) + { + vector< vector< int>> dp(n+1, vector(goal+1, -1)); + + return helper(n,k,goal, dp); + } +}; \ No newline at end of file From 6238b3d8b8ee8a0a289087db58cad00a9fa38544 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 09:48:23 +0530 Subject: [PATCH 1383/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2811-check-if-it-is-possible-to-split-array/README.md diff --git a/2811-check-if-it-is-possible-to-split-array/README.md b/2811-check-if-it-is-possible-to-split-array/README.md new file mode 100644 index 00000000..ee11d137 --- /dev/null +++ b/2811-check-if-it-is-possible-to-split-array/README.md @@ -0,0 +1,42 @@ +

2811. Check if it is Possible to Split Array

Medium


You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n non-empty arrays by performing a series of steps.

+ +

In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into two subarrays, if, for each resulting subarray, at least one of the following holds:

+ +
    +
  • The length of the subarray is one, or
  • +
  • The sum of elements of the subarray is greater than or equal to m.
  • +
+ +

Return true if you can split the given array into n arrays, otherwise return false.

+ +

Note: A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [2, 2, 1], m = 4
+Output: true
+Explanation: We can split the array into [2, 2] and [1] in the first step. Then, in the second step, we can split [2, 2] into [2] and [2]. As a result, the answer is true.
+ +

Example 2:

+ +
Input: nums = [2, 1, 3], m = 5 
+Output: false
+Explanation: We can try splitting the array in two different ways: the first way is to have [2, 1] and [3], and the second way is to have [2] and [1, 3]. However, both of these ways are not valid. So, the answer is false.
+ +

Example 3:

+ +
Input: nums = [2, 3, 3, 2, 3], m = 6
+Output: true
+Explanation: We can split the array into [2, 3, 3, 2] and [3] in the first step. Then, in the second step, we can split [2, 3, 3, 2] into [2, 3, 3] and [2]. Then, in the third step, we can split [2, 3, 3] into [2] and [3, 3]. And in the last step we can split [3, 3] into [3] and [3]. As a result, the answer is true.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
  • 1 <= m <= 200
  • +
+
\ No newline at end of file From 77f570638bb4e97ebbe998078d5e5a2d011b15b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 09:48:24 +0530 Subject: [PATCH 1384/3167] Attach NOTES - LeetHub --- 2811-check-if-it-is-possible-to-split-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2811-check-if-it-is-possible-to-split-array/NOTES.md diff --git a/2811-check-if-it-is-possible-to-split-array/NOTES.md b/2811-check-if-it-is-possible-to-split-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2811-check-if-it-is-possible-to-split-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2cbe0d8f7967159c7917839905fa67fdd555d260 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 09:48:27 +0530 Subject: [PATCH 1385/3167] Time: 486 ms (16.67%), Space: 26.4 MB (16.67%) - LeetHub --- ...check-if-it-is-possible-to-split-array.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2811-check-if-it-is-possible-to-split-array/2811-check-if-it-is-possible-to-split-array.cpp diff --git a/2811-check-if-it-is-possible-to-split-array/2811-check-if-it-is-possible-to-split-array.cpp b/2811-check-if-it-is-possible-to-split-array/2811-check-if-it-is-possible-to-split-array.cpp new file mode 100644 index 00000000..064b7327 --- /dev/null +++ b/2811-check-if-it-is-possible-to-split-array/2811-check-if-it-is-possible-to-split-array.cpp @@ -0,0 +1,56 @@ +class Solution { + +private: + + bool posibleToPartition(int start, int end, int m, vector& nums) + { + int sum = 0; + + for(int i = start; i <= end; ++i) + { + sum += nums[i]; + + if(sum >= m) + return true; + } + return false; + } + + bool helper(int start, int end, int m, vector& nums, vector>& dp) + { + if(start == end) + return true; + + if(!posibleToPartition(start, end, m, nums)) + return false; + + if(dp[start][end] != -1) + return dp[start][end]; + + for(int i = start; i < end; ++i) + { + bool leftHalf = helper(start, i, m, nums, dp); + bool rightHalf = helper(i+1 , end, m , nums, dp); + + if(leftHalf and rightHalf) + { + return dp[start][end] = true; + } + } + return dp[start][end] = false; + } + +public: + bool canSplitArray(vector& nums, int m) { + + int n = nums.size(); + + if(n <= 2) + return true; + + vector> dp(n, vector(n,-1)); + + return helper(0, n-1, m, nums, dp); + + } +}; \ No newline at end of file From 5f767465e8ed7f28f9964a157bb4797460344602 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 18:42:05 +0530 Subject: [PATCH 1386/3167] Create README - LeetHub --- 2812-find-the-safest-path-in-a-grid/README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 2812-find-the-safest-path-in-a-grid/README.md diff --git a/2812-find-the-safest-path-in-a-grid/README.md b/2812-find-the-safest-path-in-a-grid/README.md new file mode 100644 index 00000000..1565a57a --- /dev/null +++ b/2812-find-the-safest-path-in-a-grid/README.md @@ -0,0 +1,54 @@ +

2812. Find the Safest Path in a Grid

Medium


You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents:

+ +
    +
  • A cell containing a thief if grid[r][c] = 1
  • +
  • An empty cell if grid[r][c] = 0
  • +
+ +

You are initially positioned at cell (0, 0). In one move, you can move to any adjacent cell in the grid, including cells containing thieves.

+ +

The safeness factor of a path on the grid is defined as the minimum manhattan distance from any cell in the path to any thief in the grid.

+ +

Return the maximum safeness factor of all paths leading to cell (n - 1, n - 1).

+ +

An adjacent cell of cell (r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) and (r - 1, c) if it exists.

+ +

The Manhattan distance between two cells (a, b) and (x, y) is equal to |a - x| + |b - y|, where |val| denotes the absolute value of val.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,0,0],[0,0,0],[0,0,1]]
+Output: 0
+Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
+
+ +

Example 2:

+ +
Input: grid = [[0,0,1],[0,0,0],[0,0,0]]
+Output: 2
+Explanation: The path depicted in the picture above has a safeness factor of 2 since:
+- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
+It can be shown that there are no other paths with a higher safeness factor.
+
+ +

Example 3:

+ +
Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
+Output: 2
+Explanation: The path depicted in the picture above has a safeness factor of 2 since:
+- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
+- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
+It can be shown that there are no other paths with a higher safeness factor.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= grid.length == n <= 400
  • +
  • grid[i].length == n
  • +
  • grid[i][j] is either 0 or 1.
  • +
  • There is at least one thief in the grid.
  • +
+
\ No newline at end of file From 138d9dd88ce13f6cc7c034a5cbae440bb1113692 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 18:42:09 +0530 Subject: [PATCH 1387/3167] Time: 391 ms (29.41%), Space: 114.5 MB (23.53%) - LeetHub --- .../2812-find-the-safest-path-in-a-grid.cpp | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 2812-find-the-safest-path-in-a-grid/2812-find-the-safest-path-in-a-grid.cpp diff --git a/2812-find-the-safest-path-in-a-grid/2812-find-the-safest-path-in-a-grid.cpp b/2812-find-the-safest-path-in-a-grid/2812-find-the-safest-path-in-a-grid.cpp new file mode 100644 index 00000000..b8afb8d2 --- /dev/null +++ b/2812-find-the-safest-path-in-a-grid/2812-find-the-safest-path-in-a-grid.cpp @@ -0,0 +1,93 @@ +class Solution { +public: + int maximumSafenessFactor(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector> minimumMahattanDistance(n, vector(m, 0)); + + queue> q; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + { + q.push({i,j}); + minimumMahattanDistance[i][j] = 0; + } + } + } + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1, 0}; + + while(!q.empty()) + { + auto curr = q.front(); + q.pop(); + + int x = curr.first; + int y = curr.second; + + for(int i = 0; i < 4; ++i) + { + int newx = x + dx[i]; + int newy = y + dy[i]; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and grid[newx][newy] != 1 and minimumMahattanDistance[newx][newy] == 0) + { + q.push({newx, newy}); + minimumMahattanDistance[newx][newy] = minimumMahattanDistance[x][y] + 1; + } + } + } + + // for(auto itr : minimumMahattanDistance) + // { + // for(auto row : itr) + // cout<> visited(n, vector(m, false)); + + int ans = minimumMahattanDistance[0][0]; + + priority_queue> > pq; + + pq.push({minimumMahattanDistance[0][0],{0,0}}); + + visited[0][0] = true; + + while(!pq.empty()) + { + auto curr = pq.top(); + pq.pop(); + + int maximumSafenessFactor = curr.first; + int x = curr.second.first; + int y = curr.second.second; + + if(x == n-1 and y == m-1) + { + return maximumSafenessFactor; + } + + for(int i = 0; i < 4; ++i) + { + int newx = x + dx[i]; + int newy = y + dy[i]; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m and !visited[newx][newy]) + { + pq.push({min(maximumSafenessFactor,minimumMahattanDistance[newx][newy]),{newx, newy}}); + visited[newx][newy] = true; + } + } + } + + return 0; + } +}; \ No newline at end of file From ffde186469a06d06bbcafbeab6e17e8ab0bde20b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 21:59:11 +0530 Subject: [PATCH 1388/3167] Create README - LeetHub --- Solve the Sudoku - GFG/README.md | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Solve the Sudoku - GFG/README.md diff --git a/Solve the Sudoku - GFG/README.md b/Solve the Sudoku - GFG/README.md new file mode 100644 index 00000000..f2ebb406 --- /dev/null +++ b/Solve the Sudoku - GFG/README.md @@ -0,0 +1,50 @@ +# Solve the Sudoku +## Hard +

Given an incomplete Sudoku configuration in terms of a 9 x 9  2-D square matrix (grid[][]), the task is to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution.

+

A sudoku solution must satisfy all of the following rules:

+
    +
  1. Each of the digits 1-9 must occur exactly once in each row.
  2. +
  3. Each of the digits 1-9 must occur exactly once in each column.
  4. +
  5. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
  6. +
+

Zeros in the grid indicates blanks, which are to be filled with some number between 1-9. You can not replace the element in the cell which is not blank.

+


Sample Sudoku for you to get the logic for its solution:

+

Example 1:

+
Input:
+grid[][] = 
+[[3 0 6 5 0 8 4 0 0],
+[5 2 0 0 0 0 0 0 0],
+[0 8 7 0 0 0 0 3 1 ],
+[0 0 3 0 1 0 0 8 0],
+[9 0 0 8 6 3 0 0 5],
+[0 5 0 0 9 0 6 0 0],
+[1 3 0 0 0 0 2 5 0],
+[0 0 0 0 0 0 0 7 4],
+[0 0 5 2 0 6 3 0 0]]
+Output:
True +3 1 6 5 7 8 4 9 2 +5 2 9 1 3 4 7 6 8 +4 8 7 6 2 9 5 3 1 +2 6 3 4 1 5 9 8 7 +9 7 4 8 6 3 1 2 5 +8 5 1 7 9 2 6 4 3 +1 3 8 9 4 7 2 5 6 +6 9 2 3 5 1 8 7 4 +7 4 5 2 8 6 3 1 9
Explanation:
There exists a valid Sudoku for the input grid, which is shown in output. +
+

Example 2:

+
Input:
+grid[][] = 
+[[3 6 6 5 0 8 4 0 0],
+[5 2 0 0 0 0 0 0 0],
+[0 8 7 0 0 0 0 3 1 ],
+[0 0 3 0 1 0 0 8 0],
+[9 0 0 8 6 3 0 0 5],
+[0 5 0 0 9 0 6 0 0],
+[1 3 0 0 0 0 2 5 0],
+[0 0 0 0 0 0 0 7 4],
+[0 0 5 2 0 6 3 0 0]]
+Output:
False
Explanation:
There does not exists a valid Sudoku for the input grid, since there are two 6s in the first row. Which cannot replaced.
+

Your Task:
You need to complete the two functions:

SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not, on returning false driver will print "No solution exists".

printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. Do not give a new line character after printing the grid.

+

Expected Time Complexity: O(9N*N).
Expected Auxiliary Space: O(N*N).

+

Constraints:
0 ≤ grid[i][j] ≤ 9

\ No newline at end of file From 6fbff18da92659b0cf9b61b0d8c66635a7f1d8c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 21:59:12 +0530 Subject: [PATCH 1389/3167] Added solution - LeetHub --- Solve the Sudoku - GFG/solve-the-sudoku.cpp | 101 ++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Solve the Sudoku - GFG/solve-the-sudoku.cpp diff --git a/Solve the Sudoku - GFG/solve-the-sudoku.cpp b/Solve the Sudoku - GFG/solve-the-sudoku.cpp new file mode 100644 index 00000000..46f46b45 --- /dev/null +++ b/Solve the Sudoku - GFG/solve-the-sudoku.cpp @@ -0,0 +1,101 @@ +//{ Driver Code Starts +#include +using namespace std; +// UNASSIGNED is used for empty cells in sudoku grid +#define UNASSIGNED 0 + +// N is used for the size of Sudoku grid. +// Size will be NxN +#define N 9 + + +// } Driver Code Ends +class Solution +{ + private: + + bool isValid(int row, int col, int curr, int matrix[9][9]) + { + for(int i = 0; i < 9; ++i) + { + if(matrix[i][col] == curr) + return false; + if(matrix[row][i] == curr) + return false; + if(matrix[3*(row/3) + i/3][3*(col/3) + i%3] == curr) + return false; + } + return true; + } + + public: + //Function to find a solved Sudoku. + bool SolveSudoku(int grid[N][N]) + { + // Your code here + + for(int i = 0; i < N; ++i) + { + for(int j = 0; j < N; ++j) + { + if(grid[i][j] == 0) + { + for(int k = 1; k <= 9; ++k) + { + if(isValid(i, j, k, grid)) + { + grid[i][j] = k ; + + if(SolveSudoku(grid)) + return true; + else + grid[i][j] = 0; + } + } + return false; + } + + } + } + return true; + } + + //Function to print grids of the Sudoku. + void printGrid (int grid[N][N]) + { + // Your code here + + for(int i = 0; i < N; ++i) + { + for(int j = 0; j < N; ++j) + cout<>t; + while(t--) + { + int grid[N][N]; + + for(int i=0;i<9;i++) + for(int j=0;j<9;j++) + cin>>grid[i][j]; + + Solution ob; + + if (ob.SolveSudoku(grid) == true) + ob.printGrid(grid); + else + cout << "No solution exists"; + + cout< Date: Mon, 7 Aug 2023 22:07:32 +0530 Subject: [PATCH 1390/3167] Create README - LeetHub --- 0074-search-a-2d-matrix/README.md | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0074-search-a-2d-matrix/README.md diff --git a/0074-search-a-2d-matrix/README.md b/0074-search-a-2d-matrix/README.md new file mode 100644 index 00000000..17687093 --- /dev/null +++ b/0074-search-a-2d-matrix/README.md @@ -0,0 +1,34 @@ +

74. Search a 2D Matrix

Medium


You are given an m x n integer matrix matrix with the following two properties:

+ +
    +
  • Each row is sorted in non-decreasing order.
  • +
  • The first integer of each row is greater than the last integer of the previous row.
  • +
+ +

Given an integer target, return true if target is in matrix or false otherwise.

+ +

You must write a solution in O(log(m * n)) time complexity.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
+Output: true
+
+ +

Example 2:

+ +
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 100
  • +
  • -104 <= matrix[i][j], target <= 104
  • +
+
\ No newline at end of file From 73da59d6a66ab60a5435a34a2cefe4f0a958b235 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 22:07:33 +0530 Subject: [PATCH 1391/3167] Attach NOTES - LeetHub --- 0074-search-a-2d-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0074-search-a-2d-matrix/NOTES.md diff --git a/0074-search-a-2d-matrix/NOTES.md b/0074-search-a-2d-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0074-search-a-2d-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ee1cb215ab11cda80b36f9010dc02bd444bdf20b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 7 Aug 2023 22:07:36 +0530 Subject: [PATCH 1392/3167] Time: 3 ms (82.89%), Space: 9.5 MB (19.73%) - LeetHub --- .../0074-search-a-2d-matrix.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0074-search-a-2d-matrix/0074-search-a-2d-matrix.cpp diff --git a/0074-search-a-2d-matrix/0074-search-a-2d-matrix.cpp b/0074-search-a-2d-matrix/0074-search-a-2d-matrix.cpp new file mode 100644 index 00000000..f82a7a0a --- /dev/null +++ b/0074-search-a-2d-matrix/0074-search-a-2d-matrix.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + + int n = matrix.size(), m = matrix[0].size(); + + int start = 0 , end = (n*m)-1; + + while(start <= end) + { + int mid = (start + end) >> 1; + + int val = matrix[mid/m][mid%m]; + + if(val == target) + return true; + else if(val < target) + start = mid + 1; + else + end = mid - 1; + } + + return false; + } +}; \ No newline at end of file From c1580c8d008f03320984d8d34b6b7cfe7aa82531 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 8 Aug 2023 07:52:27 +0530 Subject: [PATCH 1393/3167] Create README - LeetHub --- Fraction pairs with sum 1 - GFG/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Fraction pairs with sum 1 - GFG/README.md diff --git a/Fraction pairs with sum 1 - GFG/README.md b/Fraction pairs with sum 1 - GFG/README.md new file mode 100644 index 00000000..72b810f0 --- /dev/null +++ b/Fraction pairs with sum 1 - GFG/README.md @@ -0,0 +1,15 @@ +# Fraction pairs with sum 1 +## Medium +

Given a list of N fractions, represented as two lists numerator and denominator, the task is to determine the count of pairs of fractions whose sum equals 1.

+

Example 1:

+
Input:
N = 4
numerator = [1, 2, 2, 8]
denominator = [2, 4, 6, 12]
Output: +2 +Explanation:
Fractions 1/2 and 2/4 sum to 1. Similarly fractions 2/6 and 8/12 sum to 1. So there are 2 pairs of fractions which sum to 1. +
+

Example 2:

+
Input:
N = 5
numerator = [3, 1, 12, 81, 2]
denominator = [9, 10, 18, 90, 5]
Output: +2 +Explanation:
Fractions 3/9 and 12/18 sum to 1. Similarly fractions 1/10 and 81/90 sum to 1. So there are 2 pairs of fractions which sum to 1.
+

Your task:
You don't need to read input or print anything. Your task is to complete the function countFractions() which take integer N and arrays numerator 
and denominator of size N as arguments, and returns an integer.

+

Expected Time Complexity: O(N*log(N))
Expected Auxiliary Space: O(N)

+

Constraints:
1 <= N <=105
1 <= numerator[i] <= denominator[i] <= 109

\ No newline at end of file From f8ff27360afc28ca2043d7e38e3446574feabc94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 8 Aug 2023 07:52:28 +0530 Subject: [PATCH 1394/3167] Added solution - LeetHub --- .../fraction-pairs-with-sum-1.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Fraction pairs with sum 1 - GFG/fraction-pairs-with-sum-1.cpp diff --git a/Fraction pairs with sum 1 - GFG/fraction-pairs-with-sum-1.cpp b/Fraction pairs with sum 1 - GFG/fraction-pairs-with-sum-1.cpp new file mode 100644 index 00000000..af8fdaab --- /dev/null +++ b/Fraction pairs with sum 1 - GFG/fraction-pairs-with-sum-1.cpp @@ -0,0 +1,55 @@ +//{ Driver Code Starts +//Initial Template for C++ +#include +using namespace std; + +// } Driver Code Ends +//User function Template for C++ +class Solution +{ + public: + int countFractions(int n, int numerator[], int denominator[]) + { + unordered_map mp; + + int cnt = 0; + + for(int i = 0; i < n; ++i){ + float a = (float)numerator[i]/(float)denominator[i]; + + float b = (float)(denominator[i] - numerator[i])/(float)denominator[i]; + + if(mp[b]) + cnt += mp[b]; + + // cout<>t; + while(t--) + { + int n; + cin>>n; + int numerator[n],denominator[n]; + for(int i=0;i>numerator[i]; + for(int i=0;i>denominator[i]; + Solution ob; + int ans=ob.countFractions(n,numerator,denominator); + cout< Date: Tue, 8 Aug 2023 09:19:42 +0530 Subject: [PATCH 1395/3167] Create README - LeetHub --- 0033-search-in-rotated-sorted-array/README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0033-search-in-rotated-sorted-array/README.md diff --git a/0033-search-in-rotated-sorted-array/README.md b/0033-search-in-rotated-sorted-array/README.md new file mode 100644 index 00000000..0e622ad1 --- /dev/null +++ b/0033-search-in-rotated-sorted-array/README.md @@ -0,0 +1,30 @@ +

33. Search in Rotated Sorted Array

Medium


There is an integer array nums sorted in ascending order (with distinct values).

+ +

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

+ +

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

+ +

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

+ +

 

+

Example 1:

+
Input: nums = [4,5,6,7,0,1,2], target = 0
+Output: 4
+

Example 2:

+
Input: nums = [4,5,6,7,0,1,2], target = 3
+Output: -1
+

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5000
  • +
  • -104 <= nums[i] <= 104
  • +
  • All values of nums are unique.
  • +
  • nums is an ascending array that is possibly rotated.
  • +
  • -104 <= target <= 104
  • +
+
\ No newline at end of file From 2abcb7fcb0bd2fe43fd581fd0b8870fb4312e2f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 8 Aug 2023 09:19:43 +0530 Subject: [PATCH 1396/3167] Attach NOTES - LeetHub --- 0033-search-in-rotated-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0033-search-in-rotated-sorted-array/NOTES.md diff --git a/0033-search-in-rotated-sorted-array/NOTES.md b/0033-search-in-rotated-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0033-search-in-rotated-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9b986a5b8497247cadc72a2c47af45135718cd38 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 8 Aug 2023 09:19:46 +0530 Subject: [PATCH 1397/3167] Time: 0 ms (100.00%), Space: 11 MB (45.03%) - LeetHub --- .../0033-search-in-rotated-sorted-array.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp diff --git a/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp b/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp new file mode 100644 index 00000000..7e2679b4 --- /dev/null +++ b/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int search(vector& nums, int target) { + + int low = 0 , high = nums.size()-1; + + while(low <= high) + { + int mid = (low + high) >> 1; + + if(nums[mid] == target) + return mid; + + if(nums[low] <= nums[mid]) + { + if(nums[low] <= target and target <= nums[mid]) + high = mid-1; + else + low = mid+1; + } + else + { + if(nums[mid] <= target and nums[high] >= target) + low = mid+1; + else + high = mid-1; + } + } + + return -1; + } +}; \ No newline at end of file From 1d99f489f28527e397a6eac35859f92ade3c654a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:50:17 +0530 Subject: [PATCH 1398/3167] Create README - LeetHub --- Largest prime factor - GFG/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Largest prime factor - GFG/README.md diff --git a/Largest prime factor - GFG/README.md b/Largest prime factor - GFG/README.md new file mode 100644 index 00000000..e8c58fe6 --- /dev/null +++ b/Largest prime factor - GFG/README.md @@ -0,0 +1,21 @@ +# Largest prime factor +## Medium +

Given a number N, the task is to find the largest prime factor of that number.
 Example 1:

+
Input:
+N = 5
+Output:
+5
+Explanation:
+5 has 1 prime factor i.e 5 only.
+
+

Example 2:

+
Input:
+N = 24
+Output:
+3
+Explanation:
+24 has 2 prime factors 2 and 3 in which 3 is greater.
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function largestPrimeFactor() which takes an integer N as input parameters and returns an integer, largest prime factor of N.

+

Expected Time Complexity: O(sqrt(N))
Expected Space Complexity: O(1)

+

Constraints:
2 <= N <= 109

\ No newline at end of file From 6f0692c9b6dbfdfcb63b9a5af7117aafb44424e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:50:18 +0530 Subject: [PATCH 1399/3167] Added solution - LeetHub --- .../largest-prime-factor.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Largest prime factor - GFG/largest-prime-factor.cpp diff --git a/Largest prime factor - GFG/largest-prime-factor.cpp b/Largest prime factor - GFG/largest-prime-factor.cpp new file mode 100644 index 00000000..30fefee6 --- /dev/null +++ b/Largest prime factor - GFG/largest-prime-factor.cpp @@ -0,0 +1,50 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution{ +public: + long long int largestPrimeFactor(int N){ + // code here + + int ans; + + for(int i = 2; i * i <= N; ++i) + { + if(N % i == 0) + { + ans = i; + + while((N % i) == 0) + { + N /= i; + } + } + } + + + if(N > 1) + { + ans = max(ans, N); + } + + return ans; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int N; + cin>>N; + Solution ob; + cout << ob.largestPrimeFactor(N) << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From d38d5b4f2e9f90f0ae0b78ced3327fceb869d550 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 9 Aug 2023 20:47:07 +0530 Subject: [PATCH 1400/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2616-minimize-the-maximum-difference-of-pairs/README.md diff --git a/2616-minimize-the-maximum-difference-of-pairs/README.md b/2616-minimize-the-maximum-difference-of-pairs/README.md new file mode 100644 index 00000000..3b3b0943 --- /dev/null +++ b/2616-minimize-the-maximum-difference-of-pairs/README.md @@ -0,0 +1,31 @@ +

2616. Minimize the Maximum Difference of Pairs

Medium


You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs.

+ +

Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents the absolute value of x.

+ +

Return the minimum maximum difference among all p pairs. We define the maximum of an empty set to be zero.

+ +

 

+

Example 1:

+ +
Input: nums = [10,1,2,7,1,3], p = 2
+Output: 1
+Explanation: The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5. 
+The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
+
+ +

Example 2:

+ +
Input: nums = [4,2,1,2], p = 1
+Output: 0
+Explanation: Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= p <= (nums.length)/2
  • +
+
\ No newline at end of file From d50bc81fb74fbc9d0a93312568b7d00ea9866a3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 9 Aug 2023 20:47:10 +0530 Subject: [PATCH 1401/3167] Time: 118 ms (99.33%), Space: 81.1 MB (38.62%) - LeetHub --- ...nimize-the-maximum-difference-of-pairs.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2616-minimize-the-maximum-difference-of-pairs/2616-minimize-the-maximum-difference-of-pairs.cpp diff --git a/2616-minimize-the-maximum-difference-of-pairs/2616-minimize-the-maximum-difference-of-pairs.cpp b/2616-minimize-the-maximum-difference-of-pairs/2616-minimize-the-maximum-difference-of-pairs.cpp new file mode 100644 index 00000000..8bc7ca57 --- /dev/null +++ b/2616-minimize-the-maximum-difference-of-pairs/2616-minimize-the-maximum-difference-of-pairs.cpp @@ -0,0 +1,51 @@ +class Solution { + +private: + int isPossibleToDivideInpPairs(int mid, int n, vector& nums, int p) + { + int cnt = 0; + int i = 1; + + while(i < n and cnt < p) + { + if(nums[i] - nums[i-1] <= mid) + { + i += 2; + ++cnt; + } + else + ++i; + } + + return cnt >= p; + } + +public: + int minimizeMax(vector& nums, int p) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + int right = nums[n-1] - nums[0]; + int left = 0; + int ans = right; + + + while(left <= right) + { + int mid = (left + right) >> 1; + + if(isPossibleToDivideInpPairs(mid, n, nums, p)) + { + ans = mid; + right = mid-1; + } + else + left = mid+1; + } + + return ans; + + } +}; \ No newline at end of file From 36ca2442959d55ff5cd2ce796b58a94db8a0a627 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 08:01:18 +0530 Subject: [PATCH 1402/3167] Create README - LeetHub --- Longest Common Subsequence - GFG/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Longest Common Subsequence - GFG/README.md diff --git a/Longest Common Subsequence - GFG/README.md b/Longest Common Subsequence - GFG/README.md new file mode 100644 index 00000000..b0ea3c77 --- /dev/null +++ b/Longest Common Subsequence - GFG/README.md @@ -0,0 +1,21 @@ +# Longest Common Subsequence +## Medium +

Given two strings, find the length of longest subsequence present in both of them. Both the strings are in uppercase latin alphabets.

+

Example 1:

+
Input:
+A = 6, B = 6
+str1 = ABCDGH
+str2 = AEDFHR
+Output: 3
+Explanation: LCS for input strings “ABCDGH” and “AEDFHR” is “ADH” of length 3.
+
+

Example 2:

+
Input:
+A = 3, B = 2
+str1 = ABC
+str2 = AC
+Output: 2
+Explanation: LCS of "ABC" and "AC" is "AC" of length 2.
+

Your Task:
Complete the function lcs() which takes the length of two strings respectively and two strings as input parameters and returns the length of the longest subsequence present in both of them.

+

Expected Time Complexity : O(|str1|*|str2|)
Expected Auxiliary Space: O(|str1|*|str2|)

+

Constraints:
1<=size(str1),size(str2)<=103

\ No newline at end of file From acebedade265f2ff7abad3c9e39bd260e7ab575f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 08:01:19 +0530 Subject: [PATCH 1403/3167] Added solution - LeetHub --- .../longest-common-subsequence.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Longest Common Subsequence - GFG/longest-common-subsequence.cpp diff --git a/Longest Common Subsequence - GFG/longest-common-subsequence.cpp b/Longest Common Subsequence - GFG/longest-common-subsequence.cpp new file mode 100644 index 00000000..d4b891ce --- /dev/null +++ b/Longest Common Subsequence - GFG/longest-common-subsequence.cpp @@ -0,0 +1,55 @@ +//{ Driver Code Starts +#include +const int mod=1e9+7; +using namespace std; + +// } Driver Code Ends +// function to find longest common subsequence + +class Solution +{ + public: + //Function to find the length of longest common subsequence in two strings. + int lcs(int n, int m, string s1, string s2) + { + // your code here + + vector> dp(n+1, vector(m+1,0)); + + for(int i = n-1; i >= 0; --i) + { + for(int j = m-1; j >= 0; --j) + { + if(s1[i] == s2[j]) + { + dp[i][j] = 1 + dp[i+1][j+1]; + } + else + { + dp[i][j] = max(dp[i][j+1], dp[i+1][j]); + } + } + } + + return dp[0][0]; + } +}; + + +//{ Driver Code Starts. +int main() +{ + int t,n,m; + cin>>t; + while(t--) + { + cin>>n>>m; // Take size of both the strings as input + string s1,s2; + cin>>s1>>s2; // Take both the string as input + Solution ob; + cout << ob.lcs(n, m, s1, s2) << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 65784ce835205a8e85e4bb6dae3d8abeda90087e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:13:05 +0530 Subject: [PATCH 1404/3167] Attach NOTES - LeetHub From 960e38b326f1cd66aad6e2d205652656b87269bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:13:08 +0530 Subject: [PATCH 1405/3167] Time: 10 ms (24.46%), Space: 13.9 MB (50.05%) - LeetHub --- ...0081-search-in-rotated-sorted-array-ii.cpp | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp b/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp index f86435f6..b23a54b1 100644 --- a/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp +++ b/0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp @@ -2,33 +2,39 @@ class Solution { public: bool search(vector& nums, int target) { - int low = 0, high = nums.size()-1; + int start = 0, end = nums.size()-1; - while(low <= high) + while(start <= end) { - int mid = (low + high) >> 1; + int mid = (start + end) >> 1; if(nums[mid] == target) return true; - if((nums[low] == nums[mid]) and (nums[mid] == nums[high])) - { - ++low; - --high; - } - else if(nums[low] <= nums[mid]) + + if(nums[mid] == nums[start] and nums[mid] == nums[end]) + ++start, --end; + else if(nums[mid] >= nums[start]) { - if(nums[low] <= target and target < nums[mid]) - high = mid-1; + if(target >= nums[start] and target <= nums[mid]) + { + end = mid - 1; + } else - low = mid+1; + { + start = mid + 1; + } } else { - if(nums[mid] < target and target <= nums[high]) - low = mid+1; + if(target >= nums[mid] and target <= nums[end]) + { + start = mid+1; + } else - high = mid-1; + { + end = mid - 1; + } } } From d50cfc7b7762c833c4f8a85b32580f474d299428 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:13:22 +0530 Subject: [PATCH 1406/3167] Attach NOTES - LeetHub From 8f72be9365ce5f80beb392560abf0e8c5a970360 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:13:26 +0530 Subject: [PATCH 1407/3167] Time: 10 ms (24.46%), Space: 13.9 MB (50.05%) - LeetHub From cf9ae4f4019f80e8a2cfd95a36e77bba442f3f61 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:13:40 +0530 Subject: [PATCH 1408/3167] Attach NOTES - LeetHub From fc917f040e8f8bd9c4fc2d8a0f15f1f5c600ea6c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:13:44 +0530 Subject: [PATCH 1409/3167] Time: 10 ms (24.46%), Space: 13.9 MB (50.05%) - LeetHub From 4d8ba6c79da3735485de7159f4ec7ecb10f638cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 09:46:26 +0530 Subject: [PATCH 1410/3167] Create README - LeetHub --- Coin Change - GFG/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Coin Change - GFG/README.md diff --git a/Coin Change - GFG/README.md b/Coin Change - GFG/README.md new file mode 100644 index 00000000..9c094753 --- /dev/null +++ b/Coin Change - GFG/README.md @@ -0,0 +1,20 @@ +# Coin Change +## Medium +

Given an integer array coins[ ] of sizerepresenting different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ].  
Note: Assume that you have an infinite supply of each type of coin. And you can use any coin as many times as you want.

+

Example 1:

+
Input:
+N = 3, sum = 4
+coins = {1,2,3}
+Output: 4
+Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}.
+
+

Example 2:

+
Input:
+N = 4, Sum = 10
+coins = {2,5,3,6}
+Output: 5
+Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}.
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins its size N and sum as input parameters and returns the number of ways to make change for given sum of money. 

+

Expected Time Complexity: O(sum*N)
Expected Auxiliary Space: O(sum)

+

Constraints:
1 <= sum, N, coins[i] <= 103

\ No newline at end of file From c03dfed4d01022056a8049cc0f4e93b3315e5491 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 09:46:27 +0530 Subject: [PATCH 1411/3167] Added solution - LeetHub --- Coin Change - GFG/coin-change.cpp | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Coin Change - GFG/coin-change.cpp diff --git a/Coin Change - GFG/coin-change.cpp b/Coin Change - GFG/coin-change.cpp new file mode 100644 index 00000000..dc15b708 --- /dev/null +++ b/Coin Change - GFG/coin-change.cpp @@ -0,0 +1,54 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution { + public: + long long int count(int coins[], int N, int sum) { + + // code here. + + vector> dp(N+1, vector(sum+1,0)); + + for(int i = 0; i <= sum; ++i) + { + dp[0][i] = (i % coins[0] == 0); + } + + for(int i = 1; i < N; ++i) + { + for(int j = 0; j <= sum; ++j) + { + long long int notTake = dp[i-1][j]; + long long int take = 0; + + if(coins[i] <= j) + take = dp[i][j-coins[i]]; + + dp[i][j] = take + notTake; + } + } + + return dp[N-1][sum]; + } +}; + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int sum, N; + cin >> sum >> N; + int coins[N]; + for (int i = 0; i < N; i++) cin >> coins[i]; + Solution ob; + cout << ob.count(coins, N, sum) << endl; + } + + return 0; +} + + +// } Driver Code Ends \ No newline at end of file From d4f437fbf285ea76b45fc96e8ead8efd5bd6b440 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 09:48:57 +0530 Subject: [PATCH 1412/3167] Create README - LeetHub From 40c2a61181cbab90e2725ccd4ea3213a85ff69e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 09:48:57 +0530 Subject: [PATCH 1413/3167] Added solution - LeetHub --- Coin Change - GFG/coin-change.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Coin Change - GFG/coin-change.cpp b/Coin Change - GFG/coin-change.cpp index dc15b708..bea08df6 100644 --- a/Coin Change - GFG/coin-change.cpp +++ b/Coin Change - GFG/coin-change.cpp @@ -13,14 +13,14 @@ class Solution { for(int i = 0; i <= sum; ++i) { - dp[0][i] = (i % coins[0] == 0); + dp[N-1][i] = (i % coins[N-1] == 0); } - for(int i = 1; i < N; ++i) + for(int i = N-2; i >= 0; --i) { for(int j = 0; j <= sum; ++j) { - long long int notTake = dp[i-1][j]; + long long int notTake = dp[i+1][j]; long long int take = 0; if(coins[i] <= j) @@ -30,7 +30,7 @@ class Solution { } } - return dp[N-1][sum]; + return dp[0][sum]; } }; From 8a8c11e6c27bbf541242331e5d190a7b23599fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:03:35 +0530 Subject: [PATCH 1414/3167] Create README - LeetHub --- 0322-coin-change/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0322-coin-change/README.md diff --git a/0322-coin-change/README.md b/0322-coin-change/README.md new file mode 100644 index 00000000..6aa78759 --- /dev/null +++ b/0322-coin-change/README.md @@ -0,0 +1,35 @@ +

322. Coin Change

Medium


You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

+ +

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

+ +

You may assume that you have an infinite number of each kind of coin.

+ +

 

+

Example 1:

+ +
Input: coins = [1,2,5], amount = 11
+Output: 3
+Explanation: 11 = 5 + 5 + 1
+
+ +

Example 2:

+ +
Input: coins = [2], amount = 3
+Output: -1
+
+ +

Example 3:

+ +
Input: coins = [1], amount = 0
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= coins.length <= 12
  • +
  • 1 <= coins[i] <= 231 - 1
  • +
  • 0 <= amount <= 104
  • +
+
\ No newline at end of file From 80b486f8ecfefe1974a8f24e4e67daf85e34f03f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:03:35 +0530 Subject: [PATCH 1415/3167] Attach NOTES - LeetHub --- 0322-coin-change/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0322-coin-change/NOTES.md diff --git a/0322-coin-change/NOTES.md b/0322-coin-change/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0322-coin-change/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 756b2e12bd92aefed3497ded32e54d2c4e5595aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:03:39 +0530 Subject: [PATCH 1416/3167] Time: 89 ms (72.39%), Space: 49.9 MB (10.31%) - LeetHub --- 0322-coin-change/0322-coin-change.cpp | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0322-coin-change/0322-coin-change.cpp diff --git a/0322-coin-change/0322-coin-change.cpp b/0322-coin-change/0322-coin-change.cpp new file mode 100644 index 00000000..ba24fccf --- /dev/null +++ b/0322-coin-change/0322-coin-change.cpp @@ -0,0 +1,40 @@ +class Solution { + +private: + long long int helper(int idx, int n, vector& coins, int amount, vector>& dp) + { + if(idx == n-1) + { + if(amount % coins[idx] == 0) + return amount/coins[idx]; + else + return 1e9; + } + + if(dp[idx][amount] != -1) + return dp[idx][amount]; + + int notTake = helper(idx+1, n, coins, amount, dp); + + int take = INT_MAX; + + if(coins[idx] <= amount) + { + take = 1 + helper(idx, n, coins, amount - coins[idx], dp); + } + + return dp[idx][amount] = min(take, notTake); + } +public: + int coinChange(vector& coins, int amount) { + + int n = coins.size(); + + vector> dp(n+1, vector(amount+1, -1)); + + int ans = helper(0, n, coins, amount, dp); + + return (ans >= 1e9 ? -1 : ans); + + } +}; \ No newline at end of file From 031f8e4df0f7aed716906c9b26a2d3706a07e132 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:17:53 +0530 Subject: [PATCH 1417/3167] Attach NOTES - LeetHub From 5ba15c4e964bc8e7f4b4f1222bc885c2af285496 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:17:57 +0530 Subject: [PATCH 1418/3167] Time: 114 ms (52.08%), Space: 49.3 MB (12.28%) - LeetHub --- 0322-coin-change/0322-coin-change.cpp | 49 +++++++++++---------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/0322-coin-change/0322-coin-change.cpp b/0322-coin-change/0322-coin-change.cpp index ba24fccf..c7f09f57 100644 --- a/0322-coin-change/0322-coin-change.cpp +++ b/0322-coin-change/0322-coin-change.cpp @@ -1,40 +1,31 @@ class Solution { - -private: - long long int helper(int idx, int n, vector& coins, int amount, vector>& dp) - { - if(idx == n-1) - { - if(amount % coins[idx] == 0) - return amount/coins[idx]; - else - return 1e9; - } - - if(dp[idx][amount] != -1) - return dp[idx][amount]; - - int notTake = helper(idx+1, n, coins, amount, dp); - - int take = INT_MAX; - - if(coins[idx] <= amount) - { - take = 1 + helper(idx, n, coins, amount - coins[idx], dp); - } - - return dp[idx][amount] = min(take, notTake); - } public: int coinChange(vector& coins, int amount) { int n = coins.size(); - vector> dp(n+1, vector(amount+1, -1)); + vector> dp(n+1, vector(amount+1,0)); - int ans = helper(0, n, coins, amount, dp); + for(int i = 0; i <= amount; ++i) + { + dp[n-1][i] = (i % coins[n-1] == 0 ? i/coins[n-1] : 1e9); + } - return (ans >= 1e9 ? -1 : ans); + for(int i = n-2; i >= 0; --i) + { + for(int j = 0; j <= amount; ++j) + { + int notTake = dp[i+1][j]; + + int take = INT_MAX; + + if(coins[i] <= j) + take = 1 + dp[i][j-coins[i]]; + + dp[i][j] = min(take, notTake); + } + } + return (dp[0][amount] >= 1e9 ? -1 : dp[0][amount]); } }; \ No newline at end of file From fc184d0258d77a5603628b28f2bece4e72a6bd36 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:25:51 +0530 Subject: [PATCH 1419/3167] Attach NOTES - LeetHub From 577fea12b8ed05bedf1af990463b4d820e7ca3c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:25:54 +0530 Subject: [PATCH 1420/3167] Time: 75 ms (83.92%), Space: 18.6 MB (45.17%) - LeetHub --- 0322-coin-change/0322-coin-change.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/0322-coin-change/0322-coin-change.cpp b/0322-coin-change/0322-coin-change.cpp index c7f09f57..851c27a3 100644 --- a/0322-coin-change/0322-coin-change.cpp +++ b/0322-coin-change/0322-coin-change.cpp @@ -4,28 +4,29 @@ class Solution { int n = coins.size(); - vector> dp(n+1, vector(amount+1,0)); + vector prev(amount+1,0), curr(amount+1, 0); for(int i = 0; i <= amount; ++i) { - dp[n-1][i] = (i % coins[n-1] == 0 ? i/coins[n-1] : 1e9); + prev[i] = (i % coins[n-1] == 0 ? i/coins[n-1] : 1e9); } for(int i = n-2; i >= 0; --i) { for(int j = 0; j <= amount; ++j) { - int notTake = dp[i+1][j]; + int notTake = prev[j]; int take = INT_MAX; if(coins[i] <= j) - take = 1 + dp[i][j-coins[i]]; + take = 1 + curr[j-coins[i]]; - dp[i][j] = min(take, notTake); + curr[j] = min(take, notTake); } + prev = curr; } - return (dp[0][amount] >= 1e9 ? -1 : dp[0][amount]); + return (prev[amount] >= 1e9 ? -1 : prev[amount]); } }; \ No newline at end of file From 873f48980a94b13436e1c5d10327e1f45b4e6af1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:29:56 +0530 Subject: [PATCH 1421/3167] Create README - LeetHub --- 0518-coin-change-ii/README.md | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0518-coin-change-ii/README.md diff --git a/0518-coin-change-ii/README.md b/0518-coin-change-ii/README.md new file mode 100644 index 00000000..25495b07 --- /dev/null +++ b/0518-coin-change-ii/README.md @@ -0,0 +1,43 @@ +

518. Coin Change II

Medium


You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

+ +

Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.

+ +

You may assume that you have an infinite number of each kind of coin.

+ +

The answer is guaranteed to fit into a signed 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: amount = 5, coins = [1,2,5]
+Output: 4
+Explanation: there are four ways to make up the amount:
+5=5
+5=2+2+1
+5=2+1+1+1
+5=1+1+1+1+1
+
+ +

Example 2:

+ +
Input: amount = 3, coins = [2]
+Output: 0
+Explanation: the amount of 3 cannot be made up just with coins of 2.
+
+ +

Example 3:

+ +
Input: amount = 10, coins = [10]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= coins.length <= 300
  • +
  • 1 <= coins[i] <= 5000
  • +
  • All the values of coins are unique.
  • +
  • 0 <= amount <= 5000
  • +
+
\ No newline at end of file From 60b3fc658a54232b180e66e7a78d4f63974e6db8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:29:56 +0530 Subject: [PATCH 1422/3167] Attach NOTES - LeetHub --- 0518-coin-change-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0518-coin-change-ii/NOTES.md diff --git a/0518-coin-change-ii/NOTES.md b/0518-coin-change-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0518-coin-change-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 577b04050b10298f5e5c35f97ed6b6aad8b83492 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:30:00 +0530 Subject: [PATCH 1423/3167] Time: 18 ms (65.61%), Space: 7.3 MB (83.23%) - LeetHub --- 0518-coin-change-ii/0518-coin-change-ii.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0518-coin-change-ii/0518-coin-change-ii.cpp diff --git a/0518-coin-change-ii/0518-coin-change-ii.cpp b/0518-coin-change-ii/0518-coin-change-ii.cpp new file mode 100644 index 00000000..62a8898c --- /dev/null +++ b/0518-coin-change-ii/0518-coin-change-ii.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int change(int amount, vector& coins) { + + int n = coins.size(); + + vector prev(amount+1, 0), curr(amount+1, 0); + + for(int i = 0; i <= amount; ++i) + { + prev[i] = (i % coins[n-1] == 0 ? 1 : 0); + } + + for(int i = n-2; i >= 0; --i) + { + for(int j = 0; j <= amount; ++j) + { + int notTake = prev[j]; + + int take = 0; + + if(coins[i] <= j) + { + take = curr[j-coins[i]]; + } + + curr[j] = take + notTake; + } + prev = curr; + } + + return prev[amount]; + + } +}; \ No newline at end of file From 1a61310c261a0724b97fe41147a4bbb790d3170e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 20:14:36 +0530 Subject: [PATCH 1424/3167] Update 0012. Integer to Roman.cpp --- 0012. Integer to Roman.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0012. Integer to Roman.cpp b/0012. Integer to Roman.cpp index b5361ead..062c4d79 100644 --- a/0012. Integer to Roman.cpp +++ b/0012. Integer to Roman.cpp @@ -6,7 +6,7 @@ class Solution { string ans = ""; while(num >= 1000) - {l + { ans += "M"; num = num - 1000; } @@ -78,4 +78,4 @@ class Solution { // cout< Date: Fri, 11 Aug 2023 20:31:04 +0530 Subject: [PATCH 1425/3167] Attach NOTES - LeetHub From e6a8dd01403cc4be5099399111e2ccf3bcabbbdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 20:31:08 +0530 Subject: [PATCH 1426/3167] Time: 2599 ms (5.00%), Space: 409.4 MB (5.14%) - LeetHub --- 0015-3sum/0015-3sum.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/0015-3sum/0015-3sum.cpp b/0015-3sum/0015-3sum.cpp index d58d496e..d01c77e8 100644 --- a/0015-3sum/0015-3sum.cpp +++ b/0015-3sum/0015-3sum.cpp @@ -2,12 +2,11 @@ class Solution { public: vector> threeSum(vector& nums) { - set> s; - vector> ans; + int n = nums.size(); - sort(nums.begin(),nums.end()); + vector> ans; - int n = nums.size(); + sort(nums.begin(), nums.end()); for(int i = 0; i < n; ++i) { @@ -16,20 +15,26 @@ class Solution { while(j < k) { int sum = nums[i] + nums[j] + nums[k]; - if((sum) == 0) + + if(sum == 0) { - s.insert({nums[i],nums[j],nums[k]}); - ++j,--k; + ans.push_back({nums[i],nums[j],nums[k]}); + ++j, --k; } - else if(sum < 0) - ++j; - else + else if(sum > 0) + { --k; + } + else + { + ++j; + } } } - for(auto itr : s) - ans.push_back(itr); + sort(ans.begin(), ans.end()); + + ans.erase(unique(ans.begin(),ans.end()), ans.end()); return ans; } From d3d44795ca00ab0a9b9cec9e0faf6bda7b902e76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 21:13:09 +0530 Subject: [PATCH 1427/3167] Attach NOTES - LeetHub From e7e62eb5bc9208ae5c9ca656a6008ed35235f3aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 11 Aug 2023 21:13:13 +0530 Subject: [PATCH 1428/3167] Time: 651 ms (12.07%), Space: 106.8 MB (10.07%) - LeetHub --- 0018-4sum/0018-4sum.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/0018-4sum/0018-4sum.cpp b/0018-4sum/0018-4sum.cpp index a3d337f6..67139e92 100644 --- a/0018-4sum/0018-4sum.cpp +++ b/0018-4sum/0018-4sum.cpp @@ -1,16 +1,15 @@ class Solution { public: + + const int mod = 1e9 + 7; + vector> fourSum(vector& nums, int target) { int n = nums.size(); - int mod = 1e9 + 7; - vector> ans; - set> used; - - sort(begin(nums), end(nums)); + sort(nums.begin(), nums.end()); for(int i = 0; i < n; ++i) { @@ -20,24 +19,29 @@ class Solution { while(k < l) { - long long sum = (((((nums[i] % mod) + nums[j])%mod + nums[k])% mod + nums[l])% mod) % mod; + long long sum = (((((nums[i] % mod) + nums[j])% mod + nums[k])%mod + nums[l])%mod)%mod; if(sum == target) { - used.insert({nums[i], nums[j], nums[k], nums[l]}); - ++k,--l; + ans.push_back({nums[i], nums[j], nums[k], nums[l]}); + ++k, --l; } - else if(sum < target) - ++k; - else + else if(sum > target) + { --l; + } + else + { + ++k; + } } } } - for(auto itr : used) - ans.push_back(itr); - + sort(ans.begin(), ans.end()); + + ans.erase(unique(ans.begin(), ans.end()), ans.end()); + return ans; } }; \ No newline at end of file From d16591e35b8ec371fdd165e5589b5cc447b5a6a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:00:19 +0530 Subject: [PATCH 1429/3167] Attach NOTES - LeetHub --- 0063-unique-paths-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0063-unique-paths-ii/NOTES.md diff --git a/0063-unique-paths-ii/NOTES.md b/0063-unique-paths-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0063-unique-paths-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b884369b2b6d92f0b16e8c4bac67d363ea1186c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:00:22 +0530 Subject: [PATCH 1430/3167] Time: 0 ms (100.00%), Space: 7.9 MB (25.52%) - LeetHub --- 0063-unique-paths-ii/0063-unique-paths-ii.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0063-unique-paths-ii/0063-unique-paths-ii.cpp diff --git a/0063-unique-paths-ii/0063-unique-paths-ii.cpp b/0063-unique-paths-ii/0063-unique-paths-ii.cpp new file mode 100644 index 00000000..fc8ce20b --- /dev/null +++ b/0063-unique-paths-ii/0063-unique-paths-ii.cpp @@ -0,0 +1,34 @@ +class Solution { + +private: + int helper(int i, int j, int n, int m, vector>& obstacleGrid, vector>& dp) + { + if(i == n-1 and j == m-1) + { + return obstacleGrid[i][j] == 0; + } + + if(i > n or j > m or i < n and j < m and obstacleGrid[i][j] == 1) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int right = helper(i, j+1, n, m, obstacleGrid, dp); + + int bottom = helper(i+1, j, n, m, obstacleGrid, dp); + + return dp[i][j] = right + bottom; + } + +public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + + int n = obstacleGrid.size(), m = obstacleGrid[0].size(); + + vector> dp(n+1, vector(m+1, -1)); + + return helper(0,0, n, m, obstacleGrid, dp); + + } +}; \ No newline at end of file From b8bc7a38e0fef67926fa039ddc555b5ff46dc484 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 12:13:28 +0530 Subject: [PATCH 1431/3167] Attach NOTES - LeetHub From b378929fb83c7611fdc2bb95d878ccba638fb6c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 12:13:31 +0530 Subject: [PATCH 1432/3167] Time: 66 ms (76.07%), Space: 26.3 MB (15.02%) - LeetHub --- ...count-square-submatrices-with-all-ones.cpp | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp index d32f05b9..1b0c79ae 100644 --- a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp +++ b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp @@ -1,20 +1,41 @@ class Solution { + +private: + int helper(int i, int j, int n, int m, vector>& matrix, vector>& dp) + { + if(i < 0 or j < 0) + return 0; + + if(matrix[i][j] == 0) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int topLeft = helper(i-1, j-1, n, m, matrix, dp); + int topRight = helper(i-1, j, n, m, matrix, dp); + int bottomLeft = helper(i,j-1, n, m, matrix, dp); + + return dp[i][j] = 1 + min({topLeft, topRight, bottomLeft}); + } + public: int countSquares(vector>& matrix) { int n = matrix.size(), m = matrix[0].size(); - int count = 0; + + vector> dp(n, vector(m, -1)); + + int squareSubmatrices = 0; + for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { - if(j > 0 and i > 0 and matrix[i][j] > 0) - matrix[i][j] = min({matrix[i-1][j],matrix[i][j-1],matrix[i-1][j-1]}) + 1; - count += matrix[i][j]; + squareSubmatrices += helper(i, j, n, m, matrix, dp); } } - return count; - + return squareSubmatrices; } }; \ No newline at end of file From ba0a4cfe8b3f8e757312d254782f2475a79a38b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:01:00 +0530 Subject: [PATCH 1433/3167] Attach NOTES - LeetHub From d741a0df5cb9189b96ebb3086269348e167de8da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:01:03 +0530 Subject: [PATCH 1434/3167] Time: 59 ms (83.09%), Space: 26.3 MB (15.02%) - LeetHub --- ...77-count-square-submatrices-with-all-ones.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp index 1b0c79ae..22a050dd 100644 --- a/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp +++ b/1277-count-square-submatrices-with-all-ones/1277-count-square-submatrices-with-all-ones.cpp @@ -24,7 +24,7 @@ class Solution { int n = matrix.size(), m = matrix[0].size(); - vector> dp(n, vector(m, -1)); + vector> dp(n, vector(m, 0)); int squareSubmatrices = 0; @@ -32,7 +32,19 @@ class Solution { { for(int j = 0; j < m; ++j) { - squareSubmatrices += helper(i, j, n, m, matrix, dp); + if(i == 0 or j == 0) + dp[i][j] = matrix[i][j]; + + if(i > 0 and j > 0 and matrix[i][j] != 0) + { + int leftDiag = dp[i-1][j-1]; + int left = dp[i][j-1]; + int top = dp[i-1][j]; + + dp[i][j] = min({leftDiag, left, top}) + 1; + } + + squareSubmatrices += dp[i][j]; } } From ee59ae7c5d7df7f6db11c211d0e919313da5111f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:52:03 +0530 Subject: [PATCH 1435/3167] Create README - LeetHub --- 0221-maximal-square/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0221-maximal-square/README.md diff --git a/0221-maximal-square/README.md b/0221-maximal-square/README.md new file mode 100644 index 00000000..433a2f97 --- /dev/null +++ b/0221-maximal-square/README.md @@ -0,0 +1,31 @@ +

221. Maximal Square

Medium


Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

+ +

 

+

Example 1:

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

Example 2:

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

Example 3:

+ +
Input: matrix = [["0"]]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 300
  • +
  • matrix[i][j] is '0' or '1'.
  • +
+
\ No newline at end of file From c07ba17f703ee380844bc97478bd9e08681e4898 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:52:03 +0530 Subject: [PATCH 1436/3167] Attach NOTES - LeetHub --- 0221-maximal-square/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0221-maximal-square/NOTES.md diff --git a/0221-maximal-square/NOTES.md b/0221-maximal-square/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0221-maximal-square/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6c8a5adf84f96663ea49b7326feae788c397cb85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:52:07 +0530 Subject: [PATCH 1437/3167] Time: 63 ms (95.72%), Space: 20.9 MB (69.41%) - LeetHub --- 0221-maximal-square/0221-maximal-square.cpp | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0221-maximal-square/0221-maximal-square.cpp diff --git a/0221-maximal-square/0221-maximal-square.cpp b/0221-maximal-square/0221-maximal-square.cpp new file mode 100644 index 00000000..8339a6dc --- /dev/null +++ b/0221-maximal-square/0221-maximal-square.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int maximalSquare(vector>& matrix) { + + int n = matrix.size(), m = matrix[0].size(); + + vector> dp(n, vector(m, 0)); + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(i == 0 or j == 0) + dp[i][j] = matrix[i][j] - 48; + + if(i > 0 and j > 0 and matrix[i][j] != '0') + { + int leftDiagonal = dp[i-1][j-1]; + int bottomLeft = dp[i][j-1]; + int topRight = dp[i-1][j]; + + dp[i][j] = min({leftDiagonal, bottomLeft, topRight}) + 1; + } + ans = max(ans,dp[i][j]); + } + } + + return ans * ans; + + } +}; \ No newline at end of file From 39e6bfa908bd6970446937ac52a57d7bd145123e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:54:04 +0530 Subject: [PATCH 1438/3167] Attach NOTES - LeetHub From 3318844403b825036d10796824a99da552c7bd8d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 13:54:07 +0530 Subject: [PATCH 1439/3167] Time: 63 ms (95.72%), Space: 20.9 MB (69.41%) - LeetHub From c3410820c8792afe19aa2c7dc0e1ab07a239d147 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:19:03 +0530 Subject: [PATCH 1440/3167] Attach NOTES - LeetHub From 31c4bfd473d9e177f7afb230793d4a4c7a075ace Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:19:06 +0530 Subject: [PATCH 1441/3167] Time: 72 ms (86.04%), Space: 18.7 MB (95.40%) - LeetHub --- 0221-maximal-square/0221-maximal-square.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/0221-maximal-square/0221-maximal-square.cpp b/0221-maximal-square/0221-maximal-square.cpp index 8339a6dc..81286ea2 100644 --- a/0221-maximal-square/0221-maximal-square.cpp +++ b/0221-maximal-square/0221-maximal-square.cpp @@ -4,7 +4,7 @@ class Solution { int n = matrix.size(), m = matrix[0].size(); - vector> dp(n, vector(m, 0)); + vector curr(m, 0), prev(m , 0); int ans = 0; @@ -13,18 +13,20 @@ class Solution { for(int j = 0; j < m; ++j) { if(i == 0 or j == 0) - dp[i][j] = matrix[i][j] - 48; + curr[j] = matrix[i][j] - 48; if(i > 0 and j > 0 and matrix[i][j] != '0') { - int leftDiagonal = dp[i-1][j-1]; - int bottomLeft = dp[i][j-1]; - int topRight = dp[i-1][j]; + int leftDiagonal = prev[j-1]; + int bottomLeft = curr[j-1]; + int topRight = prev[j]; - dp[i][j] = min({leftDiagonal, bottomLeft, topRight}) + 1; + curr[j] = min({leftDiagonal, bottomLeft, topRight}) + 1; } - ans = max(ans,dp[i][j]); + ans = max(ans,curr[j]); } + fill(prev.begin(),prev.end(),0); + swap(prev,curr); } return ans * ans; From b40c0bc6c53730097e7319dfa208fab95bce9d51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 13 Aug 2023 19:34:39 +0530 Subject: [PATCH 1442/3167] Create README - LeetHub --- Nth Fibonacci Number - GFG/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Nth Fibonacci Number - GFG/README.md diff --git a/Nth Fibonacci Number - GFG/README.md b/Nth Fibonacci Number - GFG/README.md new file mode 100644 index 00000000..e4c42957 --- /dev/null +++ b/Nth Fibonacci Number - GFG/README.md @@ -0,0 +1,15 @@ +# Nth Fibonacci Number +## Easy +

Given a positive integer n, find the nth fibonacci number. Since the answer can be very large, return the answer modulo 1000000007.

Example 1:

+
Input: 
n = 2 +Output: 
1  +Explanation:
1 is the 2nd number of fibonacci series.
+
+

Example 2:

+
Input: 
n = 5 +Output: 
5 +Explanation:
5 is the 5th number of fibonacci series. +
+

Your Task:  
You dont need to read input or print anything. Complete the function nthFibonacci() which takes n as input parameter and returns nth fibonacci number.

+

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

+

Constraints:
1<= n <=105

\ No newline at end of file From 2113a97256c55f5de4f4ed697e5aca9bf9c163fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 13 Aug 2023 19:34:39 +0530 Subject: [PATCH 1443/3167] Added solution - LeetHub --- .../nth-fibonacci-number.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp diff --git a/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp b/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp new file mode 100644 index 00000000..c09fbdad --- /dev/null +++ b/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp @@ -0,0 +1,49 @@ +//{ Driver Code Starts +// Initial Template for C++ +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ +class Solution { + public: + + const int mod = 1e9+7; + + int nthFibonacci(int n){ + // code here + + int first = 0, second = 1; + + if(n == 1) + return 0; + if(n == 2) + return 1; + + int ans = second; + + for(int i = 2; i <= n; ++i) + { + ans = (first + second)%mod; + first = second; + second = ans; + } + + return ans % mod; + } +}; + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + Solution ob; + cout << ob.nthFibonacci(n) << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 38e623b8e628af9110da1ca4d32b2ef431000e18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 00:02:42 +0530 Subject: [PATCH 1444/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2369-check-if-there-is-a-valid-partition-for-the-array/README.md diff --git a/2369-check-if-there-is-a-valid-partition-for-the-array/README.md b/2369-check-if-there-is-a-valid-partition-for-the-array/README.md new file mode 100644 index 00000000..9f45a5c9 --- /dev/null +++ b/2369-check-if-there-is-a-valid-partition-for-the-array/README.md @@ -0,0 +1,36 @@ +

2369. Check if There is a Valid Partition For The Array

Medium


You are given a 0-indexed integer array nums. You have to partition the array into one or more contiguous subarrays.

+ +

We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions:

+ +
    +
  1. The subarray consists of exactly 2 equal elements. For example, the subarray [2,2] is good.
  2. +
  3. The subarray consists of exactly 3 equal elements. For example, the subarray [4,4,4] is good.
  4. +
  5. The subarray consists of exactly 3 consecutive increasing elements, that is, the difference between adjacent elements is 1. For example, the subarray [3,4,5] is good, but the subarray [1,3,5] is not.
  6. +
+ +

Return true if the array has at least one valid partition. Otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: nums = [4,4,4,5,6]
+Output: true
+Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].
+This partition is valid, so we return true.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,2]
+Output: false
+Explanation: There is no valid partition for this array.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
+
\ No newline at end of file From 6b8004f7df632d53632f470d3f471dfb88b03498 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 00:02:42 +0530 Subject: [PATCH 1445/3167] Attach NOTES - LeetHub --- 2369-check-if-there-is-a-valid-partition-for-the-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2369-check-if-there-is-a-valid-partition-for-the-array/NOTES.md diff --git a/2369-check-if-there-is-a-valid-partition-for-the-array/NOTES.md b/2369-check-if-there-is-a-valid-partition-for-the-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2369-check-if-there-is-a-valid-partition-for-the-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6caaddce4381fc4a34e5035f5556e5de50243772 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 00:02:46 +0530 Subject: [PATCH 1446/3167] Time: 108 ms (99.38%), Space: 93 MB (15.08%) - LeetHub --- ...ere-is-a-valid-partition-for-the-array.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2369-check-if-there-is-a-valid-partition-for-the-array/2369-check-if-there-is-a-valid-partition-for-the-array.cpp diff --git a/2369-check-if-there-is-a-valid-partition-for-the-array/2369-check-if-there-is-a-valid-partition-for-the-array.cpp b/2369-check-if-there-is-a-valid-partition-for-the-array/2369-check-if-there-is-a-valid-partition-for-the-array.cpp new file mode 100644 index 00000000..e7d7a837 --- /dev/null +++ b/2369-check-if-there-is-a-valid-partition-for-the-array/2369-check-if-there-is-a-valid-partition-for-the-array.cpp @@ -0,0 +1,44 @@ +class Solution { + +private: + + bool helper(int idx, int n, vector& nums, vector& dp) + { + if(idx == n) + return true; + + if(dp[idx] != -1) + return dp[idx]; + + if(idx + 1 < n and nums[idx] == nums[idx+1]) + { + if(helper(idx+2, n, nums, dp)) + return true; + + if(idx + 2 < n and nums[idx] == nums[idx+2]) + { + if(helper(idx+3, n, nums , dp)) + return true; + } + } + + if(idx + 2 < n and nums[idx] == nums[idx+1] - 1 and nums[idx] == nums[idx+2] - 2) + { + if(helper(idx + 3, n, nums, dp)) + return true; + } + + return dp[idx] = false; + } + +public: + bool validPartition(vector& nums) { + + int n = nums.size(); + + vector dp(n+1, -1); + + return helper(0, n, nums, dp); + + } +}; \ No newline at end of file From 7422781439f7bf30060b6cb2f086ab9453f8c9c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 00:03:54 +0530 Subject: [PATCH 1447/3167] Attach NOTES - LeetHub From 988a15a949167dc16f2ff0c66034b1be69b93dad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 00:03:58 +0530 Subject: [PATCH 1448/3167] Time: 108 ms (99.38%), Space: 93 MB (15.08%) - LeetHub From a9d394c27c1bac0f00b7a40d0a72f6477141e76e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:09:10 +0530 Subject: [PATCH 1449/3167] Create README - LeetHub --- .../README.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0215-kth-largest-element-in-an-array/README.md diff --git a/0215-kth-largest-element-in-an-array/README.md b/0215-kth-largest-element-in-an-array/README.md new file mode 100644 index 00000000..1735cc58 --- /dev/null +++ b/0215-kth-largest-element-in-an-array/README.md @@ -0,0 +1,22 @@ +

215. Kth Largest Element in an Array

Medium


Given an integer array nums and an integer k, return the kth largest element in the array.

+ +

Note that it is the kth largest element in the sorted order, not the kth distinct element.

+ +

Can you solve it without sorting?

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= k <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+
\ No newline at end of file From 3d498284cc2b7d9befe052e1a274b69b0d3f8773 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:09:10 +0530 Subject: [PATCH 1450/3167] Attach NOTES - LeetHub --- 0215-kth-largest-element-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0215-kth-largest-element-in-an-array/NOTES.md diff --git a/0215-kth-largest-element-in-an-array/NOTES.md b/0215-kth-largest-element-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0215-kth-largest-element-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1b438a3fed7d29d3d8401922ab0f230f85503716 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:09:15 +0530 Subject: [PATCH 1451/3167] Time: 79 ms (92.90%), Space: 47.4 MB (33.60%) - LeetHub --- .../0215-kth-largest-element-in-an-array.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.cpp diff --git a/0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.cpp b/0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.cpp new file mode 100644 index 00000000..e7e13534 --- /dev/null +++ b/0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int findKthLargest(vector& nums, int k) { + + priority_queue pq(nums.begin(),nums.end()); + + while(k-- > 1) + { + pq.pop(); + } + + return pq.top(); + + } +}; \ No newline at end of file From a55c6f2e09c29fa8fd7d95a9451b70f066884857 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:10:31 +0530 Subject: [PATCH 1452/3167] Attach NOTES - LeetHub From e528e7b8d514aabf6cf76d48669fda9a051019fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:10:34 +0530 Subject: [PATCH 1453/3167] Time: 79 ms (92.90%), Space: 47.4 MB (33.60%) - LeetHub From 504540718bd28b6c4f1d52726f44408b5bac1051 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:10:35 +0530 Subject: [PATCH 1454/3167] Attach NOTES - LeetHub From cb0a15de24b99a4cbef100007023660f941703a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:10:38 +0530 Subject: [PATCH 1455/3167] Time: 79 ms (92.90%), Space: 47.4 MB (33.60%) - LeetHub From ebcd826bbdb640b30c8751ce3ddc7ebbb0bb859d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 21:08:23 +0530 Subject: [PATCH 1456/3167] Create README - LeetHub From 4734e28ec89319574ab32afd7290ba4b6a0bbfe8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 21:08:24 +0530 Subject: [PATCH 1457/3167] Added solution - LeetHub --- .../nth-fibonacci-number.cpp | 67 ++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp b/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp index c09fbdad..c97e79a5 100644 --- a/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp +++ b/Nth Fibonacci Number - GFG/nth-fibonacci-number.cpp @@ -6,30 +6,67 @@ using namespace std; // } Driver Code Ends // User function Template for C++ class Solution { - public: - const int mod = 1e9+7; + private: - int nthFibonacci(int n){ - // code here + const int mod = 1e9 + 7; + + vector> matrixMultiplication(vector> mat1, vector> mat2) + { + vector> ans(2, vector(2)); + + for(int i = 0; i < 2; ++i) + { + for(int j = 0; j < 2; ++j) + { + for(int k =0 ; k < 2 ; ++k) + { + ans[i][j] += (long long)(mat1[i][k]%mod * mat2[k][j]%mod)%mod; + ans[i][j] %= mod; + } + } + } - int first = 0, second = 1; + return ans; + } + + + vector> expo(vector>& mat, int n) + { if(n == 1) - return 0; - if(n == 2) - return 1; - - int ans = second; + return mat; + + vector> temp = mat; + - for(int i = 2; i <= n; ++i) + while(n > 0) { - ans = (first + second)%mod; - first = second; - second = ans; + if(n & 1) + { + temp = matrixMultiplication(mat, temp); + } + + mat = matrixMultiplication(mat, mat); + + n >>= 1; } - return ans % mod; + return temp; + } + + public: + int nthFibonacci(int n){ + // code here + + if(n == 0) + return 0; + + vector> mat = {{1,1},{1,0}}; + + vector> ans = expo(mat, n-1); + + return ans[0][1]%mod; } }; From acb06959816887a17204420f3089ea789a48da8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 21:58:36 +0530 Subject: [PATCH 1458/3167] Create README - LeetHub --- Non Repeating Numbers - GFG/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Non Repeating Numbers - GFG/README.md diff --git a/Non Repeating Numbers - GFG/README.md b/Non Repeating Numbers - GFG/README.md new file mode 100644 index 00000000..a369ff14 --- /dev/null +++ b/Non Repeating Numbers - GFG/README.md @@ -0,0 +1,23 @@ +# Non Repeating Numbers +## Medium +

Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.

+

Example 1:

+
Input: 
+N = 2
+arr[] = {1, 2, 3, 2, 1, 4}
+Output:
+3 4 
+Explanation:
+3 and 4 occur exactly once.
+
+

Example 2:

+
Input:
+N = 1
+arr[] = {2, 1, 3, 2}
+Output:
+1 3
+Explanation:
+1 3 occur exactly once.
+

Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.

+

Expected Time Complexity: O(N)
Expected Space Complexity: O(1)

+

Constraints:
1 <= length of array <= 10

1 <= Elements in array <= 5 * 106

\ No newline at end of file From 1d1e9ff5230ac85cadc83dd73e04be356c3ec6a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 14 Aug 2023 21:58:37 +0530 Subject: [PATCH 1459/3167] Added solution - LeetHub --- .../non-repeating-numbers.cpp | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Non Repeating Numbers - GFG/non-repeating-numbers.cpp diff --git a/Non Repeating Numbers - GFG/non-repeating-numbers.cpp b/Non Repeating Numbers - GFG/non-repeating-numbers.cpp new file mode 100644 index 00000000..838fd3ed --- /dev/null +++ b/Non Repeating Numbers - GFG/non-repeating-numbers.cpp @@ -0,0 +1,54 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution +{ +public: + vector singleNumber(vector nums) + { + // Code here. + + unordered_map mp; + + for(auto itr : nums) + { + ++mp[itr]; + } + + vector ans; + + for(auto itr : mp) + { + if(itr.second == 1) + ans.push_back(itr.first); + } + + if(ans[0] > ans[1]) + swap(ans[0], ans[1]); + + return ans; + } +}; + +//{ Driver Code Starts. +int main(){ + int T; + cin >> T; + while(T--) + { + int n; + cin >> n; + vector v(2 * n + 2); + for(int i = 0; i < 2 * n + 2; i++) + cin >> v[i]; + Solution ob; + vector ans = ob.singleNumber(v); + for(auto i: ans) + cout << i << " "; + cout << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From 204e839d1b46dcb8e0fcd8af6740ee2e3d26fb49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:54:09 +0530 Subject: [PATCH 1460/3167] Create README - LeetHub --- 0086-partition-list/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0086-partition-list/README.md diff --git a/0086-partition-list/README.md b/0086-partition-list/README.md new file mode 100644 index 00000000..372a542c --- /dev/null +++ b/0086-partition-list/README.md @@ -0,0 +1,26 @@ +

86. Partition List

Medium


Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

+ +

You should preserve the original relative order of the nodes in each of the two partitions.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [0, 200].
  • +
  • -100 <= Node.val <= 100
  • +
  • -200 <= x <= 200
  • +
+
\ No newline at end of file From 4ea02ee5fe8aa58fff36f2192111c51f729d24ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:54:13 +0530 Subject: [PATCH 1461/3167] Time: 9 ms (12.92%), Space: 10.3 MB (21.22%) - LeetHub --- 0086-partition-list/0086-partition-list.cpp | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0086-partition-list/0086-partition-list.cpp diff --git a/0086-partition-list/0086-partition-list.cpp b/0086-partition-list/0086-partition-list.cpp new file mode 100644 index 00000000..65d56f38 --- /dev/null +++ b/0086-partition-list/0086-partition-list.cpp @@ -0,0 +1,42 @@ +/** + * 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* partition(ListNode* head, int x) { + + ListNode* leftHead = new ListNode(0); + ListNode* rightHead = new ListNode(0); + + ListNode* dummy = head, *left = leftHead, *right = rightHead; + + while(dummy) + { + if(dummy->val < x) + { + left->next = new ListNode(dummy->val); + left = left->next; + } + else + { + right->next = new ListNode(dummy->val); + right = right->next; + } + + dummy = dummy->next; + } + + right->next = nullptr; + + left->next = rightHead->next; + + return leftHead->next; + } +}; \ No newline at end of file From 67fbc657a2bd1aed4ad3916d90b2c8096824d799 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:55:12 +0530 Subject: [PATCH 1462/3167] Attach NOTES - LeetHub --- 0086-partition-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0086-partition-list/NOTES.md diff --git a/0086-partition-list/NOTES.md b/0086-partition-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0086-partition-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 25a0816bcc090fa92c009f2125bd04383cb668be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:55:15 +0530 Subject: [PATCH 1463/3167] Time: 9 ms (12.92%), Space: 10.3 MB (21.22%) - LeetHub From 85293c4d92033ea42ee4a8ae24b4e719b1efff6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:56:17 +0530 Subject: [PATCH 1464/3167] Attach NOTES - LeetHub From d0a1bb8837d7086bb4a96734d5492be7bdf1556d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:56:20 +0530 Subject: [PATCH 1465/3167] Time: 8 ms (38.42%), Space: 10.2 MB (48.14%) - LeetHub --- 0086-partition-list/0086-partition-list.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0086-partition-list/0086-partition-list.cpp b/0086-partition-list/0086-partition-list.cpp index 65d56f38..b8f5f9ac 100644 --- a/0086-partition-list/0086-partition-list.cpp +++ b/0086-partition-list/0086-partition-list.cpp @@ -21,12 +21,12 @@ class Solution { { if(dummy->val < x) { - left->next = new ListNode(dummy->val); + left->next = dummy; left = left->next; } else { - right->next = new ListNode(dummy->val); + right->next = dummy; right = right->next; } From 74991d5058c69480c65cab2b719ff29c17cee81b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 18:58:01 +0530 Subject: [PATCH 1466/3167] Create README - LeetHub --- Flip Bits - GFG/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Flip Bits - GFG/README.md diff --git a/Flip Bits - GFG/README.md b/Flip Bits - GFG/README.md new file mode 100644 index 00000000..760ef31a --- /dev/null +++ b/Flip Bits - GFG/README.md @@ -0,0 +1,28 @@ +# Flip Bits +## Easy +

Given an array A[] consisting of 0’s and 1’s. A flip operation is one in which you turn 1 into 0 and a 0 into 1. You have to do at most one “Flip” operation of any subarray. Formally, select a range (l, r) in the array A[], such that (0 ≤ l ≤ r < n) holds and flip the elements in this range to get the maximum ones in the final array. You can possibly make zero operations to get the answer.

+

Example 1:

+
Input:
+N = 5
+A[] = {1, 0, 0, 1, 0} 
+Output:
+4
+Explanation:
+We can perform a flip operation in the range [1,2]
+After flip operation array is : [ 1 1 1 1 0 ]
+Count of one after fliping is : 4
+[Note: the subarray marked in bold is the flipped subarray]
+

Example 2:

+
Input:
+N = 7
+A[] = {1, 0, 0, 1, 0, 0, 1}
+Output:
+6
+Explanation:
+We can perform a flip operation in the range [1,5]
+After flip operation array is : [ 1 1 1 0 1 1 1]
+Count of one after fliping is : 6
+[Note: the subarray marked in bold is the flipped subarray]
+

Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxOnes() which takes the array A[] and its size N as inputs and returns the maximum number of 1's you can have in the array after atmost one flip operation.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 ≤ N ≤ 106
0 ≤ A[i] ≤ 1

\ No newline at end of file From d5095ca1389a7250a373b84405f463e309459155 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 15 Aug 2023 18:58:01 +0530 Subject: [PATCH 1467/3167] Added solution - LeetHub --- Flip Bits - GFG/flip-bits.cpp | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Flip Bits - GFG/flip-bits.cpp diff --git a/Flip Bits - GFG/flip-bits.cpp b/Flip Bits - GFG/flip-bits.cpp new file mode 100644 index 00000000..e9a780b5 --- /dev/null +++ b/Flip Bits - GFG/flip-bits.cpp @@ -0,0 +1,63 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + + +class Solution{ + public: + int maxOnes(int a[], int n) + { + // Your code goes here + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + if(a[i] == 1) + { + a[i] = -1; + ++cnt; + } + else + { + a[i] = 1; + } + } + + int ans = 0, sum = 0; + + for(int i = 0; i < n; ++i) + { + sum += a[i]; + + ans = max(ans, sum); + + if(sum < 0) + sum = 0; + } + + return ans + cnt; + } +}; + + +//{ Driver Code Starts. +int main() +{ + int t; cin>>t; + while(t--) + { + int n; + cin>>n; + int a[n+5]; + for(int i=0;i>a[i]; + Solution ob; + cout<< ob.maxOnes(a, n) < Date: Wed, 16 Aug 2023 07:41:57 +0530 Subject: [PATCH 1468/3167] Create README - LeetHub --- 0239-sliding-window-maximum/README.md | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0239-sliding-window-maximum/README.md diff --git a/0239-sliding-window-maximum/README.md b/0239-sliding-window-maximum/README.md new file mode 100644 index 00000000..e987f895 --- /dev/null +++ b/0239-sliding-window-maximum/README.md @@ -0,0 +1,35 @@ +

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 From 3636806e49bc81795ada77a355a44b68ad8c218f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 16 Aug 2023 07:42:00 +0530 Subject: [PATCH 1469/3167] Time: 633 ms (9.94%), Space: 202.4 MB (8.11%) - LeetHub --- .../0239-sliding-window-maximum.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0239-sliding-window-maximum/0239-sliding-window-maximum.cpp diff --git a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp new file mode 100644 index 00000000..7a77e980 --- /dev/null +++ b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + vector maxSlidingWindow(vector& nums, int k) { + + + int i = 0, j = 0; + + int n = nums.size(); + + unordered_map mp; + + set st; + + vector ans; + + while(j < n) + { + ++mp[nums[j]]; + st.insert(nums[j]); + + if(j - i + 1 == k) + { + ans.push_back(*st.rbegin()); + + --mp[nums[i]]; + + if(mp[nums[i]] == 0) + { + mp.erase(nums[i]); + st.erase(nums[i]); + } + + ++i; + } + + ++j; + } + + return ans; + + } +}; \ No newline at end of file From 8f8a8093feb79f4ddbc335c63fcd35c780e0f5ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 16 Aug 2023 07:52:12 +0530 Subject: [PATCH 1470/3167] Attach NOTES - LeetHub --- 0239-sliding-window-maximum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0239-sliding-window-maximum/NOTES.md diff --git a/0239-sliding-window-maximum/NOTES.md b/0239-sliding-window-maximum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0239-sliding-window-maximum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f49f9ccafb2520d67a7e0e556c5dcfd2d44150b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 16 Aug 2023 07:52:15 +0530 Subject: [PATCH 1471/3167] Time: 552 ms (11.37%), Space: 194.3 MB (8.29%) - LeetHub --- .../0239-sliding-window-maximum.cpp | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp index 7a77e980..93aab41f 100644 --- a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp +++ b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp @@ -2,32 +2,28 @@ class Solution { public: vector maxSlidingWindow(vector& nums, int k) { - int i = 0, j = 0; int n = nums.size(); - unordered_map mp; - - set st; - vector ans; + list l; + while(j < n) { - ++mp[nums[j]]; - st.insert(nums[j]); + while(!l.empty() and nums[l.back()] <= nums[j]) + l.pop_back(); + + l.push_back(j); if(j - i + 1 == k) { - ans.push_back(*st.rbegin()); - - --mp[nums[i]]; + ans.push_back(nums[l.front()]); - if(mp[nums[i]] == 0) + if(l.front() == i) { - mp.erase(nums[i]); - st.erase(nums[i]); + l.pop_front(); } ++i; From 69b65f15e5d5baa910a63e6acd1311bb775ad188 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 16 Aug 2023 07:52:23 +0530 Subject: [PATCH 1472/3167] Attach NOTES - LeetHub From ccf6d1e4570e05ac1e4a5382ae5ea8bc6c191d02 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 16 Aug 2023 07:52:26 +0530 Subject: [PATCH 1473/3167] Time: 552 ms (11.37%), Space: 194.3 MB (8.29%) - LeetHub From 8e2cee113a3dac33bbf59045af486025d9a298e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 17 Aug 2023 07:43:38 +0530 Subject: [PATCH 1474/3167] Attach NOTES - LeetHub From 234da65d3acd6aeb3da93076e5e1d78dd6385742 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 17 Aug 2023 07:43:41 +0530 Subject: [PATCH 1475/3167] Time: 65 ms (87.06%), Space: 33.7 MB (59.96%) - LeetHub --- 0542-01-matrix/0542-01-matrix.cpp | 41 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/0542-01-matrix/0542-01-matrix.cpp b/0542-01-matrix/0542-01-matrix.cpp index a5b241f6..5adf57ae 100644 --- a/0542-01-matrix/0542-01-matrix.cpp +++ b/0542-01-matrix/0542-01-matrix.cpp @@ -2,13 +2,13 @@ class Solution { public: vector> updateMatrix(vector>& mat) { - int n = mat.size(); - int m = mat[0].size(); + int n = mat.size(), m = mat[0].size(); - vector> visited(n,vector(m,false)); - vector> dist(n,vector(m,0)); + queue>> q; - queue, int> > q; + vector> dist(n, vector(m)); + + vector> visited(n, vector(m, false)); for(int i = 0; i < n; ++i) { @@ -16,34 +16,39 @@ class Solution { { if(mat[i][j] == 0) { - q.push({{i,j}, 0}); + dist[i][j] = 0; + q.push({0,{i,j}}); visited[i][j] = true; } } } + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1, 0}; + while(!q.empty()) { - int currRow = q.front().first.first; - int currCol = q.front().first.second; - int steps = q.front().second; - + auto curr = q.front(); q.pop(); - dist[currRow][currCol] = steps; + int curDist = curr.first; + int x = curr.second.first; + int y = curr.second.second; - vector rows = {-1, 0, 0, +1}; - vector cols = {0, -1, +1, 0}; + if(mat[x][y] == 1) + { + dist[x][y] = curDist; + } for(int i = 0; i < 4; ++i) { - int newRow = currRow + rows[i]; - int newCol = currCol + cols[i]; + int newX = dx[i] + x; + int newY = dy[i] + y; - if(newRow >= 0 and newRow < n and newCol >= 0 and newCol < m and !visited[newRow][newCol]) + if(newX >= 0 and newY >= 0 and newX < n and newY < m and !visited[newX][newY]) { - q.push({{newRow,newCol}, steps + 1}); - visited[newRow][newCol] = true; + q.push({curDist + 1, {newX, newY}}); + visited[newX][newY] = true; } } } From 81345a9dea7dadd1f2c4695e4bf22395a999cfa7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 17 Aug 2023 07:47:27 +0530 Subject: [PATCH 1476/3167] Attach NOTES - LeetHub From fafb82045ef2ff14f655d6c9f59ff0e8667a18a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 17 Aug 2023 07:47:30 +0530 Subject: [PATCH 1477/3167] Time: 65 ms (87.06%), Space: 33.7 MB (59.96%) - LeetHub From 22429951cc8acc9373f86eb8ade27bf9f2259ab8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:37:13 +0530 Subject: [PATCH 1478/3167] Create README - LeetHub --- Next Smallest Palindrome - GFG/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Next Smallest Palindrome - GFG/README.md diff --git a/Next Smallest Palindrome - GFG/README.md b/Next Smallest Palindrome - GFG/README.md new file mode 100644 index 00000000..0212fed6 --- /dev/null +++ b/Next Smallest Palindrome - GFG/README.md @@ -0,0 +1,22 @@ +# Next Smallest Palindrome +## Hard +

Given a number, in the form of an array Num[] of size N containing digits from 1 to 9(inclusive). The task is to find the next smallest palindrome strictly larger than the given number.

+

Example 1:

+
Input:
+N = 11
+Num[] = {9, 4, 1, 8, 7, 9, 7, 8, 3, 2, 2}
+Output: 9 4 1 8 8 0 8 8 1 4 9
+Explanation: Next smallest palindrome is
+9 4 1 8 8 0 8 8 1 4 9
+
+

Example 2:

+
Input:
+N = 5
+Num[] = {2, 3, 5, 4, 5}
+Output: 2 3 6 3 2
+Explanation: Next smallest palindrome is
+2 3 6 3 2
+
+

Your Task:
Complete the function generateNextPalindrome() which takes an array num, and a single integer n, as input parameters and returns an array of integers denoting the answer. You don't to print answer or take inputs.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 105
1 <= Num[i] <= 9

\ No newline at end of file From 8563ff401e654914b99bb9a463f32619580d54ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:37:14 +0530 Subject: [PATCH 1479/3167] Added solution - LeetHub --- .../next-smallest-palindrome.cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Next Smallest Palindrome - GFG/next-smallest-palindrome.cpp diff --git a/Next Smallest Palindrome - GFG/next-smallest-palindrome.cpp b/Next Smallest Palindrome - GFG/next-smallest-palindrome.cpp new file mode 100644 index 00000000..8637a17b --- /dev/null +++ b/Next Smallest Palindrome - GFG/next-smallest-palindrome.cpp @@ -0,0 +1,70 @@ +//{ Driver Code Starts +#include + +using namespace std; + +// } Driver Code Ends +//User function template for C++ +class Solution{ +public: + vector generateNextPalindrome(int arr[], int n) { + // code here + + vector num(arr,arr+n); + int mid = (n+1)/2; + bool flag = true; + for(int i=mid;inum[n-i-1]) flag = true; + break; + } + if(flag) + { + int carry = 1; + for(int i=mid-1;i>=0 && carry==1;i--) + { + num[i] = (num[i]==9? 0:num[i]+carry--); + } + if(carry){ + vector res; + res.push_back(1); + for(int i=1;i=0;i--) + { + num[n-i-1]=num[i]; + } + return num; + } + +}; + +//{ Driver Code Starts. + + +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + int num[n]; + for (int i = 0; i < n; i++) { + cin >> num[i]; + } + Solution ob; + auto ans = ob.generateNextPalindrome(num, n); + for (auto x : ans) { + cout << x << " "; + } + cout << "\n"; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 61b3f68ddf806f82bdee02ee0fcfcc87df28d955 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:37:18 +0530 Subject: [PATCH 1480/3167] Create README - LeetHub --- Leaders in an array - GFG/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Leaders in an array - GFG/README.md diff --git a/Leaders in an array - GFG/README.md b/Leaders in an array - GFG/README.md new file mode 100644 index 00000000..8288b29c --- /dev/null +++ b/Leaders in an array - GFG/README.md @@ -0,0 +1,23 @@ +# Leaders in an array +## Easy +

Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. 

+

Example 1:

+
Input:
+n = 6
+A[] = {16,17,4,3,5,2}
+Output: 17 5 2
+Explanation: The first leader is 17 
+as it is greater than all the elements
+to its right.  Similarly, the next 
+leader is 5. The right most element 
+is always a leader so it is also 
+included.
+
+

Example 2:

+
Input:
+n = 5
+A[] = {1,2,3,4,0}
+Output: 4 0
Explanation: 0 is the rightmost element
and 4 is the only element which is greater
than all the elements to its right.
+

Your Task:
You don't need to read input or print anything. The task is to complete the function leader() which takes array A and n as input parameters and returns an array of leaders in order of their appearance.

+

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

+

Constraints:
1 <= n <= 107
0 <= Ai <= 107

\ No newline at end of file From 24973d91239a32d02559390c08c5a00fa4763c4a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:37:19 +0530 Subject: [PATCH 1481/3167] Added solution - LeetHub --- .../leaders-in-an-array.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Leaders in an array - GFG/leaders-in-an-array.cpp diff --git a/Leaders in an array - GFG/leaders-in-an-array.cpp b/Leaders in an array - GFG/leaders-in-an-array.cpp new file mode 100644 index 00000000..a24c600e --- /dev/null +++ b/Leaders in an array - GFG/leaders-in-an-array.cpp @@ -0,0 +1,65 @@ +//{ Driver Code Starts +// C++ program to remove recurring digits from +// a given number +#include +using namespace std; + + +// } Driver Code Ends + + +class Solution{ + //Function to find the leaders in the array. + public: + vector leaders(int a[], int n){ + // Code here + + int maxi = a[n-1]; + + vector v; + + for(int i = n-1; i >= 0; --i) + { + if(a[i] >= maxi) + v.push_back(a[i]); + maxi = max(a[i], maxi); + } + + reverse(v.begin(), v.end()); + + return v; + } +}; + +//{ Driver Code Starts. + +int main() +{ + long long t; + cin >> t;//testcases + while (t--) + { + long long n; + cin >> n;//total size of array + + int a[n]; + + //inserting elements in the array + for(long long i =0;i> a[i]; + } + Solution obj; + //calling leaders() function + vector v = obj.leaders(a, n); + + //printing elements of the vector + for(auto it = v.begin();it!=v.end();it++){ + cout << *it << " "; + } + + cout << endl; + + } +} + +// } Driver Code Ends \ No newline at end of file From 70d32861fa5972936ae54d960fb0ca8e17433274 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:12:07 +0530 Subject: [PATCH 1482/3167] Create README - LeetHub --- 1615-maximal-network-rank/README.md | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1615-maximal-network-rank/README.md diff --git a/1615-maximal-network-rank/README.md b/1615-maximal-network-rank/README.md new file mode 100644 index 00000000..b1a5b3f2 --- /dev/null +++ b/1615-maximal-network-rank/README.md @@ -0,0 +1,46 @@ +

1615. Maximal Network Rank

Medium


There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi.

+ +

The network rank of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once.

+ +

The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities.

+ +

Given the integer n and the array roads, return the maximal network rank of the entire infrastructure.

+ +

 

+

Example 1:

+ +

+ +
Input: n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
+Output: 4
+Explanation: The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.
+
+ +

Example 2:

+ +

+ +
Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
+Output: 5
+Explanation: There are 5 roads that are connected to cities 1 or 2.
+
+ +

Example 3:

+ +
Input: n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
+Output: 5
+Explanation: The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 100
  • +
  • 0 <= roads.length <= n * (n - 1) / 2
  • +
  • roads[i].length == 2
  • +
  • 0 <= ai, bi <= n-1
  • +
  • ai != bi
  • +
  • Each pair of cities has at most one road connecting them.
  • +
+
\ No newline at end of file From fb36a1065aa053b97b180b37e2260af46a96cae5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:12:07 +0530 Subject: [PATCH 1483/3167] Attach NOTES - LeetHub --- 1615-maximal-network-rank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1615-maximal-network-rank/NOTES.md diff --git a/1615-maximal-network-rank/NOTES.md b/1615-maximal-network-rank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1615-maximal-network-rank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f974c77aece64b2587cba7bb5ac976b5672546f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:12:10 +0530 Subject: [PATCH 1484/3167] Time: 65 ms (89.29%), Space: 30.5 MB (84.72%) - LeetHub --- .../1615-maximal-network-rank.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1615-maximal-network-rank/1615-maximal-network-rank.cpp diff --git a/1615-maximal-network-rank/1615-maximal-network-rank.cpp b/1615-maximal-network-rank/1615-maximal-network-rank.cpp new file mode 100644 index 00000000..71276eb4 --- /dev/null +++ b/1615-maximal-network-rank/1615-maximal-network-rank.cpp @@ -0,0 +1,47 @@ +class Solution { + +public: + int maximalNetworkRank(int n, vector>& roads) { + + vector adj[n+1]; + + for(auto& itr : roads) + { + int u = itr[0]; + int v = itr[1]; + + adj[u].push_back(v); + adj[v].push_back(u); + } + + int maximalNetworkRank = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(i == j) + continue; + + int nodeA = i; + int nodeB = j; + + int currRank = adj[nodeA].size() + adj[nodeB].size(); + + for(auto& itr : adj[nodeA]) + { + if(itr == nodeB) + { + --currRank; + break; + } + } + + maximalNetworkRank = max(maximalNetworkRank, currRank); + } + } + + return maximalNetworkRank; + + } +}; \ No newline at end of file From 643b4d76d24f3ab2c2e19c3f265e8b141d6d45fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:15:50 +0530 Subject: [PATCH 1485/3167] Attach NOTES - LeetHub From fb8bb664e730985a398ad848a7d522c3c40a2560 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:15:53 +0530 Subject: [PATCH 1486/3167] Time: 186 ms (9.06%), Space: 55 MB (5.02%) - LeetHub --- .../1615-maximal-network-rank.cpp | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/1615-maximal-network-rank/1615-maximal-network-rank.cpp b/1615-maximal-network-rank/1615-maximal-network-rank.cpp index 71276eb4..60a10050 100644 --- a/1615-maximal-network-rank/1615-maximal-network-rank.cpp +++ b/1615-maximal-network-rank/1615-maximal-network-rank.cpp @@ -4,6 +4,8 @@ class Solution { int maximalNetworkRank(int n, vector>& roads) { vector adj[n+1]; + + map, int> mp; for(auto& itr : roads) { @@ -12,6 +14,8 @@ class Solution { adj[u].push_back(v); adj[v].push_back(u); + + ++mp[{u,v}]; } int maximalNetworkRank = 0; @@ -28,14 +32,19 @@ class Solution { int currRank = adj[nodeA].size() + adj[nodeB].size(); - for(auto& itr : adj[nodeA]) - { - if(itr == nodeB) - { - --currRank; - break; - } - } + // not intitutive + + // for(auto& itr : adj[nodeA]) + // { + // if(itr == nodeB) + // { + // --currRank; + // break; + // } + // } + + if(mp[{nodeA, nodeB}] or mp[{nodeB, nodeA}]) + --currRank; maximalNetworkRank = max(maximalNetworkRank, currRank); } From 900c2c7422fdb0ff1cc42be41efcc0d4d21b505a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:17:22 +0530 Subject: [PATCH 1487/3167] Attach NOTES - LeetHub From 4cc5fcee7d46711bd1d875da801fed17f9b124cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:17:25 +0530 Subject: [PATCH 1488/3167] Time: 186 ms (9.06%), Space: 55 MB (5.02%) - LeetHub From 23eb663265e7a73b65780d9c30bcdc2f93b70b91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:17:54 +0530 Subject: [PATCH 1489/3167] Attach NOTES - LeetHub From d206151e1fc81f0c1ba39c5e160f4b642d886e0d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:17:57 +0530 Subject: [PATCH 1490/3167] Time: 186 ms (9.06%), Space: 55 MB (5.02%) - LeetHub From 27063e1c5d48899ceb33437c379da342d8404d60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 19 Aug 2023 09:20:03 +0530 Subject: [PATCH 1491/3167] Create README - LeetHub --- Subarray with given sum - GFG/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Subarray with given sum - GFG/README.md diff --git a/Subarray with given sum - GFG/README.md b/Subarray with given sum - GFG/README.md new file mode 100644 index 00000000..9e4c595a --- /dev/null +++ b/Subarray with given sum - GFG/README.md @@ -0,0 +1,25 @@ +# Subarray with given sum +## Easy +

Given an unsorted array A of size N that contains only positive integers, find a continuous sub-array that adds to a given number and return the left and right index(1-based indexing) of that subarray.

+

In case of multiple subarrays, return the subarray indexes which come first on moving from left to right.

+

Note:- You have to return an ArrayList consisting of two elements left and right. In case no such subarray exists return an array consisting of element -1.

+

Example 1:

+
Input:
+N = 5, S = 12
+A[] = {1,2,3,7,5}
+Output: 2 4
+Explanation: The sum of elements 
+from 2nd position to 4th position 
+is 12.
+

Example 2:

+
Input:
+N = 10, S = 15
+A[] = {1,2,3,4,5,6,7,8,9,10}
+Output: 1 5
+Explanation: The sum of elements 
+from 1st position to 5th position
+is 15.
+
+

Your Task:
You don't need to read input or print anything. The task is to complete the function subarraySum() which takes arr, N, and S as input parameters and returns an ArrayList containing the starting and ending positions of the first such occurring subarray from the left where sum equals to S. The two indexes in the array should be according to 1-based indexing. If no such subarray is found, return an array consisting of only one element that is -1.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 105

1 <= Ai <= 109
0<= S <= 109

\ No newline at end of file From 665ee2b9ea4b6a283b04f26df2f38e94403f722a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 19 Aug 2023 09:20:04 +0530 Subject: [PATCH 1492/3167] Added solution - LeetHub --- .../subarray-with-given-sum.cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Subarray with given sum - GFG/subarray-with-given-sum.cpp diff --git a/Subarray with given sum - GFG/subarray-with-given-sum.cpp b/Subarray with given sum - GFG/subarray-with-given-sum.cpp new file mode 100644 index 00000000..a93489a9 --- /dev/null +++ b/Subarray with given sum - GFG/subarray-with-given-sum.cpp @@ -0,0 +1,70 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +class Solution +{ + public: + //Function to find a continuous sub-array which adds up to a given number. + vector subarraySum(vectorarr, int n, long long s) + { + // Your code here4 + + if(s == 0) + return {-1}; + + int i = 0, j = 0; + + int sum = 0; + + while(j < n) + { + sum += arr[j]; + + while(sum >= s) + { + if(sum == s) + { + return {i+1, j+1}; + } + sum -= arr[i++]; + } + ++j; + } + + return {-1}; + } +}; + +//{ Driver Code Starts. + +int main() + { + int t; + cin>>t; + while(t--) + { + int n; + long long s; + cin>>n>>s; + vectorarr(n); + // int arr[n]; + const int mx = 1e9; + for(int i=0;i>arr[i]; + } + Solution ob; + vectorres; + res = ob.subarraySum(arr, n, s); + + for(int i = 0;i Date: Sat, 19 Aug 2023 23:40:01 +0530 Subject: [PATCH 1493/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/NOTES.md diff --git a/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/NOTES.md b/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 32afd07aa67158141d616324484c861e21ea0c16 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 19 Aug 2023 23:40:05 +0530 Subject: [PATCH 1494/3167] Time: 169 ms (56.31%), Space: 26.6 MB (34.74%) - LeetHub --- ...ritical-edges-in-minimum-spanning-tree.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp diff --git a/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp b/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp new file mode 100644 index 00000000..1aa2f1da --- /dev/null +++ b/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + vector> findCriticalAndPseudoCriticalEdges(int n, vector>& edges) { + vector>> g(n); + for (auto &e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].emplace_back(v, w); + g[v].emplace_back(u, w); + } + vector> ans(2); + for (int i = 0; i < edges.size(); i++) { + auto &e = edges[i]; + int u = e[0], v = e[1], w = e[2]; + int m = minimax(g, u, v); + if (w < m) { + ans[0].push_back(i); + } else if (w == m) { + ans[1].push_back(i); + } + } + return ans; + } + + int minimax(vector>> &g, int src, int dst) { + vector dist(g.size(), 1e9); + priority_queue> pq; + pq.emplace(dist[src] = 0, src); + while (!pq.empty()) { + auto [pri, u] = pq.top(); pq.pop(); + if (-pri > dist[u]) { + continue; + } + for (auto &[v, w] : g[u]) { + if (u == src && v == dst) { + continue; + } + if (dist[v] > max(dist[u], w)) { + dist[v] = max(dist[u], w); + pq.emplace(-dist[v], v); + } + } + } + return dist[dst]; + } +}; \ No newline at end of file From 4180861c250a99ed46081ca3051e42f5fcc90f22 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:44:57 +0530 Subject: [PATCH 1495/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1203-sort-items-by-groups-respecting-dependencies/README.md diff --git a/1203-sort-items-by-groups-respecting-dependencies/README.md b/1203-sort-items-by-groups-respecting-dependencies/README.md new file mode 100644 index 00000000..3bf64a00 --- /dev/null +++ b/1203-sort-items-by-groups-respecting-dependencies/README.md @@ -0,0 +1,40 @@ +

1203. Sort Items by Groups Respecting Dependencies

Hard


There are n items each belonging to zero or one of m groups where group[i] is the group that the i-th item belongs to and it's equal to -1 if the i-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.

+ +

Return a sorted list of the items such that:

+ +
    +
  • The items that belong to the same group are next to each other in the sorted list.
  • +
  • There are some relations between these items where beforeItems[i] is a list containing all the items that should come before the i-th item in the sorted array (to the left of the i-th item).
  • +
+ +

Return any solution if there is more than one solution and return an empty list if there is no solution.

+ +

 

+

Example 1:

+ +

+ +
Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]]
+Output: [6,3,4,1,5,2,0,7]
+
+ +

Example 2:

+ +
Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]]
+Output: []
+Explanation: This is the same as example 1 except that 4 needs to be before 6 in the sorted list.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= m <= n <= 3 * 104
  • +
  • group.length == beforeItems.length == n
  • +
  • -1 <= group[i] <= m - 1
  • +
  • 0 <= beforeItems[i].length <= n - 1
  • +
  • 0 <= beforeItems[i][j] <= n - 1
  • +
  • i != beforeItems[i][j]
  • +
  • beforeItems[i] does not contain duplicates elements.
  • +
+
\ No newline at end of file From ec7ce97d2e4191ae58e93d4494a6b6b655870075 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:44:57 +0530 Subject: [PATCH 1496/3167] Attach NOTES - LeetHub --- 1203-sort-items-by-groups-respecting-dependencies/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1203-sort-items-by-groups-respecting-dependencies/NOTES.md diff --git a/1203-sort-items-by-groups-respecting-dependencies/NOTES.md b/1203-sort-items-by-groups-respecting-dependencies/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1203-sort-items-by-groups-respecting-dependencies/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 93d8741af4343b08f040c39b591c931dd0493477 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:45:00 +0530 Subject: [PATCH 1497/3167] Time: 90 ms (81.63%), Space: 47.3 MB (72.01%) - LeetHub --- ...tems-by-groups-respecting-dependencies.cpp | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp diff --git a/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp b/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp new file mode 100644 index 00000000..91d6f95c --- /dev/null +++ b/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp @@ -0,0 +1,97 @@ +class Solution { + +private: + vector topologicalSort(vector adj[] , int n) + { + vector indegree(n+1, 0); + + for(int i =0 ; i < n; ++i) + { + for(auto& itr : adj[i]) + ++indegree[itr]; + } + + queue q; + + vector topo; + + for(int i = 0 ; i < n; ++i) + { + if(indegree[i] == 0) + q.push(i); + } + + while(!q.empty()) + { + int currNode = q.front(); + q.pop(); + + topo.push_back(currNode); + + for(auto& itr : adj[currNode]) + { + --indegree[itr]; + + if(indegree[itr] == 0) + q.push(itr); + } + } + + return topo; + } + +public: + vector sortItems(int n, int m, vector& group, vector>& beforeItems) { + + // JAI SHREE RAM + + for(int i = 0; i < n; ++i) + { + if(group[i] == -1) + group[i] = m++; + } + + vector itemsGraph[n+1], groupGraph[m+1]; + + for(int i = 0; i < n; ++i) + { + for(auto& prev : beforeItems[i]) + { + itemsGraph[prev].push_back(i); + + if(group[i] != group[prev]) + { + int prevItemGroup = group[prev]; + int currItemGroup = group[i]; + + groupGraph[prevItemGroup].push_back(currItemGroup); + } + } + } + + vector itemsTopologicalOrdering = topologicalSort(itemsGraph, n); + + vector groupsTopologicalOrdering = topologicalSort(groupGraph, m); + + unordered_map> groupToItemOrder; + + for(auto& item : itemsTopologicalOrdering) + { + int itemGroup = group[item]; + + groupToItemOrder[itemGroup].push_back(item); + + } + + vector ans; + + for(auto& group : groupsTopologicalOrdering) + { + ans.insert(ans.end(), groupToItemOrder[group].begin(), groupToItemOrder[group].end()); + } + + if(ans.size() == n) + return ans; + return {}; + } +}; \ No newline at end of file From 30afc66ada2e1e95e14b0fda5b45298987070ba4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:50:30 +0530 Subject: [PATCH 1498/3167] Attach NOTES - LeetHub From 5d223756cb8d369a6ab3d739dc729bbf8fdf9cf3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:50:33 +0530 Subject: [PATCH 1499/3167] Time: 75 ms (93.59%), Space: 46.9 MB (73.18%) - LeetHub --- .../1203-sort-items-by-groups-respecting-dependencies.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp b/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp index 91d6f95c..3bf145c4 100644 --- a/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp +++ b/1203-sort-items-by-groups-respecting-dependencies/1203-sort-items-by-groups-respecting-dependencies.cpp @@ -37,7 +37,9 @@ class Solution { } } - return topo; + if(topo.size() == n) + return topo; + return {}; } public: @@ -90,8 +92,6 @@ class Solution { ans.insert(ans.end(), groupToItemOrder[group].begin(), groupToItemOrder[group].end()); } - if(ans.size() == n) - return ans; - return {}; + return ans; } }; \ No newline at end of file From 824aa9a7ce4f089a77a773ab886db20823ed27b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 09:48:09 +0530 Subject: [PATCH 1500/3167] Create README - LeetHub --- Surround the 1's - GFG/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Surround the 1's - GFG/README.md diff --git a/Surround the 1's - GFG/README.md b/Surround the 1's - GFG/README.md new file mode 100644 index 00000000..a1f2c5e9 --- /dev/null +++ b/Surround the 1's - GFG/README.md @@ -0,0 +1,19 @@ +# Surround the 1's +## Easy +

Given a matrix of order nxm, composed of only 0's and 1's, find the number of 1's in the matrix that are surrounded by an even number (>0) of 0's. The surrounding of a cell in the matrix is defined as the elements above, below, on left, on right as well as the 4 diagonal elements around the cell of the matrix. Hence, the surrounding of any matrix elements is composed of 8 elements. Find the number of such 1's.

+

Example 1:

+
Input: 
matrix = {{1, 0, 0},
{1, 1, 0}, + {0, 1, 0}} +Output:
1 +Explanation:
1 that occurs in the 1st row and 1st column, has 3 surrounding elements 0,1 and 1. The occurrence of zero is odd.
1 that occurs in 2nd row and 1st column has 5 surrounding elements 1,0,1,1 and 0. The occurrence of zero is even.
1 that occurs in 2nd row and 2nd column has 8 surrounding elements. The occurrence of 0 is odd.
Similarly, for the 1 that occurs in 3rd row and 2nd column, the occurrence of zero in it's 5 surrounding elements is odd. +Hence, the output is 1.
+
+

Example 2:

+
Input: 
matrix = {{1}} +Output:
0 +Explanation:
There is only 1 element in the matrix. Hence, it has no surroundings, so it's count for even 0's is 0 for the whole matrix.
0 is even but we want occurrence of a zero in the surrounding at least once. +Hence, output is 0.
+
+

Your Task:
You don't need to read or print anything. Your task is to complete the function Count() which takes the matrix as input parameter and returns the number of 1's which are surrounded by even number of 0's.

+

Expected Time Complexity: O(n * m)
Expected Space Complexity: O(1)

+

Constraints:
1 <= n, m <= 103

\ No newline at end of file From fc3dad8f0b2d6c6c3124f834d43c6f2c287aa9b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 09:48:10 +0530 Subject: [PATCH 1501/3167] Added solution - LeetHub --- Surround the 1's - GFG/surround-the-1s.cpp | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Surround the 1's - GFG/surround-the-1s.cpp diff --git a/Surround the 1's - GFG/surround-the-1s.cpp b/Surround the 1's - GFG/surround-the-1s.cpp new file mode 100644 index 00000000..aa61aa98 --- /dev/null +++ b/Surround the 1's - GFG/surround-the-1s.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts + +#include +using namespace std; + +// } Driver Code Ends + +class Solution { + +private: + int helper(int i, int j, int n, int m, vector>& matrix) + { + vector dx = {-1, -1, -1, 0, +1, +1, +1, 0}; + vector dy = {-1, 0, +1, +1, +1, 0, -1, -1}; + + int ans = 0; + + for(int k = 0; k < 8; ++k) + { + int newX = dx[k] + i; + int newY = dy[k] + j; + + if(newX >= 0 and newY >= 0 and newX < n and newY < m and !matrix[newX][newY]) + { + ++ans; + } + } + + return ans; + } +public: + int Count(vector >& matrix) { + // Code here + + int n = matrix.size(), m = matrix[0].size(); + + int res = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + + if(matrix[i][j] == 1) + { + int cnt = helper(i, j, n, m, matrix); + + if(cnt > 0 and cnt % 2 == 0) + ++res; + } + } + } + + return res; + } +}; + +//{ Driver Code Starts. +int main(){ + int tc; + cin >> tc; + while(tc--){ + int n, m; + cin >> n >> m; + vector> matrix(n, vector(m,0)); + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + cin >> matrix[i][j]; + } + } + Solution ob; + int ans = ob.Count(matrix); + cout << ans <<"\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From fbe7f943a1b9aa4c98eb39c18e9daa139ed98b2b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:11:48 +0530 Subject: [PATCH 1502/3167] Create README - LeetHub --- 0459-repeated-substring-pattern/README.md | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0459-repeated-substring-pattern/README.md diff --git a/0459-repeated-substring-pattern/README.md b/0459-repeated-substring-pattern/README.md new file mode 100644 index 00000000..9ed1b971 --- /dev/null +++ b/0459-repeated-substring-pattern/README.md @@ -0,0 +1,31 @@ +

459. Repeated Substring Pattern

Easy


Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

+ +

 

+

Example 1:

+ +
Input: s = "abab"
+Output: true
+Explanation: It is the substring "ab" twice.
+
+ +

Example 2:

+ +
Input: s = "aba"
+Output: false
+
+ +

Example 3:

+ +
Input: s = "abcabcabcabc"
+Output: true
+Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From 0f0e5b0a6d77be40939088a83177c3253a62f9c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:11:52 +0530 Subject: [PATCH 1503/3167] Time: 260 ms (13.45%), Space: 418.2 MB (8.66%) - LeetHub --- .../0459-repeated-substring-pattern.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0459-repeated-substring-pattern/0459-repeated-substring-pattern.cpp diff --git a/0459-repeated-substring-pattern/0459-repeated-substring-pattern.cpp b/0459-repeated-substring-pattern/0459-repeated-substring-pattern.cpp new file mode 100644 index 00000000..26cf484f --- /dev/null +++ b/0459-repeated-substring-pattern/0459-repeated-substring-pattern.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + bool repeatedSubstringPattern(string s) { + + int n = s.size(); + + if(n == 1) + return false; + + for(int i = 0; i < n/2; ++i) + { + int currLength = i+1; + + string currString = s.substr(0, i+1); + string newString = currString; + + if(n % currLength == 0) + { + while(newString.size() < s.size()) + { + newString += currString; + } + } + + if(newString == s) + return true; + } + + return false; + } +}; \ No newline at end of file From 3118e6ded1249ec5270d5e9380fd88572daf0973 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:14:49 +0530 Subject: [PATCH 1504/3167] Attach NOTES - LeetHub --- 0459-repeated-substring-pattern/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0459-repeated-substring-pattern/NOTES.md diff --git a/0459-repeated-substring-pattern/NOTES.md b/0459-repeated-substring-pattern/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0459-repeated-substring-pattern/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d84a8ce4180dba3a12147a80933fc8f488fa803d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:14:53 +0530 Subject: [PATCH 1505/3167] Time: 260 ms (13.45%), Space: 418.2 MB (8.66%) - LeetHub From acdfdd3af08990cba7e613a4f31dd335498fdf4b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 22:49:27 +0530 Subject: [PATCH 1506/3167] Attach NOTES - LeetHub From b8ebea4643ea7f26b3b0bcf02c97d1b03ff1ff29 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 21 Aug 2023 22:49:30 +0530 Subject: [PATCH 1507/3167] Time: 119 ms (46.23%), Space: 24 MB (18.77%) - LeetHub --- ...ritical-edges-in-minimum-spanning-tree.cpp | 172 ++++++++++++++---- 1 file changed, 137 insertions(+), 35 deletions(-) diff --git a/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp b/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp index 1aa2f1da..4071369d 100644 --- a/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp +++ b/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp @@ -1,45 +1,147 @@ class Solution { public: - vector> findCriticalAndPseudoCriticalEdges(int n, vector>& edges) { - vector>> g(n); - for (auto &e : edges) { - int u = e[0], v = e[1], w = e[2]; - g[u].emplace_back(v, w); - g[v].emplace_back(u, w); - } - vector> ans(2); - for (int i = 0; i < edges.size(); i++) { - auto &e = edges[i]; - int u = e[0], v = e[1], w = e[2]; - int m = minimax(g, u, v); - if (w < m) { - ans[0].push_back(i); - } else if (w == m) { - ans[1].push_back(i); + + int N; + + class DSU{ + + public : + vector parent, size, rank; + + 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; } - } - return ans; - } - - int minimax(vector>> &g, int src, int dst) { - vector dist(g.size(), 1e9); - priority_queue> pq; - pq.emplace(dist[src] = 0, src); - while (!pq.empty()) { - auto [pri, u] = pq.top(); pq.pop(); - if (-pri > dist[u]) { - continue; + + int findParent(int u) + { + if(u == parent[u]) + return u; + + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int nodeX = findParent(u); + int nodeY = findParent(v); + + if(nodeX == nodeY) + return; + + if(rank[nodeX] < rank[nodeY]) + { + parent[nodeX] = nodeY; + } + else if(rank[nodeY] < rank[nodeX]) + { + parent[nodeY] = nodeX; + } + else + { + parent[nodeY] = nodeX; + ++rank[nodeY]; + } } - for (auto &[v, w] : g[u]) { - if (u == src && v == dst) { - continue; + + void unionBySize(int u, int v) + { + int nodeX = findParent(u); + int nodeY = findParent(v); + + if(nodeX == nodeY) + return; + + if(size[nodeX] < size[nodeY]) + { + parent[nodeX] = nodeY; + size[nodeY] += size[nodeX]; } - if (dist[v] > max(dist[u], w)) { - dist[v] = max(dist[u], w); - pq.emplace(-dist[v], v); + else + { + parent[nodeY] = nodeX; + size[nodeY] += size[nodeX]; } } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } + }; + + int kruskals(vector>& edges, int skip_edge, int add_edge) + { + DSU dsu(N); + + int edges_cnt = 0; + + int mstWeight = 0; + + if(add_edge != -1) + { + dsu.unionBySize(edges[add_edge][0], edges[add_edge][1]); + mstWeight += edges[add_edge][2]; + ++edges_cnt; + } + + for(int i = 0; i < edges.size(); ++i) + { + if(i == skip_edge) + continue; + + int u = edges[i][0]; + int v = edges[i][1]; + int wt = edges[i][2]; + + if(dsu.isSame(u, v)) + { + continue; + } + else + { + dsu.unionBySize(u, v); + mstWeight += wt; + ++edges_cnt; + } + } + + if(edges_cnt != N-1) + return INT_MAX; + + return mstWeight; + } + + vector> findCriticalAndPseudoCriticalEdges(int n, vector>& edges) { + + // JAI SHREE RAM + + N = n; + + for(int i = 0; i < edges.size(); ++i) + edges[i].push_back(i); + + sort(edges.begin(), edges.end(), [&](const auto &a, const auto &b){ + return a[2] < b[2]; + }); + + int mstWeight = kruskals(edges, -1, -1); + + vector critical, pseudoCritical; + + for(int i = 0; i < edges.size(); ++i) + { + if(kruskals(edges, i, -1) > mstWeight) + critical.push_back(edges[i].back()); + else if(kruskals(edges, -1, i) == mstWeight) + pseudoCritical.push_back(edges[i].back()); } - return dist[dst]; + + return {critical, pseudoCritical}; } }; \ No newline at end of file From f3dca24fa06e79c62eadf038590be61c68118967 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 22 Aug 2023 07:38:35 +0530 Subject: [PATCH 1508/3167] Create README - LeetHub --- 0168-excel-sheet-column-title/README.md | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0168-excel-sheet-column-title/README.md diff --git a/0168-excel-sheet-column-title/README.md b/0168-excel-sheet-column-title/README.md new file mode 100644 index 00000000..bc366e52 --- /dev/null +++ b/0168-excel-sheet-column-title/README.md @@ -0,0 +1,40 @@ +

168. Excel Sheet Column Title

Easy


Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

+ +

For example:

+ +
A -> 1
+B -> 2
+C -> 3
+...
+Z -> 26
+AA -> 27
+AB -> 28 
+...
+
+ +

 

+

Example 1:

+ +
Input: columnNumber = 1
+Output: "A"
+
+ +

Example 2:

+ +
Input: columnNumber = 28
+Output: "AB"
+
+ +

Example 3:

+ +
Input: columnNumber = 701
+Output: "ZY"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= columnNumber <= 231 - 1
  • +
+
\ No newline at end of file From 47c8fe97991874de8d360ca546953d07c8c3ed7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 22 Aug 2023 07:38:39 +0530 Subject: [PATCH 1509/3167] Time: 2 ms (49.78%), Space: 6 MB (37.93%) - LeetHub --- .../0168-excel-sheet-column-title.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0168-excel-sheet-column-title/0168-excel-sheet-column-title.cpp diff --git a/0168-excel-sheet-column-title/0168-excel-sheet-column-title.cpp b/0168-excel-sheet-column-title/0168-excel-sheet-column-title.cpp new file mode 100644 index 00000000..513b5d7b --- /dev/null +++ b/0168-excel-sheet-column-title/0168-excel-sheet-column-title.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + string convertToTitle(int columnNumber) { + + string str; + + while(columnNumber > 0) + { + char last = 'A' + (columnNumber - 1) % 26; + + str = last + str; + + columnNumber = (columnNumber - 1) / 26; + } + + return str; + + } +}; \ No newline at end of file From fdb2c684a09b783ef50d62ed8b06ae635a7fbea8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 22 Aug 2023 07:43:44 +0530 Subject: [PATCH 1510/3167] Attach NOTES - LeetHub --- 0168-excel-sheet-column-title/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0168-excel-sheet-column-title/NOTES.md diff --git a/0168-excel-sheet-column-title/NOTES.md b/0168-excel-sheet-column-title/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0168-excel-sheet-column-title/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 42add8cf6a4094b12b3081e008f6673fd4540b73 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:17:47 +0530 Subject: [PATCH 1511/3167] Create README - LeetHub --- 0767-reorganize-string/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0767-reorganize-string/README.md diff --git a/0767-reorganize-string/README.md b/0767-reorganize-string/README.md new file mode 100644 index 00000000..5ae1df96 --- /dev/null +++ b/0767-reorganize-string/README.md @@ -0,0 +1,20 @@ +

767. Reorganize String

Medium


Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.

+ +

Return any possible rearrangement of s or return "" if not possible.

+ +

 

+

Example 1:

+
Input: s = "aab"
+Output: "aba"
+

Example 2:

+
Input: s = "aaab"
+Output: ""
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 500
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From ad2d2d57bae73dbece4f664d026de12bc2d675cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:17:51 +0530 Subject: [PATCH 1512/3167] Time: 0 ms (100.00%), Space: 6.3 MB (21.22%) - LeetHub --- .../0767-reorganize-string.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 0767-reorganize-string/0767-reorganize-string.cpp diff --git a/0767-reorganize-string/0767-reorganize-string.cpp b/0767-reorganize-string/0767-reorganize-string.cpp new file mode 100644 index 00000000..841d7b67 --- /dev/null +++ b/0767-reorganize-string/0767-reorganize-string.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + string reorganizeString(string s) { + + priority_queue > pq; + + unordered_map mp; + + string ans; + + for(auto& itr : s) + ++mp[itr]; + + for(auto itr : mp) + pq.push({itr.second, itr.first}); + + while(!pq.empty()) + { + pair curr1 = {-1, ' '}, curr2 = {-1, ' '}; + + if(!pq.empty()) + { + curr1 = pq.top(); + pq.pop(); + } + + if(!pq.empty()) + { + curr2 = pq.top(); + pq.pop(); + } + + if(curr2.first == -1 and curr1.first > 1) + return ""; + + ans += curr1.second; + + if(curr2.first != -1) + ans += curr2.second; + + --curr2.first; + --curr1.first; + + if(curr1.first > 0) + pq.push(curr1); + + if(curr2.first > 0) + pq.push(curr2); + } + + return ans; + } +}; \ No newline at end of file From 50df0a52702e64bb5e69382e49fd7542fd6d39a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:18:48 +0530 Subject: [PATCH 1513/3167] Attach NOTES - LeetHub --- 0767-reorganize-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0767-reorganize-string/NOTES.md diff --git a/0767-reorganize-string/NOTES.md b/0767-reorganize-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0767-reorganize-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 732dc23a8ca893d2394abb7c68906516a533b65d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:18:52 +0530 Subject: [PATCH 1514/3167] Time: 0 ms (100.00%), Space: 6.3 MB (21.22%) - LeetHub From a0220fb7c54ef71a7731b154efb2d9d5d01ff10d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:19:39 +0530 Subject: [PATCH 1515/3167] Attach NOTES - LeetHub From 14ba239fb00053ac27c531db30433d10d32d240b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:19:46 +0530 Subject: [PATCH 1516/3167] Time: 0 ms (100.00%), Space: 6.3 MB (21.22%) - LeetHub From 69d458f91e1e7ee318aa642008f195f67ace73c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 21:42:18 +0530 Subject: [PATCH 1517/3167] Attach NOTES - LeetHub From b06a6f969a57cbc2d0b74f13e92ecf7303cf8a9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 23 Aug 2023 21:42:21 +0530 Subject: [PATCH 1518/3167] Time: 1 ms (56.28%), Space: 6.4 MB (21.22%) - LeetHub --- .../0767-reorganize-string.cpp | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/0767-reorganize-string/0767-reorganize-string.cpp b/0767-reorganize-string/0767-reorganize-string.cpp index 841d7b67..1eeac192 100644 --- a/0767-reorganize-string/0767-reorganize-string.cpp +++ b/0767-reorganize-string/0767-reorganize-string.cpp @@ -1,51 +1,54 @@ class Solution { public: string reorganizeString(string s) { - - priority_queue > pq; - unordered_map mp; + int n = s.size(); - string ans; + vector freq(26, 0); - for(auto& itr : s) - ++mp[itr]; + int maxFreq = 0; - for(auto itr : mp) - pq.push({itr.second, itr.first}); + char maxFreqCharacter; - while(!pq.empty()) - { - pair curr1 = {-1, ' '}, curr2 = {-1, ' '}; + for(auto& ch : s) + { + ++freq[ch - 'a']; - if(!pq.empty()) - { - curr1 = pq.top(); - pq.pop(); - } + if(freq[ch-'a'] > (n+1)/2) + return ""; - if(!pq.empty()) + if(freq[ch-'a'] > maxFreq) { - curr2 = pq.top(); - pq.pop(); + maxFreq = freq[ch-'a']; + maxFreqCharacter = ch; } + } + + int i = 0; + + string ans(n,' '); + + while(freq[maxFreqCharacter - 'a'] > 0) + { + ans[i] = maxFreqCharacter; - if(curr2.first == -1 and curr1.first > 1) - return ""; - - ans += curr1.second; - - if(curr2.first != -1) - ans += curr2.second; - - --curr2.first; - --curr1.first; - - if(curr1.first > 0) - pq.push(curr1); + --freq[maxFreqCharacter - 'a']; - if(curr2.first > 0) - pq.push(curr2); + i += 2; + } + + for(char ch = 'a'; ch <= 'z'; ++ch) + { + while(freq[ch - 'a'] > 0) + { + if(i >= n) + i = 1; + + ans[i] = ch; + + --freq[ch - 'a']; + i += 2; + } } return ans; From 8f3598a151a6476c110dc4b6b1eb71fe43e68acb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 01:46:33 +0530 Subject: [PATCH 1519/3167] Create README - LeetHub --- 0068-text-justification/README.md | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 0068-text-justification/README.md diff --git a/0068-text-justification/README.md b/0068-text-justification/README.md new file mode 100644 index 00000000..d04967ee --- /dev/null +++ b/0068-text-justification/README.md @@ -0,0 +1,63 @@ +

68. Text Justification

Hard


Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

+ +

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.

+ +

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

+ +

For the last line of text, it should be left-justified, and no extra space is inserted between words.

+ +

Note:

+ +
    +
  • A word is defined as a character sequence consisting of non-space characters only.
  • +
  • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  • +
  • The input array words contains at least one word.
  • +
+ +

 

+

Example 1:

+ +
Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
+Output:
+[
+   "This    is    an",
+   "example  of text",
+   "justification.  "
+]
+ +

Example 2:

+ +
Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
+Output:
+[
+  "What   must   be",
+  "acknowledgment  ",
+  "shall be        "
+]
+Explanation: Note that the last line is "shall be    " instead of "shall     be", because the last line must be left-justified instead of fully-justified.
+Note that the second line is also left-justified because it contains only one word.
+ +

Example 3:

+ +
Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
+Output:
+[
+  "Science  is  what we",
+  "understand      well",
+  "enough to explain to",
+  "a  computer.  Art is",
+  "everything  else  we",
+  "do                  "
+]
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 300
  • +
  • 1 <= words[i].length <= 20
  • +
  • words[i] consists of only English letters and symbols.
  • +
  • 1 <= maxWidth <= 100
  • +
  • words[i].length <= maxWidth
  • +
+
\ No newline at end of file From 01589c7999fa7d238c8134ffb0a87b7f60018a27 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 01:46:37 +0530 Subject: [PATCH 1520/3167] Time: 0 ms (100.00%), Space: 7.3 MB (69.88%) - LeetHub --- .../0068-text-justification.cpp | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 0068-text-justification/0068-text-justification.cpp diff --git a/0068-text-justification/0068-text-justification.cpp b/0068-text-justification/0068-text-justification.cpp new file mode 100644 index 00000000..18a0c746 --- /dev/null +++ b/0068-text-justification/0068-text-justification.cpp @@ -0,0 +1,83 @@ +class Solution { +public: + vector fullJustify(vector& words, int maxWidth) { + + int n = words.size(); + + vector ans; + + string curr = ""; + + for(int i = 0; i < n; ++i) + { + int j = i+1; + int currCnt = words[i].size(), wordCount = 1; + while(j < n and currCnt + words[j].size() < maxWidth) + { + currCnt += words[j].size(); + + // cout< 1 ? (remSpaces/ (wordCount-1)) : (remSpaces / wordCount)); + + int extra = (wordCount > 1 ? (remSpaces% (wordCount-1)) : (remSpaces % wordCount)); + + // cout< 0) + { + string need(remSpaces, ' '); + curr += need; + } + + + // cout< maxWidth) + // curr.pop_back(); + + ans.push_back(curr); + + curr.clear(); + + i = j-1; + } + return ans; + } +}; \ No newline at end of file From 26f80754a334cd7b1e74da7d44928dd9be1b321c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 02:04:10 +0530 Subject: [PATCH 1521/3167] Create README - LeetHub --- Palindrome String - GFG/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Palindrome String - GFG/README.md diff --git a/Palindrome String - GFG/README.md b/Palindrome String - GFG/README.md new file mode 100644 index 00000000..73716626 --- /dev/null +++ b/Palindrome String - GFG/README.md @@ -0,0 +1,13 @@ +# Palindrome String +## Easy +

Given a string S, check if it is palindrome or not.

+

Example 1:

+
Input: S = "abba"
+Output: 1
+Explanation: S is a palindrome
+

Example 2:

+
Input: S = "abc" 
+Output: 0
+Explanation: S is not a palindrome
+

Your Task:
You don't need to read input or print anything. Complete the function isPalindrome()which accepts string S and returns an integer value 1 or 0.


Expected Time Complexity: O(Length of S)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= Length of S<= 2*105

\ No newline at end of file From cff993f8b7683eb700c03bf758d1948ea5a2cd15 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 02:04:11 +0530 Subject: [PATCH 1522/3167] Added solution - LeetHub --- Palindrome String - GFG/palindrome-string.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Palindrome String - GFG/palindrome-string.cpp diff --git a/Palindrome String - GFG/palindrome-string.cpp b/Palindrome String - GFG/palindrome-string.cpp new file mode 100644 index 00000000..c34d071c --- /dev/null +++ b/Palindrome String - GFG/palindrome-string.cpp @@ -0,0 +1,51 @@ +//{ Driver Code Starts + +#include +using namespace std; + + +// } Driver Code Ends +//User function template for C++ +class Solution{ +public: + + + int isPalindrome(string S) + { + // Your code goes here + + int n = S.size(); + + for(int i = 0; i <= n/2; ++i) + { + if(S[i] != S[n-i-1]) + return false; + } + return true; + } + +}; + +//{ Driver Code Starts. + +int main() +{ + ios_base::sync_with_stdio(0); + cin.tie(NULL); + cout.tie(NULL); + + int t; + cin >> t; + while(t--) + { + string s; + cin >> s; + + Solution ob; + + cout << ob.isPalindrome(s) << "\n"; + } + + return 0; +} +// } Driver Code Ends \ No newline at end of file From 92361cf210564815720c8656e4699e003e38204a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 07:56:32 +0530 Subject: [PATCH 1523/3167] Create README - LeetHub --- 0097-interleaving-string/README.md | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0097-interleaving-string/README.md diff --git a/0097-interleaving-string/README.md b/0097-interleaving-string/README.md new file mode 100644 index 00000000..0d3ffd22 --- /dev/null +++ b/0097-interleaving-string/README.md @@ -0,0 +1,49 @@ +

97. Interleaving String

Medium


Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.

+ +

An interleaving of two strings s and t is a configuration where s and t are divided into n and m substrings respectively, such that:

+ +
    +
  • s = s1 + s2 + ... + sn
  • +
  • t = t1 + t2 + ... + tm
  • +
  • |n - m| <= 1
  • +
  • The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
  • +
+ +

Note: a + b is the concatenation of strings a and b.

+ +

 

+

Example 1:

+ +
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
+Output: true
+Explanation: One way to obtain s3 is:
+Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
+Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
+Since s3 can be obtained by interleaving s1 and s2, we return true.
+
+ +

Example 2:

+ +
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
+Output: false
+Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
+
+ +

Example 3:

+ +
Input: s1 = "", s2 = "", s3 = ""
+Output: true
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s1.length, s2.length <= 100
  • +
  • 0 <= s3.length <= 200
  • +
  • s1, s2, and s3 consist of lowercase English letters.
  • +
+ +

 

+

Follow up: Could you solve it using only O(s2.length) additional memory space?

+
\ No newline at end of file From a39ff88a8b36bfef78f13f8442a6d6522308ae71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 07:56:36 +0530 Subject: [PATCH 1524/3167] Time: 0 ms (100.00%), Space: 7.1 MB (58.49%) - LeetHub --- .../0097-interleaving-string.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0097-interleaving-string/0097-interleaving-string.cpp diff --git a/0097-interleaving-string/0097-interleaving-string.cpp b/0097-interleaving-string/0097-interleaving-string.cpp new file mode 100644 index 00000000..8dfd0f2d --- /dev/null +++ b/0097-interleaving-string/0097-interleaving-string.cpp @@ -0,0 +1,39 @@ +class Solution { + +private: + bool helper(int i, int j, int k, string& s1, string& s2, string& s3, vector >& dp) + { + if(i == s1.size() and j == s2.size() and k == s3.size()) + return true; + + if(dp[i][j] != -1) + return dp[i][j]; + + bool one = false, two = false; + + if(i != s1.size()) + { + if(s1[i] == s3[k]) + one = helper(i+1, j, k+1, s1, s2, s3, dp); + } + + if(j != s2.size()) + { + if(s2[j] == s3[k]) + two = helper(i, j+1, k+1, s1, s2, s3, dp); + } + + return dp[i][j] = one | two; + } + +public: + bool isInterleave(string s1, string s2, string s3) { + + int n = s1.size(), m = s2.size(); + + vector > dp(n+1, vector(m+1, -1)); + + return helper(0, 0, 0, s1, s2, s3, dp); + + } +}; \ No newline at end of file From 6672d290ec06ad485b971d4b41cf1576d4e0fac7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 08:01:11 +0530 Subject: [PATCH 1525/3167] Attach NOTES - LeetHub --- 0097-interleaving-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0097-interleaving-string/NOTES.md diff --git a/0097-interleaving-string/NOTES.md b/0097-interleaving-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0097-interleaving-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 662691517acb6f61d0af1ad20106e3888ef4895b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 25 Aug 2023 08:01:14 +0530 Subject: [PATCH 1526/3167] Time: 0 ms (100.00%), Space: 7.1 MB (58.49%) - LeetHub From c73458c74403ccb746bb4c1e057d5b8335845dca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:10:53 +0530 Subject: [PATCH 1527/3167] Create README - LeetHub --- .../README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Longest K unique characters substring - GFG/README.md diff --git a/Longest K unique characters substring - GFG/README.md b/Longest K unique characters substring - GFG/README.md new file mode 100644 index 00000000..5e77d8bc --- /dev/null +++ b/Longest K unique characters substring - GFG/README.md @@ -0,0 +1,18 @@ +# Longest K unique characters substring +## Medium +

Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.

+

Example 1:

+
Input:
+S = "aabacbebebe", K = 3
+Output: 
7 +Explanation:
"cbebebe" is the longest substring with 3 distinct characters. +
+

Example 2:

+
Input: 
+S = "aaaa", K = 2
+Output: -1
+Explanation: 
There's no substring with 2 distinct characters. +
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.

+

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).

+

Constraints:
1 ≤ |S| ≤ 105
1 ≤ K ≤ 26
All characters are lowercase latin characters.

\ No newline at end of file From adbd1bd801b11501ac85d25b3b5c160fc8496d7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:10:54 +0530 Subject: [PATCH 1528/3167] Added solution - LeetHub --- .../longest-k-unique-characters-substring.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Longest K unique characters substring - GFG/longest-k-unique-characters-substring.cpp diff --git a/Longest K unique characters substring - GFG/longest-k-unique-characters-substring.cpp b/Longest K unique characters substring - GFG/longest-k-unique-characters-substring.cpp new file mode 100644 index 00000000..b934511e --- /dev/null +++ b/Longest K unique characters substring - GFG/longest-k-unique-characters-substring.cpp @@ -0,0 +1,61 @@ +//{ Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + +// } Driver Code Ends +//User function template for C++ + +class Solution{ + public: + int longestKSubstr(string s, int k) { + // your code here + + unordered_map mp; + + int i = 0, j = 0, n = s.size(); + + int ans = -1; + + while(j < n) + { + ++mp[s[j]]; + + if(mp.size() == k) + { + ans = max(ans, j - i + 1); + } + + while(mp.size() > k) + { + --mp[s[i]]; + + if(mp[s[i]] == 0) + mp.erase(s[i]); + + ++i; + } + + ++j; + } + + return ans; + } +}; + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + string s; + cin >> s; + int k; + cin >> k; + Solution ob; + cout << ob.longestKSubstr(s, k) << endl; + } +} + +// } Driver Code Ends \ No newline at end of file From bfac1a8588ceb4a5332eee056709dd9393c52ce4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:21:10 +0530 Subject: [PATCH 1529/3167] Create README - LeetHub --- 0646-maximum-length-of-pair-chain/README.md | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0646-maximum-length-of-pair-chain/README.md diff --git a/0646-maximum-length-of-pair-chain/README.md b/0646-maximum-length-of-pair-chain/README.md new file mode 100644 index 00000000..0537ff05 --- /dev/null +++ b/0646-maximum-length-of-pair-chain/README.md @@ -0,0 +1,32 @@ +

646. Maximum Length of Pair Chain

Medium


You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.

+ +

A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.

+ +

Return the length longest chain which can be formed.

+ +

You do not need to use up all the given intervals. You can select pairs in any order.

+ +

 

+

Example 1:

+ +
Input: pairs = [[1,2],[2,3],[3,4]]
+Output: 2
+Explanation: The longest chain is [1,2] -> [3,4].
+
+ +

Example 2:

+ +
Input: pairs = [[1,2],[7,8],[4,5]]
+Output: 3
+Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
+
+ +

 

+

Constraints:

+ +
    +
  • n == pairs.length
  • +
  • 1 <= n <= 1000
  • +
  • -1000 <= lefti < righti <= 1000
  • +
+
\ No newline at end of file From 3abf6d05c2f46879c7aa963381f17841e8839ee6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:21:14 +0530 Subject: [PATCH 1530/3167] Time: 458 ms (21.49%), Space: 159.2 MB (8.83%) - LeetHub --- .../0646-maximum-length-of-pair-chain.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0646-maximum-length-of-pair-chain/0646-maximum-length-of-pair-chain.cpp diff --git a/0646-maximum-length-of-pair-chain/0646-maximum-length-of-pair-chain.cpp b/0646-maximum-length-of-pair-chain/0646-maximum-length-of-pair-chain.cpp new file mode 100644 index 00000000..80200bd0 --- /dev/null +++ b/0646-maximum-length-of-pair-chain/0646-maximum-length-of-pair-chain.cpp @@ -0,0 +1,33 @@ +class Solution { + +private: + int helper(int idx, int prev, int n, vector>& pairs, vector >& dp) + { + if(idx == n) + return 0; + + if(dp[idx][prev + 1] != -1) + return dp[idx][prev + 1]; + + int take = 0; + + int notTake = helper(idx + 1, prev, n, pairs, dp); + + if(prev == -1 or pairs[idx][0] > pairs[prev][1]) + take = max(1 + helper(idx + 1, idx, n, pairs, dp), notTake); + + return dp[idx][prev + 1] = max(take, notTake); + } +public: + int findLongestChain(vector>& pairs) { + + sort(pairs.begin(), pairs.end()); + + int n = pairs.size(); + + vector> dp(n+1, vector(n+1, -1)); + + return helper(0, -1, n, pairs, dp); + + } +}; \ No newline at end of file From b46a1c338b25238377b0bbd3faed0ca41861cfac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:24:58 +0530 Subject: [PATCH 1531/3167] Attach NOTES - LeetHub --- 0646-maximum-length-of-pair-chain/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0646-maximum-length-of-pair-chain/NOTES.md diff --git a/0646-maximum-length-of-pair-chain/NOTES.md b/0646-maximum-length-of-pair-chain/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0646-maximum-length-of-pair-chain/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f0f41a5a4082dfc64c6b11c401286283962e7d45 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:25:01 +0530 Subject: [PATCH 1532/3167] Time: 458 ms (21.49%), Space: 159.2 MB (8.83%) - LeetHub From 9330f1cce2a85b9bbb0b60cc85eda0cb305e3d50 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:25:59 +0530 Subject: [PATCH 1533/3167] Attach NOTES - LeetHub From 0a7f7e6ac5416b8d6c78b3a172676a6816148dfd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:26:02 +0530 Subject: [PATCH 1534/3167] Time: 458 ms (21.49%), Space: 159.2 MB (8.83%) - LeetHub From dd5f3113ef0e1267e80d09712b82b68da3449a58 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 10:07:09 +0530 Subject: [PATCH 1535/3167] Attach NOTES - LeetHub From 23ba403f2d0961362e96a61cc80614581f1c0105 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 26 Aug 2023 10:07:12 +0530 Subject: [PATCH 1536/3167] Time: 4 ms (70.85%), Space: 7.1 MB (50.50%) - LeetHub --- .../0097-interleaving-string.cpp | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/0097-interleaving-string/0097-interleaving-string.cpp b/0097-interleaving-string/0097-interleaving-string.cpp index 8dfd0f2d..f785361c 100644 --- a/0097-interleaving-string/0097-interleaving-string.cpp +++ b/0097-interleaving-string/0097-interleaving-string.cpp @@ -1,39 +1,36 @@ class Solution { - + private: - bool helper(int i, int j, int k, string& s1, string& s2, string& s3, vector >& dp) + bool helper(int i , int j, int n, int m, int M, string& s1, string& s2, string& s3, vector >& dp) { - if(i == s1.size() and j == s2.size() and k == s3.size()) + if(i == n and j == m and i+j == M) return true; + + bool result = false; if(dp[i][j] != -1) return dp[i][j]; - bool one = false, two = false; - - if(i != s1.size()) - { - if(s1[i] == s3[k]) - one = helper(i+1, j, k+1, s1, s2, s3, dp); - } + if(s1[i] == s3[i+j]) + result |= helper(i+1, j, n, m, M, s1, s2, s3, dp); - if(j != s2.size()) - { - if(s2[j] == s3[k]) - two = helper(i, j+1, k+1, s1, s2, s3, dp); - } + if(s2[j] == s3[i+j]) + result |= helper(i, j+1, n, m, M, s1, s2, s3, dp); - return dp[i][j] = one | two; + return dp[i][j] = result; } public: bool isInterleave(string s1, string s2, string s3) { - int n = s1.size(), m = s2.size(); + int n = s1.size(), m = s2.size(), M = s3.size(); + + if(n + m != M) + return false; vector > dp(n+1, vector(m+1, -1)); - - return helper(0, 0, 0, s1, s2, s3, dp); + + return helper(0, 0, n, m, M, s1, s2, s3, dp); } }; \ No newline at end of file From 3839d6266000b07c5993c89321b6b66d56703089 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:03:01 +0530 Subject: [PATCH 1537/3167] Create README - LeetHub --- 0403-frog-jump/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0403-frog-jump/README.md diff --git a/0403-frog-jump/README.md b/0403-frog-jump/README.md new file mode 100644 index 00000000..e2b6e567 --- /dev/null +++ b/0403-frog-jump/README.md @@ -0,0 +1,31 @@ +

403. Frog Jump

Hard


A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.

+ +

Given a list of stones' positions (in units) in sorted ascending order, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be 1 unit.

+ +

If the frog's last jump was k units, its next jump must be either k - 1, k, or k + 1 units. The frog can only jump in the forward direction.

+ +

 

+

Example 1:

+ +
Input: stones = [0,1,3,5,6,8,12,17]
+Output: true
+Explanation: The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone.
+
+ +

Example 2:

+ +
Input: stones = [0,1,2,3,4,8,9,11]
+Output: false
+Explanation: There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= stones.length <= 2000
  • +
  • 0 <= stones[i] <= 231 - 1
  • +
  • stones[0] == 0
  • +
  • stones is sorted in a strictly increasing order.
  • +
+
\ No newline at end of file From ecbb8151c48f482644ef2c8f4c8c8601fbf5bd78 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:03:05 +0530 Subject: [PATCH 1538/3167] Time: 2241 ms (5.03%), Space: 47.2 MB (52.15%) - LeetHub --- 0403-frog-jump/0403-frog-jump.cpp | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0403-frog-jump/0403-frog-jump.cpp diff --git a/0403-frog-jump/0403-frog-jump.cpp b/0403-frog-jump/0403-frog-jump.cpp new file mode 100644 index 00000000..2ccd2012 --- /dev/null +++ b/0403-frog-jump/0403-frog-jump.cpp @@ -0,0 +1,50 @@ +class Solution { + +private: + bool helper(int idx, int n, int canJump, vector& stones, map, int>& dp) + { + if(idx == n-1) + return 1; + + if(idx >= n) + return 0; + + bool first = false, second = false, third = false; + + if(dp.find({idx, canJump}) != dp.end()) + return dp[{idx, canJump}]; + + int maxJump = canJump + 1; + int minJump = canJump - 1; + + for(int i = idx+1; i < n; ++i) + { + + if(stones[idx] + maxJump == stones[i]) + { + first |= helper(i, n, maxJump, stones, dp); + } + + if(stones[idx] + minJump == stones[i]) + { + second |= helper(i, n, minJump, stones, dp); + } + + if(stones[idx] + canJump == stones[i]) + { + third |= helper(i, n, canJump, stones, dp); + } + } + return dp[{idx, canJump}] = first or second or third; + } +public: + bool canCross(vector& stones) { + + int n = stones.size(); + + map, int> dp; + + return helper(0, n, 0, stones, dp); + + } +}; \ No newline at end of file From 7d18e879dc13c2673f49b05a990eb3b8cf9f4331 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:06:33 +0530 Subject: [PATCH 1539/3167] Attach NOTES - LeetHub --- 0403-frog-jump/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0403-frog-jump/NOTES.md diff --git a/0403-frog-jump/NOTES.md b/0403-frog-jump/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0403-frog-jump/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 14b0cdf1123fd96509dfbf4a4c698ef1c64a24f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:06:37 +0530 Subject: [PATCH 1540/3167] Time: 2241 ms (5.03%), Space: 47.2 MB (52.15%) - LeetHub From 1e964fa74bb13b38253aa2e37c45bcd584746d00 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:17:10 +0530 Subject: [PATCH 1541/3167] Attach NOTES - LeetHub From 737c2fbb9446f1c94a6c93d579b1a07b0cc1b57a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:17:13 +0530 Subject: [PATCH 1542/3167] Time: 333 ms (22.87%), Space: 36.9 MB (62.63%) - LeetHub --- 0403-frog-jump/0403-frog-jump.cpp | 35 ++++++++++--------------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/0403-frog-jump/0403-frog-jump.cpp b/0403-frog-jump/0403-frog-jump.cpp index 2ccd2012..ab2bef65 100644 --- a/0403-frog-jump/0403-frog-jump.cpp +++ b/0403-frog-jump/0403-frog-jump.cpp @@ -9,33 +9,21 @@ class Solution { if(idx >= n) return 0; - bool first = false, second = false, third = false; + bool result = false; if(dp.find({idx, canJump}) != dp.end()) return dp[{idx, canJump}]; - - int maxJump = canJump + 1; - int minJump = canJump - 1; - - for(int i = idx+1; i < n; ++i) - { + + for(int jump = -1; jump <= 1; ++jump) + { + int jumpIdx = lower_bound(stones.begin(), stones.end(), stones[idx] + canJump + jump) - stones.begin(); - if(stones[idx] + maxJump == stones[i]) - { - first |= helper(i, n, maxJump, stones, dp); - } - - if(stones[idx] + minJump == stones[i]) - { - second |= helper(i, n, minJump, stones, dp); - } - - if(stones[idx] + canJump == stones[i]) - { - third |= helper(i, n, canJump, stones, dp); - } + if(jumpIdx > idx and jumpIdx < n and stones[idx] + canJump + jump == stones[jumpIdx]) + result |= helper(jumpIdx, n, canJump + jump, stones, dp); } - return dp[{idx, canJump}] = first or second or third; + + + return dp[{idx, canJump}] = result; } public: bool canCross(vector& stones) { @@ -44,7 +32,6 @@ class Solution { map, int> dp; - return helper(0, n, 0, stones, dp); - + return helper(0, n, 0, stones, dp); } }; \ No newline at end of file From 2395a3b1c27bddef015f2d8bf5cf275ce1815b76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:19:27 +0530 Subject: [PATCH 1543/3167] Attach NOTES - LeetHub From cb778b24e0219d1f5b495e6a5bbd9d0bdbee1485 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:19:30 +0530 Subject: [PATCH 1544/3167] Time: 333 ms (22.87%), Space: 36.9 MB (62.63%) - LeetHub From bafe26bf7c9b34e4ccc6f0918d1d67a40c95e982 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:39:49 +0530 Subject: [PATCH 1545/3167] Attach NOTES - LeetHub --- 2833-furthest-point-from-origin/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2833-furthest-point-from-origin/NOTES.md diff --git a/2833-furthest-point-from-origin/NOTES.md b/2833-furthest-point-from-origin/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2833-furthest-point-from-origin/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c4e89e4f2b7946e9ff3760e0aa75b9ba169ac9db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:39:53 +0530 Subject: [PATCH 1546/3167] Time: 13 ms (80.00%), Space: 11.6 MB (100.00%) - LeetHub --- .../2833-furthest-point-from-origin.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2833-furthest-point-from-origin/2833-furthest-point-from-origin.cpp diff --git a/2833-furthest-point-from-origin/2833-furthest-point-from-origin.cpp b/2833-furthest-point-from-origin/2833-furthest-point-from-origin.cpp new file mode 100644 index 00000000..e428fcc0 --- /dev/null +++ b/2833-furthest-point-from-origin/2833-furthest-point-from-origin.cpp @@ -0,0 +1,39 @@ +class Solution { + +private: + int helper(int idx, int currMove, string& moves, vector>& dp) + { + if(idx == moves.size()) + return currMove; + + if(dp[idx][currMove+51] != -1) + return dp[idx][currMove+51]; + + int first = INT_MIN, second = INT_MIN, both = INT_MIN; + + if(moves[idx] == 'L') + first = abs(helper(idx+1, currMove - 1 ,moves, dp)); + if(moves[idx] == 'R') + second = abs(helper(idx+1, currMove + 1, moves, dp)); + + if(moves[idx] == '_') + both = max(abs(helper(idx+1, currMove + 1, moves, dp)) , abs(helper(idx+1,currMove - 1, moves, dp))); + + return dp[idx][currMove+51] = max({first, second, both}); + } + + // "_R__LL_" + +public: + int furthestDistanceFromOrigin(string moves) { + + int n = moves.size(); + + vector> dp(n+1, vector(n+51, -1)); + + int ans = helper(0, 0,moves, dp); + + return ans; + + } +}; \ No newline at end of file From 120158a9ef6ad118b1af349423a5b908f5285f6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:40:59 +0530 Subject: [PATCH 1547/3167] Attach NOTES - LeetHub From e04e855e340b69bc90d581bd22d8ab6c567fe226 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:41:02 +0530 Subject: [PATCH 1548/3167] Time: 13 ms (80.00%), Space: 11.6 MB (100.00%) - LeetHub From 62bf86523b7346c535e936b5e8afa3baadd0df8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:07:01 +0530 Subject: [PATCH 1549/3167] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Remove duplicate element from sorted Linked List - GFG/README.md diff --git a/Remove duplicate element from sorted Linked List - GFG/README.md b/Remove duplicate element from sorted Linked List - GFG/README.md new file mode 100644 index 00000000..e99fe30e --- /dev/null +++ b/Remove duplicate element from sorted Linked List - GFG/README.md @@ -0,0 +1,21 @@ +# Remove duplicate element from sorted Linked List +## Easy +

Given a singly linked list consisting of N nodes. The task is to remove duplicates (nodes with duplicate values) from the given list (if exists).
Note: Try not to use extra space. The nodes are arranged in a sorted way.

+

Example 1:

+
Input:
+LinkedList: 2->2->4->5
+Output: 2 4 5
+Explanation: In the given linked list 
+2 ->2 -> 4-> 5, only 2 occurs more 
+than 1 time. So we need to remove it once.
+
+

Example 2:

+
Input:
+LinkedList: 2->2->2->2->2
+Output: 2
+Explanation: In the given linked list 
+2 ->2 ->2 ->2 ->2, 2 is the only element
+and is repeated 5 times. So we need to remove
any four 2.
+

Your Task:
The task is to complete the function removeDuplicates() which takes the head of input linked list as input. The function should remove the duplicates from linked list and return the head of the linkedlist.

+

Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)

+

Constraints:
1 <= Number of nodes <= 105

\ No newline at end of file From e373af3c6fa73718c157a7967f46b51a17f39bf6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:07:01 +0530 Subject: [PATCH 1550/3167] Added solution - LeetHub --- ...licate-element-from-sorted-linked-list.cpp | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Remove duplicate element from sorted Linked List - GFG/remove-duplicate-element-from-sorted-linked-list.cpp diff --git a/Remove duplicate element from sorted Linked List - GFG/remove-duplicate-element-from-sorted-linked-list.cpp b/Remove duplicate element from sorted Linked List - GFG/remove-duplicate-element-from-sorted-linked-list.cpp new file mode 100644 index 00000000..7d6681a8 --- /dev/null +++ b/Remove duplicate element from sorted Linked List - GFG/remove-duplicate-element-from-sorted-linked-list.cpp @@ -0,0 +1,92 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node { + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } +}; + + +void print(Node *root) +{ + Node *temp = root; + while(temp!=NULL) + { + cout<data<<" "; + temp=temp->next; + } +} +Node* removeDuplicates(Node *root); +int main() { + // your code goes here + int T; + cin>>T; + + while(T--) + { + int K; + cin>>K; + Node *head = NULL; + Node *temp = head; + + for(int i=0;i>data; + if(head==NULL) + head=temp=new Node(data); + else + { + temp->next = new Node(data); + temp=temp->next; + } + } + + Node *result = removeDuplicates(head); + print(result); + cout<next) + nextNode = temp->next; + + while(nextNode and temp->data == nextNode->data) + { + temp->next = nextNode->next; + nextNode = temp->next; + } + temp = temp->next; + } + + return head; + +} \ No newline at end of file From 8566f950a77c60bf25a66666a11a3cf38734c884 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:15:50 +0530 Subject: [PATCH 1551/3167] Create README - LeetHub --- 0225-implement-stack-using-queues/README.md | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0225-implement-stack-using-queues/README.md diff --git a/0225-implement-stack-using-queues/README.md b/0225-implement-stack-using-queues/README.md new file mode 100644 index 00000000..271ce450 --- /dev/null +++ b/0225-implement-stack-using-queues/README.md @@ -0,0 +1,48 @@ +

225. Implement Stack using Queues

Easy


Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

+ +

Implement the MyStack class:

+ +
    +
  • void push(int x) Pushes element x to the top of the stack.
  • +
  • int pop() Removes the element on the top of the stack and returns it.
  • +
  • int top() Returns the element on the top of the stack.
  • +
  • boolean empty() Returns true if the stack is empty, false otherwise.
  • +
+ +

Notes:

+ +
    +
  • You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
  • +
  • Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyStack", "push", "push", "top", "pop", "empty"]
+[[], [1], [2], [], [], []]
+Output
+[null, null, null, 2, 2, false]
+
+Explanation
+MyStack myStack = new MyStack();
+myStack.push(1);
+myStack.push(2);
+myStack.top(); // return 2
+myStack.pop(); // return 2
+myStack.empty(); // return False
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= x <= 9
  • +
  • At most 100 calls will be made to push, pop, top, and empty.
  • +
  • All the calls to pop and top are valid.
  • +
+ +

 

+

Follow-up: Can you implement the stack using only one queue?

+
\ No newline at end of file From 18f53139e081c3b295dbc4b4c35c7b490334477e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:15:50 +0530 Subject: [PATCH 1552/3167] Attach NOTES - LeetHub --- 0225-implement-stack-using-queues/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0225-implement-stack-using-queues/NOTES.md diff --git a/0225-implement-stack-using-queues/NOTES.md b/0225-implement-stack-using-queues/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0225-implement-stack-using-queues/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 63d0464144dae68e00e3f5afb1459b80b693fe4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:15:54 +0530 Subject: [PATCH 1553/3167] Time: 0 ms (100.00%), Space: 6.8 MB (25.78%) - LeetHub --- .../0225-implement-stack-using-queues.cpp | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp diff --git a/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp b/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp new file mode 100644 index 00000000..3c4a93ed --- /dev/null +++ b/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp @@ -0,0 +1,62 @@ +class MyStack { +public: + + queue q; + + MyStack() { + + } + + void push(int x) { + q.push(x); + } + + int pop() { + queue q1; + + while(q.size() > 1) + { + q1.push(q.front()); + q.pop(); + } + + int x = q.front(); + q.pop(); + + swap(q, q1); + + return x; + + } + + int top() { + queue q1; + + while(q.size() > 1) + { + q1.push(q.front()); + q.pop(); + } + + int x = q.front(); + + q1.push(x); + + swap(q, q1); + + return x; + } + + bool empty() { + return q.empty(); + } +}; + +/** + * Your MyStack object will be instantiated and called as such: + * MyStack* obj = new MyStack(); + * obj->push(x); + * int param_2 = obj->pop(); + * int param_3 = obj->top(); + * bool param_4 = obj->empty(); + */ \ No newline at end of file From 9a2ab00914468ce286bd4d11bdd2617cbd73923a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:29:21 +0530 Subject: [PATCH 1554/3167] Attach NOTES - LeetHub From 039d4895a8d940863d063da11fc5776eff393f8e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:29:24 +0530 Subject: [PATCH 1555/3167] Time: 0 ms (100.00%), Space: 6.8 MB (67.22%) - LeetHub --- .../0225-implement-stack-using-queues.cpp | 45 ++++++------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp b/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp index 3c4a93ed..9c8ac6ca 100644 --- a/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp +++ b/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp @@ -1,54 +1,37 @@ class MyStack { public: - queue q; + queue q1, q2; MyStack() { } void push(int x) { - q.push(x); - } - - int pop() { - queue q1; - while(q.size() > 1) + q2.push(x); + + while(!q1.empty()) { - q1.push(q.front()); - q.pop(); + q2.push(q1.front()); + q1.pop(); } - int x = q.front(); - q.pop(); - - swap(q, q1); - + swap(q1, q2); + } + + int pop() { + int x = q1.front(); + q1.pop(); return x; - } int top() { - queue q1; - - while(q.size() > 1) - { - q1.push(q.front()); - q.pop(); - } - - int x = q.front(); - - q1.push(x); - - swap(q, q1); - - return x; + return q1.front(); } bool empty() { - return q.empty(); + return q1.empty(); } }; From 362f2a3435d93dd82a36c6d1fe8bc78c3c1c847b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:31:45 +0530 Subject: [PATCH 1556/3167] Attach NOTES - LeetHub From c8131f3684219df397dd5971437907e8219a654c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:31:48 +0530 Subject: [PATCH 1557/3167] Time: 0 ms (100.00%), Space: 6.7 MB (97.83%) - LeetHub --- .../0225-implement-stack-using-queues.cpp | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp b/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp index 9c8ac6ca..b2b7c970 100644 --- a/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp +++ b/0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp @@ -1,7 +1,7 @@ class MyStack { public: - queue q1, q2; + queue q; MyStack() { @@ -9,29 +9,27 @@ class MyStack { void push(int x) { - q2.push(x); + q.push(x); - while(!q1.empty()) + for(int i = 0; i < q.size()-1; ++i) { - q2.push(q1.front()); - q1.pop(); + q.push(q.front()); + q.pop(); } - - swap(q1, q2); } int pop() { - int x = q1.front(); - q1.pop(); + int x = q.front(); + q.pop(); return x; } int top() { - return q1.front(); + return q.front(); } bool empty() { - return q1.empty(); + return q.empty(); } }; From a91f286a35c57c2df6b772e232ec2529baf1807a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:32:00 +0530 Subject: [PATCH 1558/3167] Attach NOTES - LeetHub From 7744fe069eca0d8d2ff4f2af7166846f985da35c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:32:03 +0530 Subject: [PATCH 1559/3167] Time: 0 ms (100.00%), Space: 6.7 MB (97.83%) - LeetHub From 72a6a543183c1b96045e504129a5e4e4fde5c7d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:32:35 +0530 Subject: [PATCH 1560/3167] Attach NOTES - LeetHub From 8a36f413d4166d6e29710e2ec2bed6df0056d68c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:32:39 +0530 Subject: [PATCH 1561/3167] Time: 0 ms (100.00%), Space: 6.7 MB (97.83%) - LeetHub From d3edbf9541fb168e8d4c34ca9b03c223cd2ab6aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:32:44 +0530 Subject: [PATCH 1562/3167] Attach NOTES - LeetHub From c233be40109278c928967a4edf924bfa19821ff5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:33:28 +0530 Subject: [PATCH 1563/3167] Attach NOTES - LeetHub From f3cf9ba039797a5af91aacb49f62b050823ac09d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:33:32 +0530 Subject: [PATCH 1564/3167] Time: 0 ms (100.00%), Space: 6.7 MB (97.83%) - LeetHub From a4a233912346c25889b1d25cb94722fc160ca8b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:33:45 +0530 Subject: [PATCH 1565/3167] Attach NOTES - LeetHub From 920dca6e8e3ed69a8fe6a0a3ea450cf3b31f8ddc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:33:48 +0530 Subject: [PATCH 1566/3167] Time: 0 ms (100.00%), Space: 6.7 MB (97.83%) - LeetHub From 93237ea72232a93283b7e2dcc14ca75084d40fee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:14:21 +0530 Subject: [PATCH 1567/3167] Create README - LeetHub --- 2483-minimum-penalty-for-a-shop/README.md | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2483-minimum-penalty-for-a-shop/README.md diff --git a/2483-minimum-penalty-for-a-shop/README.md b/2483-minimum-penalty-for-a-shop/README.md new file mode 100644 index 00000000..eb9e9cc7 --- /dev/null +++ b/2483-minimum-penalty-for-a-shop/README.md @@ -0,0 +1,53 @@ +

2483. Minimum Penalty for a Shop

Medium


You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':

+ +
    +
  • if the ith character is 'Y', it means that customers come at the ith hour
  • +
  • whereas 'N' indicates that no customers come at the ith hour.
  • +
+ +

If the shop closes at the jth hour (0 <= j <= n), the penalty is calculated as follows:

+ +
    +
  • For every hour when the shop is open and no customers come, the penalty increases by 1.
  • +
  • For every hour when the shop is closed and customers come, the penalty increases by 1.
  • +
+ +

Return the earliest hour at which the shop must be closed to incur a minimum penalty.

+ +

Note that if a shop closes at the jth hour, it means the shop is closed at the hour j.

+ +

 

+

Example 1:

+ +
Input: customers = "YYNY"
+Output: 2
+Explanation: 
+- Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty.
+- Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty.
+- Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty.
+- Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty.
+- Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty.
+Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
+
+ +

Example 2:

+ +
Input: customers = "NNNNN"
+Output: 0
+Explanation: It is best to close the shop at the 0th hour as no customers arrive.
+ +

Example 3:

+ +
Input: customers = "YYYY"
+Output: 4
+Explanation: It is best to close the shop at the 4th hour as customers arrive at each hour.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= customers.length <= 105
  • +
  • customers consists only of characters 'Y' and 'N'.
  • +
+
\ No newline at end of file From 55aa005813838e896a363bae856a6046a6399ac6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:14:25 +0530 Subject: [PATCH 1568/3167] Time: 34 ms (46.46%), Space: 30 MB (6.69%) - LeetHub --- .../2483-minimum-penalty-for-a-shop.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2483-minimum-penalty-for-a-shop/2483-minimum-penalty-for-a-shop.cpp diff --git a/2483-minimum-penalty-for-a-shop/2483-minimum-penalty-for-a-shop.cpp b/2483-minimum-penalty-for-a-shop/2483-minimum-penalty-for-a-shop.cpp new file mode 100644 index 00000000..573c4245 --- /dev/null +++ b/2483-minimum-penalty-for-a-shop/2483-minimum-penalty-for-a-shop.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int bestClosingTime(string customers) { + + int n = customers.size(); + + int totPenalty = n; + int shopClosed = -1; + + vector> records; + + int customerCome = 0, customerNotCome = 0; + + for(int i = 0; i < n; ++i) + { + if(customers[i] == 'Y') + ++customerCome; + else + ++customerNotCome; + + records.push_back({customerCome, customerNotCome}); + } + + for(int i = 0; i <= n; ++i) + { + int notCameAndOpen = (i > 0 ? records[i-1].second : 0); + int cameAndClosed = (i == 0 ? records[n-1].first : records[n-1].first - records[i-1].first); + + if(notCameAndOpen + cameAndClosed < totPenalty) + { + totPenalty = notCameAndOpen + cameAndClosed; + shopClosed = i; + } + } + + return shopClosed; + + } +}; \ No newline at end of file From 4cb22a6e2912f1c3a9ae175893e4f640bd48ac9d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:17:44 +0530 Subject: [PATCH 1569/3167] Attach NOTES - LeetHub --- 2483-minimum-penalty-for-a-shop/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2483-minimum-penalty-for-a-shop/NOTES.md diff --git a/2483-minimum-penalty-for-a-shop/NOTES.md b/2483-minimum-penalty-for-a-shop/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2483-minimum-penalty-for-a-shop/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1b84ac10725fafb14c66c59a302039f20d59cf26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:21:20 +0530 Subject: [PATCH 1570/3167] Attach NOTES - LeetHub From 71cb3e4e1747dc452dccb67b91461e4b96a56aad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:21:23 +0530 Subject: [PATCH 1571/3167] Time: 34 ms (46.46%), Space: 30 MB (6.69%) - LeetHub From 75bd54aa20cf8b6916eb9db1934371f9b1b2e903 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:08:53 +0530 Subject: [PATCH 1572/3167] Create README - LeetHub --- .../README.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Delete a Node in Single Linked List - GFG/README.md diff --git a/Delete a Node in Single Linked List - GFG/README.md b/Delete a Node in Single Linked List - GFG/README.md new file mode 100644 index 00000000..7889f292 --- /dev/null +++ b/Delete a Node in Single Linked List - GFG/README.md @@ -0,0 +1,22 @@ +# Delete a Node in Single Linked List +## Easy +

Given a singly linked list and an integer x.Delete xth node from the singly linked list.

+

Example 1:

+
Input: 1 -> 3 -> 4 
+       x = 3
+Output: 1 -> 3
+Explanation:
+After deleting the node at 3rd
+position (1-base indexing), the
+linked list is as 1 -> 3. 
+
+

Example 2:

+
Input: 1 -> 5 -> 2 -> 9 
+x = 2
+Output: 1 -> 2 -> 9
+Explanation: 
+After deleting the node at 2nd
+position (1-based indexing), the
+linked list is as 1 -> 2 -> 9.
+

Your task: Your task is to complete the method deleteNode() which takes two arguments: the address of the head of the linked list and an integer x. The function returns the head of the modified linked list.

+

Constraints:
2 <= N <= 105
1 <= x <= N

\ No newline at end of file From 4a2786cdc1283b5ca17f9220b82972e9d4510f87 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:08:54 +0530 Subject: [PATCH 1573/3167] Added solution - LeetHub --- .../delete-a-node-in-single-linked-list.cpp | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp diff --git a/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp b/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp new file mode 100644 index 00000000..63d9e7a1 --- /dev/null +++ b/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp @@ -0,0 +1,110 @@ +//{ Driver Code Starts +// C program to find n'th Node in linked list +#include +#include +#include +using namespace std; + +/* Link list Node */ +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + +void append(struct Node** head_ref, struct Node **tail_ref, + int new_data) +{ + struct Node* new_node = new Node(new_data); + + if (*head_ref == NULL) + *head_ref = new_node; + else + (*tail_ref)->next = new_node; + *tail_ref = new_node; +} + +/* Function to get the middle of the linked list*/ +struct Node* deleteNode(struct Node *head,int ); + +void printList(Node *head) +{ + while (head != NULL) + { + cout << head->data << " "; + head = head->next; + } + cout << "\n"; +} + +/* Driver program to test above function*/ +int main() +{ + int T, i, n, l; + + cin>>T; + + while(T--){ + struct Node *head = NULL, *tail = NULL; + + cin>>n; + for(i=1;i<=n;i++) + { + cin>>l; + append(&head, &tail, l); + } + + int kk; + cin>>kk; + head = deleteNode(head,kk); + printList(head); + } + return 0; +} + +// } Driver Code Ends + + +/* Link list Node +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; +*/ + +/*You are required to complete below method*/ +Node* deleteNode(Node *head,int x) +{ + //Your code here + + if(x == 1) + return head->next; + + Node* ptr = head; + + int cnt = 1; + + while(ptr and cnt < x-1) + { + ++cnt; + ptr = ptr->next; + } + + if(ptr->next->next) + ptr->next = ptr->next->next; + else + ptr->next = nullptr; + + return head; +} From ec62b749e1b4bace436a3072a92d7f0647e877d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:16:41 +0530 Subject: [PATCH 1574/3167] Create README - LeetHub From 23bdc481ebc51a22d3c268851d7cc8d7616a19e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:16:42 +0530 Subject: [PATCH 1575/3167] Added solution - LeetHub --- .../delete-a-node-in-single-linked-list.cpp | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp b/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp index 63d9e7a1..e611a5c7 100644 --- a/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp +++ b/Delete a Node in Single Linked List - GFG/delete-a-node-in-single-linked-list.cpp @@ -88,23 +88,17 @@ Node* deleteNode(Node *head,int x) { //Your code here - if(x == 1) - return head->next; - - Node* ptr = head; - - int cnt = 1; - - while(ptr and cnt < x-1) - { - ++cnt; - ptr = ptr->next; - } - - if(ptr->next->next) - ptr->next = ptr->next->next; - else - ptr->next = nullptr; - - return head; + if(!head) + return nullptr; + + if(x == 1) + { + Node* newHead = head->next; + delete(head); + return newHead; + } + + head->next = deleteNode(head->next, x-1); + + return head; } From fc6da6287a71ebdd4c175ad9b988415b8eca4c10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 23:17:12 +0530 Subject: [PATCH 1576/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2366-minimum-replacements-to-sort-the-array/README.md diff --git a/2366-minimum-replacements-to-sort-the-array/README.md b/2366-minimum-replacements-to-sort-the-array/README.md new file mode 100644 index 00000000..19164773 --- /dev/null +++ b/2366-minimum-replacements-to-sort-the-array/README.md @@ -0,0 +1,35 @@ +

2366. Minimum Replacements to Sort the Array

Hard


You are given a 0-indexed integer array nums. In one operation you can replace any element of the array with any two elements that sum to it.

+ +
    +
  • For example, consider nums = [5,6,7]. In one operation, we can replace nums[1] with 2 and 4 and convert nums to [5,2,4,7].
  • +
+ +

Return the minimum number of operations to make an array that is sorted in non-decreasing order.

+ +

 

+

Example 1:

+ +
Input: nums = [3,9,3]
+Output: 2
+Explanation: Here are the steps to sort the array in non-decreasing order:
+- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
+- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
+There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
+
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,4,5]
+Output: 0
+Explanation: The array is already in non-decreasing order. Therefore, we return 0. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From f06844671eea284ee520d855f14af836cb6f22f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 23:17:12 +0530 Subject: [PATCH 1577/3167] Attach NOTES - LeetHub --- 2366-minimum-replacements-to-sort-the-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2366-minimum-replacements-to-sort-the-array/NOTES.md diff --git a/2366-minimum-replacements-to-sort-the-array/NOTES.md b/2366-minimum-replacements-to-sort-the-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2366-minimum-replacements-to-sort-the-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 121ec8b53c3940ff9a6e14255cabfe19231184c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 23:17:49 +0530 Subject: [PATCH 1578/3167] Create README - LeetHub From 361bc76e3dbf7a3b7a80994eb68c8b6ac8051c04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 23:17:49 +0530 Subject: [PATCH 1579/3167] Attach NOTES - LeetHub From 4cdca0e500772ee99655e6b21b7b9c8a9ae5a4e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 30 Aug 2023 23:17:53 +0530 Subject: [PATCH 1580/3167] Time: 91 ms (75.98%), Space: 54.8 MB (7.81%) - LeetHub --- ...minimum-replacements-to-sort-the-array.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2366-minimum-replacements-to-sort-the-array/2366-minimum-replacements-to-sort-the-array.cpp diff --git a/2366-minimum-replacements-to-sort-the-array/2366-minimum-replacements-to-sort-the-array.cpp b/2366-minimum-replacements-to-sort-the-array/2366-minimum-replacements-to-sort-the-array.cpp new file mode 100644 index 00000000..138b8834 --- /dev/null +++ b/2366-minimum-replacements-to-sort-the-array/2366-minimum-replacements-to-sort-the-array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + long long minimumReplacement(vector& nums) { + + int n = nums.size(); + + long long numberOfOperations = 0; + + for(int i = n-2; i >= 0; --i) + { + if(nums[i] <= nums[i+1]) + continue; + + long long parts = nums[i] / nums[i+1]; + + if(nums[i] % nums[i+1] != 0) + ++parts; + + numberOfOperations += (parts-1); + + nums[i] = (nums[i] / parts); + } + + return numberOfOperations; + + } +}; \ No newline at end of file From 0e2fb93f00660210496aef30435cda9764be9e14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 00:23:34 +0530 Subject: [PATCH 1581/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1326-minimum-number-of-taps-to-open-to-water-a-garden/README.md diff --git a/1326-minimum-number-of-taps-to-open-to-water-a-garden/README.md b/1326-minimum-number-of-taps-to-open-to-water-a-garden/README.md new file mode 100644 index 00000000..ce218a32 --- /dev/null +++ b/1326-minimum-number-of-taps-to-open-to-water-a-garden/README.md @@ -0,0 +1,38 @@ +

1326. Minimum Number of Taps to Open to Water a Garden

Hard


There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).

+ +

There are n + 1 taps located at points [0, 1, ..., n] in the garden.

+ +

Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.

+ +

Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.

+ +

 

+

Example 1:

+ +
Input: n = 5, ranges = [3,4,1,1,0,0]
+Output: 1
+Explanation: The tap at point 0 can cover the interval [-3,3]
+The tap at point 1 can cover the interval [-3,5]
+The tap at point 2 can cover the interval [1,3]
+The tap at point 3 can cover the interval [2,4]
+The tap at point 4 can cover the interval [4,4]
+The tap at point 5 can cover the interval [5,5]
+Opening Only the second tap will water the whole garden [0,5]
+
+ +

Example 2:

+ +
Input: n = 3, ranges = [0,0,0,0]
+Output: -1
+Explanation: Even if you activate all the four taps you cannot water the whole garden.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 104
  • +
  • ranges.length == n + 1
  • +
  • 0 <= ranges[i] <= 100
  • +
+
\ No newline at end of file From be27aee958bb9d27ca02fc17d04be87663e5c9f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 00:23:35 +0530 Subject: [PATCH 1582/3167] Attach NOTES - LeetHub --- 1326-minimum-number-of-taps-to-open-to-water-a-garden/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1326-minimum-number-of-taps-to-open-to-water-a-garden/NOTES.md diff --git a/1326-minimum-number-of-taps-to-open-to-water-a-garden/NOTES.md b/1326-minimum-number-of-taps-to-open-to-water-a-garden/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1326-minimum-number-of-taps-to-open-to-water-a-garden/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a73c8b71a211282bdaf00914cca9c51e935f1860 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 00:23:38 +0530 Subject: [PATCH 1583/3167] Time: 7 ms (97.74%), Space: 14.6 MB (55.15%) - LeetHub --- ...mber-of-taps-to-open-to-water-a-garden.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp diff --git a/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp b/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp new file mode 100644 index 00000000..db03c862 --- /dev/null +++ b/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int minTaps(int n, vector& ranges) { + + vector startEnd(n+1, 0); + + for(int i = 0; i < ranges.size(); ++i) + { + int start = max(0, i - ranges[i]); + int end = min(n, i + ranges[i]); + + startEnd[start] = max(startEnd[start], end); + } + + int taps = 0, maxEnd = 0, currEnd = 0; + + for(int i = 0; i <= n; ++i) + { + if(i > maxEnd) + return -1; + + if(i > currEnd) + { + ++taps; + currEnd = maxEnd; + } + + maxEnd = max(maxEnd, startEnd[i]); + } + + return taps; + + } +}; \ No newline at end of file From 4ac673b5cf1c2ba66e41d5b0d3b0ba833fcf5c72 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 07:35:20 +0530 Subject: [PATCH 1584/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Leftmost and rightmost nodes of binary tree - GFG/README.md diff --git a/Leftmost and rightmost nodes of binary tree - GFG/README.md b/Leftmost and rightmost nodes of binary tree - GFG/README.md new file mode 100644 index 00000000..60b2e632 --- /dev/null +++ b/Leftmost and rightmost nodes of binary tree - GFG/README.md @@ -0,0 +1,28 @@ +# Leftmost and rightmost nodes of binary tree +## Medium +

Given a Binary Tree of size N, Print the corner nodes ie- the node at the leftmost and rightmost of each level.

+

Example 1:

+
Input :
+         1
+       /  \
+     2      3
+    / \    / \
+   4   5  6   7    
+Output: 1 2 3 4 7
+Explanation:
+Corners at level 0: 1
+Corners at level 1: 2 3
+Corners at level 2: 4 7
+
+

Example 2:

+
Input:
+
+        10
+      /    \
+     20     30
+    / \  
+   40  60
+Output: 10 20 30 40 60
+

Your Task:  
You dont need to read input. Complete the function printCorner() which takes root node as input parameter and prints the corner nodes separated by spaces. The left corner should be printed before the right for each level starting from level 0.
Note: Don't print a new line after printing all the corner nodes.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(number of nodes in a level)

+

Constraints:
1 ≤ N ≤ 10^5

\ No newline at end of file From cfff662855559c636ca900c2ab2c3bb09675739b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 07:35:21 +0530 Subject: [PATCH 1585/3167] Added solution - LeetHub --- ...ost-and-rightmost-nodes-of-binary-tree.cpp | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 Leftmost and rightmost nodes of binary tree - GFG/leftmost-and-rightmost-nodes-of-binary-tree.cpp diff --git a/Leftmost and rightmost nodes of binary tree - GFG/leftmost-and-rightmost-nodes-of-binary-tree.cpp b/Leftmost and rightmost nodes of binary tree - GFG/leftmost-and-rightmost-nodes-of-binary-tree.cpp new file mode 100644 index 00000000..4f1c0b18 --- /dev/null +++ b/Leftmost and rightmost nodes of binary tree - GFG/leftmost-and-rightmost-nodes-of-binary-tree.cpp @@ -0,0 +1,165 @@ +//{ Driver Code Starts +#include +using namespace std; +#define MAX_HEIGHT 100000 + +// 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; +} + + +void printCorner(Node *root); + +// 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; +} + + + +int main() { + int t; + string tc; + getline(cin, tc); + t=stoi(tc); + while(t--) + { + string s ,ch; + getline(cin, s); + Node* root = buildTree(s); + + printCorner(root); + cout< q; + + q.push(root); + + cout<data<<' '; + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0; i < size; ++i) + { + Node* curr = q.front(); + q.pop(); + + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + } + + if(q.size() == 1) + cout<data<<' '; + else if(q.size() >= 2) + cout<data <<' '<data<<' '; + } + +} \ No newline at end of file From a3b6bb79f4b25fc2111bad0ed4bc0d9818cd4edd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 08:01:37 +0530 Subject: [PATCH 1586/3167] Create README - LeetHub --- 0338-counting-bits/README.md | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0338-counting-bits/README.md diff --git a/0338-counting-bits/README.md b/0338-counting-bits/README.md new file mode 100644 index 00000000..6ee2a914 --- /dev/null +++ b/0338-counting-bits/README.md @@ -0,0 +1,41 @@ +

338. Counting Bits

Easy


Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: [0,1,1]
+Explanation:
+0 --> 0
+1 --> 1
+2 --> 10
+
+ +

Example 2:

+ +
Input: n = 5
+Output: [0,1,1,2,1,2]
+Explanation:
+0 --> 0
+1 --> 1
+2 --> 10
+3 --> 11
+4 --> 100
+5 --> 101
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 105
  • +
+ +

 

+

Follow up:

+ +
    +
  • It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass?
  • +
  • Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
  • +
+
\ No newline at end of file From 5f40eb5fc6f9571d22d4a8b9d18e27fcb59a51f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 08:01:37 +0530 Subject: [PATCH 1587/3167] Attach NOTES - LeetHub --- 0338-counting-bits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0338-counting-bits/NOTES.md diff --git a/0338-counting-bits/NOTES.md b/0338-counting-bits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0338-counting-bits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5d712d319d8dac17dca5b8758cbdfd0e9181177d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 08:01:41 +0530 Subject: [PATCH 1588/3167] Time: 6 ms (56.68%), Space: 7.8 MB (65.80%) - LeetHub --- 0338-counting-bits/0338-counting-bits.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0338-counting-bits/0338-counting-bits.cpp diff --git a/0338-counting-bits/0338-counting-bits.cpp b/0338-counting-bits/0338-counting-bits.cpp new file mode 100644 index 00000000..ae3d944b --- /dev/null +++ b/0338-counting-bits/0338-counting-bits.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector countBits(int n) { + + vector dp(n+1, 0); + + dp[0] = 0; + + for(int i = 1; i <= n; ++i) + { + dp[i] = dp[i/2] + i%2; + } + + return dp; + } +}; \ No newline at end of file From 95693735de71892877aad4cfb2c1464458a873af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 08:03:54 +0530 Subject: [PATCH 1589/3167] Attach NOTES - LeetHub From 9539fc9096e99405e18d1ecec7c878555de3bf49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 08:03:57 +0530 Subject: [PATCH 1590/3167] Time: 6 ms (56.68%), Space: 7.8 MB (65.80%) - LeetHub From 55d36e34c4c059c07a086b3cf8f10ec678a92330 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:42:46 +0530 Subject: [PATCH 1591/3167] Attach NOTES - LeetHub From f2bac7df578eb183a665a2ecafbf3d2eeceaef21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:42:49 +0530 Subject: [PATCH 1592/3167] Time: 1321 ms (5.06%), Space: 179.7 MB (5.25%) - LeetHub --- ...mber-of-taps-to-open-to-water-a-garden.cpp | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp b/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp index db03c862..f3542250 100644 --- a/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp +++ b/1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp @@ -1,34 +1,44 @@ class Solution { + +private: + int helper(int idx, int maxEnd, int n, vector >& startEnd, map,int>& dp) + { + if(idx == startEnd.size()) + return (maxEnd >= n ? 0 : 1e9); + + if(dp.find({idx, maxEnd}) != dp.end()) + return dp[{idx, maxEnd}]; + + if(maxEnd < startEnd[idx].first) + return 1e9; + + int notTake = helper(idx+1, maxEnd, n, startEnd, dp); + + int take = 1 + helper(idx+1, max(maxEnd,startEnd[idx].second), n, startEnd , dp); + + return dp[{idx, maxEnd}] = min(take, notTake); + } + public: int minTaps(int n, vector& ranges) { - vector startEnd(n+1, 0); + vector > startEnd; for(int i = 0; i < ranges.size(); ++i) { int start = max(0, i - ranges[i]); int end = min(n, i + ranges[i]); - startEnd[start] = max(startEnd[start], end); + startEnd.push_back({start, end}); } - int taps = 0, maxEnd = 0, currEnd = 0; + sort(startEnd.begin(), startEnd.end()); - for(int i = 0; i <= n; ++i) - { - if(i > maxEnd) - return -1; - - if(i > currEnd) - { - ++taps; - currEnd = maxEnd; - } - - maxEnd = max(maxEnd, startEnd[i]); - } + map,int> dp; + + int ans = helper(0, 0, n, startEnd, dp); - return taps; + return (ans == 1e9 ? -1 : ans); } }; \ No newline at end of file From fd9b6609e3f04edee64a243b8a11c2de5ecaac60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:22:30 +0530 Subject: [PATCH 1593/3167] Attach NOTES - LeetHub --- 0955-delete-columns-to-make-sorted-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0955-delete-columns-to-make-sorted-ii/NOTES.md diff --git a/0955-delete-columns-to-make-sorted-ii/NOTES.md b/0955-delete-columns-to-make-sorted-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0955-delete-columns-to-make-sorted-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7a6c98d06f75c0199aca909c99ed8ec78b155db4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:22:31 +0530 Subject: [PATCH 1594/3167] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0955-delete-columns-to-make-sorted-ii/README.md diff --git a/0955-delete-columns-to-make-sorted-ii/README.md b/0955-delete-columns-to-make-sorted-ii/README.md new file mode 100644 index 00000000..6e9342e7 --- /dev/null +++ b/0955-delete-columns-to-make-sorted-ii/README.md @@ -0,0 +1,46 @@ +

955. Delete Columns to Make Sorted II

Medium


You are given an array of n strings strs, all of the same length.

+ +

We may choose any deletion indices, and we delete all the characters in those indices for each string.

+ +

For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"].

+ +

Suppose we chose a set of deletion indices answer such that after deletions, the final array has its elements in lexicographic order (i.e., strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]). Return the minimum possible value of answer.length.

+ +

 

+

Example 1:

+ +
Input: strs = ["ca","bb","ac"]
+Output: 1
+Explanation: 
+After deleting the first column, strs = ["a", "b", "c"].
+Now strs is in lexicographic order (ie. strs[0] <= strs[1] <= strs[2]).
+We require at least 1 deletion since initially strs was not in lexicographic order, so the answer is 1.
+
+ +

Example 2:

+ +
Input: strs = ["xc","yb","za"]
+Output: 0
+Explanation: 
+strs is already in lexicographic order, so we do not need to delete anything.
+Note that the rows of strs are not necessarily in lexicographic order:
+i.e., it is NOT necessarily true that (strs[0][0] <= strs[0][1] <= ...)
+
+ +

Example 3:

+ +
Input: strs = ["zyx","wvu","tsr"]
+Output: 3
+Explanation: We have to delete every column.
+
+ +

 

+

Constraints:

+ +
    +
  • n == strs.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= strs[i].length <= 100
  • +
  • strs[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file From e8ce1779aaba8b568a2c344acb09d6320a2049aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:22:34 +0530 Subject: [PATCH 1595/3167] Time: 7 ms (67.03%), Space: 10.1 MB (65.93%) - LeetHub --- .../0955-delete-columns-to-make-sorted-ii.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0955-delete-columns-to-make-sorted-ii/0955-delete-columns-to-make-sorted-ii.cpp diff --git a/0955-delete-columns-to-make-sorted-ii/0955-delete-columns-to-make-sorted-ii.cpp b/0955-delete-columns-to-make-sorted-ii/0955-delete-columns-to-make-sorted-ii.cpp new file mode 100644 index 00000000..fd90fcc2 --- /dev/null +++ b/0955-delete-columns-to-make-sorted-ii/0955-delete-columns-to-make-sorted-ii.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int minDeletionSize(vector& strs) { + + int n = strs.size(); + int m = strs[0].size(); + + unordered_set used; + + for(int i = 1; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(used.count(j) > 0 or strs[i-1][j] == strs[i][j]) + continue; + if(strs[i-1][j] > strs[i][j]) + { + used.insert(j); + i = 0; + } + + break; + } + } + + return used.size(); + } +}; \ No newline at end of file From d808e22a1f7d2432f036f7c78f4bc05fb8cd3a8b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:23:24 +0530 Subject: [PATCH 1596/3167] Attach NOTES - LeetHub From f75c68fa4db72e2ce0dfab8570dbc2128f93e3a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:23:27 +0530 Subject: [PATCH 1597/3167] Time: 7 ms (67.03%), Space: 10.1 MB (65.93%) - LeetHub From b717bee212df22da1716ea1f4a84acdab2799744 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:24:27 +0530 Subject: [PATCH 1598/3167] Attach NOTES - LeetHub From 81908861e2cf8b97393eacd5c500ae0f6e7d7ae9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:24:30 +0530 Subject: [PATCH 1599/3167] Time: 7 ms (67.03%), Space: 10.1 MB (65.93%) - LeetHub From 44efd2d1a36cf755ebae545458cc256f13a234bb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:26:45 +0530 Subject: [PATCH 1600/3167] Attach NOTES - LeetHub From 60d951121cd4023987749a63f0fb2b0297c68a59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:26:49 +0530 Subject: [PATCH 1601/3167] Time: 7 ms (67.03%), Space: 10.1 MB (65.93%) - LeetHub From 88dec41de42a0ea59de8f75fd154d6704003c96a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 02:06:18 +0530 Subject: [PATCH 1602/3167] Attach NOTES - LeetHub --- 0992-subarrays-with-k-different-integers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0992-subarrays-with-k-different-integers/NOTES.md diff --git a/0992-subarrays-with-k-different-integers/NOTES.md b/0992-subarrays-with-k-different-integers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0992-subarrays-with-k-different-integers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2e1d73ccb7a1d21698976040dad8905798d72b84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 02:06:22 +0530 Subject: [PATCH 1603/3167] Time: 127 ms (59.36%), Space: 46.3 MB (29.09%) - LeetHub --- ...92-subarrays-with-k-different-integers.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0992-subarrays-with-k-different-integers/0992-subarrays-with-k-different-integers.cpp diff --git a/0992-subarrays-with-k-different-integers/0992-subarrays-with-k-different-integers.cpp b/0992-subarrays-with-k-different-integers/0992-subarrays-with-k-different-integers.cpp new file mode 100644 index 00000000..b0579a54 --- /dev/null +++ b/0992-subarrays-with-k-different-integers/0992-subarrays-with-k-different-integers.cpp @@ -0,0 +1,38 @@ +class Solution { + +private: + int subarrayWithAtmost(vector& nums, int k) + { + int i = 0, j = 0, n = nums.size(); + + unordered_map mp; + + int ans = 0; + + while(j < n) + { + ++mp[nums[j]]; + + while(mp.size() > k) + { + --mp[nums[i]]; + if(mp[nums[i]] == 0) + mp.erase(nums[i]); + ++i; + } + + ans += (j - i + 1); + ++j; + } + + return ans; + } + +public: + + int subarraysWithKDistinct(vector& nums, int k) { + + return subarrayWithAtmost(nums, k) - subarrayWithAtmost(nums, k-1); + + } +}; \ No newline at end of file From 2c135fddbfa345fb31e9aca3cb5d6fe314f8c553 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 07:24:23 +0530 Subject: [PATCH 1604/3167] Create README - LeetHub --- Leaf under budget - GFG/README.md | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Leaf under budget - GFG/README.md diff --git a/Leaf under budget - GFG/README.md b/Leaf under budget - GFG/README.md new file mode 100644 index 00000000..b6c78481 --- /dev/null +++ b/Leaf under budget - GFG/README.md @@ -0,0 +1,32 @@ +# Leaf under budget +## Easy +

Given a binary tree and a budget. Assume you are at the root of the tree(level 1), you need to maximise the count of leaf nodes you can visit in your budget if the cost of visiting a leaf node is equal to the level of that leaf node.

+

Example 1:

+
Input: 
+                  10
+                /    \
+               8      2
+             /      /   \
+            3      3     6
+                    \
+                     4
+and budget = 8
+Output: 2
+Explanation:
+Cost For visiting Leaf Node 3: 3
+Cost For visiting Leaf Node 4: 4
+Cost For visiting Leaf Node 6: 3
+In budget 8 one can visit Max 2 Leaf Nodes.
+

Example 2:

+
Input: 
+         1
+       /   \
+      2     3
+     / \   / \
+    4   5 6   7
+and budget = 5
+Output: 1
Explanation: We can only visit either node 4 or 5.
+

Your Task:

+

You don't need to read input or print anything. Your task is to complete the function getCount() which takes root node of the tree and a integer denoting the budget as input parameters and returns an integer denoting the count of visited leaf nodes of the tree.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

+

Constraints:
1<=N<=105
1<=budget<=104

\ No newline at end of file From e9fafcd976feb5d99b3d2c95db161258a14b2c94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 07:24:23 +0530 Subject: [PATCH 1605/3167] Added solution - LeetHub --- Leaf under budget - GFG/leaf-under-budget.cpp | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 Leaf under budget - GFG/leaf-under-budget.cpp diff --git a/Leaf under budget - GFG/leaf-under-budget.cpp b/Leaf under budget - GFG/leaf-under-budget.cpp new file mode 100644 index 00000000..ef9c38d3 --- /dev/null +++ b/Leaf under budget - GFG/leaf-under-budget.cpp @@ -0,0 +1,193 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x) + { + data = x; + left = NULL; + right = NULL; + } +}; + +void printInorder(Node *node) +{ + if (node == NULL) + { + return; + } + printInorder(node->left); + cout << node->data << " "; + printInorder(node->right); +} +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 +/* +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x) + { + data = x; + left = NULL; + right = NULL; + } +}; +*/ +class Solution +{ +public: + int getCount(Node *root, int k) + { + //code here + + map mp; + + queue q; + + q.push(root); + + int level = 1; + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0; i < size; ++i) + { + Node* curr = q.front(); + q.pop(); + + if(!curr->left and !curr->right) + { + ++mp[level]; + continue; + } + + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + } + ++level; + } + + int leaf = 0; + + for(auto itr : mp) + { + if(itr.second * itr.first <= k) + { + leaf += itr.second; + k -= (itr.second * itr.first); + } + else{ + leaf += k/itr.first; + break; + } + } + + return leaf; + } +}; + +//{ Driver Code Starts. + +int main() +{ + int t; + scanf("%d", &t); + cin.ignore(); + while (t--) + { + string treeString; + getline(cin, treeString); + Node *root = buildTree(treeString); + int k; + cin >> k; + cin.ignore(); + Solution obj; + int res = obj.getCount(root, k); + cout << res << "\n"; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 24464bd617e6779fe55fdf0c6a6c4dd67ab3e772 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 09:03:55 +0530 Subject: [PATCH 1606/3167] Create README - LeetHub --- 2707-extra-characters-in-a-string/README.md | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2707-extra-characters-in-a-string/README.md diff --git a/2707-extra-characters-in-a-string/README.md b/2707-extra-characters-in-a-string/README.md new file mode 100644 index 00000000..1f148b75 --- /dev/null +++ b/2707-extra-characters-in-a-string/README.md @@ -0,0 +1,31 @@ +

2707. Extra Characters in a String

Medium


You are given a 0-indexed string s and a dictionary of words dictionary. You have to break s into one or more non-overlapping substrings such that each substring is present in dictionary. There may be some extra characters in s which are not present in any of the substrings.

+ +

Return the minimum number of extra characters left over if you break up s optimally.

+ +

 

+

Example 1:

+ +
Input: s = "leetscode", dictionary = ["leet","code","leetcode"]
+Output: 1
+Explanation: We can break s in two substrings: "leet" from index 0 to 3 and "code" from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.
+
+
+ +

Example 2:

+ +
Input: s = "sayhelloworld", dictionary = ["hello","world"]
+Output: 3
+Explanation: We can break s in two substrings: "hello" from index 3 to 7 and "world" from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 50
  • +
  • 1 <= dictionary.length <= 50
  • +
  • 1 <= dictionary[i].length <= 50
  • +
  • dictionary[i] and s consists of only lowercase English letters
  • +
  • dictionary contains distinct words
  • +
+
\ No newline at end of file From 0cabea0d5ab5743c8a4ecab038604a52e8ce8b9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 09:03:59 +0530 Subject: [PATCH 1607/3167] Time: 99 ms (85.25%), Space: 62.3 MB (91.23%) - LeetHub --- .../2707-extra-characters-in-a-string.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2707-extra-characters-in-a-string/2707-extra-characters-in-a-string.cpp diff --git a/2707-extra-characters-in-a-string/2707-extra-characters-in-a-string.cpp b/2707-extra-characters-in-a-string/2707-extra-characters-in-a-string.cpp new file mode 100644 index 00000000..46b0c857 --- /dev/null +++ b/2707-extra-characters-in-a-string/2707-extra-characters-in-a-string.cpp @@ -0,0 +1,41 @@ +class Solution { + +private: + int helper(int idx, int n, string& s, unordered_set& used, vector& dp) + { + if(idx == n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int minExtra = n; + + string temp; + + for(int i = idx; i < n; ++i) + { + temp += s[i]; + + int currExtra = used.find(temp) != used.end() ? 0 : temp.size(); + int nextExtra = helper(i + 1, n, s, used , dp); + int totalExtra = currExtra + nextExtra; + + minExtra = min(minExtra, totalExtra); + + } + return dp[idx] = minExtra; + } + +public: + int minExtraChar(string s, vector& dictionary) { + + int n = s.size(); + + unordered_set used(dictionary.begin(), dictionary.end()); + + vector dp(n+1, -1); + + return helper(0, n, s, used, dp); + } +}; \ No newline at end of file From 3d9d120963508aa35288fec49d70c623a9e2454a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:50:58 +0530 Subject: [PATCH 1608/3167] Create README - LeetHub --- 1797-design-authentication-manager/README.md | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1797-design-authentication-manager/README.md diff --git a/1797-design-authentication-manager/README.md b/1797-design-authentication-manager/README.md new file mode 100644 index 00000000..032431b0 --- /dev/null +++ b/1797-design-authentication-manager/README.md @@ -0,0 +1,46 @@ +

1797. Design Authentication Manager

Medium


There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire timeToLive seconds after the currentTime. If the token is renewed, the expiry time will be extended to expire timeToLive seconds after the (potentially different) currentTime.

+ +

Implement the AuthenticationManager class:

+ +
    +
  • AuthenticationManager(int timeToLive) constructs the AuthenticationManager and sets the timeToLive.
  • +
  • generate(string tokenId, int currentTime) generates a new token with the given tokenId at the given currentTime in seconds.
  • +
  • renew(string tokenId, int currentTime) renews the unexpired token with the given tokenId at the given currentTime in seconds. If there are no unexpired tokens with the given tokenId, the request is ignored, and nothing happens.
  • +
  • countUnexpiredTokens(int currentTime) returns the number of unexpired tokens at the given currentTime.
  • +
+ +

Note that if a token expires at time t, and another action happens on time t (renew or countUnexpiredTokens), the expiration takes place before the other actions.

+ +

 

+

Example 1:

+ +
Input
+["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"]
+[[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
+Output
+[null, null, null, 1, null, null, null, 0]
+
+Explanation
+AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with timeToLive = 5 seconds.
+authenticationManager.renew("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens.
+authenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2.
+authenticationManager.countUnexpiredTokens(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1.
+authenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7.
+authenticationManager.renew("aaa", 8); // The token with tokenId "aaa" expired at time 7, and 8 >= 7, so at time 8 the renew request is ignored, and nothing happens.
+authenticationManager.renew("bbb", 10); // The token with tokenId "bbb" is unexpired at time 10, so the renew request is fulfilled and now the token will expire at time 15.
+authenticationManager.countUnexpiredTokens(15); // The token with tokenId "bbb" expires at time 15, and the token with tokenId "aaa" expired at time 7, so currently no token is unexpired, so return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= timeToLive <= 108
  • +
  • 1 <= currentTime <= 108
  • +
  • 1 <= tokenId.length <= 5
  • +
  • tokenId consists only of lowercase letters.
  • +
  • All calls to generate will contain unique values of tokenId.
  • +
  • The values of currentTime across all the function calls will be strictly increasing.
  • +
  • At most 2000 calls will be made to all functions combined.
  • +
+
\ No newline at end of file From 8d99f52daa9b92f69917dd82644021eaa8cdb27f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:51:02 +0530 Subject: [PATCH 1609/3167] Time: 90 ms (61.81%), Space: 30.2 MB (59.72%) - LeetHub --- .../1797-design-authentication-manager.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1797-design-authentication-manager/1797-design-authentication-manager.cpp diff --git a/1797-design-authentication-manager/1797-design-authentication-manager.cpp b/1797-design-authentication-manager/1797-design-authentication-manager.cpp new file mode 100644 index 00000000..0bb54bef --- /dev/null +++ b/1797-design-authentication-manager/1797-design-authentication-manager.cpp @@ -0,0 +1,43 @@ +class AuthenticationManager { +public: + + int timeToLive; + + map mp; + + AuthenticationManager(int timeToLive) { + this->timeToLive = timeToLive; + } + + void generate(string tokenId, int currentTime) { + mp[tokenId] = currentTime + timeToLive; + } + + void renew(string tokenId, int currentTime) { + + if(mp[tokenId] > currentTime) + mp[tokenId] = currentTime + timeToLive; + + } + + int countUnexpiredTokens(int currentTime) { + + int counter = 0; + + for(auto& itr: mp) + { + if(itr.second > currentTime) + ++counter; + } + + return counter; + } +}; + +/** + * Your AuthenticationManager object will be instantiated and called as such: + * AuthenticationManager* obj = new AuthenticationManager(timeToLive); + * obj->generate(tokenId,currentTime); + * obj->renew(tokenId,currentTime); + * int param_3 = obj->countUnexpiredTokens(currentTime); + */ \ No newline at end of file From 9d788995dfceeec8247f0dae16408da66ef94960 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:52:36 +0530 Subject: [PATCH 1610/3167] Attach NOTES - LeetHub --- 1797-design-authentication-manager/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1797-design-authentication-manager/NOTES.md diff --git a/1797-design-authentication-manager/NOTES.md b/1797-design-authentication-manager/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1797-design-authentication-manager/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e387de10fe0f016b6343d1153acf1d244d6cfb22 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:52:40 +0530 Subject: [PATCH 1611/3167] Time: 90 ms (61.81%), Space: 30.2 MB (59.72%) - LeetHub From f830ea60595fd623d09b57756d8e484401161d28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:54:43 +0530 Subject: [PATCH 1612/3167] Attach NOTES - LeetHub From a77450b6805bfc6827e01dc4c4cd080aee302f45 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:54:46 +0530 Subject: [PATCH 1613/3167] Time: 90 ms (61.81%), Space: 30.2 MB (59.72%) - LeetHub From d9f78960b2c42d91e55a53f672ce5ac38a32076d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:59:26 +0530 Subject: [PATCH 1614/3167] Attach NOTES - LeetHub From 81ea9f59821fd38da5acbc28a3fb82db25f76d8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:59:29 +0530 Subject: [PATCH 1615/3167] Time: 90 ms (61.81%), Space: 30.2 MB (59.72%) - LeetHub From 77d29a00029c4d1a6000e9477ea3ba571ac1baf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Sep 2023 09:12:14 +0530 Subject: [PATCH 1616/3167] Attach NOTES - LeetHub From 7ffbb8cfcc0e781cd265c5ab8aa7806d9fb57375 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Sep 2023 09:12:17 +0530 Subject: [PATCH 1617/3167] Time: 0 ms (100.00%), Space: 6.7 MB (6.60%) - LeetHub --- 0062-unique-paths/0062-unique-paths.cpp | 35 +++++++++---------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/0062-unique-paths/0062-unique-paths.cpp b/0062-unique-paths/0062-unique-paths.cpp index c2c876bf..5c69fb45 100644 --- a/0062-unique-paths/0062-unique-paths.cpp +++ b/0062-unique-paths/0062-unique-paths.cpp @@ -1,7 +1,7 @@ class Solution { -public: - int helper(int i , int j, int n , int m, vector>& dp) +private: + int helper(int i, int j, int n, int m, vector>& dp) { if(i == n-1 and j == m-1) return 1; @@ -9,32 +9,21 @@ class Solution { if(dp[i][j] != -1) return dp[i][j]; - if(i >= n or j >= m) - return 0; + int down = 0, right = 0; - return dp[i][j] = helper(i+1, j, n, m, dp) + helper(i, j+1, n, m, dp); + if(i+1 < n) + down = helper(i+1, j, n, m, dp); + if(j+1 < m) + right = helper(i, j+1, n, m, dp); + + return dp[i][j] = down + right; } - +public: int uniquePaths(int m, int n) { - // vector> dp(m+1, vector(n+1,0)); - - // return helper(0,0,m ,n, dp); - - vector prev(n+1, 0), curr(n+1, 0); + vector > dp(m+1, vector(n+1, -1)); - for(int i = m-1; i>=0 ; --i) - { - for(int j = n-1; j >= 0; --j) - { - if(i == m-1 and j == n-1) - curr[j] = 1; - else - curr[j] = prev[j] + curr[j+1]; - } - prev = curr; - } + return helper(0, 0, m , n, dp); - return prev[0]; } }; \ No newline at end of file From ad2060de9a4edc8bbe2bb8acd8a467e5231cc32e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Sep 2023 09:26:20 +0530 Subject: [PATCH 1618/3167] Create README - LeetHub --- Check if Tree is Isomorphic - GFG/README.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Check if Tree is Isomorphic - GFG/README.md diff --git a/Check if Tree is Isomorphic - GFG/README.md b/Check if Tree is Isomorphic - GFG/README.md new file mode 100644 index 00000000..728fd7bb --- /dev/null +++ b/Check if Tree is Isomorphic - GFG/README.md @@ -0,0 +1,26 @@ +# Check if Tree is Isomorphic +## Easy +

Given two Binary Trees. Check whether they are Isomorphic or not.

+

Note: 
Two trees are called isomorphic if one can be obtained from another by a series of flips, i.e. by swapping left and right children of several nodes. Any number of nodes at any level can have their children swapped. Two empty trees are isomorphic.
For example, the following two trees are isomorphic with the following sub-trees flipped: 2 and 3, NULL and 6, 7 and 8.
ISomorphicTrees

+

Example 1:

+
Input:
+ T1    1     T2:   1
+     /   \        /  \
+    2     3      3    2
+   /            /
+  4            4
+Output: No
+
+
+

Example 2:

+
Input:
+T1    1     T2:    1
+    /  \         /   \
+   2    3       3     2
+  /                    \
+  4                     4
+Output: Yes
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function isomorphic() that takes the root nodes of both the Binary Trees as its input and returns True if the two trees are isomorphic. Else, it returns False. (The driver code will print Yes if the returned values are true, otherwise false.)

+

Expected Time Complexity: O(min(M, N)) where M and N are the sizes of the two trees.
Expected Auxiliary Space: O(min(H1, H2)) where H1 and H2 are the heights of the two trees.

+

Constraints:
1<=Number of nodes<=105

\ No newline at end of file From e582df4003f8293c579e8d9beeac0fcd48321acc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Sep 2023 09:26:21 +0530 Subject: [PATCH 1619/3167] Added solution - LeetHub --- .../check-if-tree-is-isomorphic.cpp | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Check if Tree is Isomorphic - GFG/check-if-tree-is-isomorphic.cpp diff --git a/Check if Tree is Isomorphic - GFG/check-if-tree-is-isomorphic.cpp b/Check if Tree is Isomorphic - GFG/check-if-tree-is-isomorphic.cpp new file mode 100644 index 00000000..3f9bc783 --- /dev/null +++ b/Check if Tree is Isomorphic - GFG/check-if-tree-is-isomorphic.cpp @@ -0,0 +1,139 @@ +//{ Driver Code Starts +#include +using namespace std; +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +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 +/*Complete the function below +Node is as follows: +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +*/ + +class Solution{ + + public: + // Return True if the given trees are isomotphic. Else return False. + bool isIsomorphic(Node *root1,Node *root2) + { + //add code here. + + if(!root1 and !root2) + return true; + + if(!root1 or !root2) + return false; + + bool valCheck = root1->data == root2->data; + + bool one = isIsomorphic(root1->left, root2->left) and isIsomorphic(root1->right, root2->right); + + bool two = isIsomorphic(root1->left, root2->right) and isIsomorphic(root1->right, root2->left); + + return valCheck & (one or two); + } +}; + +//{ Driver Code Starts. + +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + string s1,s2; + getline(cin,s1); + getline(cin,s2); + Node* root1 = buildTree(s1); + Node* root2 = buildTree(s2); + Solution obj; + if(obj.isIsomorphic(root1,root2)) + cout<<"Yes"< Date: Sun, 3 Sep 2023 13:30:48 +0530 Subject: [PATCH 1620/3167] Create README - LeetHub --- .../README.md | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 2842-count-k-subsequences-of-a-string-with-maximum-beauty/README.md diff --git a/2842-count-k-subsequences-of-a-string-with-maximum-beauty/README.md b/2842-count-k-subsequences-of-a-string-with-maximum-beauty/README.md new file mode 100644 index 00000000..3cb52c69 --- /dev/null +++ b/2842-count-k-subsequences-of-a-string-with-maximum-beauty/README.md @@ -0,0 +1,69 @@ +

2842. Count K-Subsequences of a String With Maximum Beauty

Hard


You are given a string s and an integer k.

+ +

A k-subsequence is a subsequence of s, having length k, and all its characters are unique, i.e., every character occurs once.

+ +

Let f(c) denote the number of times the character c occurs in s.

+ +

The beauty of a k-subsequence is the sum of f(c) for every character c in the k-subsequence.

+ +

For example, consider s = "abbbdd" and k = 2:

+ +
    +
  • f('a') = 1, f('b') = 3, f('d') = 2
  • +
  • Some k-subsequences of s are: +
      +
    • "abbbdd" -> "ab" having a beauty of f('a') + f('b') = 4
    • +
    • "abbbdd" -> "ad" having a beauty of f('a') + f('d') = 3
    • +
    • "abbbdd" -> "bd" having a beauty of f('b') + f('d') = 5
    • +
    +
  • +
+ +

Return an integer denoting the number of k-subsequences whose beauty is the maximum among all k-subsequences. Since the answer may be too large, return it modulo 109 + 7.

+ +

A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.

+ +

Notes

+ +
    +
  • f(c) is the number of times a character c occurs in s, not a k-subsequence.
  • +
  • Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string.
  • +
+ +

 

+

Example 1:

+ +
Input: s = "bcca", k = 2
+Output: 4
+Explanation: From s we have f('a') = 1, f('b') = 1, and f('c') = 2.
+The k-subsequences of s are: 
+bcca having a beauty of f('b') + f('c') = 3 
+bcca having a beauty of f('b') + f('c') = 3 
+bcca having a beauty of f('b') + f('a') = 2 
+bcca having a beauty of f('c') + f('a') = 3
+bcca having a beauty of f('c') + f('a') = 3 
+There are 4 k-subsequences that have the maximum beauty, 3. 
+Hence, the answer is 4. 
+
+ +

Example 2:

+ +
Input: s = "abbcd", k = 4
+Output: 2
+Explanation: From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1. 
+The k-subsequences of s are: 
+abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5
+abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5 
+There are 2 k-subsequences that have the maximum beauty, 5. 
+Hence, the answer is 2. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 2 * 105
  • +
  • 1 <= k <= s.length
  • +
  • s consists only of lowercase English letters.
  • +
+
\ No newline at end of file From 511d39cbc29923267bbefbaee62a996c1cb15140 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Sep 2023 13:30:52 +0530 Subject: [PATCH 1621/3167] Time: 27 ms (100.00%), Space: 12.4 MB (90.00%) - LeetHub --- ...uences-of-a-string-with-maximum-beauty.cpp | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 2842-count-k-subsequences-of-a-string-with-maximum-beauty/2842-count-k-subsequences-of-a-string-with-maximum-beauty.cpp diff --git a/2842-count-k-subsequences-of-a-string-with-maximum-beauty/2842-count-k-subsequences-of-a-string-with-maximum-beauty.cpp b/2842-count-k-subsequences-of-a-string-with-maximum-beauty/2842-count-k-subsequences-of-a-string-with-maximum-beauty.cpp new file mode 100644 index 00000000..fe1b1734 --- /dev/null +++ b/2842-count-k-subsequences-of-a-string-with-maximum-beauty/2842-count-k-subsequences-of-a-string-with-maximum-beauty.cpp @@ -0,0 +1,88 @@ +class Solution { + +public: + + const int mod = 1e9 + 7; + + long long minv(long long x, long long y) + { + long long res = 1; + + while(y > 0) + { + if(y & 1) + res = (res*x)%mod; + + x = (x*x) % mod; + + y >>= 1; + } + + return (int)res; + } + + long long calculate(int n, int r) + { + long long res = 1; + + for(int i = 1; i <= n; ++i) + { + res *= i; + res %= mod; + } + + for(int i = 1; i <= r; ++i) + { + res *= minv(i, mod-2); + res %= mod; + } + + for(int i = 1; i <= (n-r); ++i) + { + res *= minv(i, mod-2); + res %= mod; + } + + return (int)res; + } + + + + int countKSubsequencesWithMaxBeauty(string s, int k) { + + if(k > 26) + return 0; + + vector v(26,0); + + for(auto& itr : s) + ++v[itr - 'a']; + + sort(v.rbegin(),v.rend()); + + long long res = 1, req = 0; + + for(int i = 0; i < k; ++i) + { + res *= v[i]; + res %= mod; + if(v[i] == v[k-1]) + ++req; + } + + int f = 0; + + for(int i = 0; i < 26; ++i) + { + if(v[i] == v[k-1]) + ++f; + } + + res *= calculate(f, req); + + res %= mod; + + return (int)res; + + } +}; \ No newline at end of file From 76e1c4f865800d11b576166d7cd11ef2d7d6095c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 08:31:37 +0530 Subject: [PATCH 1622/3167] Create README - LeetHub --- 0141-linked-list-cycle/README.md | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0141-linked-list-cycle/README.md diff --git a/0141-linked-list-cycle/README.md b/0141-linked-list-cycle/README.md new file mode 100644 index 00000000..2621bdae --- /dev/null +++ b/0141-linked-list-cycle/README.md @@ -0,0 +1,40 @@ +

141. Linked List Cycle

Easy


Given head, the head of a linked list, determine if the linked list has a cycle in it.

+ +

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

+ +

Return true if there is a cycle in the linked list. Otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: head = [3,2,0,-4], pos = 1
+Output: true
+Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
+
+ +

Example 2:

+ +
Input: head = [1,2], pos = 0
+Output: true
+Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
+
+ +

Example 3:

+ +
Input: head = [1], pos = -1
+Output: false
+Explanation: There is no cycle in the linked list.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the list is in the range [0, 104].
  • +
  • -105 <= Node.val <= 105
  • +
  • pos is -1 or a valid index in the linked-list.
  • +
+ +

 

+

Follow up: Can you solve it using O(1) (i.e. constant) memory?

+
\ No newline at end of file From 49a142cfd368afc0f916e91a2d73d01eee7c330b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 08:31:37 +0530 Subject: [PATCH 1623/3167] Attach NOTES - LeetHub --- 0141-linked-list-cycle/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0141-linked-list-cycle/NOTES.md diff --git a/0141-linked-list-cycle/NOTES.md b/0141-linked-list-cycle/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0141-linked-list-cycle/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9240720892820c525a7345b2d0c197a57371dba8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 08:31:41 +0530 Subject: [PATCH 1624/3167] Time: 3 ms (99.55%), Space: 8.3 MB (21.01%) - LeetHub --- .../0141-linked-list-cycle.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0141-linked-list-cycle/0141-linked-list-cycle.cpp diff --git a/0141-linked-list-cycle/0141-linked-list-cycle.cpp b/0141-linked-list-cycle/0141-linked-list-cycle.cpp new file mode 100644 index 00000000..3518a123 --- /dev/null +++ b/0141-linked-list-cycle/0141-linked-list-cycle.cpp @@ -0,0 +1,26 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + + ListNode* slow = head, *fast = head; + + while(fast and fast->next) + { + slow = slow->next; + fast = fast->next->next; + if(fast == slow) + return true; + } + + return false; + + } +}; \ No newline at end of file From 90e4882248fbbbae49bd0c4e341b6105b6f863a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 08:32:29 +0530 Subject: [PATCH 1625/3167] Attach NOTES - LeetHub From 4da214416baf232199fcf2fcef9282280bcb259c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 08:32:32 +0530 Subject: [PATCH 1626/3167] Time: 3 ms (99.55%), Space: 8.3 MB (21.01%) - LeetHub From 58c1ae3e7e17cfc56795cf87698290bbdff62e2e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:18:09 +0530 Subject: [PATCH 1627/3167] Attach NOTES - LeetHub --- 0560-subarray-sum-equals-k/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0560-subarray-sum-equals-k/NOTES.md diff --git a/0560-subarray-sum-equals-k/NOTES.md b/0560-subarray-sum-equals-k/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0560-subarray-sum-equals-k/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ea4069e33f344866c24477127b7902348fe8bcf2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:18:12 +0530 Subject: [PATCH 1628/3167] Time: 60 ms (93.73%), Space: 41.5 MB (61.81%) - LeetHub --- .../0560-subarray-sum-equals-k.cpp | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp b/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp index 6d94c12e..617fb67d 100644 --- a/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp +++ b/0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp @@ -2,26 +2,22 @@ class Solution { public: int subarraySum(vector& nums, int k) { - int n = nums.size(); + unordered_map mp; - int sum = 0; - int count = 0; + int sum = 0, counter = 0; - unordered_map mp; - mp.insert({0,1}); + mp.insert({0, 1}); - for(int i = 0; i < n; ++i) + for(auto& itr : nums) { - sum += nums[i]; + sum += itr; if(mp.find(sum - k) != mp.end()) - count += mp[sum - k]; - if(mp.find(sum) != mp.end()) - ++mp[sum]; - else - mp[sum] = 1; + counter += mp[sum - k]; + + ++mp[sum]; } - return count; + return counter; } }; \ No newline at end of file From 56310697c9d7e1140df598dd578e120728efb325 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:21:46 +0530 Subject: [PATCH 1629/3167] Attach NOTES - LeetHub From 7ac6346eca977001f7107393c66a5050754321bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:43:58 +0530 Subject: [PATCH 1630/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1010-pairs-of-songs-with-total-durations-divisible-by-60/README.md diff --git a/1010-pairs-of-songs-with-total-durations-divisible-by-60/README.md b/1010-pairs-of-songs-with-total-durations-divisible-by-60/README.md new file mode 100644 index 00000000..7dc115db --- /dev/null +++ b/1010-pairs-of-songs-with-total-durations-divisible-by-60/README.md @@ -0,0 +1,30 @@ +

1010. Pairs of Songs With Total Durations Divisible by 60

Medium


You are given a list of songs where the ith song has a duration of time[i] seconds.

+ +

Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.

+ +

 

+

Example 1:

+ +
Input: time = [30,20,150,100,40]
+Output: 3
+Explanation: Three pairs have a total duration divisible by 60:
+(time[0] = 30, time[2] = 150): total duration 180
+(time[1] = 20, time[3] = 100): total duration 120
+(time[1] = 20, time[4] = 40): total duration 60
+
+ +

Example 2:

+ +
Input: time = [60,60,60]
+Output: 3
+Explanation: All three pairs have a total duration of 120, which is divisible by 60.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= time.length <= 6 * 104
  • +
  • 1 <= time[i] <= 500
  • +
+
\ No newline at end of file From 687b6740b3d4bec84a537e08fabb7d2d09fb4532 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:44:02 +0530 Subject: [PATCH 1631/3167] Time: 24 ms (89.96%), Space: 28.1 MB (51.98%) - LeetHub --- ...s-with-total-durations-divisible-by-60.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp diff --git a/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp b/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp new file mode 100644 index 00000000..1fd3624d --- /dev/null +++ b/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int numPairsDivisibleBy60(vector& time) { + + vector songFreq(60, 0); + + for(auto& songTime : time) + { + ++songFreq[songTime%60]; + } + + long long counter = 0; + + for(int i = 1; i < 30; ++i) + { + counter += (songFreq[i] * 1LL * songFreq[60 - i]); + } + + counter += ((songFreq[0] * 1LL * (songFreq[0]-1))/2) + ((songFreq[30] * 1LL *(songFreq[30]-1)/2)); + + return (int) counter; + } +}; \ No newline at end of file From 5fd2484c47179779c8d9f7bb00fd92f11793b97f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:47:41 +0530 Subject: [PATCH 1632/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md diff --git a/1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md b/1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d64845c15a6bb7443cac15f3aba0b5c490d00112 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:47:44 +0530 Subject: [PATCH 1633/3167] Time: 24 ms (89.96%), Space: 28.1 MB (51.98%) - LeetHub From 57af806678297957c21b92d861bf097b18079625 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:49:47 +0530 Subject: [PATCH 1634/3167] Attach NOTES - LeetHub From f2ec2eeb4d308babca2931971f29c1402d27c4d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:49:50 +0530 Subject: [PATCH 1635/3167] Time: 24 ms (89.96%), Space: 28.1 MB (51.98%) - LeetHub From faf72d91b9bd18c36eb8eea2560d2e20bf6b5cce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:51:46 +0530 Subject: [PATCH 1636/3167] Attach NOTES - LeetHub From 708ec3d5efbd0c43d0fc8d45bc0e274cf8eea2b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:51:49 +0530 Subject: [PATCH 1637/3167] Time: 24 ms (89.96%), Space: 28.1 MB (51.98%) - LeetHub From d7c01cce13ee185f13041e2479f9e27c316b709d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:52:16 +0530 Subject: [PATCH 1638/3167] Attach NOTES - LeetHub From 645a693976e8895e5c32a2c3285eeddd42ee12e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:52:20 +0530 Subject: [PATCH 1639/3167] Time: 24 ms (89.96%), Space: 28.1 MB (51.98%) - LeetHub From 35951bf046f57d7e602a76e59e5d79c9a2235e6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:53:39 +0530 Subject: [PATCH 1640/3167] Attach NOTES - LeetHub From 4f07aa239d163549c628acca42c69eabd9924c8e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 09:53:43 +0530 Subject: [PATCH 1641/3167] Time: 29 ms (76.07%), Space: 28.2 MB (21.27%) - LeetHub --- ...songs-with-total-durations-divisible-by-60.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp b/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp index 1fd3624d..296a59ae 100644 --- a/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp +++ b/1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp @@ -4,20 +4,15 @@ class Solution { vector songFreq(60, 0); + int counter = 0; + for(auto& songTime : time) { + counter += songFreq[(60 - (songTime % 60)) % 60]; + ++songFreq[songTime%60]; } - long long counter = 0; - - for(int i = 1; i < 30; ++i) - { - counter += (songFreq[i] * 1LL * songFreq[60 - i]); - } - - counter += ((songFreq[0] * 1LL * (songFreq[0]-1))/2) + ((songFreq[30] * 1LL *(songFreq[30]-1)/2)); - - return (int) counter; + return counter; } }; \ No newline at end of file From 4f7c8dba1e0eae4eb241ae0aa1a21699237ed72f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:46:26 +0530 Subject: [PATCH 1642/3167] Create README - LeetHub --- 2845-count-of-interesting-subarrays/README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 2845-count-of-interesting-subarrays/README.md diff --git a/2845-count-of-interesting-subarrays/README.md b/2845-count-of-interesting-subarrays/README.md new file mode 100644 index 00000000..f5d2311e --- /dev/null +++ b/2845-count-of-interesting-subarrays/README.md @@ -0,0 +1,54 @@ +

2845. Count of Interesting Subarrays

Medium


You are given a 0-indexed integer array nums, an integer modulo, and an integer k.

+ +

Your task is to find the count of subarrays that are interesting.

+ +

A subarray nums[l..r] is interesting if the following condition holds:

+ +
    +
  • Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k.
  • +
+ +

Return an integer denoting the count of interesting subarrays.

+ +

Note: A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [3,2,4], modulo = 2, k = 1
+Output: 3
+Explanation: In this example the interesting subarrays are: 
+The subarray nums[0..0] which is [3]. 
+- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k. 
+- Hence, cnt = 1 and cnt % modulo == k.  
+The subarray nums[0..1] which is [3,2].
+- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.  
+- Hence, cnt = 1 and cnt % modulo == k.
+The subarray nums[0..2] which is [3,2,4]. 
+- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k. 
+- Hence, cnt = 1 and cnt % modulo == k. 
+It can be shown that there are no other interesting subarrays. So, the answer is 3.
+ +

Example 2:

+ +
Input: nums = [3,1,9,6], modulo = 3, k = 0
+Output: 2
+Explanation: In this example the interesting subarrays are: 
+The subarray nums[0..3] which is [3,1,9,6]. 
+- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k. 
+- Hence, cnt = 3 and cnt % modulo == k. 
+The subarray nums[1..1] which is [1]. 
+- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k. 
+- Hence, cnt = 0 and cnt % modulo == k. 
+It can be shown that there are no other interesting subarrays. So, the answer is 2.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= modulo <= 109
  • +
  • 0 <= k < modulo
  • +
+
\ No newline at end of file From 723ed88e5206db9d1c97c9b25faec6813a0f24c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:46:30 +0530 Subject: [PATCH 1643/3167] Time: 152 ms (88.89%), Space: 110.1 MB (88.89%) - LeetHub --- .../2845-count-of-interesting-subarrays.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2845-count-of-interesting-subarrays/2845-count-of-interesting-subarrays.cpp diff --git a/2845-count-of-interesting-subarrays/2845-count-of-interesting-subarrays.cpp b/2845-count-of-interesting-subarrays/2845-count-of-interesting-subarrays.cpp new file mode 100644 index 00000000..54fd4eda --- /dev/null +++ b/2845-count-of-interesting-subarrays/2845-count-of-interesting-subarrays.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + long long countInterestingSubarrays(vector& nums, int modulo, int k) { + + unordered_map mp; + + mp.insert({0, 1}); + + long long prefix = 0, counter = 0; + + for(auto& itr : nums) + { + prefix = (prefix + (itr % modulo == k ? 1 : 0))%modulo; + + int need = (((prefix - k)% modulo) + modulo) % modulo; + + if(mp.find(need) != mp.end()) + counter += mp[need]; + + ++mp[prefix]; + } + + return counter; + } +}; \ No newline at end of file From 66e2116226e9087eb49ccbedbc45afe9739b08a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 07:19:03 +0530 Subject: [PATCH 1644/3167] Create README - LeetHub --- Print adjacency list - GFG/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Print adjacency list - GFG/README.md diff --git a/Print adjacency list - GFG/README.md b/Print adjacency list - GFG/README.md new file mode 100644 index 00000000..6172ae9b --- /dev/null +++ b/Print adjacency list - GFG/README.md @@ -0,0 +1,28 @@ +# Print adjacency list +## Easy +

Given an undirected graph with nodes and E edges, create and return an adjacency list of the graph. 0-based indexing is followed everywhere.

+

Example 1:

+
Input:
V = 5, E = 7
edges = {(0,1),(0,4),(4,1),(4,3),(1,3),(1,2),(3,2)} + +Output: +{{1,4}, + {0,2,3,4}, + {1,3}, + {1,2,4}, + {0,1,3}} +Explanation: +Node 0 is connected to 1 and 4.
Node 1 is connected to 0,2,3 and 4.
Node 2 is connected to 1 and 3.
Node 3 is connected to 1,2 and 4.
Node 4 is connected to 0,1 and 3.
+
+

Example 2:

+
Input:
V = 4, E = 3
edges = {(0,3),(0,2),(2,1)} + + +Output: +{{2,3} + {2}, + {0,1} + {0}} +Explanation:
Node 0 is connected to 2 and 3.
Node 1 is only connected to 2.
Node 2 is connected to 0 and 1.
Node 3 is only connected to 0.
+

Your task:
You don't need to read input or print anything. Your task is to complete the function printGraph() which takes the integer V denoting the number of vertices and edges as input parameters and returns the list of list denoting the adjacency list.

+

Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V + E)

+

Constraints:
1 ≤ V, E ≤ 105

\ No newline at end of file From a3c2a1bc24358e03ba56f86e2e6715c459b027d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 07:19:04 +0530 Subject: [PATCH 1645/3167] Added solution - LeetHub --- .../print-adjacency-list.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Print adjacency list - GFG/print-adjacency-list.cpp diff --git a/Print adjacency list - GFG/print-adjacency-list.cpp b/Print adjacency list - GFG/print-adjacency-list.cpp new file mode 100644 index 00000000..88dd70e5 --- /dev/null +++ b/Print adjacency list - GFG/print-adjacency-list.cpp @@ -0,0 +1,52 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution { + public: + // Function to return the adjacency list for each vertex. + vector> printGraph(int V, vector>edges) { + // Code here + + vector> adj(V+1); + + for(auto& itr : edges) + { + int u = itr.first; + int v = itr.second; + + adj[u].push_back(v); + adj[v].push_back(u); + } + + return adj; + } +}; + +//{ Driver Code Starts. +int main() { + int tc; + cin >> tc; + while (tc--) { + int V, E; + cin >> V >> E; + vector>edges; + for (int i = 0; i < E; i++) { + int u, v; + cin >> u >> v; + edges.push_back({u,v}); + } + Solution obj; + vector> adj = obj.printGraph(V, edges); + for(int i=0;i Date: Tue, 5 Sep 2023 08:25:41 +0530 Subject: [PATCH 1646/3167] Create README - LeetHub --- 0138-copy-list-with-random-pointer/README.md | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0138-copy-list-with-random-pointer/README.md diff --git a/0138-copy-list-with-random-pointer/README.md b/0138-copy-list-with-random-pointer/README.md new file mode 100644 index 00000000..687b9dab --- /dev/null +++ b/0138-copy-list-with-random-pointer/README.md @@ -0,0 +1,47 @@ +

138. Copy List with Random Pointer

Medium


A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.

+ +

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

+ +

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

+ +

Return the head of the copied linked list.

+ +

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

+ +
    +
  • val: an integer representing Node.val
  • +
  • random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
  • +
+ +

Your code will only be given the head of the original linked list.

+ +

 

+

Example 1:

+ +
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
+Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
+
+ +

Example 2:

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

Example 3:

+ +

+ +
Input: head = [[3,null],[3,0],[3,null]]
+Output: [[3,null],[3,0],[3,null]]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 1000
  • +
  • -104 <= Node.val <= 104
  • +
  • Node.random is null or is pointing to some node in the linked list.
  • +
+
\ No newline at end of file From c0f8326531a1505487c2649bcecafebf80ae5ae9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 08:25:44 +0530 Subject: [PATCH 1647/3167] Time: 11 ms (29.00%), Space: 11.3 MB (32.41%) - LeetHub --- .../0138-copy-list-with-random-pointer.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp diff --git a/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp b/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp new file mode 100644 index 00000000..3496fb29 --- /dev/null +++ b/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp @@ -0,0 +1,43 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + Node* next; + Node* random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; +*/ + +class Solution { +public: + Node* copyRandomList(Node* head) { + + unordered_map mp; + + Node* temp = head; + + while(temp) + { + Node* newNode = new Node(temp->val); + mp.insert({temp, newNode}); + temp = temp->next; + } + + temp = head; + + while(temp) + { + mp[temp]->random = (temp->random ? mp[temp->random] : nullptr); + mp[temp]->next = (temp->next ? mp[temp->next] : nullptr); + temp = temp->next; + } + + return mp[head]; + } +}; \ No newline at end of file From f5c5e36e81c94072ae4e2d4b599e70b49680533e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:18:12 +0530 Subject: [PATCH 1648/3167] Attach NOTES - LeetHub --- 0138-copy-list-with-random-pointer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0138-copy-list-with-random-pointer/NOTES.md diff --git a/0138-copy-list-with-random-pointer/NOTES.md b/0138-copy-list-with-random-pointer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0138-copy-list-with-random-pointer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5987a5573d63f95d806760abf738e13cd0be3a18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:18:15 +0530 Subject: [PATCH 1649/3167] Time: 0 ms (100.00%), Space: 11.1 MB (88.34%) - LeetHub --- .../0138-copy-list-with-random-pointer.cpp | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp b/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp index 3496fb29..74aadc20 100644 --- a/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp +++ b/0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.cpp @@ -18,26 +18,40 @@ class Solution { public: Node* copyRandomList(Node* head) { - unordered_map mp; + if(!head) + return nullptr; - Node* temp = head; + Node*temp = head; while(temp) { Node* newNode = new Node(temp->val); - mp.insert({temp, newNode}); - temp = temp->next; + newNode->next = temp->next; + temp->next = newNode; + temp = newNode->next; + } + + temp = head; + + while(temp) + { + temp->next->random = (temp->random ? temp->random->next : nullptr); + temp = temp->next->next; } temp = head; + Node* ans = temp->next, *ptr = temp->next; while(temp) { - mp[temp]->random = (temp->random ? mp[temp->random] : nullptr); - mp[temp]->next = (temp->next ? mp[temp->next] : nullptr); + temp->next = ptr->next; temp = temp->next; + if(!temp) + break; + ptr->next = temp->next; + ptr = ptr->next; } - return mp[head]; + return ans; } }; \ No newline at end of file From 80dc9a0a9a90e20dcb0669d2414096897b6d02dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:19:13 +0530 Subject: [PATCH 1650/3167] Attach NOTES - LeetHub From 71e7b0e831712ef5d6b580970fe28f79d446d6e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:19:17 +0530 Subject: [PATCH 1651/3167] Time: 0 ms (100.00%), Space: 11.1 MB (88.34%) - LeetHub From 3eda749d7fef08005fc2f9cb6ceb42a4bc8eb8c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:35:18 +0530 Subject: [PATCH 1652/3167] Attach NOTES - LeetHub --- 0133-clone-graph/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0133-clone-graph/NOTES.md diff --git a/0133-clone-graph/NOTES.md b/0133-clone-graph/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0133-clone-graph/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3acc805e8b4b6b7d6f17fe4a6e2a45dbdbee38fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:35:22 +0530 Subject: [PATCH 1653/3167] Time: 3 ms (82.00%), Space: 9 MB (56.54%) - LeetHub --- 0133-clone-graph/0133-clone-graph.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/0133-clone-graph/0133-clone-graph.cpp b/0133-clone-graph/0133-clone-graph.cpp index 0352196f..17422d11 100644 --- a/0133-clone-graph/0133-clone-graph.cpp +++ b/0133-clone-graph/0133-clone-graph.cpp @@ -20,34 +20,36 @@ class Node { */ class Solution { -public: - - Node* clone(Node* node, unordered_map& mp) + +private: + Node* clone(Node* node, unordered_map& mp) { Node* newNode = new Node(node->val); - mp.insert({node->val,newNode}); + mp.insert({node->val, newNode}); - for(auto itr : node->neighbors) + for(auto& itr : node->neighbors) { if(mp.find(itr->val) == mp.end()) { - Node* cn = clone(itr,mp); - newNode->neighbors.push_back(cn); + Node* cloneNode = clone(itr, mp); + newNode->neighbors.push_back(cloneNode); } else + { newNode->neighbors.push_back(mp[itr->val]); + } } return newNode; } - +public: Node* cloneGraph(Node* node) { - if(node == nullptr) + if(!node) return nullptr; - unordered_map mp; + unordered_map mp; return clone(node, mp); From 2c13cfd0e122c2e6371a3bdb916c99b57c8ec8a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 07:21:46 +0530 Subject: [PATCH 1654/3167] Create README - LeetHub --- Mother Vertex - GFG/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Mother Vertex - GFG/README.md diff --git a/Mother Vertex - GFG/README.md b/Mother Vertex - GFG/README.md new file mode 100644 index 00000000..426d6810 --- /dev/null +++ b/Mother Vertex - GFG/README.md @@ -0,0 +1,21 @@ +# Mother Vertex +## Easy +

Given a Directed Graph, find a Mother Vertex in the Graph (if present). 
A Mother Vertex is a vertex through which we can reach all the other vertices of the Graph.

+

Example 1:

+
Input: 
+
+Output: 0
+Explanation: According to the given edges, all 
+nodes can be reaced from nodes from 0, 1 and 2. 
+But, since 0 is minimum among 0,1 and 2, so 0 
+is the output.
+
+

Example 2:

+
Input: 
+
+Output: -1
+Explanation: According to the given edges, 
+no vertices are there from where we can 
+reach all vertices. So, output is -1.
+
+

Your Task:
You don't need to read or print anything. Your task is to complete the function findMotherVertex() which takes V denoting the number of vertices and adjacency list as imput parameter and returns the verticex from through which we can traverse all other vertices of the graph. If there is more than one possible nodes then returns the node with minimum value.If not possible returns -1.

Expected Time Complexity: O(V + E)
Expected Space Compelxity: O(V)

Constraints:
1 ≤ V ≤ 500

\ No newline at end of file From 4ae0c9e55202a2e09956032f7f3f654d0d11e474 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 07:21:47 +0530 Subject: [PATCH 1655/3167] Added solution - LeetHub --- Mother Vertex - GFG/mother-vertex.cpp | 75 +++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Mother Vertex - GFG/mother-vertex.cpp diff --git a/Mother Vertex - GFG/mother-vertex.cpp b/Mother Vertex - GFG/mother-vertex.cpp new file mode 100644 index 00000000..0f9eb0f7 --- /dev/null +++ b/Mother Vertex - GFG/mother-vertex.cpp @@ -0,0 +1,75 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + +class Solution +{ + public: + //Function to find a Mother Vertex in the Graph. + + void dfs(int sv, vector& visited, vector adj[]) + { + visited[sv] = true; + + for(auto& itr : adj[sv]) + { + if(!visited[itr]) + dfs(itr, visited, adj); + } + } + + int findMotherVertex(int V, vectoradj[]) + { + // Code here + + int mv = -1; + + vector visited(V+1, false); + + for(int i = 0; i < V; ++i) + { + if(!visited[i]) + { + dfs(i, visited, adj); + mv = i; + } + } + + for(int i = 0; i < V; ++i) + visited[i] = 0; + + dfs(mv, visited, adj); + + for(int i = 0; i < V; ++i) + { + if(visited[i] == 0) + return -1; + } + + return mv; + } + +}; + +//{ Driver Code Starts. +int main(){ + int tc; + cin >> tc; + while(tc--){ + int V, E; + cin >> V >> E; + vectoradj[V]; + for(int i = 0; i < E; i++){ + int u, v; + cin >> u >> v; + adj[u].push_back(v); + } + Solution obj; + int ans = obj.findMotherVertex(V, adj); + cout << ans <<"\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From e5c446ea36ed7a1d069aa98a0b8054713f8ea588 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:43:52 +0530 Subject: [PATCH 1656/3167] Create README - LeetHub --- 0725-split-linked-list-in-parts/README.md | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0725-split-linked-list-in-parts/README.md diff --git a/0725-split-linked-list-in-parts/README.md b/0725-split-linked-list-in-parts/README.md new file mode 100644 index 00000000..45b55207 --- /dev/null +++ b/0725-split-linked-list-in-parts/README.md @@ -0,0 +1,35 @@ +

725. Split Linked List in Parts

Medium


Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.

+ +

The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.

+ +

The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.

+ +

Return an array of the k parts.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3], k = 5
+Output: [[1],[2],[3],[],[]]
+Explanation:
+The first element output[0] has output[0].val = 1, output[0].next = null.
+The last element output[4] is null, but its string representation as a ListNode is [].
+
+ +

Example 2:

+ +
Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
+Output: [[1,2,3,4],[5,6,7],[8,9,10]]
+Explanation:
+The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [0, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
  • 1 <= k <= 50
  • +
+
\ No newline at end of file From 8ad13a625b0afbfc8e219d1f9ee62788a3205e74 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:43:55 +0530 Subject: [PATCH 1657/3167] Time: 13 ms (5.59%), Space: 8.9 MB (15.59%) - LeetHub --- .../0725-split-linked-list-in-parts.cpp | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp diff --git a/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp b/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp new file mode 100644 index 00000000..79956faf --- /dev/null +++ b/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp @@ -0,0 +1,92 @@ +/** + * 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 splitListToParts(ListNode* head, int k) { + + vector ans(k, nullptr); + + if(!head) + return ans; + + ListNode* temp = head; + + int counter = 0 ; + + while(temp) + { + ++counter; + temp = temp->next; + } + + int part = counter / k, extra = counter % k; + + int idx = 0; + + temp = head; + + ListNode* prev = temp, *nextNode = nullptr; + + + if(k >= counter) + { + while(temp) + { + temp = temp->next; + prev->next = nullptr; + ans[idx++] = prev; + prev = temp; + } + + return ans; + } + + counter = 1; + + while(temp) + { + if(counter == part) + { + if(extra) + { + nextNode = temp->next->next; + + temp->next->next = nullptr; + + --extra; + } + else + { + nextNode = temp->next; + + temp->next = nullptr; + } + + ans[idx++] = prev; + + prev = temp = nextNode; + + counter = 1; + + continue; + } + + ++counter; + temp = temp->next; + cout<val<<' '; + } + + if(counter > 1) + ans[idx++] = prev; + + return ans; + } +}; \ No newline at end of file From 7f932b5ab2ff955c1ab6fc9b6b5f37904953dd1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:44:23 +0530 Subject: [PATCH 1658/3167] Attach NOTES - LeetHub --- 0725-split-linked-list-in-parts/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0725-split-linked-list-in-parts/NOTES.md diff --git a/0725-split-linked-list-in-parts/NOTES.md b/0725-split-linked-list-in-parts/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0725-split-linked-list-in-parts/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 19f7a98ffbbcb68a06246446f9ab977779775fe2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:44:26 +0530 Subject: [PATCH 1659/3167] Time: 13 ms (5.59%), Space: 8.9 MB (15.59%) - LeetHub From fa3671969759a37e3580d17fe464e6c8a9e458a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:28:43 +0530 Subject: [PATCH 1660/3167] Attach NOTES - LeetHub From a138531bf0be6f89fc7234ee3a41adc5ab0cb8b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:28:46 +0530 Subject: [PATCH 1661/3167] Time: 0 ms (100.00%), Space: 8.9 MB (45.79%) - LeetHub --- .../0725-split-linked-list-in-parts.cpp | 78 +++++-------------- 1 file changed, 20 insertions(+), 58 deletions(-) diff --git a/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp b/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp index 79956faf..73036cf3 100644 --- a/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp +++ b/0725-split-linked-list-in-parts/0725-split-linked-list-in-parts.cpp @@ -12,81 +12,43 @@ class Solution { public: vector splitListToParts(ListNode* head, int k) { - vector ans(k, nullptr); + vector result(k, nullptr); if(!head) - return ans; + return result; - ListNode* temp = head; + int length = 0; - int counter = 0 ; + ListNode* curr = head; - while(temp) + while(curr) { - ++counter; - temp = temp->next; + ++length; + curr = curr->next; } - int part = counter / k, extra = counter % k; + int eachPartitionLength = length / k; + int extraNodes = length % k; - int idx = 0; + curr = head; - temp = head; + ListNode* prev = nullptr; - ListNode* prev = temp, *nextNode = nullptr; - - - if(k >= counter) + for(int i = 0; i < k; ++i) { - while(temp) - { - temp = temp->next; - prev->next = nullptr; - ans[idx++] = prev; - prev = temp; - } + result[i] = curr; - return ans; - } - - counter = 1; - - while(temp) - { - if(counter == part) + for(int count = 1; count <= eachPartitionLength + (extraNodes > 0 ? 1 : 0); ++count) { - if(extra) - { - nextNode = temp->next->next; - - temp->next->next = nullptr; - - --extra; - } - else - { - nextNode = temp->next; - - temp->next = nullptr; - } - - ans[idx++] = prev; - - prev = temp = nextNode; - - counter = 1; - - continue; + prev = curr; + curr = curr->next; } - ++counter; - temp = temp->next; - cout<val<<' '; + prev->next = nullptr; + --extraNodes; } - if(counter > 1) - ans[idx++] = prev; - - return ans; + return result; + } }; \ No newline at end of file From a787e1e8cecccf3c45b31e6e953d70f57bf0a34a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:31:36 +0530 Subject: [PATCH 1662/3167] Attach NOTES - LeetHub From e15cd55b85ef067a9fb3c5f55db160e69ffa81a2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Sep 2023 09:24:47 +0530 Subject: [PATCH 1663/3167] Create README - LeetHub --- 0092-reverse-linked-list-ii/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0092-reverse-linked-list-ii/README.md diff --git a/0092-reverse-linked-list-ii/README.md b/0092-reverse-linked-list-ii/README.md new file mode 100644 index 00000000..8c76add5 --- /dev/null +++ b/0092-reverse-linked-list-ii/README.md @@ -0,0 +1,27 @@ +

92. Reverse Linked List II

Medium


Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: head = [5], left = 1, right = 1
+Output: [5]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is n.
  • +
  • 1 <= n <= 500
  • +
  • -500 <= Node.val <= 500
  • +
  • 1 <= left <= right <= n
  • +
+ +

 

+Follow up: Could you do it in one pass?
\ No newline at end of file From 696e4463befd4f54968889d7a7516631b56552cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Sep 2023 09:24:50 +0530 Subject: [PATCH 1664/3167] Time: 4 ms (38.20%), Space: 7.5 MB (12.22%) - LeetHub --- .../0092-reverse-linked-list-ii.cpp | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 0092-reverse-linked-list-ii/0092-reverse-linked-list-ii.cpp diff --git a/0092-reverse-linked-list-ii/0092-reverse-linked-list-ii.cpp b/0092-reverse-linked-list-ii/0092-reverse-linked-list-ii.cpp new file mode 100644 index 00000000..0ad361ae --- /dev/null +++ b/0092-reverse-linked-list-ii/0092-reverse-linked-list-ii.cpp @@ -0,0 +1,82 @@ +/** + * 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 { + +private: + ListNode* reverse(ListNode* head) + { + ListNode* curr = head, *prev = nullptr; + + while(curr) + { + ListNode* nextNode = curr->next; + curr->next = prev; + prev = curr; + curr = nextNode; + } + + return prev; + } +public: + ListNode* reverseBetween(ListNode* head, int left, int right) { + + if(left == right) + return head; + + ListNode* leftNode = nullptr, *rightNode = nullptr; + + ListNode* prevLeftNode = nullptr, *nextRightNode = nullptr; + + ListNode* curr= head; + + int counter = 0; + + if(left == 1) + { + prevLeftNode = nullptr; + leftNode = curr; + } + + + while(curr) + { + ++counter; + + if(counter == left-1) + { + prevLeftNode = curr; + leftNode = curr->next; + } + + if(counter == right) + { + rightNode = curr; + nextRightNode = curr->next; + } + + curr = curr->next; + } + + if(rightNode) + rightNode->next = nullptr; + + ListNode* rev = reverse(leftNode); + + if(prevLeftNode) + prevLeftNode->next = rev; + if(nextRightNode) + leftNode->next = nextRightNode; + + if(prevLeftNode) + return head; + return rev; + } +}; \ No newline at end of file From 7a2a28b93c3173b057c55b08d745b5e62c7e5191 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Sep 2023 07:26:16 +0530 Subject: [PATCH 1665/3167] Create README - LeetHub --- 0118-pascals-triangle/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0118-pascals-triangle/README.md diff --git a/0118-pascals-triangle/README.md b/0118-pascals-triangle/README.md new file mode 100644 index 00000000..37f8fc62 --- /dev/null +++ b/0118-pascals-triangle/README.md @@ -0,0 +1,19 @@ +

118. Pascal's Triangle

Easy


Given an integer numRows, return the first numRows of Pascal's triangle.

+ +

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

+ +

 

+

Example 1:

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

Example 2:

+
Input: numRows = 1
+Output: [[1]]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= numRows <= 30
  • +
+
\ No newline at end of file From 193df4e9c2ccc753f6e2ed5f64c707cad4b5e82f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Sep 2023 07:26:19 +0530 Subject: [PATCH 1666/3167] Time: 0 ms (100.00%), Space: 7 MB (6.18%) - LeetHub --- .../0118-pascals-triangle.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0118-pascals-triangle/0118-pascals-triangle.cpp diff --git a/0118-pascals-triangle/0118-pascals-triangle.cpp b/0118-pascals-triangle/0118-pascals-triangle.cpp new file mode 100644 index 00000000..e8518d00 --- /dev/null +++ b/0118-pascals-triangle/0118-pascals-triangle.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + vector> generate(int numRows) { + + vector> ans; + + for(int i = 1; i <= numRows; ++i) + { + if(i == 1) + { + ans.push_back({1}); + } + else if(i == 2) + { + ans.push_back({1,1}); + } + else + { + vector pref = ans.back(); + vector curr; + + curr.push_back(1); + + for(int j = 1; j < i-1; ++j) + { + curr.push_back(pref[j] + pref[j-1]); + } + + curr.push_back(1); + + ans.push_back(curr); + } + } + + return ans; + + } +}; \ No newline at end of file From 57f268e18056818000547da463dc3fc87891b5c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Sep 2023 07:28:46 +0530 Subject: [PATCH 1667/3167] Attach NOTES - LeetHub --- 0118-pascals-triangle/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0118-pascals-triangle/NOTES.md diff --git a/0118-pascals-triangle/NOTES.md b/0118-pascals-triangle/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0118-pascals-triangle/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f0635f573af7283a0ecfb26b214971c3fa75e47a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Sep 2023 07:28:49 +0530 Subject: [PATCH 1668/3167] Time: 0 ms (100.00%), Space: 7 MB (6.18%) - LeetHub From 11c8baa8e111ac8f1455defea9792a36025d9178 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Sep 2023 20:56:11 +0530 Subject: [PATCH 1669/3167] Create README - LeetHub --- 0377-combination-sum-iv/README.md | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0377-combination-sum-iv/README.md diff --git a/0377-combination-sum-iv/README.md b/0377-combination-sum-iv/README.md new file mode 100644 index 00000000..842b55ae --- /dev/null +++ b/0377-combination-sum-iv/README.md @@ -0,0 +1,40 @@ +

377. Combination Sum IV

Medium


Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.

+ +

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

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3], target = 4
+Output: 7
+Explanation:
+The possible combination ways are:
+(1, 1, 1, 1)
+(1, 1, 2)
+(1, 2, 1)
+(1, 3)
+(2, 1, 1)
+(2, 2)
+(3, 1)
+Note that different sequences are counted as different combinations.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 200
  • +
  • 1 <= nums[i] <= 1000
  • +
  • All the elements of nums are unique.
  • +
  • 1 <= target <= 1000
  • +
+ +

 

+

Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?

+
\ No newline at end of file From bb2221f74f270799b9f9a3054193231dbe1742ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Sep 2023 20:56:15 +0530 Subject: [PATCH 1670/3167] Time: 3 ms (50.37%), Space: 6.7 MB (20.91%) - LeetHub --- .../0377-combination-sum-iv.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0377-combination-sum-iv/0377-combination-sum-iv.cpp diff --git a/0377-combination-sum-iv/0377-combination-sum-iv.cpp b/0377-combination-sum-iv/0377-combination-sum-iv.cpp new file mode 100644 index 00000000..87f2199d --- /dev/null +++ b/0377-combination-sum-iv/0377-combination-sum-iv.cpp @@ -0,0 +1,36 @@ +class Solution { + +private: + int helper(int sum, vector& nums, int target, vector& dp) + { + if(sum > target) + return 0; + + if(sum == target) + return 1; + + if(dp[sum] != -1) + return dp[sum]; + + int ways = 0; + + for(int i = 0; i < nums.size(); ++i) + { + if(nums[i] + sum <= target) + { + ways += helper(sum + nums[i], nums, target, dp); + } + } + + return dp[sum] = ways; + } + +public: + int combinationSum4(vector& nums, int target) { + + vector dp(target+1, -1); + + return helper(0, nums, target, dp); + + } +}; \ No newline at end of file From 6cb8938d0c955cbe9a08f9fa2262d58f10762001 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Sep 2023 21:30:26 +0530 Subject: [PATCH 1671/3167] Attach NOTES - LeetHub --- 0377-combination-sum-iv/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0377-combination-sum-iv/NOTES.md diff --git a/0377-combination-sum-iv/NOTES.md b/0377-combination-sum-iv/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0377-combination-sum-iv/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ac348f2bb095ca99f2873c805c2ded19201ae69a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Sep 2023 21:30:30 +0530 Subject: [PATCH 1672/3167] Time: 5 ms (18.41%), Space: 7.5 MB (6.68%) - LeetHub --- .../0377-combination-sum-iv.cpp | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/0377-combination-sum-iv/0377-combination-sum-iv.cpp b/0377-combination-sum-iv/0377-combination-sum-iv.cpp index 87f2199d..3aaa86fa 100644 --- a/0377-combination-sum-iv/0377-combination-sum-iv.cpp +++ b/0377-combination-sum-iv/0377-combination-sum-iv.cpp @@ -1,36 +1,33 @@ class Solution { - + private: - int helper(int sum, vector& nums, int target, vector& dp) + int helper(int idx, int sum, vector& nums, int target, vector>& dp) { - if(sum > target) - return 0; + if(idx == nums.size()) + { + return sum == target; + } - if(sum == target) - return 1; + if(dp[idx][sum] != -1) + return dp[idx][sum]; - if(dp[sum] != -1) - return dp[sum]; + int notTake = helper(idx+1, sum, nums, target, dp); - int ways = 0; + int take = 0; - for(int i = 0; i < nums.size(); ++i) - { - if(nums[i] + sum <= target) - { - ways += helper(sum + nums[i], nums, target, dp); - } - } - - return dp[sum] = ways; + if(sum + nums[idx] <= target) + take = helper(0, sum + nums[idx], nums, target, dp); + + return dp[idx][sum] = take + notTake; } - public: int combinationSum4(vector& nums, int target) { - vector dp(target+1, -1); + int n = nums.size(); + + vector> dp(n+1, vector(target+1, -1)); - return helper(0, nums, target, dp); + return helper(0, 0, nums, target, dp); } }; \ No newline at end of file From 915a9237ebb1f02155dde763e8faff1ebd0f3b37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Sep 2023 21:25:19 +0530 Subject: [PATCH 1673/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1359-count-all-valid-pickup-and-delivery-options/README.md diff --git a/1359-count-all-valid-pickup-and-delivery-options/README.md b/1359-count-all-valid-pickup-and-delivery-options/README.md new file mode 100644 index 00000000..35b4e77b --- /dev/null +++ b/1359-count-all-valid-pickup-and-delivery-options/README.md @@ -0,0 +1,36 @@ +

1359. Count All Valid Pickup and Delivery Options

Hard


Given n orders, each order consist in pickup and delivery services. 

+ +

Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). 

+ +

Since the answer may be too large, return it modulo 10^9 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 1
+Output: 1
+Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup 1.
+
+ +

Example 2:

+ +
Input: n = 2
+Output: 6
+Explanation: All possible orders: 
+(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).
+This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.
+
+ +

Example 3:

+ +
Input: n = 3
+Output: 90
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 500
  • +
+
\ No newline at end of file From 17e126ae898413f356025a96c5f3ef997b797b68 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Sep 2023 21:25:24 +0530 Subject: [PATCH 1674/3167] Time: 0 ms (100.00%), Space: 6 MB (43.33%) - LeetHub --- ...count-all-valid-pickup-and-delivery-options.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 1359-count-all-valid-pickup-and-delivery-options/1359-count-all-valid-pickup-and-delivery-options.cpp diff --git a/1359-count-all-valid-pickup-and-delivery-options/1359-count-all-valid-pickup-and-delivery-options.cpp b/1359-count-all-valid-pickup-and-delivery-options/1359-count-all-valid-pickup-and-delivery-options.cpp new file mode 100644 index 00000000..2958330b --- /dev/null +++ b/1359-count-all-valid-pickup-and-delivery-options/1359-count-all-valid-pickup-and-delivery-options.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int countOrders(int n) { + int odd=3, mod=1e9+7; + long long ans=1; + for(int i=2; i<=n; i++) + { + int sum = (odd)*(odd+1)%mod/2; + ans=ans*sum%mod; + odd+=2; + } + return ans%mod; + } +}; \ No newline at end of file From a3d6122fa118badf552b292b91e127506ab933f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:03:56 +0530 Subject: [PATCH 1675/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1282-group-the-people-given-the-group-size-they-belong-to/README.md diff --git a/1282-group-the-people-given-the-group-size-they-belong-to/README.md b/1282-group-the-people-given-the-group-size-they-belong-to/README.md new file mode 100644 index 00000000..3861a420 --- /dev/null +++ b/1282-group-the-people-given-the-group-size-they-belong-to/README.md @@ -0,0 +1,35 @@ +

1282. Group the People Given the Group Size They Belong To

Medium


There are n people that are split into some unknown number of groups. Each person is labeled with a unique ID from 0 to n - 1.

+ +

You are given an integer array groupSizes, where groupSizes[i] is the size of the group that person i is in. For example, if groupSizes[1] = 3, then person 1 must be in a group of size 3.

+ +

Return a list of groups such that each person i is in a group of size groupSizes[i].

+ +

Each person should appear in exactly one group, and every person must be in a group. If there are multiple answers, return any of them. It is guaranteed that there will be at least one valid solution for the given input.

+ +

 

+

Example 1:

+ +
Input: groupSizes = [3,3,3,3,3,1,3]
+Output: [[5],[0,1,2],[3,4,6]]
+Explanation: 
+The first group is [5]. The size is 1, and groupSizes[5] = 1.
+The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1] = groupSizes[2] = 3.
+The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4] = groupSizes[6] = 3.
+Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • groupSizes.length == n
  • +
  • 1 <= n <= 500
  • +
  • 1 <= groupSizes[i] <= n
  • +
+
\ No newline at end of file From 637599b1c53df44644e6e87d364c9d2fdff56097 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:04:00 +0530 Subject: [PATCH 1676/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub --- ...le-given-the-group-size-they-belong-to.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp diff --git a/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp b/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp new file mode 100644 index 00000000..973cebad --- /dev/null +++ b/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector> groupThePeople(vector& groupSizes) { + + int n = groupSizes.size(); + + unordered_map> mp; + + vector> ans; + + for(int i = 0; i < n; ++i) + { + if(mp.find(groupSizes[i]) == mp.end()) + { + mp[groupSizes[i]].push_back(i); + } + else + { + if(mp[groupSizes[i]].size() == groupSizes[i] ) + { + ans.push_back(mp[groupSizes[i]]); + mp.erase(groupSizes[i]); + } + + mp[groupSizes[i]].push_back(i); + } + } + + for(auto& itr : mp) + { + ans.push_back(itr.second); + } + + return ans; + } +}; \ No newline at end of file From 85c6389405b316e91b13c179b393fe371d38b4ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:04:22 +0530 Subject: [PATCH 1677/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1282-group-the-people-given-the-group-size-they-belong-to/NOTES.md diff --git a/1282-group-the-people-given-the-group-size-they-belong-to/NOTES.md b/1282-group-the-people-given-the-group-size-they-belong-to/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1282-group-the-people-given-the-group-size-they-belong-to/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f7c925d60aa258675d3fc7b70988f803ad4fae9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:04:26 +0530 Subject: [PATCH 1678/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From 4ddf72886b5b7a55f58e747f5936c97121d6361e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:05:10 +0530 Subject: [PATCH 1679/3167] Attach NOTES - LeetHub From 8683468ba0ff195718e757bdf8a20a8863a9f5e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:05:13 +0530 Subject: [PATCH 1680/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From 95b41312dea70ff423ffca0e42b613eaf927ac19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:06:05 +0530 Subject: [PATCH 1681/3167] Attach NOTES - LeetHub From 11769bce969bf12fd48e64f761283963e71a01f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:06:09 +0530 Subject: [PATCH 1682/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From 2b71dfd51af6eb0b11ad144a8795d635152fd3c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:06:38 +0530 Subject: [PATCH 1683/3167] Attach NOTES - LeetHub From fe0e73cbe42db41dc33170abaf141a591d673086 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:06:41 +0530 Subject: [PATCH 1684/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From f471d17ebab592ba410622addfbac10d1e30582f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:07:30 +0530 Subject: [PATCH 1685/3167] Attach NOTES - LeetHub From ec1ebcaa23aecc59903fcc2e46d1176b93b3f511 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:07:34 +0530 Subject: [PATCH 1686/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From e6a4f73d3f6f42766ea077cc29b06b0ae5e71fd7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:07:37 +0530 Subject: [PATCH 1687/3167] Attach NOTES - LeetHub From 4b77ce4ea460dd784ae1e4d40e01a78f4fdf75a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:07:41 +0530 Subject: [PATCH 1688/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From 59a2317d498fdde8fc89480ea52345d1f89b2758 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:07:42 +0530 Subject: [PATCH 1689/3167] Attach NOTES - LeetHub From 4656fe6955d6d1c4f94a3216ca2d895ad101799e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:07:46 +0530 Subject: [PATCH 1690/3167] Time: 15 ms (28.98%), Space: 14.2 MB (8.70%) - LeetHub From 0f165947354b284555788a581a5634f3c83589fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:08:07 +0530 Subject: [PATCH 1691/3167] Attach NOTES - LeetHub From f0d81d85962e7742cf4278ff012036595c7764e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:08:10 +0530 Subject: [PATCH 1692/3167] Time: 4 ms (95.05%), Space: 12.5 MB (88.22%) - LeetHub --- ...le-given-the-group-size-they-belong-to.cpp | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp b/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp index 973cebad..4a6c40d9 100644 --- a/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp +++ b/1282-group-the-people-given-the-group-size-they-belong-to/1282-group-the-people-given-the-group-size-they-belong-to.cpp @@ -10,26 +10,14 @@ class Solution { for(int i = 0; i < n; ++i) { - if(mp.find(groupSizes[i]) == mp.end()) + mp[groupSizes[i]].push_back(i); + + if(mp[groupSizes[i]].size() == groupSizes[i] ) { - mp[groupSizes[i]].push_back(i); + ans.push_back(mp[groupSizes[i]]); + mp[groupSizes[i]].clear(); } - else - { - if(mp[groupSizes[i]].size() == groupSizes[i] ) - { - ans.push_back(mp[groupSizes[i]]); - mp.erase(groupSizes[i]); - } - - mp[groupSizes[i]].push_back(i); - } - } - - for(auto& itr : mp) - { - ans.push_back(itr.second); - } + } return ans; } From 5d398394b06926c9d9dff844692f342c80c642d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 07:20:46 +0530 Subject: [PATCH 1693/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1647-minimum-deletions-to-make-character-frequencies-unique/README.md diff --git a/1647-minimum-deletions-to-make-character-frequencies-unique/README.md b/1647-minimum-deletions-to-make-character-frequencies-unique/README.md new file mode 100644 index 00000000..3cf42be1 --- /dev/null +++ b/1647-minimum-deletions-to-make-character-frequencies-unique/README.md @@ -0,0 +1,37 @@ +

1647. Minimum Deletions to Make Character Frequencies Unique

Medium


A string s is called good if there are no two different characters in s that have the same frequency.

+ +

Given a string s, return the minimum number of characters you need to delete to make s good.

+ +

The frequency of a character in a string is the number of times it appears in the string. For example, in the string "aab", the frequency of 'a' is 2, while the frequency of 'b' is 1.

+ +

 

+

Example 1:

+ +
Input: s = "aab"
+Output: 0
+Explanation: s is already good.
+
+ +

Example 2:

+ +
Input: s = "aaabbbcc"
+Output: 2
+Explanation: You can delete two 'b's resulting in the good string "aaabcc".
+Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
+ +

Example 3:

+ +
Input: s = "ceabaacb"
+Output: 2
+Explanation: You can delete both 'c's resulting in the good string "eabaab".
+Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s contains only lowercase English letters.
  • +
+
\ No newline at end of file From e52b6f99357db14daa2ba3428a346b8a21684144 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 07:20:50 +0530 Subject: [PATCH 1694/3167] Time: 82 ms (33.63%), Space: 17.4 MB (58.00%) - LeetHub --- ...s-to-make-character-frequencies-unique.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1647-minimum-deletions-to-make-character-frequencies-unique/1647-minimum-deletions-to-make-character-frequencies-unique.cpp diff --git a/1647-minimum-deletions-to-make-character-frequencies-unique/1647-minimum-deletions-to-make-character-frequencies-unique.cpp b/1647-minimum-deletions-to-make-character-frequencies-unique/1647-minimum-deletions-to-make-character-frequencies-unique.cpp new file mode 100644 index 00000000..32f451d3 --- /dev/null +++ b/1647-minimum-deletions-to-make-character-frequencies-unique/1647-minimum-deletions-to-make-character-frequencies-unique.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int minDeletions(string s) { + + unordered_map mp; + + for(auto& itr : s) + ++mp[itr]; + + priority_queue pq; + + for(auto&itr : mp) + { + pq.push(itr.second); + } + + int counter = 0; + + while(pq.size() >= 2) + { + int a = pq.top(); pq.pop(); + int b = pq.top(); pq.pop(); + + if(a == b) + { + ++counter; + if(--a > 0) + pq.push(a); + pq.push(b); + } + else + pq.push(b); + } + + return counter; + } +}; \ No newline at end of file From 97d60b80cc5ffd2b6bc28fdf2564f62aae6f50d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 07:23:31 +0530 Subject: [PATCH 1695/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1647-minimum-deletions-to-make-character-frequencies-unique/NOTES.md diff --git a/1647-minimum-deletions-to-make-character-frequencies-unique/NOTES.md b/1647-minimum-deletions-to-make-character-frequencies-unique/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1647-minimum-deletions-to-make-character-frequencies-unique/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4460c603b1d91bc9823b1af4a2f2bcdacf75247b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 07:37:41 +0530 Subject: [PATCH 1696/3167] Create README - LeetHub --- Perfect Numbers - GFG/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Perfect Numbers - GFG/README.md diff --git a/Perfect Numbers - GFG/README.md b/Perfect Numbers - GFG/README.md new file mode 100644 index 00000000..126a56c6 --- /dev/null +++ b/Perfect Numbers - GFG/README.md @@ -0,0 +1,26 @@ +# Perfect Numbers +## Easy +

Given a number N, check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number. Return 1 if the number is Perfect otherwise return 0.

+

Example 1:

+
Input:
+N = 6
+Output:
+1 
+Explanation:
+Factors of 6 are 1, 2, 3 and 6.
+Excluding 6 their sum is 6 which
+is equal to N itself. So, it's a
+Perfect Number.
+

Example 2:

+
Input:
+N = 10
+Output:
+0
+Explanation:
+Factors of 10 are 1, 2, 5 and 10.
+Excluding 10 their sum is 8 which
+is not equal to N itself. So, it's
+not a Perfect Number.
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function isPerfectNumber() which takes an Integer N as input and returns 1 if N is a Perfect number else returns 0.

+

Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 1012

\ No newline at end of file From 2ae153cd0f20c438cde0ee1eaf42e7984c6eb717 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 07:37:41 +0530 Subject: [PATCH 1697/3167] Added solution - LeetHub --- Perfect Numbers - GFG/perfect-numbers.cpp | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Perfect Numbers - GFG/perfect-numbers.cpp diff --git a/Perfect Numbers - GFG/perfect-numbers.cpp b/Perfect Numbers - GFG/perfect-numbers.cpp new file mode 100644 index 00000000..a7ed1d00 --- /dev/null +++ b/Perfect Numbers - GFG/perfect-numbers.cpp @@ -0,0 +1,44 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution { + public: + int isPerfectNumber(long long N) { + // code here + + if(N == 1) return 0; + + long long sum = 1; + + for(long long i = 2; i * i < N; ++i) + { + if(N % i == 0) + { + sum += i; + + if(i != N/i) + sum += (N/i); + } + } + + return sum == N; + } +}; + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + long long N; + + cin>>N; + + Solution ob; + cout << ob.isPerfectNumber(N) << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From c074869c80af13066ad5ef627c6a3968d1528bc3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 08:06:59 +0530 Subject: [PATCH 1698/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2091-removing-minimum-and-maximum-from-array/README.md diff --git a/2091-removing-minimum-and-maximum-from-array/README.md b/2091-removing-minimum-and-maximum-from-array/README.md new file mode 100644 index 00000000..8df05034 --- /dev/null +++ b/2091-removing-minimum-and-maximum-from-array/README.md @@ -0,0 +1,49 @@ +

2091. Removing Minimum and Maximum From Array

Medium


You are given a 0-indexed array of distinct integers nums.

+ +

There is an element in nums that has the lowest value and an element that has the highest value. We call them the minimum and maximum respectively. Your goal is to remove both these elements from the array.

+ +

A deletion is defined as either removing an element from the front of the array or removing an element from the back of the array.

+ +

Return the minimum number of deletions it would take to remove both the minimum and maximum element from the array.

+ +

 

+

Example 1:

+ +
Input: nums = [2,10,7,5,4,1,8,6]
+Output: 5
+Explanation: 
+The minimum element in the array is nums[5], which is 1.
+The maximum element in the array is nums[1], which is 10.
+We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
+This results in 2 + 3 = 5 deletions, which is the minimum number possible.
+
+ +

Example 2:

+ +
Input: nums = [0,-4,19,1,8,-2,-3,5]
+Output: 3
+Explanation: 
+The minimum element in the array is nums[1], which is -4.
+The maximum element in the array is nums[2], which is 19.
+We can remove both the minimum and maximum by removing 3 elements from the front.
+This results in only 3 deletions, which is the minimum number possible.
+
+ +

Example 3:

+ +
Input: nums = [101]
+Output: 1
+Explanation:  
+There is only one element in the array, which makes it both the minimum and maximum element.
+We can remove it with 1 deletion.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -105 <= nums[i] <= 105
  • +
  • The integers in nums are distinct.
  • +
+
\ No newline at end of file From 04ab2555c5988e80444dd7b6f1b7f3d46208349b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 08:06:59 +0530 Subject: [PATCH 1699/3167] Attach NOTES - LeetHub --- 2091-removing-minimum-and-maximum-from-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2091-removing-minimum-and-maximum-from-array/NOTES.md diff --git a/2091-removing-minimum-and-maximum-from-array/NOTES.md b/2091-removing-minimum-and-maximum-from-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2091-removing-minimum-and-maximum-from-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f636e6c0b6c16117cf09d2526cca67a494b8fb26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 08:07:02 +0530 Subject: [PATCH 1700/3167] Time: 135 ms (38.98%), Space: 88.2 MB (72.47%) - LeetHub --- ...emoving-minimum-and-maximum-from-array.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2091-removing-minimum-and-maximum-from-array/2091-removing-minimum-and-maximum-from-array.cpp diff --git a/2091-removing-minimum-and-maximum-from-array/2091-removing-minimum-and-maximum-from-array.cpp b/2091-removing-minimum-and-maximum-from-array/2091-removing-minimum-and-maximum-from-array.cpp new file mode 100644 index 00000000..6b4b650e --- /dev/null +++ b/2091-removing-minimum-and-maximum-from-array/2091-removing-minimum-and-maximum-from-array.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int minimumDeletions(vector& nums) { + + int n = nums.size(); + + int minIdx = min_element(nums.begin(), nums.end()) - nums.begin(); + int maxIdx = max_element(nums.begin(), nums.end()) - nums.begin(); + + int fromFirst = max(minIdx, maxIdx) + 1; + + int fromLast = n - min(minIdx, maxIdx); + + int bothSide = min(minIdx, maxIdx) + 1 + n - max(minIdx, maxIdx); + + // cout< Date: Tue, 12 Sep 2023 08:10:00 +0530 Subject: [PATCH 1701/3167] Attach NOTES - LeetHub From d0b35b6f625833a3cc4b10b60fc652984b9881a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Sep 2023 19:54:12 +0530 Subject: [PATCH 1702/3167] Attach NOTES - LeetHub From 3e063601edbbda7653b54c0c6c463dec5e4d93c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Sep 2023 08:34:46 +0530 Subject: [PATCH 1703/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Maximum product subset of an array - GFG/README.md diff --git a/Maximum product subset of an array - GFG/README.md b/Maximum product subset of an array - GFG/README.md new file mode 100644 index 00000000..976db5da --- /dev/null +++ b/Maximum product subset of an array - GFG/README.md @@ -0,0 +1,36 @@ +# Maximum product subset of an array +## Medium +

Given an array A[]. The task is to find the maximum product possible with the subset of elements present in the array. The maximum product can be a single element also.
+Since the product can be large, return it modulo (109 + 7).

+ +

Example 1:

+ +
Input:
+A[] = {-1, -1, -2, 4, 3}
+Output: 24
+Explanation: Maximum product will be ( -2 * -1 * 4 * 3 ) = 24
+
+
+ +

Example 2:

+ +
Input:
+A[] = {-1, 0}
+Output: 0
+
+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function findMaxProduct() which takes an array of size N and returns an integer.

+ +

Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 <= N <= 2 * 104
+-10 <= A[i] <= 10

+
\ No newline at end of file From b54e362bf21a0e7da27252e743947caf7aa65d31 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Sep 2023 08:34:47 +0530 Subject: [PATCH 1704/3167] Added solution - LeetHub --- .../maximum-product-subset-of-an-array.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Maximum product subset of an array - GFG/maximum-product-subset-of-an-array.cpp diff --git a/Maximum product subset of an array - GFG/maximum-product-subset-of-an-array.cpp b/Maximum product subset of an array - GFG/maximum-product-subset-of-an-array.cpp new file mode 100644 index 00000000..55da1d80 --- /dev/null +++ b/Maximum product subset of an array - GFG/maximum-product-subset-of-an-array.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts +/* Driver program to test above function */ + +#include +using namespace std; + +// } Driver Code Ends +//User function Template for C++ +class Solution{ + public: + long long int findMaxProduct(vector&a, int n){ + //Write your code here + + + int negCount = 0, zeroCount = 0; + + int maximumMin = INT_MIN; + + if(n == 1) + return a[0]; + + long long int prod = 1; + + const int mod = 1e9+7; + + for(int i = 0; i < n; ++i) + { + if(a[i] == 0) + { + ++zeroCount; + continue; + } + + if(a[i] < 0) + { + ++negCount; + maximumMin = max(maximumMin, a[i]); + } + + prod = (prod * 1LL * a[i]) % mod; + } + + if(negCount == 1 and zeroCount == n-1 or zeroCount == n) + return 0; + + if(negCount & 1) + { + prod /= maximumMin; + } + + return abs(prod); + } +}; + + + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin >> n; + vectorarr(n); + for(int i = 0 ; i < n; i++){ + cin >> arr[i]; + } + Solution ob; + long long int ans = ob.findMaxProduct(arr, n); + cout << ans< Date: Wed, 13 Sep 2023 23:04:49 +0530 Subject: [PATCH 1705/3167] Create README - LeetHub --- 0135-candy/README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0135-candy/README.md diff --git a/0135-candy/README.md b/0135-candy/README.md new file mode 100644 index 00000000..71c49dd1 --- /dev/null +++ b/0135-candy/README.md @@ -0,0 +1,36 @@ +

135. Candy

Hard


There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.

+ +

You are giving candies to these children subjected to the following requirements:

+ +
    +
  • Each child must have at least one candy.
  • +
  • Children with a higher rating get more candies than their neighbors.
  • +
+ +

Return the minimum number of candies you need to have to distribute the candies to the children.

+ +

 

+

Example 1:

+ +
Input: ratings = [1,0,2]
+Output: 5
+Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
+
+ +

Example 2:

+ +
Input: ratings = [1,2,2]
+Output: 4
+Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
+The third child gets 1 candy because it satisfies the above two conditions.
+
+ +

 

+

Constraints:

+ +
    +
  • n == ratings.length
  • +
  • 1 <= n <= 2 * 104
  • +
  • 0 <= ratings[i] <= 2 * 104
  • +
+
\ No newline at end of file From 5a266125b57c1fc2fbec81b64256054753fab3db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Sep 2023 23:04:49 +0530 Subject: [PATCH 1706/3167] Attach NOTES - LeetHub --- 0135-candy/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0135-candy/NOTES.md diff --git a/0135-candy/NOTES.md b/0135-candy/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0135-candy/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a788ef21f47d73b4ce8b06ab2111b35a1f98b3cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Sep 2023 23:04:53 +0530 Subject: [PATCH 1707/3167] Time: 13 ms (86.34%), Space: 18.5 MB (10.64%) - LeetHub --- 0135-candy/0135-candy.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0135-candy/0135-candy.cpp diff --git a/0135-candy/0135-candy.cpp b/0135-candy/0135-candy.cpp new file mode 100644 index 00000000..9da63e51 --- /dev/null +++ b/0135-candy/0135-candy.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int candy(vector& ratings) { + + int n = ratings.size(); + + int total = 0; + + vector left(n, 1), right(n, 1); + + for(int i = 1; i < n; ++i) + { + if(ratings[i] > ratings[i-1]) + left[i] = left[i-1] + 1; + } + + for(int i = n-2; i >= 0; --i) + { + if(ratings[i] > ratings[i+1]) + { + right[i] = right[i+1] + 1; + } + } + + for(int i = 0; i < n; ++i) + total += max(left[i], right[i]); + + return total; + + } +}; \ No newline at end of file From 2fbd97abfeb459bd990d6f103d735ddc42296aab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Sep 2023 23:27:20 +0530 Subject: [PATCH 1708/3167] Create README - LeetHub --- Largest number possible - GFG/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Largest number possible - GFG/README.md diff --git a/Largest number possible - GFG/README.md b/Largest number possible - GFG/README.md new file mode 100644 index 00000000..eb5566b9 --- /dev/null +++ b/Largest number possible - GFG/README.md @@ -0,0 +1,16 @@ +# Largest number possible +## Easy +

Given two numbers 'N' and 'S' , find the largest number that can be formed with 'N' digits and whose sum of digits should be equals to 'S'. Return -1 if it is not possible.

+

Example 1:

+
Input: N = 2, S = 9
+Output: 90
+Explaination: It is the biggest number 
+with sum of digits equals to 9.
+

Example 2:

+
Input: N = 3, S = 20
+Output: 992
+Explaination: It is the biggest number 
+with sum of digits equals to 20.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function findLargest() which takes N and S as input parameters and returns the largest possible number. Return -1 if no such number is possible.

+

Expected Time Complexity: O(N)
Exepcted Auxiliary Space: O(1)

+

Constraints:
1 ≤ N ≤ 104
0 ≤ S ≤ 105

\ No newline at end of file From ecca1e663285040e9c69d193afcb83df7fc69ebb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Sep 2023 23:27:21 +0530 Subject: [PATCH 1709/3167] Added solution - LeetHub --- .../largest-number-possible.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Largest number possible - GFG/largest-number-possible.cpp diff --git a/Largest number possible - GFG/largest-number-possible.cpp b/Largest number possible - GFG/largest-number-possible.cpp new file mode 100644 index 00000000..b8408e03 --- /dev/null +++ b/Largest number possible - GFG/largest-number-possible.cpp @@ -0,0 +1,52 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution{ +public: + string findLargest(int N, int sum){ + // code here + + string ans=""; + if( N>1 && sum==0) return "-1"; + + for(int i=0 ;i= 0 ){ + ans.push_back('9'); + sum-=9; + } + else if( sum ==0 ){ + ans.push_back('0') ; + } + else{ + ans += to_string(sum); + sum=0; + } + } + + if(sum==0) return ans; + return "-1"; + } +}; + +//{ Driver Code Starts. + +int main(){ + int t; + cin>>t; + while(t--){ + int N, S; + cin>>N>>S; + + Solution ob; + cout< Date: Thu, 14 Sep 2023 07:58:09 +0530 Subject: [PATCH 1710/3167] Attach NOTES - LeetHub From 137144417c612f90cb84e77abd56e3ceefb1919a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Sep 2023 07:58:55 +0530 Subject: [PATCH 1711/3167] Attach NOTES - LeetHub From 336335b544aecd978448bd65e7721acef4aabe86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Sep 2023 07:58:59 +0530 Subject: [PATCH 1712/3167] Time: 16 ms (66.99%), Space: 17.4 MB (82.24%) - LeetHub --- 0135-candy/0135-candy.cpp | 51 ++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/0135-candy/0135-candy.cpp b/0135-candy/0135-candy.cpp index 9da63e51..941d332d 100644 --- a/0135-candy/0135-candy.cpp +++ b/0135-candy/0135-candy.cpp @@ -4,28 +4,45 @@ class Solution { int n = ratings.size(); - int total = 0; + int candies = n; - vector left(n, 1), right(n, 1); + int i = 1; - for(int i = 1; i < n; ++i) - { - if(ratings[i] > ratings[i-1]) - left[i] = left[i-1] + 1; - } - - for(int i = n-2; i >= 0; --i) - { - if(ratings[i] > ratings[i+1]) + while(i < n) + { + if(ratings[i] == ratings[i-1]) + { + ++i; + continue; + } + + int peak = 0; + + while(ratings[i] > ratings[i-1]) { - right[i] = right[i+1] + 1; + + ++peak; + candies += peak; + + ++i; + + if(i == n) + return candies; } + + int dip = 0; + + while(i < n and ratings[i] < ratings[i-1]) + { + ++dip; + candies += dip; + + ++i; + } + + candies -= min(peak, dip); } - for(int i = 0; i < n; ++i) - total += max(left[i], right[i]); - - return total; - + return candies; } }; \ No newline at end of file From 2d8255e660204ed0764333a66b1df8c4c8f1c7eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Sep 2023 08:03:51 +0530 Subject: [PATCH 1713/3167] Create README - LeetHub --- Perfect Sum Problem - GFG/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Perfect Sum Problem - GFG/README.md diff --git a/Perfect Sum Problem - GFG/README.md b/Perfect Sum Problem - GFG/README.md new file mode 100644 index 00000000..17111478 --- /dev/null +++ b/Perfect Sum Problem - GFG/README.md @@ -0,0 +1,18 @@ +# Perfect Sum Problem +## Medium +

Given an array arr[] of non-negative integers and an integer sum, the task is to count all subsets of the given array with a sum equal to a given sum.

+

Note: Answer can be very large, so, output answer modulo 109+7

+

Example 1:

+
Input: N = 6, arr[] = {2, 3, 5, 6, 8, 10}
+       sum = 10
+Output: 3
+Explanation: {2, 3, 5}, {2, 8}, {10} are 
possible subsets.
+
Example 2:
+
Input: N = 5, arr[] = {1, 2, 3, 4, 5}
+       sum = 10
+Output: 3
+Explanation: {1, 2, 3, 4}, {1, 4, 5}, 
+{2, 3, 5} are possible subsets.
+
Your Task:  
You don't need to read input or print anything. Complete the function perfectSum() which takes N, array arr[] and sum as input parameters and returns an integer value
+

Expected Time Complexity: O(N*sum)
Expected Auxiliary Space: O(N*sum)

Constraints:
1 ≤ N*sum ≤ 106
+
0<=arr[I]<=106
\ No newline at end of file From 7bc482bffd289721f858b957e9c1e1da821a6a3d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Sep 2023 08:03:52 +0530 Subject: [PATCH 1714/3167] Added solution - LeetHub --- .../perfect-sum-problem.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Perfect Sum Problem - GFG/perfect-sum-problem.cpp diff --git a/Perfect Sum Problem - GFG/perfect-sum-problem.cpp b/Perfect Sum Problem - GFG/perfect-sum-problem.cpp new file mode 100644 index 00000000..e02ce00c --- /dev/null +++ b/Perfect Sum Problem - GFG/perfect-sum-problem.cpp @@ -0,0 +1,72 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution{ + + + private: + + const int mod = 1e9+7; + + int helper(int idx, int n, int sum, int arr[], vector>& dp) + { + if(idx == n) + { + return sum == 0; + } + + if(dp[idx][sum] != -1) + return dp[idx][sum]; + + int notTake = helper(idx+1, n, sum, arr, dp); + + int take = 0; + + if(arr[idx] <= sum) + take = helper(idx+1 , n, sum - arr[idx], arr, dp); + + return dp[idx][sum] = (take + notTake) % mod;; + } + + public: + int perfectSum(int arr[], int n, int sum) + { + // Your code goes here + + vector> dp(n+1, vector(sum+1, -1)); + + return helper(0, n, sum, arr, dp); + + } + +}; + +//{ Driver Code Starts. +int main() +{ + + + int t; + cin >> t; + while (t--) + { + int n, sum; + + cin >> n >> sum; + + int a[n]; + for(int i = 0; i < n; i++) + cin >> a[i]; + + + + Solution ob; + cout << ob.perfectSum(a, n, sum) << "\n"; + + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 47e945ac182e80d44b24c8feac34802315de195a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:45:50 +0530 Subject: [PATCH 1715/3167] Create README - LeetHub --- 0332-reconstruct-itinerary/README.md | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0332-reconstruct-itinerary/README.md diff --git a/0332-reconstruct-itinerary/README.md b/0332-reconstruct-itinerary/README.md new file mode 100644 index 00000000..732c4175 --- /dev/null +++ b/0332-reconstruct-itinerary/README.md @@ -0,0 +1,36 @@ +

332. Reconstruct Itinerary

Hard


You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.

+ +

All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.

+ +
    +
  • For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
  • +
+ +

You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.

+ +

 

+

Example 1:

+ +
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
+Output: ["JFK","MUC","LHR","SFO","SJC"]
+
+ +

Example 2:

+ +
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
+Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
+Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= tickets.length <= 300
  • +
  • tickets[i].length == 2
  • +
  • fromi.length == 3
  • +
  • toi.length == 3
  • +
  • fromi and toi consist of uppercase English letters.
  • +
  • fromi != toi
  • +
+
\ No newline at end of file From 2dd8e8fbabebed51412b50629b8e8cc2567ab25e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:45:54 +0530 Subject: [PATCH 1716/3167] Time: 11 ms (98.42%), Space: 14.9 MB (49.29%) - LeetHub --- .../0332-reconstruct-itinerary.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0332-reconstruct-itinerary/0332-reconstruct-itinerary.cpp diff --git a/0332-reconstruct-itinerary/0332-reconstruct-itinerary.cpp b/0332-reconstruct-itinerary/0332-reconstruct-itinerary.cpp new file mode 100644 index 00000000..9f9058ab --- /dev/null +++ b/0332-reconstruct-itinerary/0332-reconstruct-itinerary.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + + unordered_map,greater>> map; + vector ans; + void dfs(string s) + { + auto &x=map[s]; + while(!x.empty()) + { + string to=x.top(); + x.pop(); + dfs(to); + } + cout< findItinerary(vector>& tickets) { + for(auto &x:tickets) + map[x[0]].push(x[1]); + dfs("JFK"); + reverse(ans.begin(),ans.end()); + return ans; + } +}; + + + \ No newline at end of file From 779f81c631305823fe0c5cbdacfff31ae5103540 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 09:17:30 +0530 Subject: [PATCH 1717/3167] Create README - LeetHub --- Partition Equal Subset Sum - GFG/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Partition Equal Subset Sum - GFG/README.md diff --git a/Partition Equal Subset Sum - GFG/README.md b/Partition Equal Subset Sum - GFG/README.md new file mode 100644 index 00000000..d31f9fc6 --- /dev/null +++ b/Partition Equal Subset Sum - GFG/README.md @@ -0,0 +1,19 @@ +# Partition Equal Subset Sum +## Medium +

Given an array arr[] of size N, check if it can be partitioned into two parts such that the sum of elements in both parts is the same.

+

Example 1:

+
Input: N = 4
+arr = {1, 5, 11, 5}
+Output: YES
+Explanation: 
+The two parts are {1, 5, 5} and {11}.
+
+

Example 2:

+
Input: N = 3
+arr = {1, 3, 5}
+Output: NO
+Explanation: This array can never be 
+partitioned into two such parts.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function equalPartition() which takes the value N and the array as input parameters and returns 1 if the partition is possible. Otherwise, returns 0.

+

Expected Time Complexity: O(N*sum of elements)
Expected Auxiliary Space: O(N*sum of elements)

+

Constraints:
1 ≤ N ≤ 100
1 ≤ arr[i] ≤ 1000
N*sum of elements ≤ 5*106

\ No newline at end of file From 7ebb4c989d3b54d3c39bc97971f013d131714040 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 09:17:31 +0530 Subject: [PATCH 1718/3167] Added solution - LeetHub --- .../partition-equal-subset-sum.cpp | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Partition Equal Subset Sum - GFG/partition-equal-subset-sum.cpp diff --git a/Partition Equal Subset Sum - GFG/partition-equal-subset-sum.cpp b/Partition Equal Subset Sum - GFG/partition-equal-subset-sum.cpp new file mode 100644 index 00000000..ed8e90c6 --- /dev/null +++ b/Partition Equal Subset Sum - GFG/partition-equal-subset-sum.cpp @@ -0,0 +1,71 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution{ + +private: + bool checkEqualPartition(int idx, int n, int sum, int arr[], vector>& dp) + { + if(idx == n) + { + return sum == 0; + } + + if(dp[idx][sum] != -1) + return dp[idx][sum]; + + bool notTake = checkEqualPartition(idx + 1, n, sum, arr, dp); + + bool take = false; + + if(arr[idx] <= sum) + take = checkEqualPartition(idx + 1, n, sum - arr[idx], arr, dp); + + return dp[idx][sum] = take | notTake; + } + + +public: + int equalPartition(int N, int arr[]) + { + // code here + + + int sum = accumulate(arr, arr+N, 0); + + if(sum & 1) + return false; + + vector> dp(N+1, vector((sum/2) + 1, -1)); + + return checkEqualPartition(0, N, sum/2, arr, dp); + } +}; + +//{ 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; + if(ob.equalPartition(N, arr)) + cout<<"YES\n"; + else + cout<<"NO\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From a89c4528dd68668b5911893442ceeb9e34118dd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 11:59:59 +0530 Subject: [PATCH 1719/3167] Create README - LeetHub From 9ad915b979cd02e24bc77739891d642bba99f453 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:33:09 +0530 Subject: [PATCH 1720/3167] Create README - LeetHub --- 1584-min-cost-to-connect-all-points/README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1584-min-cost-to-connect-all-points/README.md diff --git a/1584-min-cost-to-connect-all-points/README.md b/1584-min-cost-to-connect-all-points/README.md new file mode 100644 index 00000000..87d9ecda --- /dev/null +++ b/1584-min-cost-to-connect-all-points/README.md @@ -0,0 +1,32 @@ +

1584. Min Cost to Connect All Points

Medium


You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

+ +

The cost of connecting two points [xi, yi] and [xj, yj] is the manhattan distance between them: |xi - xj| + |yi - yj|, where |val| denotes the absolute value of val.

+ +

Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points.

+ +

 

+

Example 1:

+ +
Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
+Output: 20
+Explanation: 
+
+We can connect the points as shown above to get the minimum cost of 20.
+Notice that there is a unique path between every pair of points.
+
+ +

Example 2:

+ +
Input: points = [[3,12],[-2,5],[-4,1]]
+Output: 18
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= points.length <= 1000
  • +
  • -106 <= xi, yi <= 106
  • +
  • All pairs (xi, yi) are distinct.
  • +
+
\ No newline at end of file From 341a6284be0c1044c82f66fedb83c5ea4d490b1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:33:09 +0530 Subject: [PATCH 1721/3167] Attach NOTES - LeetHub --- 1584-min-cost-to-connect-all-points/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1584-min-cost-to-connect-all-points/NOTES.md diff --git a/1584-min-cost-to-connect-all-points/NOTES.md b/1584-min-cost-to-connect-all-points/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1584-min-cost-to-connect-all-points/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From eab72d1b8144782dd07925dd3314de82569ce1d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:33:13 +0530 Subject: [PATCH 1722/3167] Time: 354 ms (68.19%), Space: 58.7 MB (54.80%) - LeetHub --- .../1584-min-cost-to-connect-all-points.cpp | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 1584-min-cost-to-connect-all-points/1584-min-cost-to-connect-all-points.cpp diff --git a/1584-min-cost-to-connect-all-points/1584-min-cost-to-connect-all-points.cpp b/1584-min-cost-to-connect-all-points/1584-min-cost-to-connect-all-points.cpp new file mode 100644 index 00000000..b131ebcb --- /dev/null +++ b/1584-min-cost-to-connect-all-points/1584-min-cost-to-connect-all-points.cpp @@ -0,0 +1,112 @@ +class DSU{ + private: + vector rank, parent, size; + 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 parent[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: + int minCostConnectPoints(vector>& points) { + + int n = points.size(); + + vector > adj; + + for(int i = 0; i < n; ++i) + { + for(int j = i+1; j < n; ++j) + { + int weight = abs(points[j][0] - points[i][0]) + abs(points[j][1] - points[i][1]); + + adj.push_back({weight, i, j}); + } + } + + + sort(adj.begin(), adj.end()); + + DSU dsu(n+1); + + int minimumCost = 0 ; + + for(auto& edges : adj) + { + int weight = get<0>(edges); + int u = get<1>(edges); + int v = get<2>(edges); + + if(!dsu.isSame(u, v)) + { + minimumCost += weight; + dsu.unionBySize(u, v); + } + } + + return minimumCost; + } +}; \ No newline at end of file From 01344009e5da5edcfe68dbccdcb3424fae38b4cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:33:37 +0530 Subject: [PATCH 1723/3167] Attach NOTES - LeetHub From 636c116a4406d728c6c2aff5596e0cf8ebb611e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:33:41 +0530 Subject: [PATCH 1724/3167] Time: 354 ms (68.19%), Space: 58.7 MB (54.80%) - LeetHub From d1568088f457d49c3a748bd55634b9ec3bd3b939 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 00:15:22 +0530 Subject: [PATCH 1725/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort/README.md 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-div1631-path-with-minimum-effort/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-div1631-path-with-minimum-effort/README.md new file mode 100644 index 00000000..04c9049d --- /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-div1631-path-with-minimum-effort/README.md @@ -0,0 +1,42 @@ +

No companies found for this problem
1631. Path With Minimum Effort

Medium


You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.

+ +

A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.

+ +

Return the minimum effort required to travel from the top-left cell to the bottom-right cell.

+ +

 

+

Example 1:

+ +

+ +
Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
+Output: 2
+Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
+This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
+
+ +

Example 2:

+ +

+ +
Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
+Output: 1
+Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
+
+ +

Example 3:

+ +
Input: heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
+Output: 0
+Explanation: This route does not require any effort.
+
+ +

 

+

Constraints:

+ +
    +
  • rows == heights.length
  • +
  • columns == heights[i].length
  • +
  • 1 <= rows, columns <= 100
  • +
  • 1 <= heights[i][j] <= 106
  • +
\ No newline at end of file From 100ba40c13d462bac26693cad11a3aa02a98a761 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 00:15:26 +0530 Subject: [PATCH 1726/3167] Time: 165 ms (36.79%), Space: 20.4 MB (35.64%) - LeetHub --- ...v-div-div1631-path-with-minimum-effort.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.cpp 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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.cpp new file mode 100644 index 00000000..312b5dd0 --- /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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int minimumEffortPath(vector>& h) { + int si = h.size(), sj = h[0].size(); + vector> eff(si, vector(sj, 1000000)); + vector dir{0, 1, 0, -1, 0}; + auto comp = [&](const array &p1, const array &p2) { return p1[0] >= p2[0]; }; + priority_queue, vector>, decltype(comp)> pq(comp); + eff[0][0] = 0; + pq.push({0, 0, 0}); + while(!pq.empty()) { + auto [e, i, j] = pq.top(); + pq.pop(); + if (i == si - 1 && j == sj - 1) + break; + for (auto d = 0; d < 4; ++d) { + int x = i + dir[d], y = j + dir[d + 1]; + if (x >= 0 && y >= 0 && x < si && y < sj) { + int new_eff = max(eff[i][j], abs(h[i][j] - h[x][y])); + if (new_eff < eff[x][y]) { + eff[x][y] = new_eff; + pq.push({new_eff, x, y}); + } + } + } + } + return eff[si - 1][sj - 1]; +} +}; \ No newline at end of file From a1c74e0a514f872f6266c8eebba96e8f2f848d79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 07:59:30 +0530 Subject: [PATCH 1727/3167] Create README - LeetHub --- Print first n Fibonacci Numbers - GFG/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Print first n Fibonacci Numbers - GFG/README.md diff --git a/Print first n Fibonacci Numbers - GFG/README.md b/Print first n Fibonacci Numbers - GFG/README.md new file mode 100644 index 00000000..9cde00f5 --- /dev/null +++ b/Print first n Fibonacci Numbers - GFG/README.md @@ -0,0 +1,15 @@ +# Print first n Fibonacci Numbers +## Easy +

Given a number N, find the first N Fibonacci numbers. The first two number of the series are 1 and 1.

+

Example 1:

+
Input:
+N = 5
+Output: 1 1 2 3 5
+
+

Example 2:

+
Input:
+N = 7
+Output: 1 1 2 3 5 8 13
+

Your Task:
Your task is to complete printFibb() which takes single argument N and returns a list of first N Fibonacci numbers.

+

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Note: This space is used to store and return the answer for printing purpose.

+

Constraints:
1<= N <=84

\ No newline at end of file From 4db8ab451d06b885d740e71da2d1db630908e8dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 07:59:31 +0530 Subject: [PATCH 1728/3167] Added solution - LeetHub --- .../print-first-n-fibonacci-numbers.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Print first n Fibonacci Numbers - GFG/print-first-n-fibonacci-numbers.cpp diff --git a/Print first n Fibonacci Numbers - GFG/print-first-n-fibonacci-numbers.cpp b/Print first n Fibonacci Numbers - GFG/print-first-n-fibonacci-numbers.cpp new file mode 100644 index 00000000..68cf1a0c --- /dev/null +++ b/Print first n Fibonacci Numbers - GFG/print-first-n-fibonacci-numbers.cpp @@ -0,0 +1,65 @@ +//{ Driver Code Starts +//Initial function template for C++ + +#include +using namespace std; + +// } Driver Code Ends +//User function template for C++ + + +class Solution +{ + public: + //Function to return list containing first n fibonacci numbers. + vector printFibb(int n) + { + //code here + + vector ans; + + + long long a = 0, b = 1, c; + + if(n>=1) + ans.push_back(b); + + for(int i = 0; i < n-1; ++i) + { + c = a + b; + + a = b; + + b = c; + + ans.push_back(c); + } + + return ans; + + } +}; + +//{ Driver Code Starts. +int main() + { + //taking total testcases + int t; + cin>>t; + while(t--) + { + //taking number of elements + int n; + cin>>n; + Solution obj; + //calling function printFibb() + vector ans = obj.printFibb(n); + + //printing the elements of vector + for(long long i:ans)cout< Date: Sun, 17 Sep 2023 14:23:03 +0530 Subject: [PATCH 1729/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort/NOTES.md 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-div1631-path-with-minimum-effort/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-div1631-path-with-minimum-effort/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-div1631-path-with-minimum-effort/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7e102a913fe708bf91208d41b133d9132d75d87d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 14:23:06 +0530 Subject: [PATCH 1730/3167] Time: 144 ms (45.26%), Space: 20.2 MB (37.90%) - LeetHub --- ...v-div-div1631-path-with-minimum-effort.cpp | 67 ++++++++++++------- 1 file changed, 43 insertions(+), 24 deletions(-) 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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.cpp index 312b5dd0..68298589 100644 --- 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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.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-div1631-path-with-minimum-effort/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1631-path-with-minimum-effort.cpp @@ -1,29 +1,48 @@ class Solution { public: - int minimumEffortPath(vector>& h) { - int si = h.size(), sj = h[0].size(); - vector> eff(si, vector(sj, 1000000)); - vector dir{0, 1, 0, -1, 0}; - auto comp = [&](const array &p1, const array &p2) { return p1[0] >= p2[0]; }; - priority_queue, vector>, decltype(comp)> pq(comp); - eff[0][0] = 0; - pq.push({0, 0, 0}); - while(!pq.empty()) { - auto [e, i, j] = pq.top(); - pq.pop(); - if (i == si - 1 && j == sj - 1) - break; - for (auto d = 0; d < 4; ++d) { - int x = i + dir[d], y = j + dir[d + 1]; - if (x >= 0 && y >= 0 && x < si && y < sj) { - int new_eff = max(eff[i][j], abs(h[i][j] - h[x][y])); - if (new_eff < eff[x][y]) { - eff[x][y] = new_eff; - pq.push({new_eff, x, y}); - } - } + int minimumEffortPath(vector>& heights) { + + int n = heights.size(); + int m = heights[0].size(); + priority_queue, vector>, greater> > pq; + + vector> dist(n, vector(m, INT_MAX)); + + dist[0][0] = 0; + + pq.push({0, 0, 0}); + + vector dx = {0, +1, 0, -1}; + vector dy = {+1, 0, -1, 0}; + + while(!pq.empty()) + { + auto currRoute = pq.top(); + pq.pop(); + + int currDiff = get<0> (currRoute); + int x = get<1> (currRoute); + int y = get<2> (currRoute); + + for(int i = 0; i < 4; ++i) + { + int newx = dx[i] + x; + int newy = dy[i] + y; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m) + { + int diff = max(currDiff, abs(heights[x][y] - heights[newx][newy])); + + if(diff < dist[newx][newy]) + { + pq.push({diff, newx, newy}); + dist[newx][newy] = diff; + } + } + } } + + return dist[n-1][m-1]; + } - return eff[si - 1][sj - 1]; -} }; \ No newline at end of file From 097775f6301b8827e78376084cea2f9ff80b8beb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 22:39:58 +0530 Subject: [PATCH 1731/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div847-shortest-path-visiting-all-nodes/README.md 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-div847-shortest-path-visiting-all-nodes/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-div847-shortest-path-visiting-all-nodes/README.md new file mode 100644 index 00000000..6635b22a --- /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-div847-shortest-path-visiting-all-nodes/README.md @@ -0,0 +1,31 @@ +

No companies found for this problem
847. Shortest Path Visiting All Nodes

Hard


You have an undirected, connected graph of n nodes labeled from 0 to n - 1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge.

+ +

Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

+ +

 

+

Example 1:

+ +
Input: graph = [[1,2,3],[0],[0],[0]]
+Output: 4
+Explanation: One possible path is [1,0,2,0,3]
+
+ +

Example 2:

+ +
Input: graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
+Output: 4
+Explanation: One possible path is [0,1,4,2,3]
+
+ +

 

+

Constraints:

+ +
    +
  • n == graph.length
  • +
  • 1 <= n <= 12
  • +
  • 0 <= graph[i].length < n
  • +
  • graph[i] does not contain i.
  • +
  • If graph[a] contains b, then graph[b] contains a.
  • +
  • The input graph is always connected.
  • +
+
\ No newline at end of file From bc234e7c6365b6c2d84b8ca037acac0571b2732c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 22:40:03 +0530 Subject: [PATCH 1732/3167] Time: 99 ms (42.96%), Space: 17.8 MB (45.73%) - LeetHub --- ...iv847-shortest-path-visiting-all-nodes.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div847-shortest-path-visiting-all-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div847-shortest-path-visiting-all-nodes.cpp 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-div847-shortest-path-visiting-all-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div847-shortest-path-visiting-all-nodes.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-div847-shortest-path-visiting-all-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div847-shortest-path-visiting-all-nodes.cpp new file mode 100644 index 00000000..a9571d67 --- /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-div847-shortest-path-visiting-all-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div847-shortest-path-visiting-all-nodes.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + int shortestPathLength(vector>& graph) { + + int n = graph.size(); + + if(n == 1) + return 0; + + queue > q; + + set > visited; + + int allVisited = (1 << n) - 1; + + for(int i = 0; i < n; ++i) + { + int mask = 1 << i; + + q.push({i, mask}); + + visited.insert({i, mask}); + } + + int path = 0; + + while(!q.empty()) + { + int size = q.size(); + + ++path; + + while(size--) + { + auto curr = q.front(); + q.pop(); + + int node = curr.first; + int mask = curr.second; + + for(auto& adjNode : graph[node]) + { + int newMask = mask | (1 << adjNode); + + if(newMask == allVisited) + return path; + + if(!visited.count({adjNode, newMask})) + { + q.push({adjNode, newMask}); + visited.insert({adjNode, newMask}); + } + } + } + } + + return -1; + + } +}; \ No newline at end of file From dab183e551d2d8d1a45e544675d8bcfba1bb0d60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 22:42:22 +0530 Subject: [PATCH 1733/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0847-shortest-path-visiting-all-nodes/README.md diff --git a/0847-shortest-path-visiting-all-nodes/README.md b/0847-shortest-path-visiting-all-nodes/README.md new file mode 100644 index 00000000..16ac110c --- /dev/null +++ b/0847-shortest-path-visiting-all-nodes/README.md @@ -0,0 +1,31 @@ +

847. Shortest Path Visiting All Nodes

Hard


You have an undirected, connected graph of n nodes labeled from 0 to n - 1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge.

+ +

Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

+ +

 

+

Example 1:

+ +
Input: graph = [[1,2,3],[0],[0],[0]]
+Output: 4
+Explanation: One possible path is [1,0,2,0,3]
+
+ +

Example 2:

+ +
Input: graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
+Output: 4
+Explanation: One possible path is [0,1,4,2,3]
+
+ +

 

+

Constraints:

+ +
    +
  • n == graph.length
  • +
  • 1 <= n <= 12
  • +
  • 0 <= graph[i].length < n
  • +
  • graph[i] does not contain i.
  • +
  • If graph[a] contains b, then graph[b] contains a.
  • +
  • The input graph is always connected.
  • +
+
\ No newline at end of file From f866e17e9e34cfa565e71481675d4561337e41d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Sep 2023 22:42:26 +0530 Subject: [PATCH 1734/3167] Time: 99 ms (42.96%), Space: 17.8 MB (45.73%) - LeetHub --- .../0847-shortest-path-visiting-all-nodes.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 0847-shortest-path-visiting-all-nodes/0847-shortest-path-visiting-all-nodes.cpp diff --git a/0847-shortest-path-visiting-all-nodes/0847-shortest-path-visiting-all-nodes.cpp b/0847-shortest-path-visiting-all-nodes/0847-shortest-path-visiting-all-nodes.cpp new file mode 100644 index 00000000..a9571d67 --- /dev/null +++ b/0847-shortest-path-visiting-all-nodes/0847-shortest-path-visiting-all-nodes.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + int shortestPathLength(vector>& graph) { + + int n = graph.size(); + + if(n == 1) + return 0; + + queue > q; + + set > visited; + + int allVisited = (1 << n) - 1; + + for(int i = 0; i < n; ++i) + { + int mask = 1 << i; + + q.push({i, mask}); + + visited.insert({i, mask}); + } + + int path = 0; + + while(!q.empty()) + { + int size = q.size(); + + ++path; + + while(size--) + { + auto curr = q.front(); + q.pop(); + + int node = curr.first; + int mask = curr.second; + + for(auto& adjNode : graph[node]) + { + int newMask = mask | (1 << adjNode); + + if(newMask == allVisited) + return path; + + if(!visited.count({adjNode, newMask})) + { + q.push({adjNode, newMask}); + visited.insert({adjNode, newMask}); + } + } + } + } + + return -1; + + } +}; \ No newline at end of file From 203794174e8e0b5db718754db890e00bc02a0984 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 07:47:40 +0530 Subject: [PATCH 1735/3167] Create README - LeetHub --- .../README.md | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 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-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-div1337-the-k-weakest-rows-in-a-matrix/README.md 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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix/README.md new file mode 100644 index 00000000..ad6781fe --- /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-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-div1337-the-k-weakest-rows-in-a-matrix/README.md @@ -0,0 +1,61 @@ +

Amazon
1
1337. The K Weakest Rows in a Matrix

Easy


You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.

+ +

A row i is weaker than a row j if one of the following is true:

+ +
    +
  • The number of soldiers in row i is less than the number of soldiers in row j.
  • +
  • Both rows have the same number of soldiers and i < j.
  • +
+ +

Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.

+ +

 

+

Example 1:

+ +
Input: mat = 
+[[1,1,0,0,0],
+ [1,1,1,1,0],
+ [1,0,0,0,0],
+ [1,1,0,0,0],
+ [1,1,1,1,1]], 
+k = 3
+Output: [2,0,3]
+Explanation: 
+The number of soldiers in each row is: 
+- Row 0: 2 
+- Row 1: 4 
+- Row 2: 1 
+- Row 3: 2 
+- Row 4: 5 
+The rows ordered from weakest to strongest are [2,0,3,1,4].
+
+ +

Example 2:

+ +
Input: mat = 
+[[1,0,0,0],
+ [1,1,1,1],
+ [1,0,0,0],
+ [1,0,0,0]], 
+k = 2
+Output: [0,2]
+Explanation: 
+The number of soldiers in each row is: 
+- Row 0: 1 
+- Row 1: 4 
+- Row 2: 1 
+- Row 3: 1 
+The rows ordered from weakest to strongest are [0,2,3,1].
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 2 <= n, m <= 100
  • +
  • 1 <= k <= m
  • +
  • matrix[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From ad75008e78325a056cc0a68673e6f812619f3660 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 07:47:44 +0530 Subject: [PATCH 1736/3167] Time: 6 ms (91.97%), Space: 10.8 MB (9.15%) - LeetHub --- ...div1337-the-k-weakest-rows-in-a-matrix.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.cpp 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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.cpp new file mode 100644 index 00000000..303f9688 --- /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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector kWeakestRows(vector>& mat, int k) { + + int n = mat.size(); + int m = mat[0].size(); + + vector ans; + + priority_queue, vector>, greater> > pq; + + for(int i = 0; i < n; ++i) + { + int currRowOnesCount = 0; + for(int j = 0; j < m; ++j) + { + if(mat[i][j] == 1) + ++currRowOnesCount; + else + break; + } + + pq.push({currRowOnesCount, i}); + } + + while(k--) + { + int kthWeakestIdx = pq.top().second; + pq.pop(); + + ans.push_back(kthWeakestIdx); + } + + return ans; + } +}; \ No newline at end of file From a1d133b1e1ea7db4f0e42f092b147b057d48ef83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 07:51:13 +0530 Subject: [PATCH 1737/3167] Create README - LeetHub --- 1337-the-k-weakest-rows-in-a-matrix/README.md | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 1337-the-k-weakest-rows-in-a-matrix/README.md diff --git a/1337-the-k-weakest-rows-in-a-matrix/README.md b/1337-the-k-weakest-rows-in-a-matrix/README.md new file mode 100644 index 00000000..7f0784e5 --- /dev/null +++ b/1337-the-k-weakest-rows-in-a-matrix/README.md @@ -0,0 +1,61 @@ +

1337. The K Weakest Rows in a Matrix

Easy


You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.

+ +

A row i is weaker than a row j if one of the following is true:

+ +
    +
  • The number of soldiers in row i is less than the number of soldiers in row j.
  • +
  • Both rows have the same number of soldiers and i < j.
  • +
+ +

Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.

+ +

 

+

Example 1:

+ +
Input: mat = 
+[[1,1,0,0,0],
+ [1,1,1,1,0],
+ [1,0,0,0,0],
+ [1,1,0,0,0],
+ [1,1,1,1,1]], 
+k = 3
+Output: [2,0,3]
+Explanation: 
+The number of soldiers in each row is: 
+- Row 0: 2 
+- Row 1: 4 
+- Row 2: 1 
+- Row 3: 2 
+- Row 4: 5 
+The rows ordered from weakest to strongest are [2,0,3,1,4].
+
+ +

Example 2:

+ +
Input: mat = 
+[[1,0,0,0],
+ [1,1,1,1],
+ [1,0,0,0],
+ [1,0,0,0]], 
+k = 2
+Output: [0,2]
+Explanation: 
+The number of soldiers in each row is: 
+- Row 0: 1 
+- Row 1: 4 
+- Row 2: 1 
+- Row 3: 1 
+The rows ordered from weakest to strongest are [0,2,3,1].
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 2 <= n, m <= 100
  • +
  • 1 <= k <= m
  • +
  • matrix[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From fa33b5fa926919b16f16526efff4d2146fbbb517 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 07:51:17 +0530 Subject: [PATCH 1738/3167] Time: 6 ms (91.97%), Space: 10.8 MB (9.15%) - LeetHub --- .../1337-the-k-weakest-rows-in-a-matrix.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1337-the-k-weakest-rows-in-a-matrix/1337-the-k-weakest-rows-in-a-matrix.cpp diff --git a/1337-the-k-weakest-rows-in-a-matrix/1337-the-k-weakest-rows-in-a-matrix.cpp b/1337-the-k-weakest-rows-in-a-matrix/1337-the-k-weakest-rows-in-a-matrix.cpp new file mode 100644 index 00000000..303f9688 --- /dev/null +++ b/1337-the-k-weakest-rows-in-a-matrix/1337-the-k-weakest-rows-in-a-matrix.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector kWeakestRows(vector>& mat, int k) { + + int n = mat.size(); + int m = mat[0].size(); + + vector ans; + + priority_queue, vector>, greater> > pq; + + for(int i = 0; i < n; ++i) + { + int currRowOnesCount = 0; + for(int j = 0; j < m; ++j) + { + if(mat[i][j] == 1) + ++currRowOnesCount; + else + break; + } + + pq.push({currRowOnesCount, i}); + } + + while(k--) + { + int kthWeakestIdx = pq.top().second; + pq.pop(); + + ans.push_back(kthWeakestIdx); + } + + return ans; + } +}; \ No newline at end of file From 1a23a8657aa399c327cfff383fd8895c4936cbe1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 07:57:10 +0530 Subject: [PATCH 1739/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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-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-div1337-the-k-weakest-rows-in-a-matrix/NOTES.md 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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c05f8185960aae1717a8d164705ac9edad812b7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 07:57:14 +0530 Subject: [PATCH 1740/3167] Time: 8 ms (76.19%), Space: 10.8 MB (9.15%) - LeetHub --- ...div1337-the-k-weakest-rows-in-a-matrix.cpp | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) 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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.cpp index 303f9688..f9a788d0 100644 --- 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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.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-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-div1337-the-k-weakest-rows-in-a-matrix/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-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-div1337-the-k-weakest-rows-in-a-matrix.cpp @@ -7,30 +7,44 @@ class Solution { vector ans; - priority_queue, vector>, greater> > pq; - - for(int i = 0; i < n; ++i) + auto binarySearch = [&](vector& arr) { - int currRowOnesCount = 0; - for(int j = 0; j < m; ++j) + int low = 0, high = m-1; + + while(low <= high) { - if(mat[i][j] == 1) - ++currRowOnesCount; + int mid = (low + high) >> 1; + + if(arr[mid] == 0) + high = mid-1; else - break; + low = mid+1; } + return low; + }; + + priority_queue > pq; + + for(int i = 0; i < n; ++i) + { + int currRowOnesCount = binarySearch(mat[i]); pq.push({currRowOnesCount, i}); + + if(pq.size() > k) + pq.pop(); } while(k--) { - int kthWeakestIdx = pq.top().second; + int kthStrongestIdx = pq.top().second; pq.pop(); - ans.push_back(kthWeakestIdx); + ans.push_back(kthStrongestIdx); } + reverse(ans.begin(), ans.end()); + return ans; } }; \ No newline at end of file From b177087a10f09a22e9c8bf4945288573084d53dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 08:05:40 +0530 Subject: [PATCH 1741/3167] Create README - LeetHub --- Power of 2 - GFG/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Power of 2 - GFG/README.md diff --git a/Power of 2 - GFG/README.md b/Power of 2 - GFG/README.md new file mode 100644 index 00000000..dc4810fa --- /dev/null +++ b/Power of 2 - GFG/README.md @@ -0,0 +1,14 @@ +# Power of 2 +## Easy +

Given a non-negative integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed as 2x for some integer x.

+

Example 1:

+
Input: 
N = 8 +Output:
YES +Explanation:
8 is equal to 2 raised to 3 (23 = 8).
+

Example 2:

+
Input: 
N = 98 +Output:
NO +Explanation:
98 cannot be obtained by any power of 2.
+

Your Task:Your task is to complete the function isPowerofTwo() which takes n as a parameter and returns true or false by checking if the given number can be represented as a power of two or not.

+

Expected Time Complexity:O(log N).
Expected Auxiliary Space:O(1).

+

Constraints:
0 ≤ N ≤1018

\ No newline at end of file From 6bec79e94d7339e89137ab44cefbe149da679518 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Sep 2023 08:05:41 +0530 Subject: [PATCH 1742/3167] Added solution - LeetHub --- Power of 2 - GFG/power-of-2.cpp | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Power of 2 - GFG/power-of-2.cpp diff --git a/Power of 2 - GFG/power-of-2.cpp b/Power of 2 - GFG/power-of-2.cpp new file mode 100644 index 00000000..a31b0eb0 --- /dev/null +++ b/Power of 2 - GFG/power-of-2.cpp @@ -0,0 +1,48 @@ +//{ 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 given number n is a power of two. + bool isPowerofTwo(long long n){ + + // Your code here + + return __builtin_popcountll(n) == 1; + + } +}; + +//{ Driver Code Starts. + +// Driver code +int main() +{ + + int t; + cin>>t;//testcases + + for(int i=0;i>n; + + Solution ob; + if(ob.isPowerofTwo(n))//Now, if log2 produces an integer not decimal then we are sure raising 2 to this value + cout<<"YES"< Date: Tue, 19 Sep 2023 01:22:05 +0530 Subject: [PATCH 1743/3167] Added solution - LeetHub --- .../find-first-set-bit.cpp | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Find first set bit - GFG/find-first-set-bit.cpp diff --git a/Find first set bit - GFG/find-first-set-bit.cpp b/Find first set bit - GFG/find-first-set-bit.cpp new file mode 100644 index 00000000..1bcfc21c --- /dev/null +++ b/Find first set bit - GFG/find-first-set-bit.cpp @@ -0,0 +1,54 @@ +//{ Driver Code Starts +//Initial Template for C++ + + +#include +using namespace std; + + +// } Driver Code Ends +//User function Template for C++ + +class Solution +{ + public: + //Function to find position of first set bit in the given number. + unsigned int getFirstSetBit(int n) + { + // Your code here + + int posFromRight = 0; + + while(n > 0) + { + ++posFromRight; + + if(n&1) + return posFromRight; + + n/=2; + + } + + return posFromRight; + } +}; + +//{ Driver Code Starts. + +// Driver code +int main() +{ + int t; + cin>>t; // testcases + while(t--) + { + int n; + cin>>n; //input n + Solution ob; + printf("%u\n", ob.getFirstSetBit(n)); // function to get answer + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From d8dcb04e049e5d05ba06ada1dcb7bf5df9a75c30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:02:46 +0530 Subject: [PATCH 1744/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/README.md 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/README.md new file mode 100644 index 00000000..2935c464 --- /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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/README.md @@ -0,0 +1,37 @@ +

Amazon
16
Microsoft
11
Facebook
4
Uber
3
Apple
3
Google
2
Qualcomm
2
287. Find the Duplicate Number

Medium


Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

+ +

There is only one repeated number in nums, return this repeated number.

+ +

You must solve the problem without modifying the array nums and uses only constant extra space.

+ +

 

+

Example 1:

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

Example 2:

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

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • nums.length == n + 1
  • +
  • 1 <= nums[i] <= n
  • +
  • All the integers in nums appear only once except for precisely one integer which appears two or more times.
  • +
+ +

 

+

Follow up:

+ +
    +
  • How can we prove that at least one duplicate number must exist in nums?
  • +
  • Can you solve the problem in linear runtime complexity?
  • +
+
\ No newline at end of file From f1cb39dd84da4c0ee0413227b081dfd5866b6c2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:02:50 +0530 Subject: [PATCH 1745/3167] Time: 85 ms (63.28%), Space: 61.4 MB (30.42%) - LeetHub --- ...v-div-div287-find-the-duplicate-number.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.cpp 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.cpp new file mode 100644 index 00000000..855fb681 --- /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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int findDuplicate(vector& nums) { + + int fast = nums[0]; + int slow = nums[0]; + + do{ + fast = nums[nums[fast]]; + slow = nums[slow]; + }while(fast != slow); + + fast = nums[0]; + + while(fast != slow) + { + fast = nums[fast]; + slow = nums[slow]; + } + + return slow; + + } +}; \ No newline at end of file From c75fedc03151d6d128810662c1b750a8d1837b87 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:22:43 +0530 Subject: [PATCH 1746/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/NOTES.md 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From eb8b212c18e23fd3b3614bc0bc7a04eab77d6215 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:22:47 +0530 Subject: [PATCH 1747/3167] Time: 80 ms (74.03%), Space: 61.5 MB (28.42%) - LeetHub --- ...v-div-div287-find-the-duplicate-number.cpp | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.cpp index 855fb681..981afefa 100644 --- 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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number/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-tagoccurence16-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence11-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divqualcomm-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-div287-find-the-duplicate-number.cpp @@ -2,23 +2,18 @@ class Solution { public: int findDuplicate(vector& nums) { - int fast = nums[0]; - int slow = nums[0]; + int n = nums.size(); - do{ - fast = nums[nums[fast]]; - slow = nums[slow]; - }while(fast != slow); - - fast = nums[0]; - - while(fast != slow) + for(int i = 0; i < n; ++i) { - fast = nums[fast]; - slow = nums[slow]; + int idx = abs(nums[i]); + + if(nums[idx] < 0) + return idx; + + nums[idx] = -nums[idx]; } - return slow; - + return n; } }; \ No newline at end of file From e528ba57e07630335c7f1e807f7bb6cd1724641c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Sep 2023 23:40:05 +0530 Subject: [PATCH 1748/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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-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-div1658-minimum-operations-to-reduce-x-to-zero/NOTES.md 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-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-div1658-minimum-operations-to-reduce-x-to-zero/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-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-div1658-minimum-operations-to-reduce-x-to-zero/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-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-div1658-minimum-operations-to-reduce-x-to-zero/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4226036a779681c64ed11b7aea2c75a7e26c632d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Sep 2023 23:40:09 +0530 Subject: [PATCH 1749/3167] Time: 138 ms (43.53%), Space: 98.8 MB (36.20%) - LeetHub --- ...minimum-operations-to-reduce-x-to-zero.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 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-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-div1658-minimum-operations-to-reduce-x-to-zero/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-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-div1658-minimum-operations-to-reduce-x-to-zero.cpp 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-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-div1658-minimum-operations-to-reduce-x-to-zero/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-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-div1658-minimum-operations-to-reduce-x-to-zero.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-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-div1658-minimum-operations-to-reduce-x-to-zero/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-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-div1658-minimum-operations-to-reduce-x-to-zero.cpp new file mode 100644 index 00000000..a5741f66 --- /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-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-div1658-minimum-operations-to-reduce-x-to-zero/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-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-div1658-minimum-operations-to-reduce-x-to-zero.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int minOperations(vector& nums, int x) { + int tot = 0; + for(auto itr : nums) + tot += itr; + int ans = 0; + int n = nums.size(); + + if(tot == x) + return n; + + int i = 0, j = 0; + int ind = tot - x; + int sum = 0; + + while(j < n) + { + sum += nums[j]; + + while(i < j && sum > ind) + sum -= nums[i++]; + + if(sum == ind) + ans = max(ans,j - i + 1); + ++j; + } + + if(!ans) + return -1; + else + return n - ans; + } +}; \ No newline at end of file From 242edc636de75a3be3bd053f774e7cd71ec6b34c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 00:19:16 +0530 Subject: [PATCH 1750/3167] Create README - LeetHub --- 0004-median-of-two-sorted-arrays/README.md | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0004-median-of-two-sorted-arrays/README.md diff --git a/0004-median-of-two-sorted-arrays/README.md b/0004-median-of-two-sorted-arrays/README.md new file mode 100644 index 00000000..e547cad4 --- /dev/null +++ b/0004-median-of-two-sorted-arrays/README.md @@ -0,0 +1,31 @@ +

4. Median of Two Sorted Arrays

Hard


Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

+ +

The overall run time complexity should be O(log (m+n)).

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,3], nums2 = [2]
+Output: 2.00000
+Explanation: merged array = [1,2,3] and median is 2.
+
+ +

Example 2:

+ +
Input: nums1 = [1,2], nums2 = [3,4]
+Output: 2.50000
+Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
+
+ +

 

+

Constraints:

+ +
    +
  • nums1.length == m
  • +
  • nums2.length == n
  • +
  • 0 <= m <= 1000
  • +
  • 0 <= n <= 1000
  • +
  • 1 <= m + n <= 2000
  • +
  • -106 <= nums1[i], nums2[i] <= 106
  • +
+
\ No newline at end of file From 19293840911c012ed964b0986a372325d11a57c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 00:19:20 +0530 Subject: [PATCH 1751/3167] Time: 19 ms (89.46%), Space: 89.8 MB (33.58%) - LeetHub --- .../0004-median-of-two-sorted-arrays.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0004-median-of-two-sorted-arrays/0004-median-of-two-sorted-arrays.cpp diff --git a/0004-median-of-two-sorted-arrays/0004-median-of-two-sorted-arrays.cpp b/0004-median-of-two-sorted-arrays/0004-median-of-two-sorted-arrays.cpp new file mode 100644 index 00000000..caf6766a --- /dev/null +++ b/0004-median-of-two-sorted-arrays/0004-median-of-two-sorted-arrays.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + + if(nums2.size() < nums1.size()) + return findMedianSortedArrays(nums2,nums1); + + int n1 = nums1.size(), n2 = nums2.size(); + int low = 0, high = nums1.size(); + + int l1,l2 , r1, r2; + + while(low <= high) + { + int cut1 = (low + high) / 2; + int cut2 = (n1 + n2 + 1)/2 - cut1; + + l1 = cut1 == 0 ? INT_MIN : nums1[cut1-1]; + l2 = cut2 == 0 ? INT_MIN : nums2[cut2-1]; + + r1 = cut1 == n1 ? INT_MAX : nums1[cut1]; + r2 = cut2 == n2 ? INT_MAX : nums2[cut2]; + + + if(l1 <= r2 and l2 <= r1) + { + if((n1 + n2)%2 == 0) + { + return (max(l1,l2) + min(r1,r2)) / 2.0; + } + else + return max(l1,l2); + } + else if(l1 > r2) + high = cut1 - 1; + else + low = cut1 + 1; + } + + return 0.0; + } +}; \ No newline at end of file From 5081ea7f6ffc78ddc9826bdfcef41941411503ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 19:16:17 +0530 Subject: [PATCH 1752/3167] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/README.md 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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/README.md new file mode 100644 index 00000000..ff4571c0 --- /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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/README.md @@ -0,0 +1,23 @@ +

Amazon
4
Google
3
Yandex
3
Adobe
2
392. Is Subsequence

Easy


Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

+ +

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

+ +

 

+

Example 1:

+
Input: s = "abc", t = "ahbgdc"
+Output: true
+

Example 2:

+
Input: s = "axc", t = "ahbgdc"
+Output: false
+
+

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 100
  • +
  • 0 <= t.length <= 104
  • +
  • s and t consist only of lowercase English letters.
  • +
+ +

 

+Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
\ No newline at end of file From 6cd94b5034ec890b1173e5150b11715671d3f8ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 19:16:21 +0530 Subject: [PATCH 1753/3167] Time: 3 ms (49.10%), Space: 6.7 MB (17.60%) - LeetHub --- ...rect-svg-div-div-div-div392-is-subsequence.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence.cpp 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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence.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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence.cpp new file mode 100644 index 00000000..d31a4cb6 --- /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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence/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-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-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-div392-is-subsequence.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + bool isSubsequence(string s, string t) { + + int k = 0; + + for(int i = 0; i < t.size() and k < s.size(); ++i) + { + if(t[i] == s[k]) + ++k; + } + + return k == s.size(); + } +}; \ No newline at end of file From d3684560e91cfc6d58cd92e10968377bf86ce19e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 19:41:31 +0530 Subject: [PATCH 1754/3167] Create README - LeetHub --- .../README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 First and last occurrences of x - GFG/README.md diff --git a/First and last occurrences of x - GFG/README.md b/First and last occurrences of x - GFG/README.md new file mode 100644 index 00000000..fee07a58 --- /dev/null +++ b/First and last occurrences of x - GFG/README.md @@ -0,0 +1,19 @@ +# First and last occurrences of x +## Medium +

Given a sorted array arr containing n elements with possibly duplicate is to find indexes of first elements, the task is to find the first and last occurrences of an element x in the given array.
Note: If the number x is not found in the array then return both the indices as -1.

+

Example 1:

+
Input:
+n=9, x=5
+arr[] = { 1, 3, 5, 5, 5, 5, 67, 123, 125 }
+Output:  
2 5 +Explanation:
First occurrence of 5 is at index 2 and last occurrence of 5 is at index 5. +
+

Example 2:

+
Input:
+n=9, x=7
+arr[] = { 1, 3, 5, 5, 5, 5, 7, 123, 125 }
+Output:  
6 6
Explanation:
First and last occurrence of 7 is at index 6. +
+

Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function find() that takes array arr, integer n and integer x as parameters and returns the required answer.

+

Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1).

+

Constraints:
1 ≤ N ≤ 106
1 ≤ arr[i],x ≤ 109

\ No newline at end of file From d5aaa88ae1e9de74231f0d63ad0518335131406b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 19:41:32 +0530 Subject: [PATCH 1755/3167] Added solution - LeetHub --- .../first-and-last-occurrences-of-x.cpp | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 First and last occurrences of x - GFG/first-and-last-occurrences-of-x.cpp diff --git a/First and last occurrences of x - GFG/first-and-last-occurrences-of-x.cpp b/First and last occurrences of x - GFG/first-and-last-occurrences-of-x.cpp new file mode 100644 index 00000000..e85b429f --- /dev/null +++ b/First and last occurrences of x - GFG/first-and-last-occurrences-of-x.cpp @@ -0,0 +1,54 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution +{ + public: + vector find(int arr[], int n , int x ) + { + // code here + + int idx1 = lower_bound(arr,arr+n, x) - arr; + int idx2 = lower_bound(arr, arr+n,x+1) - arr; + + bool ok = false, ok2 = false; + + if(idx1 >= 0 and idx1 < n and arr[idx1] == x) + ok = true; + if(idx2-1 >= 0 and idx2-1 < n and arr[idx2-1] == x) + ok2 = true; + + // cout<>t; + while(t--) + { + int n,x; + cin>>n>>x; + int arr[n],i; + for(i=0;i>arr[i]; + vector ans; + Solution ob; + ans=ob.find(arr,n,x); + cout< Date: Fri, 22 Sep 2023 22:46:50 +0530 Subject: [PATCH 1756/3167] Create README - LeetHub --- 0392-is-subsequence/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0392-is-subsequence/README.md diff --git a/0392-is-subsequence/README.md b/0392-is-subsequence/README.md new file mode 100644 index 00000000..56e9b407 --- /dev/null +++ b/0392-is-subsequence/README.md @@ -0,0 +1,23 @@ +

392. Is Subsequence

Easy


Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

+ +

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

+ +

 

+

Example 1:

+
Input: s = "abc", t = "ahbgdc"
+Output: true
+

Example 2:

+
Input: s = "axc", t = "ahbgdc"
+Output: false
+
+

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 100
  • +
  • 0 <= t.length <= 104
  • +
  • s and t consist only of lowercase English letters.
  • +
+ +

 

+Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
\ No newline at end of file From 067c118e5cc0f7dc6fc260d50c04d100ea84b9b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:46:51 +0530 Subject: [PATCH 1757/3167] Attach NOTES - LeetHub --- 0392-is-subsequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0392-is-subsequence/NOTES.md diff --git a/0392-is-subsequence/NOTES.md b/0392-is-subsequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0392-is-subsequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2c8ff70f184d54eec99f4d19a0292370fb511046 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:46:54 +0530 Subject: [PATCH 1758/3167] Time: 1 ms (89.69%), Space: 40.4 MB (52.53%) - LeetHub --- 0392-is-subsequence/0392-is-subsequence.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0392-is-subsequence/0392-is-subsequence.java diff --git a/0392-is-subsequence/0392-is-subsequence.java b/0392-is-subsequence/0392-is-subsequence.java new file mode 100644 index 00000000..e6742030 --- /dev/null +++ b/0392-is-subsequence/0392-is-subsequence.java @@ -0,0 +1,15 @@ +class Solution { + public boolean isSubsequence(String s, String t) { + + int k = 0; + + for(int i = 0; i < t.length() && k < s.length(); ++i) + { + if(s.charAt(k) == t.charAt(i)) + ++k; + } + + return (k == s.length()); + + } +} \ No newline at end of file From 0154cbfd6875358e87315d077a960ade2acdaabf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:46:59 +0530 Subject: [PATCH 1759/3167] Attach NOTES - LeetHub From 71517e901f327c79467c7a4c77937c76bb457f3d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:47:03 +0530 Subject: [PATCH 1760/3167] Attach NOTES - LeetHub From d712b7013f750ea667abfd85cbb4e8530749eacd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:47:06 +0530 Subject: [PATCH 1761/3167] Time: 1 ms (89.69%), Space: 40.4 MB (52.53%) - LeetHub From a573e222fe61eb80d51e7ab3df30f804f35d3313 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 08:07:47 +0530 Subject: [PATCH 1762/3167] Added solution - LeetHub --- .../equilibrium-point.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Equilibrium Point - GFG/equilibrium-point.java diff --git a/Equilibrium Point - GFG/equilibrium-point.java b/Equilibrium Point - GFG/equilibrium-point.java new file mode 100644 index 00000000..54489661 --- /dev/null +++ b/Equilibrium Point - GFG/equilibrium-point.java @@ -0,0 +1,63 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; +import java.util.stream.*; + +class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = + new BufferedReader(new InputStreamReader(System.in)); + int t = + Integer.parseInt(br.readLine().trim()); // Inputting the testcases + while (t-- > 0) { + + //taking input n + int n = Integer.parseInt(br.readLine().trim()); + long arr[] = new long[n]; + String inputLine[] = br.readLine().trim().split(" "); + + //adding elements to the array + for (int i = 0; i < n; i++) { + arr[i] = Long.parseLong(inputLine[i]); + } + + Solution ob = new Solution(); + + //calling equilibriumPoint() function + System.out.println(ob.equilibriumPoint(arr, n)); + } + } +} +// } Driver Code Ends + + +class Solution { + + + // a: input array + // n: size of array + // Function to find equilibrium point in the array. + public static int equilibriumPoint(long arr[], int n) { + + // Your code here + + long[] pref = new long[n]; + + pref[0] = arr[0]; + + for(int i = 1; i < n; ++i) + pref[i] = arr[i] + pref[i-1]; + + for(int i = 0; i < n; ++i) + { + long left = (i == 0 ? 0 : pref[i-1]); + long right = (i == n-1) ? 0 : pref[n-1] - pref[i]; + + if(left == right) + return i+1; + } + + return -1; + } +} From 4a577a1e42de310b28c85db2d142cb610f445127 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 08:15:41 +0530 Subject: [PATCH 1763/3167] Create README - LeetHub --- Equilibrium Point - GFG/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Equilibrium Point - GFG/README.md diff --git a/Equilibrium Point - GFG/README.md b/Equilibrium Point - GFG/README.md new file mode 100644 index 00000000..78a09c4b --- /dev/null +++ b/Equilibrium Point - GFG/README.md @@ -0,0 +1,22 @@ +# Equilibrium Point +## Easy +

Given an array A of n positive numbers. The task is to find the first equilibrium point in an array. Equilibrium point in an array is a position such that the sum of elements before it is equal to the sum of elements after it.

+

Note: Return equilibrium point in 1-based indexing. Return -1 if no such point exists. 

+

Example 1:

+
Input: 
+n = 5 
+A[] = {1,3,5,2,2} 
+Output: 
3 +Explanation: +equilibrium point is at position 3 as sum of elements before it (1+3) = sum of elements after it (2+2).
+
+

Example 2:

+
Input:
+n = 1
+A[] = {1}
+Output: 
1 +Explanation: +Since there's only element hence its only the equilibrium point.
+

Your Task:
The task is to complete the function equilibriumPoint() which takes the array and n as input parameters and returns the point of equilibrium. 

+

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= n <= 105
1 <= A[i] <= 109

\ No newline at end of file From 120a4d17f1b0d4670231dc200a21cafb422cf178 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 08:15:41 +0530 Subject: [PATCH 1764/3167] Added solution - LeetHub --- Equilibrium Point - GFG/equilibrium-point.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Equilibrium Point - GFG/equilibrium-point.java b/Equilibrium Point - GFG/equilibrium-point.java index 54489661..749dd797 100644 --- a/Equilibrium Point - GFG/equilibrium-point.java +++ b/Equilibrium Point - GFG/equilibrium-point.java @@ -42,17 +42,21 @@ public static int equilibriumPoint(long arr[], int n) { // Your code here - long[] pref = new long[n]; + int totalSum = 0 ; - pref[0] = arr[0]; - - for(int i = 1; i < n; ++i) - pref[i] = arr[i] + pref[i-1]; + for(int i = 0; i < n; ++i) + totalSum += arr[i]; + + int currSum = 0 ; for(int i = 0; i < n; ++i) { - long left = (i == 0 ? 0 : pref[i-1]); - long right = (i == n-1) ? 0 : pref[n-1] - pref[i]; + currSum += arr[i]; + + long left = (i == 0 ? 0 : currSum - arr[i]); + long right = (i == n-1 ? 0 : totalSum - currSum); + + // System.out.println(left + " " + right); if(left == right) return i+1; From 58ff2675418de100788a418d51845644f1e5060b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 21:24:16 +0530 Subject: [PATCH 1765/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/README.md 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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/README.md new file mode 100644 index 00000000..5270bb1c --- /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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/README.md @@ -0,0 +1,32 @@ +

Amazon
2
Flipkart
2
646. Maximum Length of Pair Chain

Medium


You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.

+ +

A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.

+ +

Return the length longest chain which can be formed.

+ +

You do not need to use up all the given intervals. You can select pairs in any order.

+ +

 

+

Example 1:

+ +
Input: pairs = [[1,2],[2,3],[3,4]]
+Output: 2
+Explanation: The longest chain is [1,2] -> [3,4].
+
+ +

Example 2:

+ +
Input: pairs = [[1,2],[7,8],[4,5]]
+Output: 3
+Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
+
+ +

 

+

Constraints:

+ +
    +
  • n == pairs.length
  • +
  • 1 <= n <= 1000
  • +
  • -1000 <= lefti < righti <= 1000
  • +
+
\ No newline at end of file From 0921aa1b7c54eea2d56d59cb7b22c5a1341aba7c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 21:24:20 +0530 Subject: [PATCH 1766/3167] Time: 57 ms (56.04%), Space: 22.8 MB (54.82%) - LeetHub --- ...iv-div646-maximum-length-of-pair-chain.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain.cpp 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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain.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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain.cpp new file mode 100644 index 00000000..0eacc27d --- /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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain/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-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divflipkart-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-div646-maximum-length-of-pair-chain.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int findLongestChain(vector>& pairs) { + + int n = pairs.size(); + + sort(pairs.begin(), pairs.end(),[&](const auto& a, const auto& b){ + return a[1] < b[1]; + }); + + int count = 1; + + int curr = pairs[0][1]; + + for(int i = 1; i < n; ++i) + { + if(pairs[i][0] > curr) + { + curr = pairs[i][1]; + ++count; + } + } + + return count; + } +}; \ No newline at end of file From 6a3fbddcde8ea1338966e3b12ad70e22fbee275d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 22:53:41 +0530 Subject: [PATCH 1767/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1048-longest-string-chain/README.md 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-div1048-longest-string-chain/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-div1048-longest-string-chain/README.md new file mode 100644 index 00000000..318adb0c --- /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-div1048-longest-string-chain/README.md @@ -0,0 +1,44 @@ +

No companies found for this problem
1048. Longest String Chain

Medium


You are given an array of words where each word consists of lowercase English letters.

+ +

wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.

+ +
    +
  • For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".
  • +
+ +

A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2, word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1.

+ +

Return the length of the longest possible word chain with words chosen from the given list of words.

+ +

 

+

Example 1:

+ +
Input: words = ["a","b","ba","bca","bda","bdca"]
+Output: 4
+Explanation: One of the longest word chains is ["a","ba","bda","bdca"].
+
+ +

Example 2:

+ +
Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
+Output: 5
+Explanation: All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
+
+ +

Example 3:

+ +
Input: words = ["abcd","dbqca"]
+Output: 1
+Explanation: The trivial word chain ["abcd"] is one of the longest word chains.
+["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length <= 16
  • +
  • words[i] only consists of lowercase English letters.
  • +
+
\ No newline at end of file From e3343be040876281b3b45ba482d0e20acf7fd968 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Sep 2023 22:53:46 +0530 Subject: [PATCH 1768/3167] Time: 192 ms (6.09%), Space: 55.5 MB (5.31%) - LeetHub --- ...-div-div-div1048-longest-string-chain.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1048-longest-string-chain/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1048-longest-string-chain.java 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-div1048-longest-string-chain/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1048-longest-string-chain.java 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-div1048-longest-string-chain/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1048-longest-string-chain.java new file mode 100644 index 00000000..cab2a85c --- /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-div1048-longest-string-chain/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1048-longest-string-chain.java @@ -0,0 +1,56 @@ +class Solution { + + int[][]dp = new int[1001][1001]; + + boolean isPredecessor(String s, String t) + { + int n = s.length(); + int m = t.length(); + + if(n >= m || m - n != 1) + return false; + + int k = 0; + + for(int i = 0; i < m && k < n; ++i) + { + if(t.charAt(i) == s.charAt(k)) + ++k; + } + + return k == n; + } + + int helper(int idx, int prevIdx, int n, String[] words) + { + if(idx == n) + return 0; + + if(dp[idx][prevIdx+1] != -1) + return dp[idx][prevIdx+1]; + + int notTake = helper(idx+1, prevIdx, n, words); + + int take = 0; + + if(prevIdx == -1 || isPredecessor(words[prevIdx], words[idx])) + { + take = 1 + helper(idx+1, idx, n, words); + } + + return dp[idx][prevIdx + 1] = Math.max(take, notTake); + } + + public int longestStrChain(String[] words) { + + int n = words.length; + + Arrays.sort(words, (String a, String b) -> a.length() - b.length()); + + for(int[] row : dp) + Arrays.fill(row,-1); + + return helper(0, -1, n, words); + + } +} \ No newline at end of file From 71952115f621d5b16417aee516ed79ab35e12191 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:26:23 +0530 Subject: [PATCH 1769/3167] Create README - LeetHub --- Find duplicates in an array - GFG/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Find duplicates in an array - GFG/README.md 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 From 864fcf09f23547120fe3c258fcc0a74fcd8ee521 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:26:24 +0530 Subject: [PATCH 1770/3167] Added solution - LeetHub --- .../find-duplicates-in-an-array.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Find duplicates in an array - GFG/find-duplicates-in-an-array.java 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; + } +} From 758e030c8f530be00cd1461fa58f80c4ae36059b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 23:06:58 +0530 Subject: [PATCH 1771/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 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 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 From 22e3416af8826d92f429b422f3d9407b57d4717a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 23:07:01 +0530 Subject: [PATCH 1772/3167] Time: 5 ms (70.70%), Space: 6.4 MB (76.54%) - LeetHub --- ...svg-div-div-div-div799-champagne-tower.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 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 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 From efd7708b9d21f3196d0d93f70d9fd5eceb5f361a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:08:27 +0530 Subject: [PATCH 1773/3167] Create README - LeetHub --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 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 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 From fbbb555a3b8022329a8fa03f0c8470bb53973470 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:08:31 +0530 Subject: [PATCH 1774/3167] Time: 6 ms (19.69%), Space: 7.1 MB (8.68%) - LeetHub --- ...div-div-div-div389-find-the-difference.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 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 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 From 8b18c64b5e33a165c40c470ca388c2f5107980e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:12:56 +0530 Subject: [PATCH 1775/3167] Create README - LeetHub From bd067b27a79386c71ca3da47b454ad663557eba7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:12:57 +0530 Subject: [PATCH 1776/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 0eae5eac3e1b24c342efcac1acb2755a35172e69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:13:31 +0530 Subject: [PATCH 1777/3167] Create README - LeetHub --- 0389-find-the-difference/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0389-find-the-difference/README.md 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 From 8519ee115d6071ef0d53687d6008d396d50ba926 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:13:31 +0530 Subject: [PATCH 1778/3167] Attach NOTES - LeetHub --- 0389-find-the-difference/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0389-find-the-difference/NOTES.md 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 From cd4e7dcd0d4a42671f829b5a1c5d754344f566f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:13:35 +0530 Subject: [PATCH 1779/3167] Time: 3 ms (45.31%), Space: 41.1 MB (20.11%) - LeetHub --- .../0389-find-the-difference.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0389-find-the-difference/0389-find-the-difference.java 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..ba8c7d44 --- /dev/null +++ b/0389-find-the-difference/0389-find-the-difference.java @@ -0,0 +1,19 @@ +class Solution { + public char findTheDifference(String s, String t) { + + char[] sChar = s.toCharArray(); + char[] tChar = t.toCharArray(); + + Arrays.sort(sChar); + Arrays.sort(tChar); + + for(int i = 0; i < sChar.length; ++i) + { + if(sChar[i] != tChar[i]) + return tChar[i]; + } + + return tChar[tChar.length-1]; + + } +} \ No newline at end of file From 449986e1ddf73d22195533c03e914a8a8b61647f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:15:01 +0530 Subject: [PATCH 1780/3167] Attach NOTES - LeetHub From 3614e7209b2a452f8e899128c12183774cdff766 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:15:04 +0530 Subject: [PATCH 1781/3167] Time: 3 ms (45.31%), Space: 41.1 MB (20.11%) - LeetHub From 5d8f314d5d7879d3df7a8df28bf3fed07acf0a17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:07 +0530 Subject: [PATCH 1782/3167] Attach NOTES - LeetHub From c4ce72bc3321a609d7265df172214a2ea4bd3311 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:13 +0530 Subject: [PATCH 1783/3167] Attach NOTES - LeetHub From d8ee75617d9eaf6f9af4ff50087ce70476e1f8a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:17 +0530 Subject: [PATCH 1784/3167] Time: 3 ms (45.31%), Space: 41.1 MB (20.11%) - LeetHub From baa076d7433c971a9148fd82d09f3a6199164a35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:50 +0530 Subject: [PATCH 1785/3167] Attach NOTES - LeetHub From c2b00bea7ee3f11ed4aaa96efadb864cb2410d26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:54 +0530 Subject: [PATCH 1786/3167] Time: 1 ms (100.00%), Space: 40.9 MB (42.11%) - LeetHub --- .../0389-find-the-difference.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/0389-find-the-difference/0389-find-the-difference.java b/0389-find-the-difference/0389-find-the-difference.java index ba8c7d44..e0564259 100644 --- a/0389-find-the-difference/0389-find-the-difference.java +++ b/0389-find-the-difference/0389-find-the-difference.java @@ -1,19 +1,14 @@ class Solution { public char findTheDifference(String s, String t) { - char[] sChar = s.toCharArray(); - char[] tChar = t.toCharArray(); + char ch = 0; - Arrays.sort(sChar); - Arrays.sort(tChar); + for(char c : s.toCharArray()) + ch ^= c; - for(int i = 0; i < sChar.length; ++i) - { - if(sChar[i] != tChar[i]) - return tChar[i]; - } - - return tChar[tChar.length-1]; + for(char c : t.toCharArray()) + ch ^= c; + return ch; } } \ No newline at end of file From a3a943328e5ca9a8d5372150506dd4c18489c6df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:17:14 +0530 Subject: [PATCH 1787/3167] Attach NOTES - LeetHub From 4cfa9db1553ead8a1f2e3ddbc1b600626c8d6e77 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:17:17 +0530 Subject: [PATCH 1788/3167] Time: 1 ms (100.00%), Space: 40.9 MB (42.11%) - LeetHub From 8cd595373c7b53aa1bc5f2dfcbed119de29997e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:17:25 +0530 Subject: [PATCH 1789/3167] Attach NOTES - LeetHub From 94d13215035d564ac3b56386d0ad5982f3cf0b61 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:53:41 +0530 Subject: [PATCH 1790/3167] Create README - LeetHub --- Maximum Sum Combination - GFG/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Maximum Sum Combination - GFG/README.md diff --git a/Maximum Sum Combination - GFG/README.md b/Maximum Sum Combination - GFG/README.md new file mode 100644 index 00000000..05474e85 --- /dev/null +++ b/Maximum Sum Combination - GFG/README.md @@ -0,0 +1,11 @@ +# Maximum Sum Combination +## Medium +

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 From 0bc7561231d62ce0fa6a1cfa0f69b7624dc20517 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:53:42 +0530 Subject: [PATCH 1791/3167] Added solution - LeetHub --- .../maximum-sum-combination.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Maximum Sum Combination - GFG/maximum-sum-combination.cpp 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 From 14f00c41cfcb4e66ec302919b5e9fc7c9b758cba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:13:39 +0530 Subject: [PATCH 1792/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 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 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 From 7bc3d1324e0c70779fc8f7d7dda9e98f04af64ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:35:11 +0530 Subject: [PATCH 1793/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 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 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 From d7796a15e0392fa8c77f5e347870254af01b23e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:35:15 +0530 Subject: [PATCH 1794/3167] Time: 0 ms (100.00%), Space: 7 MB (17.68%) - LeetHub --- ...iv-div-div316-remove-duplicate-letters.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 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 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 From 67a12a6a55997f99fa8750e11aa5e8675fb2ceb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:44:46 +0530 Subject: [PATCH 1795/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From b9173143f0468b7b4efdb2523d442eeb556f4c6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:44:51 +0530 Subject: [PATCH 1796/3167] Time: 0 ms (100.00%), Space: 6.6 MB (28.42%) - LeetHub --- ...est-subsequence-of-distinct-characters.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From f3a960f54ac4ac8d464e22eb17bb477bc76e54a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:45:02 +0530 Subject: [PATCH 1797/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1081-smallest-subsequence-of-distinct-characters/README.md 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 From 71196480b14969080ab2d5e38ecfda1cd65fe966 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:45:03 +0530 Subject: [PATCH 1798/3167] Attach NOTES - LeetHub --- 1081-smallest-subsequence-of-distinct-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1081-smallest-subsequence-of-distinct-characters/NOTES.md 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 From b3b9d4fe9c9061f3fd684e9f538e60c6c8d7ffec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:45:06 +0530 Subject: [PATCH 1799/3167] Time: 0 ms (100.00%), Space: 6.6 MB (28.42%) - LeetHub --- ...est-subsequence-of-distinct-characters.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp 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 From 2d2b17da8401ed391d938ef8e900a8b4bca2ca09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 22:41:04 +0530 Subject: [PATCH 1800/3167] Create README - LeetHub --- 0880-decoded-string-at-index/README.md | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0880-decoded-string-at-index/README.md 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 From eaed5b1f5b320ccbe3d73fcdb0aabe206ef9b363 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 22:41:07 +0530 Subject: [PATCH 1801/3167] Time: 2 ms (45.12%), Space: 6.5 MB (7.16%) - LeetHub --- .../0880-decoded-string-at-index.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0880-decoded-string-at-index/0880-decoded-string-at-index.cpp 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 From 4132ea9eaf7c842eb1fb219f11d15e64fe6557c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Sep 2023 07:57:33 +0530 Subject: [PATCH 1802/3167] Create README - LeetHub --- Wave Array - GFG/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Wave Array - GFG/README.md diff --git a/Wave Array - GFG/README.md b/Wave Array - GFG/README.md new file mode 100644 index 00000000..dc773438 --- /dev/null +++ b/Wave Array - GFG/README.md @@ -0,0 +1,24 @@ +# Wave Array +## Easy +

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 From c2a253fb0d21de4b68466d4972eb044f9ff3adfc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Sep 2023 07:57:33 +0530 Subject: [PATCH 1803/3167] Added solution - LeetHub --- Wave Array - GFG/wave-array.cpp | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Wave Array - GFG/wave-array.cpp 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 Date: Thu, 28 Sep 2023 08:26:47 +0530 Subject: [PATCH 1804/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 6ba15dee5e178e42c3a18848a3ecc9145e917de3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Sep 2023 08:26:50 +0530 Subject: [PATCH 1805/3167] Time: 14 ms (13.55%), Space: 16.6 MB (18.22%) - LeetHub --- ...iv-div-div-div905-sort-array-by-parity.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 19d81384446e35799d1a6c55381a554ac5191f91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:17:26 +0530 Subject: [PATCH 1806/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 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 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 From feaf03ecc319647717ac364ee8305a296ee55f2f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:17:30 +0530 Subject: [PATCH 1807/3167] Time: 129 ms (35.19%), Space: 96.8 MB (22.05%) - LeetHub --- ...t-svg-div-div-div-div896-monotonic-array.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 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 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 From 717547cb59cb35587d7fb130a3cbdca387cbcd85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:41:18 +0530 Subject: [PATCH 1808/3167] Create README - LeetHub --- Number Of Enclaves - GFG/README.md | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Number Of Enclaves - GFG/README.md diff --git a/Number Of Enclaves - GFG/README.md b/Number Of Enclaves - GFG/README.md new file mode 100644 index 00000000..37711e95 --- /dev/null +++ b/Number Of Enclaves - GFG/README.md @@ -0,0 +1,45 @@ +# Number Of Enclaves +## Medium +

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 From ed12b7095ef9c58ea7807a7f1eeaf662a640c0ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:41:19 +0530 Subject: [PATCH 1809/3167] Added solution - LeetHub --- .../number-of-enclaves.cpp | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Number Of Enclaves - GFG/number-of-enclaves.cpp 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 From f226916b09703f6a5a0292dd4dcbac4218cafd75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:14:38 +0530 Subject: [PATCH 1810/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 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 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 From a236469e76504ef15db5e85dcca6b6960d87839b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:14:41 +0530 Subject: [PATCH 1811/3167] Time: 68 ms (58.88%), Space: 49.7 MB (22.77%) - LeetHub --- ...ect-svg-div-div-div-div456-132-pattern.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 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 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 From 1f34488e1adb7989eec688f8a73b5bb61d6cfcd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:18:32 +0530 Subject: [PATCH 1812/3167] Create README - LeetHub --- 0456-132-pattern/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0456-132-pattern/README.md 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 From 2495ac029029ed5c1bca5773ec506e54d9e86435 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:18:32 +0530 Subject: [PATCH 1813/3167] Attach NOTES - LeetHub --- 0456-132-pattern/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0456-132-pattern/NOTES.md 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 From 8cbf6b0af886473e992dde6352ab21d6ae60ab16 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:18:35 +0530 Subject: [PATCH 1814/3167] Time: 68 ms (58.88%), Space: 49.7 MB (22.77%) - LeetHub --- 0456-132-pattern/0456-132-pattern.cpp | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0456-132-pattern/0456-132-pattern.cpp diff --git a/0456-132-pattern/0456-132-pattern.cpp b/0456-132-pattern/0456-132-pattern.cpp new file mode 100644 index 00000000..ead08dec --- /dev/null +++ b/0456-132-pattern/0456-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 From 58be5760861e604002c6c5bed293578bf1694052 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 19:33:31 +0530 Subject: [PATCH 1815/3167] Create README - LeetHub --- Boolean Matrix - GFG/README.md | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Boolean Matrix - GFG/README.md 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 From bcc7153d7f77dc02444e1050f0e577bbb2d98e52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 19:33:32 +0530 Subject: [PATCH 1816/3167] Added solution - LeetHub --- Boolean Matrix - GFG/boolean-matrix.cpp | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Boolean Matrix - GFG/boolean-matrix.cpp 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< Date: Sat, 30 Sep 2023 23:47:46 +0530 Subject: [PATCH 1817/3167] Attach NOTES - LeetHub From a1d4cf6e0af96b9255ac9567e56a6203068a33da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:47:50 +0530 Subject: [PATCH 1818/3167] Time: 58 ms (92.94%), Space: 48.9 MB (37.36%) - LeetHub --- 0456-132-pattern/0456-132-pattern.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/0456-132-pattern/0456-132-pattern.cpp b/0456-132-pattern/0456-132-pattern.cpp index ead08dec..a1973605 100644 --- a/0456-132-pattern/0456-132-pattern.cpp +++ b/0456-132-pattern/0456-132-pattern.cpp @@ -4,25 +4,22 @@ class Solution { 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]); + int s3 = INT_MIN; stack st; for(int i = n-1; i >= 0; --i) { - if(nums[i] > prefMin[i]) + if(nums[i] < s3) + return true; + + while(!st.empty() and st.top() < nums[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]); + s3 = st.top(); + st.pop(); } + + st.push(nums[i]); } return false; From eebd7750cc515225ac03a6de2bab483daba97efa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:06:30 +0530 Subject: [PATCH 1819/3167] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 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 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 From 6ec9a92c55d9da58b055715cf671f9fdc69ce0a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:06:34 +0530 Subject: [PATCH 1820/3167] Time: 18 ms (35.38%), Space: 14.5 MB (16.76%) - LeetHub --- ...v-div557-reverse-words-in-a-string-iii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 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 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 From f5542df0e87bd6c8837e9bd1add0eee36320fcff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:07:26 +0530 Subject: [PATCH 1821/3167] Create README - LeetHub --- 0557-reverse-words-in-a-string-iii/README.md | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0557-reverse-words-in-a-string-iii/README.md 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 From e9286719274ba5620a495688d453b4bd3053c7cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:07:27 +0530 Subject: [PATCH 1822/3167] Attach NOTES - LeetHub --- 0557-reverse-words-in-a-string-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0557-reverse-words-in-a-string-iii/NOTES.md 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 From d2b268ba089b341e384ab406fc5d1fb32c33d28e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:07:30 +0530 Subject: [PATCH 1823/3167] Time: 18 ms (35.38%), Space: 14.5 MB (16.76%) - LeetHub --- .../0557-reverse-words-in-a-string-iii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp 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 From 3a9248693d4cb5637b0e2df4c86bed0ed98322fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:09:00 +0530 Subject: [PATCH 1824/3167] Attach NOTES - LeetHub From ae6149fa06835e3618af466d693ee7d7d9da995b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:09:03 +0530 Subject: [PATCH 1825/3167] Time: 18 ms (35.38%), Space: 14.5 MB (16.76%) - LeetHub From b0fdffba3df0e041b46d4a68d27e64ed42a73d21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:41:38 +0530 Subject: [PATCH 1826/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From dd2770383668752fb2a8ebc29adee067ed144a38 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:41:38 +0530 Subject: [PATCH 1827/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 6dc1651687d999df16a848991ba4cf78f9960b43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:41:42 +0530 Subject: [PATCH 1828/3167] Time: 247 ms (40.00%), Space: 161 MB (100.00%) - LeetHub --- ...ount-visited-nodes-in-a-directed-graph.cpp | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From ef1556c76c50ca55488d966830e3a93c28211963 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 19:44:24 +0530 Subject: [PATCH 1829/3167] Create README - LeetHub --- Boundary traversal of matrix - GFG/README.md | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Boundary traversal of matrix - GFG/README.md diff --git a/Boundary traversal of matrix - GFG/README.md b/Boundary traversal of matrix - GFG/README.md new file mode 100644 index 00000000..99be67a8 --- /dev/null +++ b/Boundary traversal of matrix - GFG/README.md @@ -0,0 +1,28 @@ +# Boundary traversal of matrix +## Easy +

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 From 5a5636a3fcea04da914ecbd8e2788047b65561d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 19:44:24 +0530 Subject: [PATCH 1830/3167] Added solution - LeetHub --- .../boundary-traversal-of-matrix.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp 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< Date: Mon, 2 Oct 2023 08:05:23 +0530 Subject: [PATCH 1831/3167] Create README - LeetHub --- .../README.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 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 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 From 9c0e359903e8be6f4d4213a0f156aad46c5c4dd6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:05:27 +0530 Subject: [PATCH 1832/3167] Time: 39 ms (15.83%), Space: 13.6 MB (17.42%) - LeetHub --- ...s-if-both-neighbors-are-the-same-color.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 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 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 From 90b989cd0a8b03b647543ce52fb3f8dbc6954649 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:06:59 +0530 Subject: [PATCH 1833/3167] Create README - LeetHub --- .../README.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md 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 From 4ad50643200379dbdc377fab035766730a65629e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:06:59 +0530 Subject: [PATCH 1834/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md 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 From 19ed49b13ca8153426a2b32165876e9e5c5a4f9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:07:03 +0530 Subject: [PATCH 1835/3167] Time: 39 ms (15.83%), Space: 13.6 MB (17.42%) - LeetHub --- ...s-if-both-neighbors-are-the-same-color.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp 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 From 6cdffa5d8cead0aeff60f23f3b28dd2ec79550da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:14:37 +0530 Subject: [PATCH 1836/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 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 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 From 757b953937b898881a41579c2b48565e63bbe590 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:14:37 +0530 Subject: [PATCH 1837/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 935c154ed91a2ed6b301cc15365cb962e49824d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:14:42 +0530 Subject: [PATCH 1838/3167] Time: 0 ms (100.00%), Space: 7.6 MB (7.10%) - LeetHub --- ...v-div-div-div1512-number-of-good-pairs.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 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 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 From 44e3acd049ce5243dfb722f85779b008257014de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:16:28 +0530 Subject: [PATCH 1839/3167] Create README - LeetHub --- 1512-number-of-good-pairs/README.md | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1512-number-of-good-pairs/README.md 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 From d6b58c65aff173bc58a8f42a0ef87962eed7ad23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:16:28 +0530 Subject: [PATCH 1840/3167] Attach NOTES - LeetHub --- 1512-number-of-good-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1512-number-of-good-pairs/NOTES.md 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 From b1a86746c478ab72665258339532714e758f4e04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:16:32 +0530 Subject: [PATCH 1841/3167] Time: 0 ms (100.00%), Space: 7.6 MB (7.10%) - LeetHub --- .../1512-number-of-good-pairs.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1512-number-of-good-pairs/1512-number-of-good-pairs.cpp 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 From bc023cf8d5b7214d1e19664a52416c2388be4958 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:17:04 +0530 Subject: [PATCH 1842/3167] Attach NOTES - LeetHub From f9ca3c989978c562ec73f4f44f5b03f2a31728db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:17:08 +0530 Subject: [PATCH 1843/3167] Time: 0 ms (100.00%), Space: 7.6 MB (7.10%) - LeetHub From 23f2aa5d1125f851aa9e622e7b61e790b44ff5cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:19:19 +0530 Subject: [PATCH 1844/3167] Attach NOTES - LeetHub From 8f9f75cb022ca50dfb85b99123e72ff4ecef2382 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:58:25 +0530 Subject: [PATCH 1845/3167] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Column name from a given column number - GFG/README.md 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 From aa523568799229fe9c7ea03376fbe27a122fa7ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:58:26 +0530 Subject: [PATCH 1846/3167] Added solution - LeetHub --- ...column-name-from-a-given-column-number.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp 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 From bb6cd9273c9cc167c7ef74fe76c8890c81bb2381 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:21:04 +0530 Subject: [PATCH 1847/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 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 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 From e8a5854b4a446a90d61b8d09b0c4743a795125fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:21:04 +0530 Subject: [PATCH 1848/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 2edaa2067206ad85e4c1559cf2e6d8d12d3adccb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:21:08 +0530 Subject: [PATCH 1849/3167] Time: 106 ms (70.74%), Space: 208.4 MB (14.05%) - LeetHub --- ...-svg-div-div-div-div706-design-hashmap.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 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 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 From acc613b6f63bd28ee2178c822717d0c2d4fd3f95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:23:11 +0530 Subject: [PATCH 1850/3167] Create README - LeetHub --- 0706-design-hashmap/README.md | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0706-design-hashmap/README.md 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 From 93d2306f8c0410b942ca0d08a63e29d3f0233619 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:58:15 +0530 Subject: [PATCH 1851/3167] Create README - LeetHub From 1046326d1004ba228d2c3a7d19ad67ecb24820cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:58:19 +0530 Subject: [PATCH 1852/3167] Time: 105 ms (72.93%), Space: 62.2 MB (48.49%) - LeetHub --- 0706-design-hashmap/0706-design-hashmap.cpp | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 0706-design-hashmap/0706-design-hashmap.cpp 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 From f5487e16a2c2b47a756d93e7feda8b035f9b6dcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:42:23 +0530 Subject: [PATCH 1853/3167] Attach NOTES - LeetHub From 2491298c653e764c8398c963c99fd8415ddcae79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:42:26 +0530 Subject: [PATCH 1854/3167] Time: 91 ms (36.72%), Space: 50.6 MB (31.53%) - LeetHub --- 0705-design-hashset/0705-design-hashset.cpp | 43 ++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) 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; } }; From c2b724eaf314286b98ef1baed52a54130a5933af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Oct 2023 07:25:12 +0530 Subject: [PATCH 1855/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 669b993117aac15423c15b1c503907a3fb63604b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Oct 2023 07:25:15 +0530 Subject: [PATCH 1856/3167] Time: 7 ms (87.90%), Space: 16.3 MB (18.73%) - LeetHub --- ...div-div-div-div229-majority-element-ii.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 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 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 From 740b8e897b20c46696dee8ee1910bced6f7bab5a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:09:00 +0530 Subject: [PATCH 1857/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 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 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 From ab2873180b2fe1fe3e5cc202cb00efb157a4daba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:09:00 +0530 Subject: [PATCH 1858/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 852e7cda1708c8733eb8f5fdf3243e8dace60d13 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:09:03 +0530 Subject: [PATCH 1859/3167] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub --- ...t-svg-div-div-div-div343-integer-break.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 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 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..75c189d1 --- /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,30 @@ +class Solution { +public: + + int ans; + + void helper(int idx, int prod, int n, int k) + { + if(n == 0 and k >= 2) + { + ans = max(ans, prod); + return; + } + + if(n-idx >= 0) + { + helper(idx, prod*idx, n-idx, k+1); + helper(idx+1, prod, n, k); + } + + } + + int integerBreak(int n) { + + ans = 1; + + helper(1, 1, n, 0); + + return ans; + } +}; \ No newline at end of file From 35659d29e546ea995c29474133070734bdc22b06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:11:34 +0530 Subject: [PATCH 1860/3167] Create README - LeetHub --- 0343-integer-break/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0343-integer-break/README.md 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 From 902b7e9fba6a6f7b008721e8989a16b303629d3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:11:38 +0530 Subject: [PATCH 1861/3167] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub --- 0343-integer-break/0343-integer-break.cpp | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0343-integer-break/0343-integer-break.cpp diff --git a/0343-integer-break/0343-integer-break.cpp b/0343-integer-break/0343-integer-break.cpp new file mode 100644 index 00000000..75c189d1 --- /dev/null +++ b/0343-integer-break/0343-integer-break.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + int ans; + + void helper(int idx, int prod, int n, int k) + { + if(n == 0 and k >= 2) + { + ans = max(ans, prod); + return; + } + + if(n-idx >= 0) + { + helper(idx, prod*idx, n-idx, k+1); + helper(idx+1, prod, n, k); + } + + } + + int integerBreak(int n) { + + ans = 1; + + helper(1, 1, n, 0); + + return ans; + } +}; \ No newline at end of file From f15a0b8349aefc9a0030ee06a807dc9740b0f17f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:12:14 +0530 Subject: [PATCH 1862/3167] Attach NOTES - LeetHub --- 0343-integer-break/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0343-integer-break/NOTES.md 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 From e5488947d260f73becc17d7bb61ea4f0df581401 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:12:18 +0530 Subject: [PATCH 1863/3167] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub From b321a6f4266441eab73757ab5f8cbcc63a1f02f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:16:56 +0530 Subject: [PATCH 1864/3167] Attach NOTES - LeetHub From 36c2d824b46b16196e07c2a8bfc79e6d371ee270 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:16:59 +0530 Subject: [PATCH 1865/3167] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub From fe12a856e97a6f18587d0cbd9e5a05a1fdff0e27 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:39:46 +0530 Subject: [PATCH 1866/3167] Attach NOTES - LeetHub From d8b38fcc15af727ce66f743b4682909f748028fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:39:50 +0530 Subject: [PATCH 1867/3167] Time: 1487 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub --- ...t-svg-div-div-div-div343-integer-break.cpp | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) 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 index 75c189d1..7a82da2d 100644 --- 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 @@ -1,30 +1,24 @@ class Solution { public: - int ans; - - void helper(int idx, int prod, int n, int k) + int helper(int idx, int sum, int n) { - if(n == 0 and k >= 2) - { - ans = max(ans, prod); - return; - } + if(sum == 0) + return 1; + + if(sum < 0 or idx >= n) + return 0; + + int res = helper(idx+1, sum, n); + + res = max(res, idx * helper(idx, sum-idx, n)); - if(n-idx >= 0) - { - helper(idx, prod*idx, n-idx, k+1); - helper(idx+1, prod, n, k); - } - + return res; } int integerBreak(int n) { - - ans = 1; - helper(1, 1, n, 0); + return helper(1, n, n); - return ans; } }; \ No newline at end of file From cd67c6ca3d4b64339446a2db8cb4a655411da2dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:42:40 +0530 Subject: [PATCH 1868/3167] Attach NOTES - LeetHub From 7b808845d704f00c23cdf302fc3efd715aef144a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:42:44 +0530 Subject: [PATCH 1869/3167] Time: 0 ms (100.00%), Space: 7.2 MB (6.83%) - LeetHub --- 0343-integer-break/0343-integer-break.cpp | 37 +++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/0343-integer-break/0343-integer-break.cpp b/0343-integer-break/0343-integer-break.cpp index 75c189d1..23c42f94 100644 --- a/0343-integer-break/0343-integer-break.cpp +++ b/0343-integer-break/0343-integer-break.cpp @@ -1,30 +1,29 @@ class Solution { public: - int ans; - - void helper(int idx, int prod, int n, int k) + int helper(int idx, int sum, int n, vector>& dp) { - if(n == 0 and k >= 2) - { - ans = max(ans, prod); - return; - } - - if(n-idx >= 0) - { - helper(idx, prod*idx, n-idx, k+1); - helper(idx+1, prod, n, k); - } - + 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) { - - ans = 1; - helper(1, 1, n, 0); + vector> dp(n+1, vector(n+1, -1)); + + return helper(1, n, n, dp); - return ans; } }; \ No newline at end of file From 72f0545d5f15d78a9ff66cac5eebcc3ae975789b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:44:48 +0530 Subject: [PATCH 1870/3167] Attach NOTES - LeetHub From bc3297636d7cddc36a7d9f11f32324b9440a7907 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:44:52 +0530 Subject: [PATCH 1871/3167] Time: 0 ms (100.00%), Space: 7.2 MB (6.83%) - LeetHub From d3e0d64526a8cf80dced8c3286bbbcc487ac3211 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:19:30 +0530 Subject: [PATCH 1872/3167] Attach NOTES - LeetHub From daff4e8ab7cc4dc16e84fd60c66d20383867886b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:19:34 +0530 Subject: [PATCH 1873/3167] Time: 0 ms (100.00%), Space: 6.5 MB (29.03%) - LeetHub --- ...t-svg-div-div-div-div343-integer-break.cpp | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) 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 index 7a82da2d..03f2b022 100644 --- 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 @@ -1,24 +1,29 @@ class Solution { public: - int helper(int idx, int sum, int n) + int helper(int n, vector& dp) { - if(sum == 0) - return 1; + if(n == 1) + return 1; - if(sum < 0 or idx >= n) - return 0; + if(dp[n] != -1) + return dp[n]; - int res = helper(idx+1, sum, n); + int res = 1; - res = max(res, idx * helper(idx, sum-idx, n)); + for(int i = 1; i <= n-1; ++i) + { + int prod = i * max(n - i, helper(n-i, dp)); + res = max(res, prod); + } - return res; + return dp[n] = res; } int integerBreak(int n) { - return helper(1, n, n); + vector dp(n+1, -1); + return helper(n, dp); } }; \ No newline at end of file From 89ce18450652c4f6c14387edf2ab9c13befc62b7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:03 +0530 Subject: [PATCH 1874/3167] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Reverse alternate nodes in Link List - GFG/README.md 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 From 983af858b8b32b43374950dc548418ce2415d0c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:03 +0530 Subject: [PATCH 1875/3167] Added solution - LeetHub --- .../reverse-alternate-nodes-in-link-list.cpp | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp 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 From f671fbf682d7d0f655075aff60edc68caabc2c2d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 07:29:32 +0530 Subject: [PATCH 1876/3167] Create README - LeetHub --- .../README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Pairwise swap elements of a linked list - GFG/README.md diff --git a/Pairwise swap elements of a linked list - GFG/README.md b/Pairwise swap elements of a linked list - GFG/README.md new file mode 100644 index 00000000..168956ee --- /dev/null +++ b/Pairwise swap elements of a linked list - GFG/README.md @@ -0,0 +1,17 @@ +# Pairwise swap elements of a linked list +## Easy +

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 From 100b6c164f27a38b24cdb0e3ed8a548829f12308 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 07:29:32 +0530 Subject: [PATCH 1877/3167] Added solution - LeetHub --- ...airwise-swap-elements-of-a-linked-list.cpp | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp 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 From 0d09819ff696d37964ae37cd54707abc358ac9b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:23:41 +0530 Subject: [PATCH 1878/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 6e4fc14321273a594bafceeabeacacd10f407f9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:23:41 +0530 Subject: [PATCH 1879/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 6a52e00bebbbd2648acc0082a33a215eae5042ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:23:45 +0530 Subject: [PATCH 1880/3167] Time: 0 ms (100.00%), Space: 7.6 MB (61.75%) - LeetHub --- ...find-the-maximum-exactly-k-comparisons.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From b5a881557e05eafdfa10b9873e0edd1cf89fc069 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:04:01 +0530 Subject: [PATCH 1881/3167] Create README - LeetHub --- Insert in a Sorted List - GFG/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Insert in a Sorted List - GFG/README.md diff --git a/Insert in a Sorted List - GFG/README.md b/Insert in a Sorted List - GFG/README.md new file mode 100644 index 00000000..61d4403b --- /dev/null +++ b/Insert in a Sorted List - GFG/README.md @@ -0,0 +1,16 @@ +# Insert in a Sorted List +## Easy +

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 From ec252aa14be46134844d6af67558998d046f76fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:04:02 +0530 Subject: [PATCH 1882/3167] Added solution - LeetHub --- .../insert-in-a-sorted-list.cpp | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp 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 From 454fd45e8cfc27032d21f272bdd86281bdacb432 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:53:17 +0530 Subject: [PATCH 1883/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 049fbfbf18f727de092160e38d81382dbc2ed348 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:53:21 +0530 Subject: [PATCH 1884/3167] Time: 45 ms (35.73%), Space: 13.7 MB (22.88%) - LeetHub --- ...58-max-dot-product-of-two-subsequences.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..4413d5c2 --- /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,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 From ed77b1f4f2222ca1e519c468952ec9bdac4aecd3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:58:01 +0530 Subject: [PATCH 1885/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1458-max-dot-product-of-two-subsequences/README.md 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 From 9c980dc047dfb99d3da91a4817d1d77bb169223b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:58:07 +0530 Subject: [PATCH 1886/3167] Time: 45 ms (35.73%), Space: 13.7 MB (22.88%) - LeetHub --- ...58-max-dot-product-of-two-subsequences.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp 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 From 9a51fa0d6d3afe20d62c2d64523e706d611490cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:06:10 +0530 Subject: [PATCH 1887/3167] Attach NOTES - LeetHub --- 1458-max-dot-product-of-two-subsequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1458-max-dot-product-of-two-subsequences/NOTES.md 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 From edf0d918f5c4c5a3dc2b98139830d063bb8e5a26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:06:14 +0530 Subject: [PATCH 1888/3167] Time: 45 ms (35.73%), Space: 13.7 MB (22.88%) - LeetHub From 5d849c7f88e7c2daf70a015630dbe072940bf30e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:09:18 +0530 Subject: [PATCH 1889/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 1cfe8c3ceafe208ea7286f1aaba977cccc689ac9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:09:22 +0530 Subject: [PATCH 1890/3167] Time: 36 ms (49.61%), Space: 13.7 MB (21.08%) - LeetHub --- ...58-max-dot-product-of-two-subsequences.cpp | 34 +++---------------- 1 file changed, 5 insertions(+), 29 deletions(-) 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 index 4413d5c2..145c11a2 100644 --- 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 @@ -4,38 +4,21 @@ class Solution { int helper(int i, int j, int n, int m, vector& nums1, vector& nums2, vector>& dp) { if(i == n or j == m) - return 0; + return -1e9; 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 take = max(nums1[i] * nums2[j], 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 notTake = max(helper(i+1, j, n, m, nums1, nums2, dp), helper(i, j+1, n, m, nums1, nums2, dp)); - int moveJ = helper(i, j+1, n, m, nums1, nums2, dp); - - return dp[i][j] = max({take, moveI, moveJ}); + return dp[i][j] = max(take, notTake); } - 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(); @@ -43,14 +26,7 @@ class Solution { 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; + return helper(0, 0, n, m, nums1, nums2, dp); } }; \ No newline at end of file From 705a6ac1eac5660ed15117ea6283bfcaadaed6d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:13:13 +0530 Subject: [PATCH 1891/3167] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 8c2c4ee705c9a36929badf2180144d9b0736e78d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:13:13 +0530 Subject: [PATCH 1892/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From a86eeeb0792c4c1ad4b4f8e89d1821ad70846bb0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:13:17 +0530 Subject: [PATCH 1893/3167] Time: 7 ms (51.15%), Space: 14 MB (14.74%) - LeetHub --- ...st-position-of-element-in-sorted-array.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From b5c17cc9a81b177311f26e0e38d6d3550efc416a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:14:19 +0530 Subject: [PATCH 1894/3167] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0034-find-first-and-last-position-of-element-in-sorted-array/README.md 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 From 5bb8aae5a2d246ab3cd911780e049188f3601a0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:14:23 +0530 Subject: [PATCH 1895/3167] Time: 7 ms (51.15%), Space: 14 MB (14.74%) - LeetHub --- ...st-position-of-element-in-sorted-array.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp 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 From cd3563e7de80c60837aaf02e99d152ca55f7ef07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:18:00 +0530 Subject: [PATCH 1896/3167] Create README - LeetHub --- Height of Binary Tree - GFG/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Height of Binary Tree - GFG/README.md 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 From 8fe26cee2010b463ce2fbbe29d09780771105b54 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:18:01 +0530 Subject: [PATCH 1897/3167] Added solution - LeetHub --- .../height-of-binary-tree.cpp | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Height of Binary Tree - GFG/height-of-binary-tree.cpp 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< Date: Tue, 10 Oct 2023 22:00:21 +0530 Subject: [PATCH 1898/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 22dfc04c305d1d0e83204c48f8b9bc4c41acedc7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 10 Oct 2023 22:00:25 +0530 Subject: [PATCH 1899/3167] Time: 151 ms (73.01%), Space: 69.9 MB (50.84%) - LeetHub --- ...of-operations-to-make-array-continuous.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 0b0ed88098f18e60a460ac6356298486ce81ff72 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 20:02:31 +0530 Subject: [PATCH 1900/3167] Create README - LeetHub --- Check for Balanced Tree - GFG/README.md | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Check for Balanced Tree - GFG/README.md diff --git a/Check for Balanced Tree - GFG/README.md b/Check for Balanced Tree - GFG/README.md new file mode 100644 index 00000000..470fdd32 --- /dev/null +++ b/Check for Balanced Tree - GFG/README.md @@ -0,0 +1,32 @@ +# Check for Balanced Tree +## Easy +

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 From e69a3068e0367c0ab4293a43d6ea9f7529527221 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 20:02:32 +0530 Subject: [PATCH 1901/3167] Added solution - LeetHub --- .../check-for-balanced-tree.cpp | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 Check for Balanced Tree - GFG/check-for-balanced-tree.cpp 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 From 096896245374ad8209b036e86e889bd74e57d0de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:40:47 +0530 Subject: [PATCH 1902/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From c1b2c9e7e776724c4389c040cb085cd6780fb817 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:40:50 +0530 Subject: [PATCH 1903/3167] Time: 247 ms (63.34%), Space: 82.5 MB (74.95%) - LeetHub --- ...-div2251-number-of-flowers-in-full-bloom.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From d7b573170668e15649ef21cc0a435b3dcf88ebf9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:41:13 +0530 Subject: [PATCH 1904/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2251-number-of-flowers-in-full-bloom/README.md 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 From 1dbf96abc7d910a7721b6981b834331937a1a890 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:41:13 +0530 Subject: [PATCH 1905/3167] Attach NOTES - LeetHub --- 2251-number-of-flowers-in-full-bloom/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2251-number-of-flowers-in-full-bloom/NOTES.md 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 From ef4a9cbe79a2282f231728e63e12aff0c02ee08a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:41:17 +0530 Subject: [PATCH 1906/3167] Time: 247 ms (63.34%), Space: 82.5 MB (74.95%) - LeetHub --- .../2251-number-of-flowers-in-full-bloom.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp 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 From c3b51b3745c54245a1d5333c009b175ab131fcd1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 12 Oct 2023 23:44:19 +0530 Subject: [PATCH 1907/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 3db385891bc7102a123edb4d2bfcf9bb17cc5fa2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 12 Oct 2023 23:44:23 +0530 Subject: [PATCH 1908/3167] Time: 3 ms (78.78%), Space: 7.4 MB (14.97%) - LeetHub --- ...div-div-div1095-find-in-mountain-array.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From af4becbefff558d88e96b5cc435e0fcfd7d20d06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:43:24 +0530 Subject: [PATCH 1909/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 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 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 From 1389626c1c596f00db6e9751376e44234e2f8f19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:43:27 +0530 Subject: [PATCH 1910/3167] Time: 8 ms (20.63%), Space: 14 MB (26.23%) - LeetHub --- ...iv-div-div746-min-cost-climbing-stairs.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 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 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..f2dbaf0d --- /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,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 From a08f20a5a2287336247238eb3cda1b995bd86ad1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:26 +0530 Subject: [PATCH 1911/3167] Create README - LeetHub --- 0746-min-cost-climbing-stairs/README.md | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0746-min-cost-climbing-stairs/README.md 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 From 48c4e833fdbdaa6e57c0ead3b04be74417a3b55c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:27 +0530 Subject: [PATCH 1912/3167] Attach NOTES - LeetHub --- 0746-min-cost-climbing-stairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0746-min-cost-climbing-stairs/NOTES.md 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 From 41a078bb63f002c7b26b8a5e44d3865be32436a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:30 +0530 Subject: [PATCH 1913/3167] Time: 8 ms (20.63%), Space: 14 MB (26.23%) - LeetHub --- .../0746-min-cost-climbing-stairs.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp 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 From d483527997a5dbe9e8ba05b91d4932ff1afbcda4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:53 +0530 Subject: [PATCH 1914/3167] Attach NOTES - LeetHub From 8ae85ddbc04f9489792af1a0f9bb2ed8df29bdc0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:57 +0530 Subject: [PATCH 1915/3167] Time: 8 ms (20.63%), Space: 14 MB (26.23%) - LeetHub From f0b5fb4e7794eef3684bf22d82de1c20dca211ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 21:02:03 +0530 Subject: [PATCH 1916/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 00a44c3f58c343ef6ea01efbd3de30e5930d5ebe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 21:02:06 +0530 Subject: [PATCH 1917/3167] Time: 4 ms (61.59%), Space: 14 MB (37.30%) - LeetHub --- ...iv-div-div746-min-cost-climbing-stairs.cpp | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) 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 index f2dbaf0d..383cab45 100644 --- 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 @@ -1,27 +1,18 @@ 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); + vector dp(n+1, 0); - return min(helper(0, n, cost, dp), helper(1, n, cost, dp)); + 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 From bed86c54ca8c338da8c07c0604fcb54730efe3fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 22:19:21 +0530 Subject: [PATCH 1918/3167] Create README - LeetHub --- Find Common Nodes in two BSTs - GFG/README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Find Common Nodes in two BSTs - GFG/README.md 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 From b8bfc2ace00229d9ac2f9f8031852e71492a18e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 22:19:22 +0530 Subject: [PATCH 1919/3167] Added solution - LeetHub --- .../find-common-nodes-in-two-bsts.cpp | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp 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 From 6df3c17ae511eca4937ea465a35ed73fdce63e26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:06:50 +0530 Subject: [PATCH 1920/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 0915cf8584b48e13396f5f6d65c0c3419fd0f061 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:06:50 +0530 Subject: [PATCH 1921/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 0e5993cfc82726703b82392c969e0231f5288077 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:07:36 +0530 Subject: [PATCH 1922/3167] Attach NOTES - LeetHub From b3648e3a63730998e99a25b10ec19e3858e8715d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:16:11 +0530 Subject: [PATCH 1923/3167] Create README - LeetHub --- Normal BST to Balanced BST - GFG/README.md | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Normal BST to Balanced BST - GFG/README.md diff --git a/Normal BST to Balanced BST - GFG/README.md b/Normal BST to Balanced BST - GFG/README.md new file mode 100644 index 00000000..4a51938e --- /dev/null +++ b/Normal BST to Balanced BST - GFG/README.md @@ -0,0 +1,30 @@ +# Normal BST to Balanced BST +## Easy +

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 From ec04f52ca1c8f4c879dbaed7d2af7a11bff87a35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:16:12 +0530 Subject: [PATCH 1924/3167] Added solution - LeetHub --- .../normal-bst-to-balanced-bst.cpp | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp 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< Date: Sun, 15 Oct 2023 21:13:29 +0530 Subject: [PATCH 1925/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 1078b5f27107a10f9a4217c730ad734a020fe8c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Oct 2023 21:13:33 +0530 Subject: [PATCH 1926/3167] Time: 29 ms (34.85%), Space: 13.8 MB (35.28%) - LeetHub --- ...tay-in-the-same-place-after-some-steps.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 999c0d95548d9921468936752474faf656fd6692 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:10:56 +0530 Subject: [PATCH 1927/3167] Create README - LeetHub --- 0119-pascals-triangle-ii/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0119-pascals-triangle-ii/README.md 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 From 10342f4eb37547cc0bbe5763a04752186cbedc3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:11:00 +0530 Subject: [PATCH 1928/3167] Time: 0 ms (100.00%), Space: 7.5 MB (7.53%) - LeetHub --- .../0119-pascals-triangle-ii.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp 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 From 3233ccc272ba8f3b6529ad57ece43c019284dafe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:12:43 +0530 Subject: [PATCH 1929/3167] Attach NOTES - LeetHub --- 0119-pascals-triangle-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0119-pascals-triangle-ii/NOTES.md 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 From 400d7ec3f70dc431e1e98eb94af474879a3a82ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:12:46 +0530 Subject: [PATCH 1930/3167] Time: 0 ms (100.00%), Space: 7.5 MB (7.53%) - LeetHub From b497f342dae20303fc74d64edb179647651220ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:20:08 +0530 Subject: [PATCH 1931/3167] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 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 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 From ac8b38ebe224babcc919fc4566e482d43fe1dbbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:20:12 +0530 Subject: [PATCH 1932/3167] Time: 0 ms (100.00%), Space: 7 MB (11.01%) - LeetHub --- ...svg-div-div-div-div119-pascals-triangle-ii.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 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 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; + } +}; + From d31a401ddef32c1495ad426c6a843454e68c1a37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:11:39 +0530 Subject: [PATCH 1933/3167] Create README - LeetHub --- Transitive closure of a Graph - GFG/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Transitive closure of a Graph - GFG/README.md diff --git a/Transitive closure of a Graph - GFG/README.md b/Transitive closure of a Graph - GFG/README.md new file mode 100644 index 00000000..b38a80ce --- /dev/null +++ b/Transitive closure of a Graph - GFG/README.md @@ -0,0 +1,26 @@ +# Transitive closure of a Graph +## Medium +

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 From 75f778f87e2b71db94c2b53488769290ef35be40 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:11:40 +0530 Subject: [PATCH 1934/3167] Added solution - LeetHub --- .../transitive-closure-of-a-graph.cpp | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp 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< Date: Tue, 17 Oct 2023 22:30:20 +0530 Subject: [PATCH 1935/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 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 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 From dd51f2c6654090d3956c0758e09f662cc366fa50 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:30:23 +0530 Subject: [PATCH 1936/3167] Time: 39 ms (59.88%), Space: 34.1 MB (51.67%) - LeetHub --- ...div-div1361-validate-binary-tree-nodes.cpp | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 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 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..12b70529 --- /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.unionBySize(i, leftChild[i]); + } + + if(rightChild[i] != -1) + { + if(dsu.findParent(i) == dsu.findParent(rightChild[i])) + return false; + else + dsu.unionBySize(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 From 5f491b9f748dfc6a7144fe6fb1e4ce1aa115d97c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:30:46 +0530 Subject: [PATCH 1937/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From cc1e4c4f42dfe565914dbd39de6de0562f77a15d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:30:50 +0530 Subject: [PATCH 1938/3167] Time: 35 ms (72.95%), Space: 34.2 MB (51.67%) - LeetHub --- ...ect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 12b70529..5549c5cf 100644 --- 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 @@ -90,7 +90,7 @@ class Solution { if(dsu.findParent(i) == dsu.findParent(leftChild[i])) return false; else - dsu.unionBySize(i, leftChild[i]); + dsu.unionByRank(i, leftChild[i]); } if(rightChild[i] != -1) @@ -98,7 +98,7 @@ class Solution { if(dsu.findParent(i) == dsu.findParent(rightChild[i])) return false; else - dsu.unionBySize(i, rightChild[i]); + dsu.unionByRank(i, rightChild[i]); } } From f2b7348dd2144adc93cca512e062a29adbd0e5d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:10:36 +0530 Subject: [PATCH 1939/3167] Create README - LeetHub --- Eventual Safe States - GFG/README.md | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Eventual Safe States - GFG/README.md 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 From 34ff6166ff07e36162ba3ae51d50d619ab093faa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:10:36 +0530 Subject: [PATCH 1940/3167] Added solution - LeetHub --- .../eventual-safe-states.cpp | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Eventual Safe States - GFG/eventual-safe-states.cpp 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 From cedbdaabb93fb233cd5abdab855585205cb8cd65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:49:25 +0530 Subject: [PATCH 1941/3167] Create README - LeetHub --- .../README.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 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 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 From d780ffe65c94dde68efd3a2a7583dd608759de27 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:49:28 +0530 Subject: [PATCH 1942/3167] Time: 311 ms (77.70%), Space: 132 MB (71.58%) - LeetHub --- ...v-div-div-div2050-parallel-courses-iii.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 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 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 From 5c20e121b396bba08d8de2aaa66120e4dcd935cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:34:12 +0530 Subject: [PATCH 1943/3167] Create README - LeetHub --- .../README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Form a number divisible by 3 using array digits - GFG/README.md 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 From e276b0ace0fad4bfe19c02a6f57a446db9fde4f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:34:13 +0530 Subject: [PATCH 1944/3167] Added solution - LeetHub --- ...mber-divisible-by-3-using-array-digits.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp 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 From 529504adbb6bddd4fe7ce73c2179a11c2d1aca79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:40:17 +0530 Subject: [PATCH 1945/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 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 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 From 28a514fbac2a05bc1f93a669f0ddc281eb1fea7f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:40:20 +0530 Subject: [PATCH 1946/3167] Time: 0 ms (100.00%), Space: 6.4 MB (70.46%) - LeetHub --- ...iv-div-div844-backspace-string-compare.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 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 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..0e7ac67b --- /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,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 From 1940beebfbc31e8fb98436fd79ad26965a77fb90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:49:04 +0530 Subject: [PATCH 1947/3167] Attach NOTES - LeetHub --- 0844-backspace-string-compare/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0844-backspace-string-compare/NOTES.md 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 From 80c521325d1c72f815cd3511fced8b15925e7537 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:49:07 +0530 Subject: [PATCH 1948/3167] Time: 0 ms (100.00%), Space: 6.4 MB (70.46%) - LeetHub --- .../0844-backspace-string-compare.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0844-backspace-string-compare/0844-backspace-string-compare.cpp 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 From 295694c6519929d5100d3eaad9da1506a2b5a169 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:00:32 +0530 Subject: [PATCH 1949/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 3ff4b06dd0244530c87cfdb1e17996b1958250a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:00:35 +0530 Subject: [PATCH 1950/3167] Time: 0 ms (100.00%), Space: 6.6 MB (40.98%) - LeetHub --- ...iv-div-div844-backspace-string-compare.cpp | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) 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 index 0e7ac67b..da6b4577 100644 --- 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 @@ -2,31 +2,42 @@ class Solution { public: bool backspaceCompare(string s, string t) { - string one, two; + int p(0), q(0); + + int n = s.size(), m = t.size(); - for(auto& itr : s) + for(int i = 0; i < n; ++i) { - if(itr == '#') + if(s[i] == '#') { - if(!one.empty()) - one.pop_back(); + p = max(0, --p); } else - one.push_back(itr); + { + s[p++] = s[i]; + } } - for(auto& itr : t) + for(int i = 0; i < m; ++i) { - if(itr == '#') + if(t[i] == '#') { - if(!two.empty()) - two.pop_back(); + q = max(0, --q); } else - two.push_back(itr); + { + t[q++] = t[i]; + } } - return one == two; + if(p != q) return false; + + for(int i = 0; i < p; ++i) + { + if(s[i] != t[i]) + return false; + } + return true; } }; \ No newline at end of file From cbba084f50f77d803c9fa9372b0b91d79631aa3f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:02:42 +0530 Subject: [PATCH 1951/3167] Attach NOTES - LeetHub From 40f3c85c0a773ebe44194f4f9ca51c7af312633f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:02:45 +0530 Subject: [PATCH 1952/3167] Time: 0 ms (100.00%), Space: 6.6 MB (40.98%) - LeetHub --- ...iv-div-div844-backspace-string-compare.cpp | 41 +++++++------------ 1 file changed, 14 insertions(+), 27 deletions(-) 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 index da6b4577..e4860bae 100644 --- 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 @@ -1,43 +1,30 @@ class Solution { public: - bool backspaceCompare(string s, string t) { - - int p(0), q(0); - - int n = s.size(), m = t.size(); + + string solve(string& str, int n) + { + int k = 0; for(int i = 0; i < n; ++i) { - if(s[i] == '#') - { - p = max(0, --p); - } - else - { - s[p++] = s[i]; - } - } - - for(int i = 0; i < m; ++i) - { - if(t[i] == '#') + if(str[i] == '#') { - q = max(0, --q); + k = max(0, --k); } else { - t[q++] = t[i]; + str[k++] = str[i]; } } - if(p != q) return false; + return str.substr(0, k); + } + + bool backspaceCompare(string s, string t) { + + int n = s.size(), m = t.size(); - for(int i = 0; i < p; ++i) - { - if(s[i] != t[i]) - return false; - } + return (solve(s, n) == solve(t, m)); - return true; } }; \ No newline at end of file From 18c6b024530928e5516dbf34f7d376ea01605e7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:46:10 +0530 Subject: [PATCH 1953/3167] Create README - LeetHub --- 0341-flatten-nested-list-iterator/README.md | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0341-flatten-nested-list-iterator/README.md 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 From 12e3696b8a428f0113967cca7485e58af01b04a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:46:14 +0530 Subject: [PATCH 1954/3167] Time: 12 ms (12.87%), Space: 15.9 MB (12.67%) - LeetHub --- .../0341-flatten-nested-list-iterator.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp 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..531e1fc2 --- /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 From fa1c48cbfaca529754ede863cbaea30725e574a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:47:40 +0530 Subject: [PATCH 1955/3167] Attach NOTES - LeetHub --- 0341-flatten-nested-list-iterator/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0341-flatten-nested-list-iterator/NOTES.md 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 From 55d78ee0acec4a3d2c6816c9a22cd390393eb77c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:47:43 +0530 Subject: [PATCH 1956/3167] Time: 11 ms (18.71%), Space: 13.6 MB (76.12%) - LeetHub --- .../0341-flatten-nested-list-iterator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 531e1fc2..f450d064 100644 --- a/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp +++ b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp @@ -22,7 +22,7 @@ class NestedIterator { vector v; int k = 0; - void helper(vector nestedList) + void helper(vector& nestedList) { for(auto& itr : nestedList) { From f4ffffd9ef3ab202d69b71b27e478ff02f808625 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:32:47 +0530 Subject: [PATCH 1957/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Sum of all divisors from 1 to n - GFG/README.md 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 From 3401e7d2d4fb18adbe7588e4f7fadcf59b6d567b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:32:47 +0530 Subject: [PATCH 1958/3167] Added solution - LeetHub --- .../sum-of-all-divisors-from-1-to-n.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp 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< Date: Sat, 21 Oct 2023 23:26:40 +0530 Subject: [PATCH 1959/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From c84fdc389e883ba3998e2f2d9c7a2d3274b1e38f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:26:43 +0530 Subject: [PATCH 1960/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub --- ...iv-div1425-constrained-subsequence-sum.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..0120df06 --- /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,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 From a94061ad79fe61ca1f5f58bfae325eaba443c864 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:37:40 +0530 Subject: [PATCH 1961/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 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 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 From 852771c2872d038aaab32437df2a266523448ebf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:37:40 +0530 Subject: [PATCH 1962/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 324f454ad23986ddf7ccfbcf30b77658431a34e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:38:50 +0530 Subject: [PATCH 1963/3167] Attach NOTES - LeetHub From c90f8f0abc7b95b55d478a6cfa0278f627feefdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:38:54 +0530 Subject: [PATCH 1964/3167] Time: 354 ms (15.43%), Space: 194.5 MB (9.70%) - LeetHub --- .../0239-sliding-window-maximum.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 From 80f12963cf7a0a080e7d8749508c81b00606415b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:47:20 +0530 Subject: [PATCH 1965/3167] Create README - LeetHub --- 1425-constrained-subsequence-sum/README.md | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1425-constrained-subsequence-sum/README.md 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 From 2640823f4bf71825e81d8b6a4cd8f18d2a34b6f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:47:22 +0530 Subject: [PATCH 1966/3167] Attach NOTES - LeetHub --- 1425-constrained-subsequence-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1425-constrained-subsequence-sum/NOTES.md 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 From 6121bf2ba59a72b718ccca89715d942fbea85930 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:47:24 +0530 Subject: [PATCH 1967/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub --- .../1425-constrained-subsequence-sum.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp 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 From 47e1f61e498d83f2a9bfdaeb9f5ceb96dce82bef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:53:12 +0530 Subject: [PATCH 1968/3167] Attach NOTES - LeetHub From e208c15eabb8bf783927b899957baa14d97d94de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:53:15 +0530 Subject: [PATCH 1969/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 5f4fe8848a607a4a8fba71e0a0a001291bf17ecb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:55:11 +0530 Subject: [PATCH 1970/3167] Attach NOTES - LeetHub From 2f3ace0d5a8b137e9ff945ac7b91a63590aa9b96 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:55:14 +0530 Subject: [PATCH 1971/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From e3faabc5f1396597e74e9f26d428475621db9a66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:57:15 +0530 Subject: [PATCH 1972/3167] Attach NOTES - LeetHub From 99dff23de65e4dbd1dba9e4b88243dc163a7a695 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:57:19 +0530 Subject: [PATCH 1973/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 30e12a622e97bafcb9d79370969350ec51ca2c7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:01:34 +0530 Subject: [PATCH 1974/3167] Attach NOTES - LeetHub From b0015c5feb69d15c30e542afb94abaedb2ac0572 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:01:37 +0530 Subject: [PATCH 1975/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 95071bab4261934d58d79d35becd335e4cbcc2cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:07 +0530 Subject: [PATCH 1976/3167] Attach NOTES - LeetHub From 86fb18678a1d0d296fe1f503ce7ccf93c1106cac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:11 +0530 Subject: [PATCH 1977/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 6192da5ed13e106dd4b1709725e42747ed0b282a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:31 +0530 Subject: [PATCH 1978/3167] Attach NOTES - LeetHub From ad5003a3d006c243bc54eda43153dda8cf104b85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:34 +0530 Subject: [PATCH 1979/3167] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From b193e540534fb06618f67f0890bd46dc19256912 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:08:32 +0530 Subject: [PATCH 1980/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 13045689ccc6a1871445ad646fdeee72bb13df3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:08:35 +0530 Subject: [PATCH 1981/3167] Time: 202 ms (84.49%), Space: 120.1 MB (75.92%) - LeetHub --- ...iv-div1425-constrained-subsequence-sum.cpp | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) 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 index 0120df06..2a6a2dc4 100644 --- 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 @@ -4,31 +4,28 @@ class Solution { int n = nums.size(); - vector dp(n+1); + vector dp(nums); + + deque dq; - 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}); + int ans = dp[0]; - for(int i = 1; i < n; ++i) + for(int i = 0; i < n; ++i) { - while(!pq.empty() and i - pq.top().second > k) - pq.pop(); + while(!dq.empty() and i - dq.front() > k) + dq.pop_front(); - dp[i] = max(dp[i], nums[i] + pq.top().first); + if(!dq.empty()) + dp[i] = max(dp[i], nums[i] + dp[dq.front()]); - ans = max(ans, dp[i]); + while(!dq.empty() and dp[i] >= dp[dq.back()]) + dq.pop_back(); + + dq.push_back(i); - pq.push({dp[i], i}); + ans = max(ans, dp[i]); } return ans; } }; - - \ No newline at end of file From f49367df82b99ea2e008f170ab155d4b4ddd27b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:41:42 +0530 Subject: [PATCH 1982/3167] Create README - LeetHub --- Number of paths - GFG/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Number of paths - GFG/README.md 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 From e4ba45d1b1756ee450c63daef879bb681b35b403 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:41:43 +0530 Subject: [PATCH 1983/3167] Added solution - LeetHub --- Number of paths - GFG/number-of-paths.cpp | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Number of paths - GFG/number-of-paths.cpp 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)< Date: Mon, 23 Oct 2023 00:44:53 +0530 Subject: [PATCH 1984/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From dd47d5eff1a14b20249b6f653000b0d105feeab3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:44:57 +0530 Subject: [PATCH 1985/3167] Time: 118 ms (93.20%), Space: 89.6 MB (80.92%) - LeetHub --- ...v1793-maximum-score-of-a-good-subarray.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From f8b58448ece833a45289ff66e895269040ef882d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:45:19 +0530 Subject: [PATCH 1986/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1793-maximum-score-of-a-good-subarray/README.md 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 From 94759f22b4245f9f208bca65574de31154ed4268 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:45:19 +0530 Subject: [PATCH 1987/3167] Attach NOTES - LeetHub --- 1793-maximum-score-of-a-good-subarray/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1793-maximum-score-of-a-good-subarray/NOTES.md 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 From 92617fd0ec0a82fd34ac3e172277d1599007e412 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:45:22 +0530 Subject: [PATCH 1988/3167] Time: 118 ms (93.20%), Space: 89.6 MB (80.92%) - LeetHub --- .../1793-maximum-score-of-a-good-subarray.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp 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 From 2889b1f1dc2c25130e8479b2733701ddaa928512 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:49:44 +0530 Subject: [PATCH 1989/3167] Create README - LeetHub --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 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 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 From 98b6df6cb219f9c7e2cc51aa3390c5ee12dd8cef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:49:44 +0530 Subject: [PATCH 1990/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 6a23356bac1a4aaab2d58f2dfbfb8682673bde73 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:49:48 +0530 Subject: [PATCH 1991/3167] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub --- ...t-svg-div-div-div-div342-power-of-four.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 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 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 From 7567a1ba7b8229cd7fd6a0b14d9d0c3277f6bafe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:01:09 +0530 Subject: [PATCH 1992/3167] Create README - LeetHub --- 0342-power-of-four/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0342-power-of-four/README.md 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 From a732bcf4a7041d97197f1f3967b786eae49ed9d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:01:12 +0530 Subject: [PATCH 1993/3167] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub --- 0342-power-of-four/0342-power-of-four.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0342-power-of-four/0342-power-of-four.cpp 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 From 6c36a2484352aabb82e6ae6e9992d16e22609061 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:04:14 +0530 Subject: [PATCH 1994/3167] Attach NOTES - LeetHub --- 0342-power-of-four/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0342-power-of-four/NOTES.md 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 From 80fcda9bc0e37bf796cf497df51656a2da702af6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:04:17 +0530 Subject: [PATCH 1995/3167] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub From e28bb156533a392d013340e933e016e86123c42f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:08:26 +0530 Subject: [PATCH 1996/3167] Attach NOTES - LeetHub From fe29e50d34dc872fb50426b81a3828c24583a922 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:08:30 +0530 Subject: [PATCH 1997/3167] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub From b7a4f16407786390f3d87ccb7d5fb176a6f3e814 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:09:10 +0530 Subject: [PATCH 1998/3167] Attach NOTES - LeetHub From 49f8ef379f16d7209a9b0bbac31c1b36db9f3787 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:09:14 +0530 Subject: [PATCH 1999/3167] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub From 8149b51f4e1fb419622df445449a6859b4ef5865 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:10:24 +0530 Subject: [PATCH 2000/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 7eca7e7f960830353d21a2d41b8cff18865eb173 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:10:28 +0530 Subject: [PATCH 2001/3167] Time: 4 ms (91.88%), Space: 22.8 MB (9.19%) - LeetHub --- ...15-find-largest-value-in-each-tree-row.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 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 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 From 0c519e70546e3d6a4c013f26dc2bd6ce3c52e90e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:14:39 +0530 Subject: [PATCH 2002/3167] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0515-find-largest-value-in-each-tree-row/README.md 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 From 18abad6a2a0fd7879f23029ead87762276efe1a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:14:39 +0530 Subject: [PATCH 2003/3167] Attach NOTES - LeetHub --- 0515-find-largest-value-in-each-tree-row/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0515-find-largest-value-in-each-tree-row/NOTES.md 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 From 296d0395c7126f6e5e4421b524362d6db53de2d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:14:43 +0530 Subject: [PATCH 2004/3167] Time: 4 ms (91.88%), Space: 22.8 MB (9.19%) - LeetHub --- ...15-find-largest-value-in-each-tree-row.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp 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 From 1f66f056fb215ebc79bd59f3d7cff0dbeae69a9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:37:24 +0530 Subject: [PATCH 2005/3167] Attach NOTES - LeetHub From 723106d646ae5bb6ab5fd751022ebaf581d179ee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:37:27 +0530 Subject: [PATCH 2006/3167] Time: 4 ms (91.88%), Space: 22.8 MB (9.19%) - LeetHub From acd7849509e3d40649ed7b7dcd7ec4b6451427ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:30:35 +0530 Subject: [PATCH 2007/3167] Create README - LeetHub --- Knapsack with Duplicate Items - GFG/README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Knapsack with Duplicate Items - GFG/README.md 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 From ad5d25f51479f1a270fe294dba1d76facd72e9f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:30:36 +0530 Subject: [PATCH 2008/3167] Added solution - LeetHub --- .../knapsack-with-duplicate-items.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp 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< Date: Wed, 25 Oct 2023 08:58:38 +0530 Subject: [PATCH 2009/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From ffaee1ea5ed7923d83e72ca0f90cdb189d61870d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:58:42 +0530 Subject: [PATCH 2010/3167] Time: 2 ms (51.37%), Space: 6.4 MB (28.86%) - LeetHub --- ...-div-div-div-div779-k-th-symbol-in-grammar.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 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 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 From 95f8df6f4fbdea6433fe0ffbf8ab8e913e0353da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 07:51:10 +0530 Subject: [PATCH 2011/3167] Create README - LeetHub --- Minimum Operations - GFG/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Minimum Operations - GFG/README.md 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 From 33009433154dcdb5df452f136833a236b8d870e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 07:51:11 +0530 Subject: [PATCH 2012/3167] Added solution - LeetHub --- .../minimum-operations.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Minimum Operations - GFG/minimum-operations.cpp 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< Date: Thu, 26 Oct 2023 20:54:29 +0530 Subject: [PATCH 2013/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 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 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 From 4030ef5a1d10f3cb1da69dab610ddcce0bfcc107 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:54:33 +0530 Subject: [PATCH 2014/3167] Time: 72 ms (25.98%), Space: 17.1 MB (11.81%) - LeetHub --- ...v-div-div823-binary-trees-with-factors.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 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 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 From 522ca015c2f27d327cd3e37ccd504cb86896fca9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:55:27 +0530 Subject: [PATCH 2015/3167] Create README - LeetHub --- 0823-binary-trees-with-factors/README.md | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0823-binary-trees-with-factors/README.md 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 From b859f608a3c23e93c0eb66bcf12631ab166e9f49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:55:27 +0530 Subject: [PATCH 2016/3167] Attach NOTES - LeetHub --- 0823-binary-trees-with-factors/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0823-binary-trees-with-factors/NOTES.md 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 From ddaad8c7ab9718bf219b829e30c8edb09eb74a65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:55:31 +0530 Subject: [PATCH 2017/3167] Time: 72 ms (25.98%), Space: 17.1 MB (11.81%) - LeetHub --- .../0823-binary-trees-with-factors.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp 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 From b775546062272a79a44785d0f4d6284e63b5ee77 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 07:45:53 +0530 Subject: [PATCH 2018/3167] Create README - LeetHub --- Minimum Deletions - GFG/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Minimum Deletions - GFG/README.md 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 From 261a824e651cd38bbb1034d7852f6cff091ac696 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 07:45:54 +0530 Subject: [PATCH 2019/3167] Added solution - LeetHub --- Minimum Deletions - GFG/minimum-deletions.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Minimum Deletions - GFG/minimum-deletions.cpp 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 From 5be25870a241a01d1c6d9362fd5097a4026e7847 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:17:25 +0530 Subject: [PATCH 2020/3167] Create README - LeetHub --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 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 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 From 88e1cc468a98b0d13d69f263bdb1b2f58df87c82 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:17:25 +0530 Subject: [PATCH 2021/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 02b6da1b26fc43dbeb2547e274231ef5c4c8913a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:17:29 +0530 Subject: [PATCH 2022/3167] Time: 326 ms (30.26%), Space: 302.9 MB (9.14%) - LeetHub --- ...div-div5-longest-palindromic-substring.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 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 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 From 4fdcec227b13cbf25be61be8b04639a13bb06f46 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:06 +0530 Subject: [PATCH 2023/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 8e8b6c559a06d1481d383267e044fe9f8f4cf358 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:09 +0530 Subject: [PATCH 2024/3167] Time: 111 ms (18.40%), Space: 91.3 MB (10.40%) - LeetHub --- ...v-div-div1220-count-vowels-permutation.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From af23a702789dc5b11ff6101a7d1413781bba1b42 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:49 +0530 Subject: [PATCH 2025/3167] Create README - LeetHub --- 1220-count-vowels-permutation/README.md | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1220-count-vowels-permutation/README.md 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 From a5622e37ccc6fda7c247c3fdaecff39b55389e1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:49 +0530 Subject: [PATCH 2026/3167] Attach NOTES - LeetHub --- 1220-count-vowels-permutation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1220-count-vowels-permutation/NOTES.md 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 From cc0a7d6238ac5fc4693d79edc5123a4ff107c9ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:53 +0530 Subject: [PATCH 2027/3167] Time: 111 ms (18.40%), Space: 91.3 MB (10.40%) - LeetHub --- .../1220-count-vowels-permutation.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 1220-count-vowels-permutation/1220-count-vowels-permutation.cpp 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 From 3fe5112cd7fc9dffa6588ec818e53100fa702442 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:57:07 +0530 Subject: [PATCH 2028/3167] Create README - LeetHub --- .../README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Check whether K-th bit is set or not - GFG/README.md 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 From 14ef3daa87260bcced057aa7b2cc5fb48b820153 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:57:08 +0530 Subject: [PATCH 2029/3167] Added solution - LeetHub --- .../check-whether-kth-bit-is-set-or-not.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp 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 From bd454914046a2aa5a28fc0b45acb0a52650c0cdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:12:50 +0530 Subject: [PATCH 2030/3167] Create README - LeetHub --- .../README.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 732b20bb183d46067d717b9a3c14acb922a32b29 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:12:50 +0530 Subject: [PATCH 2031/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 16e3e439c1815ac9b9df854a2c28eff15b08c13c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:12:54 +0530 Subject: [PATCH 2032/3167] Time: 117 ms (100.00%), Space: 97.4 MB (50.00%) - LeetHub --- ...ent-operations-to-make-array-beautiful.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From b14ba1439849f3b6768fbef299ce729d7889508c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:17:21 +0530 Subject: [PATCH 2033/3167] Create README - LeetHub --- .../README.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2919-minimum-increment-operations-to-make-array-beautiful/README.md 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 From bf3509c9489acaf30b08eaa2bab53f850c8b79ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:04:08 +0530 Subject: [PATCH 2034/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 7735275bea10eadc650f2268c626697eaf5150f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:04:09 +0530 Subject: [PATCH 2035/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 0f16aba7c3368dd53ea1d827a7d8e98049f69f76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:04:12 +0530 Subject: [PATCH 2036/3167] Time: 2 ms (52.97%), Space: 6.5 MB (26.69%) - LeetHub --- ...ht32-rect-svg-div-div-div-div458-poor-pigs.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..001c90e8 --- /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,15 @@ +class Solution { +public: + int poorPigs(int buckets, int minutesToDie, int minutesToTest) { + + int trial = (minutesToTest / minutesToDie) + 1; + + int pigs = 0; + + while(pow(trial, pigs) < buckets) + ++pigs; + + return pigs; + + } +}; \ No newline at end of file From ed91e30e3a206eaf8028c47e69450eda0e7c8cff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:09:12 +0530 Subject: [PATCH 2037/3167] Attach NOTES - LeetHub From 7b44b0ccd85ff56eb8e40a709147b957b6e35c97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:09:16 +0530 Subject: [PATCH 2038/3167] Time: 2 ms (52.97%), Space: 6.4 MB (58.05%) - LeetHub --- ...-height32-rect-svg-div-div-div-div458-poor-pigs.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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 index 001c90e8..c9a68d06 100644 --- 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 @@ -2,14 +2,8 @@ class Solution { public: int poorPigs(int buckets, int minutesToDie, int minutesToTest) { - int trial = (minutesToTest / minutesToDie) + 1; - - int pigs = 0; - - while(pow(trial, pigs) < buckets) - ++pigs; - - return pigs; + // trial^pigs = buckets + return ceil(log2(buckets) / log2(minutesToTest / minutesToDie + 1)); } }; \ No newline at end of file From 915a9bd8323bdd004537c4a46137474a47da7abf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 07:38:57 +0530 Subject: [PATCH 2039/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 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 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 From 56c713c60722b98593735accb0eb50448cc9c5b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 07:39:00 +0530 Subject: [PATCH 2040/3167] Time: 9 ms (43.13%), Space: 10.5 MB (66.29%) - LeetHub --- ...356-sort-integers-by-the-number-of-1-bits.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 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 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 From f85c8a12b29cb55981443891f92f82c537039dda Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:02:41 +0530 Subject: [PATCH 2041/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2433-find-the-original-array-of-prefix-xor/README.md diff --git a/2433-find-the-original-array-of-prefix-xor/README.md b/2433-find-the-original-array-of-prefix-xor/README.md new file mode 100644 index 00000000..380a3f99 --- /dev/null +++ b/2433-find-the-original-array-of-prefix-xor/README.md @@ -0,0 +1,38 @@ +

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 From 005f92f844126ac878e7ac3e950093c0ea0ec25d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:02:45 +0530 Subject: [PATCH 2042/3167] Time: 90 ms (41.60%), Space: 76.4 MB (69.22%) - LeetHub --- ...-find-the-original-array-of-prefix-xor.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp 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< Date: Tue, 31 Oct 2023 18:05:04 +0530 Subject: [PATCH 2043/3167] Attach NOTES - LeetHub --- 2433-find-the-original-array-of-prefix-xor/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2433-find-the-original-array-of-prefix-xor/NOTES.md diff --git a/2433-find-the-original-array-of-prefix-xor/NOTES.md b/2433-find-the-original-array-of-prefix-xor/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2433-find-the-original-array-of-prefix-xor/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d28d4be808bac4dd69ea69aae83123662bd0ab39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:05:08 +0530 Subject: [PATCH 2044/3167] Time: 90 ms (41.60%), Space: 76.4 MB (69.22%) - LeetHub From bb2dfaabfb99ef3d61a6b3e7355e0a5352cbb677 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:08:30 +0530 Subject: [PATCH 2045/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 3dbc23c4c40d1a9533ad2fbf0c41919cdb09fe21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:08:31 +0530 Subject: [PATCH 2046/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 2ed39c470ad5f6135b35b4560d6753a22b779b40 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:08:34 +0530 Subject: [PATCH 2047/3167] Time: 96 ms (18.61%), Space: 76.3 MB (69.22%) - LeetHub --- ...433-find-the-original-array-of-prefix-xor.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 9c966f5a7401d1dbb2fee4b1f3eef9f081c9841e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:10:23 +0530 Subject: [PATCH 2048/3167] Attach NOTES - LeetHub From e056ea9ba6e4b195619689db8b4702b7ddb2812e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:07:36 +0530 Subject: [PATCH 2049/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 7e7b9ab58a7b1886b48be5729af42654c14b775a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:07:40 +0530 Subject: [PATCH 2050/3167] Time: 8 ms (96.00%), Space: 25.6 MB (44.12%) - LeetHub --- ...div501-find-mode-in-binary-search-tree.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 03acbc1a85860502f984499f7b9e0b8889298593 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:13:31 +0530 Subject: [PATCH 2051/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0501-find-mode-in-binary-search-tree/README.md 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 From 9de76c53349f02eefecf5a6eacf15bc832500dd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:13:34 +0530 Subject: [PATCH 2052/3167] Time: 8 ms (96.00%), Space: 25.6 MB (44.12%) - LeetHub --- .../0501-find-mode-in-binary-search-tree.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp 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 From c7124572bca7e0b00b1251323aca1c38aea3f5a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:38:01 +0530 Subject: [PATCH 2053/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 033bb780ac7b19dbfd4a4ccf392d41eea74d5ca4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:38:05 +0530 Subject: [PATCH 2054/3167] Time: 0 ms (100.00%), Space: 12.2 MB (71.89%) - LeetHub --- ...ount-nodes-equal-to-average-of-subtree.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 650c4efcecf6deb3cda41014cc87d4d58319f2ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:41:27 +0530 Subject: [PATCH 2055/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2265-count-nodes-equal-to-average-of-subtree/README.md 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 From 5e6a7280080edb9562e521ad11cbc67459292c49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:41:31 +0530 Subject: [PATCH 2056/3167] Time: 0 ms (100.00%), Space: 12.2 MB (71.89%) - LeetHub --- ...ount-nodes-equal-to-average-of-subtree.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp 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 From 74bb140e5d04285514e269890e08ed90d207bd75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:26:04 +0530 Subject: [PATCH 2057/3167] Create README - LeetHub --- .../README.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 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 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 From 6c129c1e685f156f1bcab93b16ecec8c60bf0cf5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:26:08 +0530 Subject: [PATCH 2058/3167] Time: 0 ms (100.00%), Space: 8.4 MB (11.73%) - LeetHub --- ...1-build-an-array-with-stack-operations.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 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 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 From 0c74378e78668084ff8ed8b32084e3063966af0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:06 +0530 Subject: [PATCH 2059/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md 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 From 124a82e0ce9a4c73068e3790355ec9f6e9e50423 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:10 +0530 Subject: [PATCH 2060/3167] Time: 19 ms (48.28%), Space: 23.2 MB (31.47%) - LeetHub --- ...nt-before-all-ants-fall-out-of-a-plank.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp 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 From 9b4895b277ef3f7967e5e5b457852c7bd591d680 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:31 +0530 Subject: [PATCH 2061/3167] Attach NOTES - LeetHub --- 1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md 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 From 87721749d27d363f48e26fa322e906ecd90e4b33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:35 +0530 Subject: [PATCH 2062/3167] Time: 19 ms (48.28%), Space: 23.2 MB (31.47%) - LeetHub From 4f36e6dc36190c30b5ae99bc16a42a47c8750fd4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:42:21 +0530 Subject: [PATCH 2063/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 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 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 From 3403f6222c1fd310b8d35bdaef9078f5adf35f68 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:42:24 +0530 Subject: [PATCH 2064/3167] Time: 88 ms (56.48%), Space: 63.6 MB (29.63%) - LeetHub --- ...v1535-find-the-winner-of-an-array-game.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 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 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 From 53f81cea8e6a773e2f4fd3065721b8febb12e8e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:03:20 +0530 Subject: [PATCH 2065/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 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 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 From 9a3ec4546702c53863ff2bba046a4ba5a97b7b24 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:03:23 +0530 Subject: [PATCH 2066/3167] Time: 301 ms (66.43%), Space: 151.1 MB (28.55%) - LeetHub --- ...v-div-div1845-seat-reservation-manager.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 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 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..1931d631 --- /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,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 From 91eca447b459097a2aa1493e9eec4eaccf1e666b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:04:38 +0530 Subject: [PATCH 2067/3167] Create README - LeetHub --- 1845-seat-reservation-manager/README.md | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1845-seat-reservation-manager/README.md 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 From 2f09d7bd6b4017a9a7b30b8bf45518ca0f74eb83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:04:39 +0530 Subject: [PATCH 2068/3167] Attach NOTES - LeetHub --- 1845-seat-reservation-manager/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1845-seat-reservation-manager/NOTES.md 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 From 4b32a569387eeb1c51c12c0ab392abb3b96577eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:04:42 +0530 Subject: [PATCH 2069/3167] Time: 301 ms (66.43%), Space: 151.1 MB (28.55%) - LeetHub --- .../1845-seat-reservation-manager.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1845-seat-reservation-manager/1845-seat-reservation-manager.cpp 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 From 3b09b9f5e34d65547498019f6a101ff621b6152a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:11:53 +0530 Subject: [PATCH 2070/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 322747de3d2bcf0aaa2af30201065e8eccb8afcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:11:56 +0530 Subject: [PATCH 2071/3167] Time: 282 ms (89.24%), Space: 148.6 MB (30.56%) - LeetHub --- ...ct-svg-div-div-div-div1845-seat-reservation-manager.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) 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 index 1931d631..323ea613 100644 --- 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 @@ -2,13 +2,11 @@ class SeatManager { public: set st; - vector seat; int counter = 1; int N; SeatManager(int n) { - N = n; - seat.resize(n+1, 0); + N = n; } int reserve() { @@ -17,13 +15,11 @@ class SeatManager { { 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; @@ -34,7 +30,6 @@ class SeatManager { void unreserve(int seatNumber) { st.insert(seatNumber); - seat[seatNumber] = 0; } }; From 5076555bb7425ade6f1e8ef3bfbf602a5b7921af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:22:13 +0530 Subject: [PATCH 2072/3167] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 5051da412f03b983e73a957b164776ace56e7004 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:22:17 +0530 Subject: [PATCH 2073/3167] Time: 108 ms (65.60%), Space: 85.3 MB (49.33%) - LeetHub --- ...1-eliminate-maximum-number-of-monsters.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 8e2396d708d1079fa6829d2c0a850a4166194f5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:30:36 +0530 Subject: [PATCH 2074/3167] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1921-eliminate-maximum-number-of-monsters/README.md 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 From 9c7804266b3faecab6992e321e7f8ec6b5b86c95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:30:40 +0530 Subject: [PATCH 2075/3167] Time: 108 ms (65.60%), Space: 85.3 MB (49.33%) - LeetHub --- ...1-eliminate-maximum-number-of-monsters.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp 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 From f661d19620f0df29779f1c4768fe078b6d989efd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:51:38 +0530 Subject: [PATCH 2076/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 724c019949c5b3c38da2600206431c0cfedc10fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:51:38 +0530 Subject: [PATCH 2077/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 95667ddc97c170495adcc86a26827e4ed136f8bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:51:41 +0530 Subject: [PATCH 2078/3167] Time: 0 ms (100.00%), Space: 6.2 MB (10.74%) - LeetHub --- ...e-if-a-cell-is-reachable-at-a-given-time.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 47e9df963b165538816da564d82544c41ce0e6a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:53:00 +0530 Subject: [PATCH 2079/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md 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 From 524e8be405eedfcc91939413d0947a36f56fde81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:53:01 +0530 Subject: [PATCH 2080/3167] Attach NOTES - LeetHub --- 2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md 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 From 2f3d6a490bf5cf50b67cd716dce088ae66e5c047 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:53:04 +0530 Subject: [PATCH 2081/3167] Time: 0 ms (100.00%), Space: 6.2 MB (10.74%) - LeetHub --- ...e-if-a-cell-is-reachable-at-a-given-time.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp 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 From d4ad503ce816ff80edebbd33029abe4cd0a9de99 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:25:42 +0530 Subject: [PATCH 2082/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 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 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 From 2dec9bba804ae555d6dfe6c5b8ca406e290b67a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:25:45 +0530 Subject: [PATCH 2083/3167] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 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 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..3ac68cd1 --- /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,42 @@ +class Solution { +public: + int countHomogenous(string s) { + + int n = s.size(); + + const int mod = 1e9 + 7; + + long long cnt = 1; + + long long ans = 0; + + for(int i = 1; i < n; ++i) + { + if(s[i] == s[i-1]) + { + ++cnt; + } + + else + { + int curr = ((cnt * 1LL* (cnt+1))/2) % mod; + + ans = (ans + curr) % mod; + + cnt = 1; + } + } + + if(cnt) + { + long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; + + ans = (ans + curr) % mod; + + cnt = 1; + } + + return ans; + + } +}; \ No newline at end of file From cba40117b58a7cf01c4ca92bdfe66be342738d7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:27:11 +0530 Subject: [PATCH 2084/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1759-count-number-of-homogenous-substrings/README.md 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 From afe48ff9cf94f927630602d6becbde3e9915138e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:27:15 +0530 Subject: [PATCH 2085/3167] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp 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..3ac68cd1 --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + int countHomogenous(string s) { + + int n = s.size(); + + const int mod = 1e9 + 7; + + long long cnt = 1; + + long long ans = 0; + + for(int i = 1; i < n; ++i) + { + if(s[i] == s[i-1]) + { + ++cnt; + } + + else + { + int curr = ((cnt * 1LL* (cnt+1))/2) % mod; + + ans = (ans + curr) % mod; + + cnt = 1; + } + } + + if(cnt) + { + long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; + + ans = (ans + curr) % mod; + + cnt = 1; + } + + return ans; + + } +}; \ No newline at end of file From 2a09fa5b0be998b6c1703b8f8d46f6a10a853eb3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:27:41 +0530 Subject: [PATCH 2086/3167] Attach NOTES - LeetHub --- 1759-count-number-of-homogenous-substrings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1759-count-number-of-homogenous-substrings/NOTES.md 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 From 84a0b79c87cae84d72dbf693c6f64d87f15ba839 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:03 +0530 Subject: [PATCH 2087/3167] Attach NOTES - LeetHub From 5eb3673c653ed7a910bb33b763dbaf83a7d0af57 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:06 +0530 Subject: [PATCH 2088/3167] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub From b8ce966867e59e31f357f8f866583935ca41e6e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:34 +0530 Subject: [PATCH 2089/3167] Attach NOTES - LeetHub From 0ffb2adb6721b32c261655453e542cb158468963 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:37 +0530 Subject: [PATCH 2090/3167] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub From 039c7605598b1d5c432ec058bb0e55d2947cef9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:11 +0530 Subject: [PATCH 2091/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 42a2899eb86729482ccfdd45b80ccb6c7343b2ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:15 +0530 Subject: [PATCH 2092/3167] Time: 24 ms (71.96%), Space: 12.2 MB (22.39%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) 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 index 3ac68cd1..02b8bc5c 100644 --- 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 @@ -2,41 +2,20 @@ class Solution { public: int countHomogenous(string s) { - int n = s.size(); + int n = s.size(), cnt = 0, prev = 0, res = 0; const int mod = 1e9 + 7; - long long cnt = 1; - - long long ans = 0; - - for(int i = 1; i < n; ++i) + for(auto& itr : s) { - if(s[i] == s[i-1]) - { - ++cnt; - } + cnt = (itr == prev ? cnt+1 : 1); - else - { - int curr = ((cnt * 1LL* (cnt+1))/2) % mod; - - ans = (ans + curr) % mod; - - cnt = 1; - } - } - - if(cnt) - { - long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; - - ans = (ans + curr) % mod; - - cnt = 1; + prev = itr; + + res = (res + cnt) % mod; } - return ans; + return res; } }; \ No newline at end of file From a7d00a8e2b60f030ea89b6b0d6cdc70911dd2935 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:42 +0530 Subject: [PATCH 2093/3167] Attach NOTES - LeetHub From c8006cf4c0515b350546eecff01022780407369a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:45 +0530 Subject: [PATCH 2094/3167] Time: 24 ms (71.96%), Space: 12.2 MB (22.39%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) 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 index 3ac68cd1..02b8bc5c 100644 --- 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 @@ -2,41 +2,20 @@ class Solution { public: int countHomogenous(string s) { - int n = s.size(); + int n = s.size(), cnt = 0, prev = 0, res = 0; const int mod = 1e9 + 7; - long long cnt = 1; - - long long ans = 0; - - for(int i = 1; i < n; ++i) + for(auto& itr : s) { - if(s[i] == s[i-1]) - { - ++cnt; - } + cnt = (itr == prev ? cnt+1 : 1); - else - { - int curr = ((cnt * 1LL* (cnt+1))/2) % mod; - - ans = (ans + curr) % mod; - - cnt = 1; - } - } - - if(cnt) - { - long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; - - ans = (ans + curr) % mod; - - cnt = 1; + prev = itr; + + res = (res + cnt) % mod; } - return ans; + return res; } }; \ No newline at end of file From 102626fe64586c95cccf742289b872b41505983d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:30:08 +0530 Subject: [PATCH 2095/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 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/README.md 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/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-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/README.md new file mode 100644 index 00000000..dd43c1b0 --- /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/README.md @@ -0,0 +1,43 @@ +

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 From a468e9a9541463523a9ae1ce42b6e197155c001e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:30:12 +0530 Subject: [PATCH 2096/3167] Time: 478 ms (39.09%), Space: 192.1 MB (5.15%) - LeetHub --- ...-restore-the-array-from-adjacent-pairs.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 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 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< Date: Fri, 10 Nov 2023 08:32:38 +0530 Subject: [PATCH 2097/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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/NOTES.md 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/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-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/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-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/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ccbbeb6295949d781688a9dfa5df322c3f5393f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:34:44 +0530 Subject: [PATCH 2098/3167] Attach NOTES - LeetHub --- 1743-restore-the-array-from-adjacent-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1743-restore-the-array-from-adjacent-pairs/NOTES.md diff --git a/1743-restore-the-array-from-adjacent-pairs/NOTES.md b/1743-restore-the-array-from-adjacent-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1743-restore-the-array-from-adjacent-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 76fe8898fc5b76e20e9c09be9cd3319332a6339a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:35:42 +0530 Subject: [PATCH 2099/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1743-restore-the-array-from-adjacent-pairs/README.md diff --git a/1743-restore-the-array-from-adjacent-pairs/README.md b/1743-restore-the-array-from-adjacent-pairs/README.md new file mode 100644 index 00000000..0c83a514 --- /dev/null +++ b/1743-restore-the-array-from-adjacent-pairs/README.md @@ -0,0 +1,43 @@ +

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 From 3abd6cd1fff29a7f714bc24e412e17e337142588 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:35:42 +0530 Subject: [PATCH 2100/3167] Attach NOTES - LeetHub From d4c8f4a7fa17b5845f4720f16c75021fee223fd5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:35:46 +0530 Subject: [PATCH 2101/3167] Time: 450 ms (46.97%), Space: 192 MB (5.15%) - LeetHub --- ...-restore-the-array-from-adjacent-pairs.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp 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< Date: Sat, 11 Nov 2023 14:45:09 +0530 Subject: [PATCH 2102/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From e6d74a1d1e216e7095baf8cef81b7cd360541118 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:45:13 +0530 Subject: [PATCH 2103/3167] Time: 140 ms (94.12%), Space: 76.4 MB (73.39%) - LeetHub --- ...gn-graph-with-shortest-path-calculator.cpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 19a1e1098604729d4ed79189d712b10d04ccb89c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:46:31 +0530 Subject: [PATCH 2104/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2642-design-graph-with-shortest-path-calculator/README.md 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 From faee9ab74e0950c5faca6b54f4894bb5c15a4b33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:46:31 +0530 Subject: [PATCH 2105/3167] Attach NOTES - LeetHub --- 2642-design-graph-with-shortest-path-calculator/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2642-design-graph-with-shortest-path-calculator/NOTES.md 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 From 1f58940787f1bb6903745ecaae1a97b58f7410b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:46:35 +0530 Subject: [PATCH 2106/3167] Time: 140 ms (94.12%), Space: 76.4 MB (73.39%) - LeetHub --- ...gn-graph-with-shortest-path-calculator.cpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp 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 From a68e363aa8c1fda242838e5c41f4d7be84ff8e86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:47:01 +0530 Subject: [PATCH 2107/3167] Attach NOTES - LeetHub From f3238a28b57c15fbe21880bcf4d39493e57d246c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:47:04 +0530 Subject: [PATCH 2108/3167] Time: 140 ms (94.12%), Space: 76.4 MB (73.39%) - LeetHub From 5de984e2dbb0eccc1ffe7f01dac21c0a2f4cb89c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 01:51:25 +0530 Subject: [PATCH 2109/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From dd8fe08512826eef41eb82607e7f332897fce31f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 01:51:29 +0530 Subject: [PATCH 2110/3167] Time: 608 ms (38.08%), Space: 55.5 MB (89.95%) - LeetHub --- ...rect-svg-div-div-div-div815-bus-routes.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 6f04bc39348661435e7729af6505d8e2272ddc7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:47:17 +0530 Subject: [PATCH 2111/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 1ebb5fdb9490724f36ca50f19ca1c189dfc3d879 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:47:21 +0530 Subject: [PATCH 2112/3167] Time: 43 ms (47.30%), Space: 12.1 MB (85.13%) - LeetHub --- ...iv-div-div2785-sort-vowels-in-a-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 15ed7d6260612321887567f937ec41bd3f68e70d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:50:19 +0530 Subject: [PATCH 2113/3167] Create README - LeetHub --- 2785-sort-vowels-in-a-string/README.md | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2785-sort-vowels-in-a-string/README.md 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 From e5b97df88ffe0ba3c4a51caa03788bdfa2ae70aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:50:19 +0530 Subject: [PATCH 2114/3167] Attach NOTES - LeetHub --- 2785-sort-vowels-in-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2785-sort-vowels-in-a-string/NOTES.md 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 From c40c83c69ba9a812a83f1a47ea79236b91849c64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:22:05 +0530 Subject: [PATCH 2115/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1930-unique-length-3-palindromic-subsequences/README.md 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 From 08b1c8dff145f41ec721fde23225d85d25361231 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:22:06 +0530 Subject: [PATCH 2116/3167] Attach NOTES - LeetHub --- 1930-unique-length-3-palindromic-subsequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1930-unique-length-3-palindromic-subsequences/NOTES.md 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 From 39e810817684222d66c8d76c83d202d58c27b0fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:22:09 +0530 Subject: [PATCH 2117/3167] Time: 67 ms (93.61%), Space: 23.6 MB (20.09%) - LeetHub --- ...ique-length-3-palindromic-subsequences.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp 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 From 94eb90ef71fa109edf0fa21ef5a0eb0c0e08180d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:27:51 +0530 Subject: [PATCH 2118/3167] Attach NOTES - LeetHub From 194fc9ec853078faa0168ddeb999305a7ae45d23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:27:54 +0530 Subject: [PATCH 2119/3167] Time: 67 ms (93.61%), Space: 23.6 MB (20.09%) - LeetHub From 7f45de4b5091e41ed8058bc7d1cbd7e726ab58c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:32:05 +0530 Subject: [PATCH 2120/3167] Attach NOTES - LeetHub From b7aa11e953f3f7817bc13a5a8e9b6bac00782330 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:32:09 +0530 Subject: [PATCH 2121/3167] Time: 67 ms (93.61%), Space: 23.6 MB (20.09%) - LeetHub From f85b34a0569394c92bcf8e3e8fb745cd67333711 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:27:25 +0530 Subject: [PATCH 2122/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 8307c12dfbf7906ac949d4aff6780106b933cb4e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:27:29 +0530 Subject: [PATCH 2123/3167] Time: 68 ms (94.40%), Space: 51.5 MB (56.72%) - LeetHub --- ...ement-after-decreasing-and-rearranging.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 3f391853eb004ecc04e0b77ceca50382e121b3be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:37:30 +0530 Subject: [PATCH 2124/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 47ca52f3157c11171db16236b14e1f816cc2d6a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:37:33 +0530 Subject: [PATCH 2125/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub --- ...-div-div1980-find-unique-binary-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 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 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..e5ab733e --- /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,30 @@ +class Solution { +public: + + string ans; + + void helper(int n, string curr, unordered_set& st) + { + if(curr.size() == n) + { + if(!st.count(curr)) + ans = curr; + return; + } + curr += '0'; + helper(n, curr, st); + curr.pop_back(); + curr += '1'; + helper(n, curr, st); + } + + string findDifferentBinaryString(vector& nums) { + + unordered_set st(nums.begin(), nums.end()); + + helper(nums.size(), "", st); + + return ans; + + } +}; \ No newline at end of file From ed41da85bb9e43931ce63d8c117d63077eaa9699 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:45:24 +0530 Subject: [PATCH 2126/3167] Create README - LeetHub --- 1980-find-unique-binary-string/README.md | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1980-find-unique-binary-string/README.md 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 From 26d9ddd86a9db60f7aa6f1936fc05e9580faf8dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:45:27 +0530 Subject: [PATCH 2127/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub --- .../1980-find-unique-binary-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1980-find-unique-binary-string/1980-find-unique-binary-string.cpp 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..e5ab733e --- /dev/null +++ b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + string ans; + + void helper(int n, string curr, unordered_set& st) + { + if(curr.size() == n) + { + if(!st.count(curr)) + ans = curr; + return; + } + curr += '0'; + helper(n, curr, st); + curr.pop_back(); + curr += '1'; + helper(n, curr, st); + } + + string findDifferentBinaryString(vector& nums) { + + unordered_set st(nums.begin(), nums.end()); + + helper(nums.size(), "", st); + + return ans; + + } +}; \ No newline at end of file From 1fcee2662d915917d237d08dc8d22d6cef8186dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:46:11 +0530 Subject: [PATCH 2128/3167] Attach NOTES - LeetHub --- 1980-find-unique-binary-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1980-find-unique-binary-string/NOTES.md 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 From 28eeffa97120f3edaea4ecc0583399b9e7b09f94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:46:14 +0530 Subject: [PATCH 2129/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 6a961ff266bd3b8a569aea569d57a965f9b1f84a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:13 +0530 Subject: [PATCH 2130/3167] Attach NOTES - LeetHub From 923510768ddae96144e47af34d0f1a37a8117b75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:16 +0530 Subject: [PATCH 2131/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 5d600782f2a01e25d017ce482feff1f13e701c84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:32 +0530 Subject: [PATCH 2132/3167] Attach NOTES - LeetHub From c4fac30b9ec11ec207e454b4ed4aa954f680794d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:35 +0530 Subject: [PATCH 2133/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 56e1813e74bb8d29bffcffab9a4e8e66afec1264 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:49:46 +0530 Subject: [PATCH 2134/3167] Attach NOTES - LeetHub From 919ca1a1e6ef764e8c5a689701c2d256734cb760 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:49:49 +0530 Subject: [PATCH 2135/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 56f80536bc32fdf609bb838574b100e297d819e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:50:54 +0530 Subject: [PATCH 2136/3167] Attach NOTES - LeetHub From 127bbf43f4b9edb62e7726e4fff1ebfb5c4ed8f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:50:58 +0530 Subject: [PATCH 2137/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From ddc300f97b0849f9bc58c2360711a0eca1ecd898 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:00 +0530 Subject: [PATCH 2138/3167] Attach NOTES - LeetHub From 46b371f90ea6035ae39cfb44bbf3fe4bdd3d5e5c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:04 +0530 Subject: [PATCH 2139/3167] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 9d0589647c95d6dd2ce1773266ae77b2ff6183af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:46 +0530 Subject: [PATCH 2140/3167] Attach NOTES - LeetHub From d720897369c8a409475d1b29d417d9a229a2ff76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:50 +0530 Subject: [PATCH 2141/3167] Time: 6 ms (42.30%), Space: 10.4 MB (86.71%) - LeetHub --- ...-div-div1980-find-unique-binary-string.cpp | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) 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 index e5ab733e..776c6783 100644 --- 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 @@ -1,30 +1,14 @@ class Solution { public: - - string ans; - - void helper(int n, string curr, unordered_set& st) - { - if(curr.size() == n) - { - if(!st.count(curr)) - ans = curr; - return; - } - curr += '0'; - helper(n, curr, st); - curr.pop_back(); - curr += '1'; - helper(n, curr, st); - } - string findDifferentBinaryString(vector& nums) { - unordered_set st(nums.begin(), nums.end()); + int n = nums.size(); - helper(nums.size(), "", st); - - return ans; + 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 From 460566c33d95592783e98374760f5592b1c9f87d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:53:52 +0530 Subject: [PATCH 2142/3167] Attach NOTES - LeetHub From 464587185f29a1273adcbe37cb7237842d3063f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:53:56 +0530 Subject: [PATCH 2143/3167] Time: 6 ms (42.30%), Space: 10.4 MB (86.71%) - LeetHub --- .../1980-find-unique-binary-string.cpp | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) 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 index e5ab733e..776c6783 100644 --- a/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp +++ b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp @@ -1,30 +1,14 @@ class Solution { public: - - string ans; - - void helper(int n, string curr, unordered_set& st) - { - if(curr.size() == n) - { - if(!st.count(curr)) - ans = curr; - return; - } - curr += '0'; - helper(n, curr, st); - curr.pop_back(); - curr += '1'; - helper(n, curr, st); - } - string findDifferentBinaryString(vector& nums) { - unordered_set st(nums.begin(), nums.end()); + int n = nums.size(); - helper(nums.size(), "", st); - - return ans; + 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 From 6c4330be0bdced964e9e5294306f22dbde22eef5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:14:59 +0530 Subject: [PATCH 2144/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From e0d15f75e49e747a8da1b4431af4664b30bde7da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:04 +0530 Subject: [PATCH 2145/3167] Time: 198 ms (21.02%), Space: 96.6 MB (61.58%) - LeetHub --- ...877-minimize-maximum-pair-sum-in-array.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 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 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 From 3eab55a86f1c75315624d78c8fb9beb7bb80663b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:51 +0530 Subject: [PATCH 2146/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1877-minimize-maximum-pair-sum-in-array/README.md 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 From 3094fa728bf9f0bbe42f14c5b8f589ed7cf42dcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:51 +0530 Subject: [PATCH 2147/3167] Attach NOTES - LeetHub --- 1877-minimize-maximum-pair-sum-in-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1877-minimize-maximum-pair-sum-in-array/NOTES.md 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 From f218b5b9183903c511f244a022aa2dcf8520c884 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 21:40:01 +0530 Subject: [PATCH 2148/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 3e72d706065c65b8d72c917e1e26389c50c603c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 21:40:04 +0530 Subject: [PATCH 2149/3167] Time: 297 ms (5.15%), Space: 108 MB (5.28%) - LeetHub --- ...frequency-of-the-most-frequent-element.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 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 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 From 3468d5f78047f1d7541ce38ce6dc8b6bc61d17ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:18:34 +0530 Subject: [PATCH 2150/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From e6ff2911ca41d04d23793b121a9cf6243fb934f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:18:35 +0530 Subject: [PATCH 2151/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 95d5383217b26f854680d2e1fafe55101bd0d433 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:18:38 +0530 Subject: [PATCH 2152/3167] Time: 169 ms (60.76%), Space: 99.3 MB (85.20%) - LeetHub --- ...frequency-of-the-most-frequent-element.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..191717d7 --- /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,34 @@ +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]; + + while(windowSum - currSum > k) + { + currSum -= nums[i]; + windowSum -= nums[j]; + ++i; + } + + ans = max(ans, (j-i+1)); + + ++j; + } + return ans; + } +}; \ No newline at end of file From 71d7ddfc56eebb0e436701184518dc4e40868494 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:23:35 +0530 Subject: [PATCH 2153/3167] Attach NOTES - LeetHub From 9961fef62063877f4dddec855331ffc461079e91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:23:39 +0530 Subject: [PATCH 2154/3167] Time: 174 ms (44.80%), Space: 99.3 MB (43.97%) - LeetHub --- ...iv-div-div1838-frequency-of-the-most-frequent-element.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index 191717d7..39056bf9 100644 --- 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 @@ -18,11 +18,10 @@ class Solution { long long windowSum = cnt * 1LL * nums[j]; - while(windowSum - currSum > k) + if(windowSum - currSum > k) { - currSum -= nums[i]; windowSum -= nums[j]; - ++i; + currSum -= nums[i++]; } ans = max(ans, (j-i+1)); From 5240a0de260fdf3e94f005cae267e61ab36f751a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:51:15 +0530 Subject: [PATCH 2155/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 895249de0ac1295460da3e7de5614c0b26856041 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:51:15 +0530 Subject: [PATCH 2156/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From deff160a5fa2fa1a758c7ebfa91bc0cee4a90d7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:51:19 +0530 Subject: [PATCH 2157/3167] Time: 330 ms (31.90%), Space: 132.1 MB (9.20%) - LeetHub --- ...tions-to-make-the-array-elements-equal.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..2c986caa --- /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,30 @@ +class Solution { +public: + int reductionOperations(vector& nums) { + + int n = nums.size(); + + unordered_map mp; + + for(auto& itr : nums) + { + ++mp[itr]; + } + + vector> vp; + + copy(mp.begin(),mp.end(), back_inserter(vp)); + + sort(vp.rbegin(),vp.rend()); + + int ans = 0; + + for(int i = 0; i < vp.size()-1; ++i) + { + ans += vp[i].second; + vp[i+1].second += vp[i].second; + } + + return ans; + } +}; \ No newline at end of file From 8c4f40dccf45de485736c9a017136543b0bc5267 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:57:08 +0530 Subject: [PATCH 2158/3167] Attach NOTES - LeetHub From 18d29835f55aa26d2edfe9784716cb4e979dcdf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:57:11 +0530 Subject: [PATCH 2159/3167] Time: 163 ms (60.43%), Space: 83.2 MB (59.20%) - LeetHub --- ...tions-to-make-the-array-elements-equal.cpp | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) 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 index 2c986caa..894f7ffb 100644 --- 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 @@ -2,29 +2,19 @@ class Solution { public: int reductionOperations(vector& nums) { - int n = nums.size(); + int n = nums.size(), ans = 0; - unordered_map mp; + sort(nums.begin(), nums.end()); - for(auto& itr : nums) + for(int i = n-1; i > 0; --i) { - ++mp[itr]; - } - - vector> vp; - - copy(mp.begin(),mp.end(), back_inserter(vp)); - - sort(vp.rbegin(),vp.rend()); - - int ans = 0; - - for(int i = 0; i < vp.size()-1; ++i) - { - ans += vp[i].second; - vp[i+1].second += vp[i].second; + if(nums[i] != nums[i-1]) + { + ans += (n - i); + } } return ans; + } }; \ No newline at end of file From bd0ac2be25aae23a3c797c8a4f60dca482bdb068 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:59:43 +0530 Subject: [PATCH 2160/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1887-reduction-operations-to-make-the-array-elements-equal/README.md 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 From e204f4946e25812294d26169d264c7db004241fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:59:46 +0530 Subject: [PATCH 2161/3167] Time: 163 ms (60.43%), Space: 83.2 MB (59.20%) - LeetHub --- ...tions-to-make-the-array-elements-equal.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp 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 From f69004082831643272da3ba20602bfa20a9623f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:56:53 +0530 Subject: [PATCH 2162/3167] Create README - LeetHub --- .../README.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From a309d30433059d34f37c514f0dcdad2d4df5761e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:56:57 +0530 Subject: [PATCH 2163/3167] Time: 231 ms (7.05%), Space: 102.5 MB (29.91%) - LeetHub --- ...imum-amount-of-time-to-collect-garbage.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 76be25c36e68ec8094c3cf32e83c48e846b922e7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:59:52 +0530 Subject: [PATCH 2164/3167] Create README - LeetHub --- .../README.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2391-minimum-amount-of-time-to-collect-garbage/README.md 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 From 2327ca4de674fb59b2ee47b08ff20fbd00184fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:59:52 +0530 Subject: [PATCH 2165/3167] Attach NOTES - LeetHub --- 2391-minimum-amount-of-time-to-collect-garbage/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2391-minimum-amount-of-time-to-collect-garbage/NOTES.md 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 From b52ef92cfd05183e34f76ce434540e4af8bc606e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:59:55 +0530 Subject: [PATCH 2166/3167] Time: 231 ms (7.05%), Space: 102.5 MB (29.91%) - LeetHub --- ...imum-amount-of-time-to-collect-garbage.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp 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 From 428ba85347150cd206adabb60597f0909fcd0436 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:37:54 +0530 Subject: [PATCH 2167/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 2cbec1e71aae9b328b3ad81f79a28a5e4ee30feb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:37:57 +0530 Subject: [PATCH 2168/3167] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub --- ...absolute-differences-in-a-sorted-array.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 58efefd8ed77313028707c5415187c20e3e672c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:41:24 +0530 Subject: [PATCH 2169/3167] Attach NOTES - LeetHub --- 1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md 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 From 260e7aa102069bc9b74aef5e317cac405921514d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:41:28 +0530 Subject: [PATCH 2170/3167] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub --- ...absolute-differences-in-a-sorted-array.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp 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 From fccb090cd1687e5e4acd73f0a5402a66533b5ad6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:29 +0530 Subject: [PATCH 2171/3167] Attach NOTES - LeetHub From 8d2d63aa43b5b90cd5ac9a8cd4ea288545131061 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:32 +0530 Subject: [PATCH 2172/3167] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub From 33f015a87128e320f07bf5ff7ac2239a77a77ce2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:51 +0530 Subject: [PATCH 2173/3167] Attach NOTES - LeetHub From 07b544a7b3cedde6522699ffbf0ceaf7a5812118 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:55 +0530 Subject: [PATCH 2174/3167] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub From 7247690fcae53aa80f543349f403d78618163b30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:28:25 +0530 Subject: [PATCH 2175/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 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 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 From 4ff0896b444b756b0ceacd74a54e87b727fe8f0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:28:26 +0530 Subject: [PATCH 2176/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 94fe42e83825b928bfe7b75217d863538030f143 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:28:28 +0530 Subject: [PATCH 2177/3167] Time: 155 ms (65.34%), Space: 66.2 MB (96.02%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 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 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..3eed51f2 --- /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,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 From 32bcc108d11b4cc4a833e4d93315db4417b60b6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:22 +0530 Subject: [PATCH 2178/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1727-largest-submatrix-with-rearrangements/README.md 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 From 77ae3d992cdb6bd04e61646406eba3f84e44f378 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:22 +0530 Subject: [PATCH 2179/3167] Attach NOTES - LeetHub --- 1727-largest-submatrix-with-rearrangements/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1727-largest-submatrix-with-rearrangements/NOTES.md 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 From 19e3fd849552c3ef487b14b738e2b0ecb84c5259 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:26 +0530 Subject: [PATCH 2180/3167] Time: 155 ms (65.34%), Space: 66.2 MB (96.02%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp 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 From a755b623949632d0e55c20c4f3dd74c1a30fb129 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:39 +0530 Subject: [PATCH 2181/3167] Attach NOTES - LeetHub From a1fdb924630a6280b594b896f5616184d0cfbc0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:43 +0530 Subject: [PATCH 2182/3167] Time: 155 ms (65.34%), Space: 66.2 MB (96.02%) - LeetHub From 10e9bdf501148dc3e9de58e46cc0c048833c20e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:52:59 +0530 Subject: [PATCH 2183/3167] Attach NOTES - LeetHub From 7b8f250d221c6eb743274251b6c501bb5f783da4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:53:01 +0530 Subject: [PATCH 2184/3167] Time: 145 ms (85.55%), Space: 66.2 MB (95.95%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) 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 index 3eed51f2..9183417e 100644 --- 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 @@ -2,27 +2,33 @@ class Solution { public: int largestSubmatrix(vector>& matrix) { - int i, j, ans = 0, n = matrix.size(), m = matrix[0].size(); + int n = matrix.size(); + int m = matrix[0].size(); - for(i = 0; i < m; i++) + for(int i = 1; i < n; ++i) { - for(j = 1; j < n; j++) + for(int j = 0; j < m; ++j) { - if(matrix[j][i] == 1) - matrix[j][i] = matrix[j-1][i] + matrix[j][i]; + if(matrix[i][j] == 1) + matrix[i][j] += matrix[i-1][j]; else - matrix[j][i] = 0; + matrix[i][j] = 0; } } - - for(i = 0; i < n; i++) + + int ans = 0; + + for(int 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)); + sort(matrix[i].begin(), matrix[i].end()); + + for(int j = 0; j < m; ++j) + { + ans = max(ans, matrix[i][j] * (m-j)); + } } return ans; + } }; \ No newline at end of file From ef29064f886c44c857debf5d6691a2dd4f60cccc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:32:14 +0530 Subject: [PATCH 2185/3167] Attach NOTES - LeetHub From f4f900dae97433ad22ea8b11f9edf45d2b20d9b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:32:17 +0530 Subject: [PATCH 2186/3167] Time: 198 ms (8.09%), Space: 96.1 MB (5.20%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) 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 index 9183417e..de864305 100644 --- 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 @@ -5,30 +5,41 @@ class Solution { int n = matrix.size(); int m = matrix[0].size(); - for(int i = 1; i < n; ++i) - { - for(int j = 0; j < m; ++j) - { - if(matrix[i][j] == 1) - matrix[i][j] += matrix[i-1][j]; - else - matrix[i][j] = 0; - } - } - int ans = 0; - for(int i = 0; i < n; ++i) + vector> prevHeights; + + for(int row = 0; row < n; ++row) { - sort(matrix[i].begin(), matrix[i].end()); + vector> currHeights; + vector visited(m, false); - for(int j = 0; j < m; ++j) + for(auto& [height, col] : prevHeights) { - ans = max(ans, matrix[i][j] * (m-j)); + 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 From 4a1f318b4ef4b0dc0e92a516f244cd22f5e3f0d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 22:56:49 +0530 Subject: [PATCH 2187/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From 9866e74b0edd048c4c27d0aa96b0cd0ecbb31e64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 22:56:53 +0530 Subject: [PATCH 2188/3167] Time: 206 ms (28.07%), Space: 29.7 MB (52.78%) - LeetHub --- ...t-svg-div-div-div-div935-knight-dialer.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 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 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 From 349a68c41af062fc02b24f59917707e96507f985 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:36:43 +0530 Subject: [PATCH 2189/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From e8af40691804e88d04934c663ae31e3ecb69a277 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:36:47 +0530 Subject: [PATCH 2190/3167] Time: 1211 ms (6.86%), Space: 404.8 MB (5.71%) - LeetHub --- ...mber-of-ways-to-divide-a-long-corridor.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..ca35c3d4 --- /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,45 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int idx, int cnt, int n, string& corridor, vector>& dp) + { + if(cnt > 2) + return 0; + + if(idx == n) + { + return (cnt == 2); + } + + if(dp[idx][cnt] != -1) + return dp[idx][cnt]; + + int ans = 0; + + if(corridor[idx] == 'S') + { + ans = (ans + helper(idx+1, cnt+1, n, corridor, dp)) % mod; + if(cnt == 2) + ans = (ans + helper(idx+1, 1, n, corridor, dp)) % mod; + } + else + { + ans = (ans + helper(idx+1, cnt, n, corridor, dp)) % mod; + if(cnt == 2) + ans = (ans + helper(idx+1, 0, n, corridor, dp)) % mod; + } + return dp[idx][cnt] = ans; + } + + int numberOfWays(string corridor) { + + int n = corridor.size(); + + vector> dp(n, vector(3, -1)); + + return helper(0, 0, n, corridor, dp); + + } +}; \ No newline at end of file From 3658cc127bcf83d952ec98b04a0d6010290c2d6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:39:16 +0530 Subject: [PATCH 2191/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From fe73c267e25b6e1bb8548df352916bb228029e15 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:39:18 +0530 Subject: [PATCH 2192/3167] Time: 140 ms (50.86%), Space: 44 MB (43.43%) - LeetHub --- ...mber-of-ways-to-divide-a-long-corridor.cpp | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) 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 index ca35c3d4..89923009 100644 --- 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 @@ -1,45 +1,48 @@ -class Solution { +class Solution { public: const int mod = 1e9+7; - int helper(int idx, int cnt, int n, string& corridor, vector>& dp) - { - if(cnt > 2) - return 0; + int numberOfWays(string corridor) { - if(idx == n) - { - return (cnt == 2); - } + int n = corridor.size(); + + int cnt = count(corridor.begin(), corridor.end(), 'S'); + + if(!cnt or cnt & 1) return 0; + + int inc = 0; - if(dp[idx][cnt] != -1) - return dp[idx][cnt]; + vector v; - int ans = 0; + int currCnt = 0; - if(corridor[idx] == 'S') + for(int i = 0; i < n; ++i) { - ans = (ans + helper(idx+1, cnt+1, n, corridor, dp)) % mod; - if(cnt == 2) - ans = (ans + helper(idx+1, 1, n, corridor, dp)) % mod; + 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); } - else + + if(currCnt) { - ans = (ans + helper(idx+1, cnt, n, corridor, dp)) % mod; - if(cnt == 2) - ans = (ans + helper(idx+1, 0, n, corridor, dp)) % mod; + v.push_back(inc); } - return dp[idx][cnt] = ans; - } - - int numberOfWays(string corridor) { - int n = corridor.size(); - - vector> dp(n, vector(3, -1)); + long long ans = v[0]; + + for(int i = 1; i < v.size(); ++i) + ans = (ans *1LL* v[i]) % mod; - return helper(0, 0, n, corridor, dp); - + return ans; } }; \ No newline at end of file From 32001cb4232bc78dcb9b0b5c884a0ebceea8e147 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Nov 2023 07:15:20 +0530 Subject: [PATCH 2193/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From cf45a03ad798980eee3549088642d9b839ccd0ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Nov 2023 07:15:26 +0530 Subject: [PATCH 2194/3167] Time: 4 ms (30.61%), Space: 6.4 MB (57.22%) - LeetHub --- ...t-svg-div-div-div-div191-number-of-1-bits.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 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 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 From 2ec173673770fbfb2b1bc3a1184c5417a1250462 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Nov 2023 22:00:37 +0530 Subject: [PATCH 2195/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From c532108faa75dd5f5765ec3f456948b5d932e50e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Nov 2023 22:00:40 +0530 Subject: [PATCH 2196/3167] Time: 3 ms (45.97%), Space: 6.3 MB (80.09%) - LeetHub --- ...nimum-one-bit-operations-to-make-integers-zero.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From a980aebb2740b56e2e7c72dd4f648978b2ee0428 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:45:37 +0530 Subject: [PATCH 2197/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 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 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 From 6a38c37b87440dc90d592cbc59fef42baf403f17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:45:41 +0530 Subject: [PATCH 2198/3167] Time: 0 ms (100.00%), Space: 11.9 MB (77.65%) - LeetHub --- ...ck-if-two-string-arrays-are-equivalent.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 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 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 From dfe2c4742af3e562239a0c0674317b325266e57e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:22:34 +0530 Subject: [PATCH 2199/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 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 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 From 8cf6dde5cf01d0a8aeed1bac333034f92b98ba0d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:22:34 +0530 Subject: [PATCH 2200/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 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 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 From e4fa46a5e17cf935643778fa2550bfe997060266 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:22:38 +0530 Subject: [PATCH 2201/3167] Time: 174 ms (15.47%), Space: 49.6 MB (30.78%) - LeetHub --- ...words-that-can-be-formed-by-characters.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 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 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 From 057ebf9ae344879aefdef50fcefdc7117cf8a33e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:06 +0530 Subject: [PATCH 2202/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1160-find-words-that-can-be-formed-by-characters/README.md 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 From cd1fbe856b565bda0e648c97f19bd76e3b58fd51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:06 +0530 Subject: [PATCH 2203/3167] Attach NOTES - LeetHub --- 1160-find-words-that-can-be-formed-by-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1160-find-words-that-can-be-formed-by-characters/NOTES.md 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 From 5c530079d8fe4162a8461945f5e2e66ba070ea32 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:10 +0530 Subject: [PATCH 2204/3167] Time: 174 ms (15.47%), Space: 49.6 MB (30.78%) - LeetHub --- ...words-that-can-be-formed-by-characters.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp 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 From 2a0977ba80690d01a10747b6c8357e3438e5adc2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:21 +0530 Subject: [PATCH 2205/3167] Attach NOTES - LeetHub From 07e84d268cdd4cd464e977d90ba62f0439fb5a39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:25 +0530 Subject: [PATCH 2206/3167] Time: 174 ms (15.47%), Space: 49.6 MB (30.78%) - LeetHub From bfd325aaf6548fabdae81ff01751898cca571770 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:47:18 +0530 Subject: [PATCH 2207/3167] Create README - LeetHub --- .../README.md | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 38f92ea79ddb6071abf4209f44243297f6627273 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:47:22 +0530 Subject: [PATCH 2208/3167] Time: 314 ms (49.52%), Space: 49.1 MB (14.44%) - LeetHub --- ...-div2949-count-beautiful-substrings-ii.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 9309fa44eb38a764e848e49282879369dcee2c28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:49:32 +0530 Subject: [PATCH 2209/3167] Attach NOTES - LeetHub --- 2949-count-beautiful-substrings-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2949-count-beautiful-substrings-ii/NOTES.md 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 From 28eeed5fcea40537a82080c069ee2cfd137b039d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:49:35 +0530 Subject: [PATCH 2210/3167] Time: 314 ms (49.52%), Space: 49.1 MB (14.44%) - LeetHub --- .../2949-count-beautiful-substrings-ii.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp 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 From 49b7095ed7131167ff5276438da82ed2fe26306d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 07:42:47 +0530 Subject: [PATCH 2211/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 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 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 From 5929eb154b77d9ee1f07a168c1467c9f4bbf3cb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 07:42:51 +0530 Subject: [PATCH 2212/3167] Time: 11 ms (13.23%), Space: 10.4 MB (95.08%) - LeetHub --- ...div1266-minimum-time-visiting-all-points.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 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 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 From 7288e0f7d05151ac0115d73dd0cc97fffbfafae9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:02:28 +0530 Subject: [PATCH 2213/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 7d9b117fc38e55ea0c49463a34c301aede6f7199 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:02:28 +0530 Subject: [PATCH 2214/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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 From 9e7d2e03f5b323185fe5ec0dc28ccd1578625506 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:02:32 +0530 Subject: [PATCH 2215/3167] Time: 9 ms (5.22%), Space: 8.1 MB (5.80%) - LeetHub --- ...-largest-3-same-digit-number-in-string.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-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 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..70a8ccbe --- /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,44 @@ +class Solution { +public: + string largestGoodInteger(string num) { + + int n = num.size(); + + unordered_map mp; + + int maxi = 0; + string ans; + + int i = 0, j = 0, k = 3; + + string curr; + + while(j < n) + { + ++mp[num[j]]; + + curr += num[j]; + + if(curr.size() == 3) + { + int number = stoi(curr); + if(number >= maxi and mp.size() == 1) + { + maxi = number; + ans = curr; + } + + --mp[num[i]]; + if(mp[num[i]] == 0) + mp.erase(num[i]); + ++i; + + curr.erase(curr.begin()); + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file From baaf2223e10f1a1d56203806e11260ffd2ec5b9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:03:10 +0530 Subject: [PATCH 2216/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2264-largest-3-same-digit-number-in-string/README.md 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 From bb41e77364a81261bbd4768c5aea3e08a1e263a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:03:10 +0530 Subject: [PATCH 2217/3167] Attach NOTES - LeetHub --- 2264-largest-3-same-digit-number-in-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2264-largest-3-same-digit-number-in-string/NOTES.md 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 From d7676343395e30b9bb2d9a916cf299695816f63b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:03:14 +0530 Subject: [PATCH 2218/3167] Time: 9 ms (5.22%), Space: 8.1 MB (5.80%) - LeetHub --- ...-largest-3-same-digit-number-in-string.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp 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..70a8ccbe --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + string largestGoodInteger(string num) { + + int n = num.size(); + + unordered_map mp; + + int maxi = 0; + string ans; + + int i = 0, j = 0, k = 3; + + string curr; + + while(j < n) + { + ++mp[num[j]]; + + curr += num[j]; + + if(curr.size() == 3) + { + int number = stoi(curr); + if(number >= maxi and mp.size() == 1) + { + maxi = number; + ans = curr; + } + + --mp[num[i]]; + if(mp[num[i]] == 0) + mp.erase(num[i]); + ++i; + + curr.erase(curr.begin()); + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file From 4945bbbcccf987efdd13f3dc04536c306c8d965d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:07 +0530 Subject: [PATCH 2219/3167] Attach NOTES - LeetHub From 0b0801bc8153029191f36fad9d3b9648d5930449 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:10 +0530 Subject: [PATCH 2220/3167] Time: 2 ms (53.04%), Space: 6.8 MB (91.88%) - LeetHub --- ...64-largest-3-same-digit-number-in-string.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) 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 index 70a8ccbe..2dc69092 100644 --- 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 @@ -4,9 +4,6 @@ class Solution { int n = num.size(); - unordered_map mp; - - int maxi = 0; string ans; int i = 0, j = 0, k = 3; @@ -15,24 +12,14 @@ class Solution { while(j < n) { - ++mp[num[j]]; - curr += num[j]; if(curr.size() == 3) { - int number = stoi(curr); - if(number >= maxi and mp.size() == 1) + if(j>=2 and num[j] == num[j-1] and num[j-1] == num[j-2]) { - maxi = number; - ans = curr; + ans = max(ans, curr); } - - --mp[num[i]]; - if(mp[num[i]] == 0) - mp.erase(num[i]); - ++i; - curr.erase(curr.begin()); } From 382a1c8d0182591d54b25423975c7919ce13d5f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:43 +0530 Subject: [PATCH 2221/3167] Attach NOTES - LeetHub From f66fdf06ad9790d4b5eb8cd9688ceb48648442db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:47 +0530 Subject: [PATCH 2222/3167] Time: 2 ms (53.04%), Space: 6.8 MB (91.88%) - LeetHub --- ...64-largest-3-same-digit-number-in-string.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) 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 index 70a8ccbe..2dc69092 100644 --- 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 @@ -4,9 +4,6 @@ class Solution { int n = num.size(); - unordered_map mp; - - int maxi = 0; string ans; int i = 0, j = 0, k = 3; @@ -15,24 +12,14 @@ class Solution { while(j < n) { - ++mp[num[j]]; - curr += num[j]; if(curr.size() == 3) { - int number = stoi(curr); - if(number >= maxi and mp.size() == 1) + if(j>=2 and num[j] == num[j-1] and num[j-1] == num[j-2]) { - maxi = number; - ans = curr; + ans = max(ans, curr); } - - --mp[num[i]]; - if(mp[num[i]] == 0) - mp.erase(num[i]); - ++i; - curr.erase(curr.begin()); } From 926fffbe13b21e6a2734ad3f729e18041db7a646 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:25:31 +0530 Subject: [PATCH 2223/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1716-calculate-money-in-leetcode-bank/README.md 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 From 4febfedb260e44ecee7df0cb7b6dbe57a417687e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:25:34 +0530 Subject: [PATCH 2224/3167] Time: 3 ms (13.68%), Space: 6.4 MB (35.53%) - LeetHub --- .../1716-calculate-money-in-leetcode-bank.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp 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 From 5ee4129aa1a395a9fdb43e999d69f5bf8a8d2441 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:29:44 +0530 Subject: [PATCH 2225/3167] Attach NOTES - LeetHub --- 1716-calculate-money-in-leetcode-bank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1716-calculate-money-in-leetcode-bank/NOTES.md 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 From 1efaab6fe80d563b6651104b66723de24f93226b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:29:47 +0530 Subject: [PATCH 2226/3167] Time: 3 ms (13.68%), Space: 6.4 MB (35.53%) - LeetHub From 30d6c792f0c6ed88bd2a0bfd0a648c782bf02e0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:01:08 +0530 Subject: [PATCH 2227/3167] Create README - LeetHub --- 1903-largest-odd-number-in-string/README.md | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1903-largest-odd-number-in-string/README.md 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 From 4723d73cfafab4b131ce467658a384afa81665d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:01:08 +0530 Subject: [PATCH 2228/3167] Attach NOTES - LeetHub --- 1903-largest-odd-number-in-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1903-largest-odd-number-in-string/NOTES.md 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 From fc94e317a8267bb217a94721a6c8a70161e2b8b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:01:11 +0530 Subject: [PATCH 2229/3167] Time: 23 ms (91.89%), Space: 15.4 MB (37.80%) - LeetHub --- .../1903-largest-odd-number-in-string.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp 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 From 8d8d8904571f25e49c0d42d8bd24a9341e049ae7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:02:36 +0530 Subject: [PATCH 2230/3167] Attach NOTES - LeetHub From 594462ae8a000664b278c94f6f229feca4fa969c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:02:39 +0530 Subject: [PATCH 2231/3167] Time: 23 ms (91.89%), Space: 15.4 MB (37.80%) - LeetHub From 9ae84fd53e831f8440ce219c77b72c2432f99fa1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:38:43 +0530 Subject: [PATCH 2232/3167] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0606-construct-string-from-binary-tree/README.md 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 From 52af689cd305c799f35fe29b77e3281cd4d928c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:38:43 +0530 Subject: [PATCH 2233/3167] Attach NOTES - LeetHub --- 0606-construct-string-from-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0606-construct-string-from-binary-tree/NOTES.md 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 From 456b0b14fd60cf24b83f865d6d401dae81ab787c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:38:46 +0530 Subject: [PATCH 2234/3167] Time: 0 ms (100.00%), Space: 23.8 MB (80.74%) - LeetHub --- ...0606-construct-string-from-binary-tree.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp 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 From 0b8b82cfdcf87fa1d929b13554321c7006fcc280 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:21:40 +0530 Subject: [PATCH 2235/3167] Create README - LeetHub --- 0867-transpose-matrix/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0867-transpose-matrix/README.md 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 From b473006d25dadba025d1a0facbc588e9d5758773 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:21:40 +0530 Subject: [PATCH 2236/3167] Attach NOTES - LeetHub --- 0867-transpose-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0867-transpose-matrix/NOTES.md 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 From 7bfe6344c0eb83a531ecd548f518dc4b3643ad0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:21:44 +0530 Subject: [PATCH 2237/3167] Time: 7 ms (87.64%), Space: 11.2 MB (10.71%) - LeetHub --- .../0867-transpose-matrix.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0867-transpose-matrix/0867-transpose-matrix.cpp 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 From 59ce32b4286d5e52ce8634b5787e72c2b37b9c1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:37:21 +0530 Subject: [PATCH 2238/3167] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1287-element-appearing-more-than-25-in-sorted-array/README.md 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 From 1ae90984be1c8b6779d0cff0f17f0baeb0c65177 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:37:25 +0530 Subject: [PATCH 2239/3167] Time: 6 ms (92.41%), Space: 12.7 MB (72.53%) - LeetHub --- ...nt-appearing-more-than-25-in-sorted-array.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp 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 From a06742d13ed3d7d9e4d6c87799030579bbeaacaf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:24:01 +0530 Subject: [PATCH 2240/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2963-count-the-number-of-good-partitions/README.md 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 From cc15f28d062b28ca71ccaeb4de970afb23b7bf83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:24:05 +0530 Subject: [PATCH 2241/3167] Time: 226 ms (80.00%), Space: 134.3 MB (80.00%) - LeetHub --- ...63-count-the-number-of-good-partitions.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp 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 From 029aad36c79d1cc802234eec62d68c26fd8f7927 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:29:52 +0530 Subject: [PATCH 2242/3167] Attach NOTES - LeetHub --- 2963-count-the-number-of-good-partitions/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2963-count-the-number-of-good-partitions/NOTES.md 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 From df301911c3a36eaa0edaded1d86914c9f8ee384a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:29:55 +0530 Subject: [PATCH 2243/3167] Time: 226 ms (80.00%), Space: 134.3 MB (80.00%) - LeetHub From 33d31ed741393d35873ead031818397fc7b1e62c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:03:33 +0530 Subject: [PATCH 2244/3167] Attach NOTES - LeetHub From c1178f7d93dc084ed0b3c801396ff2e8a9d1874b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:03:36 +0530 Subject: [PATCH 2245/3167] Time: 226 ms (80.00%), Space: 134.3 MB (80.00%) - LeetHub From 1708fa4adcccf16151459e5ce135a3f1848e329e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:31:32 +0530 Subject: [PATCH 2246/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1464-maximum-product-of-two-elements-in-an-array/README.md 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 From e40df366ccbb6bb32b0a92b22597d8f5b0c925c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:31:35 +0530 Subject: [PATCH 2247/3167] Time: 0 ms (100.00%), Space: 10.4 MB (38.51%) - LeetHub --- ...464-maximum-product-of-two-elements-in-an-array.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp 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 From 1fd28e2bc8bd5cf6ae5e1030d09a79aeb1cbf233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:32:33 +0530 Subject: [PATCH 2248/3167] Attach NOTES - LeetHub --- 1464-maximum-product-of-two-elements-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1464-maximum-product-of-two-elements-in-an-array/NOTES.md 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 From f7e4a4d34b31c227db1b6d777a41286977c87e7c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:32:36 +0530 Subject: [PATCH 2249/3167] Time: 0 ms (100.00%), Space: 10.4 MB (38.51%) - LeetHub From 15599bd56e6e4b52f38152d0944245352c5558c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:13:32 +0530 Subject: [PATCH 2250/3167] Create README - LeetHub --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1582-special-positions-in-a-binary-matrix/README.md 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 From b257280df09e957e3cb0488820dddf416a35525f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:13:33 +0530 Subject: [PATCH 2251/3167] Attach NOTES - LeetHub --- 1582-special-positions-in-a-binary-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1582-special-positions-in-a-binary-matrix/NOTES.md 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 From 2013862733e21823f4916383c7cb3bd1114d8574 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:13:37 +0530 Subject: [PATCH 2252/3167] Time: 16 ms (68.95%), Space: 13.3 MB (46.58%) - LeetHub --- ...2-special-positions-in-a-binary-matrix.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp 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 From d14a561533a7b6f2d107a22fc06770aa414e6c52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:14:39 +0530 Subject: [PATCH 2253/3167] Attach NOTES - LeetHub From 3c95bd9e65b2659fe51b5075ca393ccf3b8bf42a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:14:42 +0530 Subject: [PATCH 2254/3167] Time: 16 ms (68.95%), Space: 13.3 MB (46.58%) - LeetHub From 959e59b84bfe7d06f8d96608d81c7ae6189abdf7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:24:06 +0530 Subject: [PATCH 2255/3167] Create README - LeetHub --- .../README.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2482-difference-between-ones-and-zeros-in-row-and-column/README.md 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 From b19456ab154158b120c52af78f2daa43b0e2fd99 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:24:06 +0530 Subject: [PATCH 2256/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md 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 From 986565b93ec3f0a7c039cf68b34de0b1999db4b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:24:11 +0530 Subject: [PATCH 2257/3167] Time: 201 ms (47.29%), Space: 117.5 MB (65.73%) - LeetHub --- ...tween-ones-and-zeros-in-row-and-column.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp 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 From ece63523731d20f51c7e8817eec183bb0f7829cb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:25:48 +0530 Subject: [PATCH 2258/3167] Attach NOTES - LeetHub From 483641701d0bbba576a281c777b3f6dfe41810ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:25:51 +0530 Subject: [PATCH 2259/3167] Time: 201 ms (47.29%), Space: 117.5 MB (65.73%) - LeetHub From 34e82c2e68ecb8aca8c84e0c5d94be9be0bb05fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:18 +0530 Subject: [PATCH 2260/3167] Create README - LeetHub --- 0242-valid-anagram/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0242-valid-anagram/README.md 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 From 6ed56ddbb8daf0b2baa3d8e16c4273007765ea47 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:22 +0530 Subject: [PATCH 2261/3167] Time: 2 ms (98.77%), Space: 7.6 MB (79.21%) - LeetHub --- 0242-valid-anagram/0242-valid-anagram.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0242-valid-anagram/0242-valid-anagram.cpp 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 From d3c4083bf0551c1a0211ecb383fc07715d379f84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:36 +0530 Subject: [PATCH 2262/3167] Attach NOTES - LeetHub --- 0242-valid-anagram/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0242-valid-anagram/NOTES.md 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 From 4638a93f8c7e795811213f5e132f5598bc171279 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:40 +0530 Subject: [PATCH 2263/3167] Time: 2 ms (98.77%), Space: 7.6 MB (79.21%) - LeetHub From bbd273d4dc5eed863b223258711d2ad310a9df71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:39:21 +0530 Subject: [PATCH 2264/3167] Create README - LeetHub --- 0938-range-sum-of-bst/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0938-range-sum-of-bst/README.md 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 From 5c0f7a38b5af69dc4f98fef736100864afbe4053 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:39:22 +0530 Subject: [PATCH 2265/3167] Attach NOTES - LeetHub --- 0938-range-sum-of-bst/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0938-range-sum-of-bst/NOTES.md 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 From d7e010f8ecc25a51cb7b94dcb970e02f4c2307d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:39:59 +0530 Subject: [PATCH 2266/3167] Create README - LeetHub From 536815bac1eb8fcb1281092a29248f3494b6ec9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:00 +0530 Subject: [PATCH 2267/3167] Attach NOTES - LeetHub From ac7d164fb8566101498672f69b21729dd4c7a654 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:03 +0530 Subject: [PATCH 2268/3167] Time: 92 ms (72.75%), Space: 65 MB (50.95%) - LeetHub --- .../0938-range-sum-of-bst.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0938-range-sum-of-bst/0938-range-sum-of-bst.cpp 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 From 0666c6991ce6c6aed965c5825400413aa6999deb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:20 +0530 Subject: [PATCH 2269/3167] Attach NOTES - LeetHub From 69a730579724221fbdebeb7d837c9b7bf0bf6213 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:24 +0530 Subject: [PATCH 2270/3167] Time: 92 ms (72.75%), Space: 65 MB (50.95%) - LeetHub From 957aee2ddd31f164fdba68008fdc9437c3227d4b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:41:17 +0530 Subject: [PATCH 2271/3167] Create README - LeetHub --- 2353-design-a-food-rating-system/README.md | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2353-design-a-food-rating-system/README.md 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 From b6d1d05cfdf64b1ada84d710555f5a256b88aec4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:41:17 +0530 Subject: [PATCH 2272/3167] Attach NOTES - LeetHub --- 2353-design-a-food-rating-system/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2353-design-a-food-rating-system/NOTES.md 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 From 12629e817cc8432a3aefbeb30379fbe1be9177e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:41:21 +0530 Subject: [PATCH 2273/3167] Time: 414 ms (65.55%), Space: 167.1 MB (30.14%) - LeetHub --- .../2353-design-a-food-rating-system.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp 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 From 573d54269cd1486f3ee38c8aa97798896d33582e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:34:58 +0530 Subject: [PATCH 2274/3167] Create README - LeetHub --- .../README.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 0160-intersection-of-two-linked-lists/README.md 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 From fd6a338335b574300548447b53ce37e2a9f3d844 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:50:11 +0530 Subject: [PATCH 2275/3167] Create README - LeetHub --- 0200-number-of-islands/README.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0200-number-of-islands/README.md 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 From ad880607ac9e06d25a75507b8a218afd71d4dc09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:50:12 +0530 Subject: [PATCH 2276/3167] Attach NOTES - LeetHub --- 0200-number-of-islands/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0200-number-of-islands/NOTES.md 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 From cd6e380f041333557689dcf0e7e13d59a45be3df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:50:17 +0530 Subject: [PATCH 2277/3167] Time: 20 ms (97.87%), Space: 13 MB (53.30%) - LeetHub --- .../0200-number-of-islands.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0200-number-of-islands/0200-number-of-islands.cpp 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..7a793f47 --- /dev/null +++ b/0200-number-of-islands/0200-number-of-islands.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + + vector dy = {-1, +1, 0, 0}; + vector dx = {0, 0, -1, +1}; + + bool isValid(int i, int j, int n, int m) + { + return (i >= 0 and j >= 0 and i < n and j < m); + } + + void dfs(int i, int j, int n, int m, vector>& grid, vector>& visited) + { + 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 !visited[newx][newy] and grid[newx][newy] == '1') + { + dfs(newx, newy, n, m, grid, visited); + } + } + } + + int numIslands(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + int numberOfIslands = 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] == '1' and !visited[i][j]) + { + ++numberOfIslands; + dfs(i, j, n, m, grid, visited); + } + } + } + + return numberOfIslands; + + } +}; \ No newline at end of file From 95eeac1d68d0e9fbc030683360940b4d8671fba1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 00:39:16 +0530 Subject: [PATCH 2278/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1358-number-of-substrings-containing-all-three-characters/README.md 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 From 2c4a5c77f9f72b1eaee9f2542bde2160a81fe38f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 00:39:16 +0530 Subject: [PATCH 2279/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1358-number-of-substrings-containing-all-three-characters/NOTES.md 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 From 53360f9e5c791cf187799aac610daae64f6208f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 03:26:55 +0530 Subject: [PATCH 2280/3167] Create README - LeetHub --- 0514-freedom-trail/README.md | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0514-freedom-trail/README.md 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 From 610818a87badc876431320e9b5e57d0d5c5dc967 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 03:26:58 +0530 Subject: [PATCH 2281/3167] Time: 20 ms (62.83%), Space: 13.7 MB (48.67%) - LeetHub --- 0514-freedom-trail/0514-freedom-trail.cpp | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0514-freedom-trail/0514-freedom-trail.cpp 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 From 036889f4e18001ed963e384a6dba42732ccbd61b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:00:53 +0530 Subject: [PATCH 2282/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1913-maximum-product-difference-between-two-pairs/README.md 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 From 348f2201d1ad17c5f3412ef2ae25009bcdf8f6d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:00:57 +0530 Subject: [PATCH 2283/3167] Time: 33 ms (8.20%), Space: 20.6 MB (64.73%) - LeetHub --- ...3-maximum-product-difference-between-two-pairs.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp 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 From 6b242367996ff49277770fb84ab311afbf12485c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:02:10 +0530 Subject: [PATCH 2284/3167] Attach NOTES - LeetHub --- 1913-maximum-product-difference-between-two-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1913-maximum-product-difference-between-two-pairs/NOTES.md 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 From 9c8e8b6036ab4d6272e62d594465310009d4bd9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:02:13 +0530 Subject: [PATCH 2285/3167] Time: 33 ms (8.20%), Space: 20.6 MB (64.73%) - LeetHub From 2ca1736c58970289b705142dd3577b242c8ccc63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:05:05 +0530 Subject: [PATCH 2286/3167] Create README - LeetHub --- 0402-remove-k-digits/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0402-remove-k-digits/README.md 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 From be47d1b117ca0d1e61465eba04b81b2c1117ddb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:05:06 +0530 Subject: [PATCH 2287/3167] Attach NOTES - LeetHub --- 0402-remove-k-digits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0402-remove-k-digits/NOTES.md 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 From 788a4a5552292c6d8b530aac22c5da86646d0c4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:05:09 +0530 Subject: [PATCH 2288/3167] Time: 14 ms (39.19%), Space: 9.1 MB (65.72%) - LeetHub --- 0402-remove-k-digits/0402-remove-k-digits.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0402-remove-k-digits/0402-remove-k-digits.cpp 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..a7e3602b --- /dev/null +++ b/0402-remove-k-digits/0402-remove-k-digits.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + string removeKdigits(string num, int k) { + + if(k >= num.size()) + return "0"; + + stack st; + + int n = num.size(); + + st.push(num[0]); + + for(int i = 1; i < n; ++i) + { + while(k > 0 and !st.empty() and st.top() > num[i]) + { + --k; + st.pop(); + } + st.push(num[i]); + + if(st.size() == 1 and num[i] == '0') + st.pop(); + } + + while(k and !st.empty()) + { + --k; + st.pop(); + } + + string res; + + while(!st.empty()) + { + res += st.top(); + st.pop(); + } + + if(res.empty()) + return "0"; + + reverse(res.begin(), res.end()); + + return res; + } +}; \ No newline at end of file From 16de5add79ca3ad28aacafd733f05d3d5b90068e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:56:11 +0530 Subject: [PATCH 2289/3167] Create README - LeetHub --- 0556-next-greater-element-iii/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0556-next-greater-element-iii/README.md 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 From e081583344ff0728f017d0969ade0506f413b533 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:56:15 +0530 Subject: [PATCH 2290/3167] Time: 0 ms (100.00%), Space: 6.5 MB (17.41%) - LeetHub --- .../0556-next-greater-element-iii.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0556-next-greater-element-iii/0556-next-greater-element-iii.cpp 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 From 39acd0d160a1c274f9d6891dddd88dcd9daeac1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:55:10 +0530 Subject: [PATCH 2291/3167] Create README - LeetHub --- 0661-image-smoother/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0661-image-smoother/README.md 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 From 3fa9614a570cf778aba53510aa3c1e4f511c03a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:55:14 +0530 Subject: [PATCH 2292/3167] Time: 32 ms (96.92%), Space: 22 MB (59.38%) - LeetHub --- 0661-image-smoother/0661-image-smoother.cpp | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0661-image-smoother/0661-image-smoother.cpp 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 From 40a773064c176242097d45ad2bce8faaa63a9381 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:05:40 +0530 Subject: [PATCH 2293/3167] Create README - LeetHub --- 2706-buy-two-chocolates/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2706-buy-two-chocolates/README.md 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 From f626b122b7aee3104be860959e3c74368b4ec1e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:05:41 +0530 Subject: [PATCH 2294/3167] Attach NOTES - LeetHub --- 2706-buy-two-chocolates/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2706-buy-two-chocolates/NOTES.md 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 From f392a7d4044add3bbf8c28fa9cb599d8f9a77592 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:05:44 +0530 Subject: [PATCH 2295/3167] Time: 11 ms (73.75%), Space: 47 MB (58.30%) - LeetHub --- 2706-buy-two-chocolates/2706-buy-two-chocolates.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 2706-buy-two-chocolates/2706-buy-two-chocolates.cpp 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 From e750add6f84cc1bf1921ad97866bcb5abe036b8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:14:02 +0530 Subject: [PATCH 2296/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1637-widest-vertical-area-between-two-points-containing-no-points/README.md 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 From c0968d3a7e1e605a71db51c5ca46460447b500ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:14:02 +0530 Subject: [PATCH 2297/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md 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 From afa2b7d95ce55be7b6de2c9be279bfd859ac0e2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:14:05 +0530 Subject: [PATCH 2298/3167] Time: 281 ms (6.93%), Space: 68.7 MB (51.13%) - LeetHub --- ...etween-two-points-containing-no-points.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp 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..ff3ea789 --- /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,36 @@ +class Solution { +public: + int maxWidthOfVerticalArea(vector>& points) { + + int n = points.size(); + + int ans = 0; + + sort(points.begin(), points.end()); + + vector v; + + for(int i = 0; i < n; ++i) + { + v.push_back(points[i][0]); + } + + + for(int i = 0; i < n; ++i) + { + int x1 = v[i]; + int idx = upper_bound(v.begin(), v.end(), v[i]) - v.begin(); + + if(idx == n) + continue; + + int x2 = v[idx]; + + // cout< Date: Thu, 21 Dec 2023 22:16:02 +0530 Subject: [PATCH 2299/3167] Attach NOTES - LeetHub From bb757d55aba8f41ca1eff9cfcedd48865771d51e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:16:07 +0530 Subject: [PATCH 2300/3167] Time: 248 ms (40.64%), Space: 66.5 MB (87.58%) - LeetHub --- ...etween-two-points-containing-no-points.cpp | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) 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 index ff3ea789..8da3ae7b 100644 --- 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 @@ -1,36 +1,17 @@ class Solution { public: int maxWidthOfVerticalArea(vector>& points) { - + int n = points.size(); - int ans = 0; - sort(points.begin(), points.end()); - vector v; - - for(int i = 0; i < n; ++i) - { - v.push_back(points[i][0]); - } - + int ans = 0; - for(int i = 0; i < n; ++i) - { - int x1 = v[i]; - int idx = upper_bound(v.begin(), v.end(), v[i]) - v.begin(); - - if(idx == n) - continue; - - int x2 = v[idx]; - - // cout< Date: Fri, 22 Dec 2023 00:06:26 +0530 Subject: [PATCH 2301/3167] Update README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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) From 6411c931f5023fd1af44389e3c34b09cbd6979dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:53:35 +0530 Subject: [PATCH 2302/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1422-maximum-score-after-splitting-a-string/README.md 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 From 56d5147298f77db90966b476b32240b8033bf88b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:53:39 +0530 Subject: [PATCH 2303/3167] Time: 4 ms (23.21%), Space: 6.9 MB (30.36%) - LeetHub --- ...maximum-score-after-splitting-a-string.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp 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..2617543b --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int maxScore(string s) { + + int n = s.size(); + int ans = 0; + + vector suffOne(n, 0); + + suffOne[n-1] = (s[n-1] == '1' ? 1 : 0); + + for(int i = n-2; i >= 1; --i) + { + suffOne[i] = (s[i] == '1' ? suffOne[i+1]+1 : suffOne[i+1]); + + } + + int zeroCnt = 0; + + for(int i = 0; i < n; ++i) + { + if(s[i] == '0' and i != n-1) + ++zeroCnt; + + ans = max(ans, zeroCnt + suffOne[i]); + } + + return ans; + + } +}; \ No newline at end of file From 6483bc6723a858c97d6e444d3dd2ae0b5ed4f9a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:55:04 +0530 Subject: [PATCH 2304/3167] Attach NOTES - LeetHub --- 1422-maximum-score-after-splitting-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1422-maximum-score-after-splitting-a-string/NOTES.md 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 From f6c7b85043d73851d066843568e4b7915b2b82cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:55:07 +0530 Subject: [PATCH 2305/3167] Time: 4 ms (23.21%), Space: 6.9 MB (30.36%) - LeetHub From 9df0ffa098fa49f58a1baa009a22a26ee3d4330f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:56:54 +0530 Subject: [PATCH 2306/3167] Attach NOTES - LeetHub From 6d35ed6f1824e984ddc9312d61d2bfc53d5b09ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:56:58 +0530 Subject: [PATCH 2307/3167] Time: 3 ms (50.89%), Space: 6.6 MB (62.95%) - LeetHub --- ...maximum-score-after-splitting-a-string.cpp | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) 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 index 2617543b..4aa0af90 100644 --- 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 @@ -3,29 +3,19 @@ class Solution { int maxScore(string s) { int n = s.size(); - int ans = 0; - vector suffOne(n, 0); + int ones = count(s.begin(), s.end(), '1'); + + int zero = 0, ans = 0; - suffOne[n-1] = (s[n-1] == '1' ? 1 : 0); - - for(int i = n-2; i >= 1; --i) + for(int i = 0; i < n-1; ++i) { - suffOne[i] = (s[i] == '1' ? suffOne[i+1]+1 : suffOne[i+1]); + if(s[i] == '0') ++zero; + else --ones; - } - - int zeroCnt = 0; - - for(int i = 0; i < n; ++i) - { - if(s[i] == '0' and i != n-1) - ++zeroCnt; - - ans = max(ans, zeroCnt + suffOne[i]); + ans = max(ans, zero + ones); } return ans; - } }; \ No newline at end of file From c01ccecc914d3e12f8026da43d6e24020a56500f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 23:47:05 +0530 Subject: [PATCH 2308/3167] Create README - LeetHub --- 0091-decode-ways/README.md | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0091-decode-ways/README.md 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 From 1efded292389aeab2c249ef2243863d65fadee89 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 23:47:08 +0530 Subject: [PATCH 2309/3167] Time: 0 ms (100.00%), Space: 6.8 MB (29.13%) - LeetHub --- 0091-decode-ways/0091-decode-ways.cpp | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0091-decode-ways/0091-decode-ways.cpp diff --git a/0091-decode-ways/0091-decode-ways.cpp b/0091-decode-ways/0091-decode-ways.cpp new file mode 100644 index 00000000..149b5847 --- /dev/null +++ b/0091-decode-ways/0091-decode-ways.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + + int helper(int idx, int n, string& str, vector& dp) + { + if(idx == n) + { + return 1; + } + + if(str[idx] == '0') + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int first = 0, second = 0; + + if(idx + 2 <= n) + { + string curr = str.substr(idx, 2); + + int num = stoi(curr); + + if(num >= 1 and num <= 26) + { + first = helper(idx+2, n, str, dp); + } + } + + int num = str[idx] - '0'; + + if(num >= 1 and num <= 26) + { + second = helper(idx+1, n, str, 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 From 5ace66e435a3664b65a937f1f7fa6d2dbe2e7f83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:58:41 +0530 Subject: [PATCH 2310/3167] Create README - LeetHub --- 1496-path-crossing/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1496-path-crossing/README.md 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 From 0d9be1411704c493203e260fe3d07f80a482bda6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:58:44 +0530 Subject: [PATCH 2311/3167] Time: 0 ms (100.00%), Space: 7.1 MB (93.92%) - LeetHub --- 1496-path-crossing/1496-path-crossing.cpp | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1496-path-crossing/1496-path-crossing.cpp 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 From 3af949c9a2e5f64bb3df64245401ce13a16b56d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:59:15 +0530 Subject: [PATCH 2312/3167] Attach NOTES - LeetHub --- 1496-path-crossing/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1496-path-crossing/NOTES.md 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 From 779a94a093e1373c52b53a9701f615b53c5c52b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:59:18 +0530 Subject: [PATCH 2313/3167] Time: 0 ms (100.00%), Space: 7.1 MB (93.92%) - LeetHub From c91318612b24b391704268e923c2d0c46dd43852 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:52:21 +0530 Subject: [PATCH 2314/3167] Create README - LeetHub --- 0827-making-a-large-island/README.md | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0827-making-a-large-island/README.md 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 From 0d4dd20fb9f28422d07c2b89ba1bf9d20606c696 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:52:21 +0530 Subject: [PATCH 2315/3167] Attach NOTES - LeetHub --- 0827-making-a-large-island/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0827-making-a-large-island/NOTES.md 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 From f7c8f0d2867d6bb33718d1a7736f56b6f30b18ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:52:25 +0530 Subject: [PATCH 2316/3167] Time: 371 ms (64.16%), Space: 93.5 MB (95.28%) - LeetHub --- .../0827-making-a-large-island.cpp | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 0827-making-a-large-island/0827-making-a-large-island.cpp 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 From 7fa4b6fdc06c39960092ae611159575e7730dcd6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 22:29:26 +0530 Subject: [PATCH 2317/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1758-minimum-changes-to-make-alternating-binary-string/README.md 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 From 723ce98654e40ab9767d6e7699f88549806bd07a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 22:29:30 +0530 Subject: [PATCH 2318/3167] Time: 6 ms (35.61%), Space: 7.2 MB (86.65%) - LeetHub --- ...nges-to-make-alternating-binary-string.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp 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..98bd5d13 --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minOperations(string s) { + + int zcnt = 0, ocnt = 0; + for(int i = 0; i Date: Mon, 25 Dec 2023 08:31:24 +0530 Subject: [PATCH 2319/3167] Attach NOTES - LeetHub --- 1758-minimum-changes-to-make-alternating-binary-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1758-minimum-changes-to-make-alternating-binary-string/NOTES.md 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 From e841248cb4e483ae7c9ed82c7b1fc450112baa07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 08:31:26 +0530 Subject: [PATCH 2320/3167] Time: 9 ms (8.38%), Space: 8.2 MB (5.99%) - LeetHub --- ...nges-to-make-alternating-binary-string.cpp | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) 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 index 98bd5d13..636a953e 100644 --- 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 @@ -1,25 +1,30 @@ class Solution { public: int minOperations(string s) { - - int zcnt = 0, ocnt = 0; - for(int i = 0; i Date: Mon, 25 Dec 2023 11:47:34 +0530 Subject: [PATCH 2321/3167] Create README - LeetHub --- 0069-sqrtx/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0069-sqrtx/README.md 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 From 10f7851609b9b0d9f247e4ed04ae0e0d6a479abb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 11:47:37 +0530 Subject: [PATCH 2322/3167] Time: 0 ms (100.00%), Space: 6.3 MB (73.43%) - LeetHub --- 0069-sqrtx/0069-sqrtx.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0069-sqrtx/0069-sqrtx.cpp 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 From a1a8dcc678a9a6c1d63a1aca919ae3db835fd6bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:03:57 +0530 Subject: [PATCH 2323/3167] Attach NOTES - LeetHub --- 0069-sqrtx/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0069-sqrtx/NOTES.md 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 From 3000740e10c72a19a33726b01453830837a4dfdd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:04:00 +0530 Subject: [PATCH 2324/3167] Time: 0 ms (100.00%), Space: 6.3 MB (73.43%) - LeetHub From d5bef42a09df9caaccae1acbdfe025f48334d31b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:05:28 +0530 Subject: [PATCH 2325/3167] Attach NOTES - LeetHub From e269a31e17e795f01a39dce47eabca69e7b13b04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:05:31 +0530 Subject: [PATCH 2326/3167] Time: 0 ms (100.00%), Space: 6.3 MB (73.43%) - LeetHub From 83955a84c7c8343e725950153cfcd8a01ce19681 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 22:54:34 +0530 Subject: [PATCH 2327/3167] Attach NOTES - LeetHub --- 0091-decode-ways/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0091-decode-ways/NOTES.md 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 From 9b99901da3a5675bd912eabc3da67ca2bf92ca43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 22:54:36 +0530 Subject: [PATCH 2328/3167] Time: 0 ms (100.00%), Space: 6.5 MB (65.44%) - LeetHub --- 0091-decode-ways/0091-decode-ways.cpp | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/0091-decode-ways/0091-decode-ways.cpp b/0091-decode-ways/0091-decode-ways.cpp index 149b5847..ce5333e9 100644 --- a/0091-decode-ways/0091-decode-ways.cpp +++ b/0091-decode-ways/0091-decode-ways.cpp @@ -1,14 +1,12 @@ class Solution { public: - int helper(int idx, int n, string& str, vector& dp) - { + int helper(int idx, int n, string& s, vector& dp) + { if(idx == n) - { return 1; - } - if(str[idx] == '0') + if(s[idx] == '0') return 0; if(dp[idx] != -1) @@ -18,22 +16,18 @@ class Solution { if(idx + 2 <= n) { - string curr = str.substr(idx, 2); - - int num = stoi(curr); + string curr = s.substr(idx, 2); - if(num >= 1 and num <= 26) + if(stoi(curr) >= 1 and stoi(curr) <= 26) { - first = helper(idx+2, n, str, dp); + first = helper(idx + 2, n, s, dp); } } - int num = str[idx] - '0'; + int ch = s[idx] - '0'; - if(num >= 1 and num <= 26) - { - second = helper(idx+1, n, str, dp); - } + if(ch >= 1 and ch <= 26) + second = helper(idx + 1,n, s, dp); return dp[idx] = first + second; } From 19ab4d74e5eb045f27bddfe9bd27f70375315d41 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:54:26 +0530 Subject: [PATCH 2329/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1155-number-of-dice-rolls-with-target-sum/README.md 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 From e7ea87ec50b884896a9f48763ad85ee86df825ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:54:26 +0530 Subject: [PATCH 2330/3167] Attach NOTES - LeetHub --- 1155-number-of-dice-rolls-with-target-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1155-number-of-dice-rolls-with-target-sum/NOTES.md 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 From 0ba46f5884141433cd66c9c8bfdc86c14752e46d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:54:30 +0530 Subject: [PATCH 2331/3167] Time: 26 ms (53.55%), Space: 9.1 MB (29.50%) - LeetHub --- ...5-number-of-dice-rolls-with-target-sum.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp 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..57738d20 --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int idx, int n, int k, int target, vector>& dp) + { + if(idx == n) + return (target == 0); + + if(dp[idx][target] != -1) + return dp[idx][target]; + + int ways = 0; + + for(int i = 1; i <= k; ++i) + { + if(i <= target) + ways = (ways + helper(idx+1, n, k, target - i, dp))%mod; + + } + + return dp[idx][target] = ways; + } + + int numRollsToTarget(int n, int k, int target) { + + vector> dp(n+1, vector(target+1, -1)); + + return helper(0, n, k, target, dp); + } +}; \ No newline at end of file From 56cc286d4699db36ee337647f7fab491704173ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:55:11 +0530 Subject: [PATCH 2332/3167] Attach NOTES - LeetHub From cd7b8eb5349651435a48c6576666898885e0bf59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:55:14 +0530 Subject: [PATCH 2333/3167] Time: 26 ms (53.55%), Space: 9.1 MB (29.50%) - LeetHub From ed14d60d2eb483b41be9ae8fabd3d86f2f797a8e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:58:38 +0530 Subject: [PATCH 2334/3167] Attach NOTES - LeetHub From c50057d67dba69ef5141c2ceba163632d457159b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:58:42 +0530 Subject: [PATCH 2335/3167] Time: 26 ms (53.55%), Space: 9.1 MB (29.50%) - LeetHub From bbc480af98f167a8f6756770a3e31e53172b138a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:35:28 +0530 Subject: [PATCH 2336/3167] Attach NOTES - LeetHub From ea20c4336f44b82034a662012e50851705077f11 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:35:31 +0530 Subject: [PATCH 2337/3167] Time: 73 ms (13.06%), Space: 8.8 MB (65.29%) - LeetHub --- ...5-number-of-dice-rolls-with-target-sum.cpp | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) 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 index 57738d20..af9c24b2 100644 --- 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 @@ -1,32 +1,26 @@ class Solution { public: - - const int mod = 1e9+7; - - int helper(int idx, int n, int k, int target, vector>& dp) - { - if(idx == n) - return (target == 0); + int numRollsToTarget(int n, int k, int target) { + + const int mod = 1e9+7; - if(dp[idx][target] != -1) - return dp[idx][target]; + vector> dp(n+1, vector(target+1, 0)); - int ways = 0; + dp[0][0] = 1; - for(int i = 1; i <= k; ++i) + for(int i = 1; i <= n; ++i) { - if(i <= target) - ways = (ways + helper(idx+1, n, k, target - i, dp))%mod; - + 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[idx][target] = ways; - } - - int numRollsToTarget(int n, int k, int target) { + return dp[n][target]; - vector> dp(n+1, vector(target+1, -1)); - - return helper(0, n, k, target, dp); } }; \ No newline at end of file From c2d1f26fa623b0598fc3eebdc607e28749b90b00 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:03:23 +0530 Subject: [PATCH 2338/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1578-minimum-time-to-make-rope-colorful/README.md 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 From 581c53e79ccd01a1bfc731edff8084e26c14341a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:03:24 +0530 Subject: [PATCH 2339/3167] Attach NOTES - LeetHub --- 1578-minimum-time-to-make-rope-colorful/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1578-minimum-time-to-make-rope-colorful/NOTES.md 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 From 4660a4b63c523b33f60dc55d942af9adab37653c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:03:27 +0530 Subject: [PATCH 2340/3167] Time: 118 ms (61.10%), Space: 95.7 MB (49.48%) - LeetHub --- ...578-minimum-time-to-make-rope-colorful.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp 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 From 2a84bc35e58f64003b16b95fbff8858db1031a86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:05:33 +0530 Subject: [PATCH 2341/3167] Attach NOTES - LeetHub From aee7fc5a67ab3c3360079ee446ef8400ed1969f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:05:36 +0530 Subject: [PATCH 2342/3167] Time: 118 ms (61.10%), Space: 95.7 MB (49.48%) - LeetHub From 5ec03a16bd7b7653adbf23402b8299f6602b7a4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 01:29:49 +0530 Subject: [PATCH 2343/3167] Create README - LeetHub --- 1531-string-compression-ii/README.md | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1531-string-compression-ii/README.md 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 From d97d7a8bef3f4776c547077eee234f04dfa3c256 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 01:29:53 +0530 Subject: [PATCH 2344/3167] Time: 26 ms (87.64%), Space: 6.6 MB (92.13%) - LeetHub --- .../1531-string-compression-ii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1531-string-compression-ii/1531-string-compression-ii.cpp 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); + } +}; From 700f1d3a33737e449e231b0c9800f96f3180780f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:39:09 +0530 Subject: [PATCH 2345/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1335-minimum-difficulty-of-a-job-schedule/README.md 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 From 750f69e6bc3394b6992a998a1cf713d49e8d910b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:39:12 +0530 Subject: [PATCH 2346/3167] Time: 69 ms (32.29%), Space: 7.6 MB (78.07%) - LeetHub --- ...5-minimum-difficulty-of-a-job-schedule.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp 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 From 7ea3fea6257832205d07f455b186102c60a8e700 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:28:18 +0530 Subject: [PATCH 2347/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1897-redistribute-characters-to-make-all-strings-equal/README.md 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 From 7a1fe94b34abd26322a41e98af66ba78ade8d098 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:28:19 +0530 Subject: [PATCH 2348/3167] Attach NOTES - LeetHub --- 1897-redistribute-characters-to-make-all-strings-equal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1897-redistribute-characters-to-make-all-strings-equal/NOTES.md 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 From b7a3ad77bce828038e9603aaf754a2d48893e10d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:28:22 +0530 Subject: [PATCH 2349/3167] Time: 16 ms (47.86%), Space: 12.1 MB (68.09%) - LeetHub --- ...e-characters-to-make-all-strings-equal.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp 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 From b42f75a1b759e19650288725752c80470a68ae03 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 13:21:01 +0530 Subject: [PATCH 2350/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2982-find-longest-special-substring-that-occurs-thrice-ii/README.md 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 From c390d27298aa170981cd7498c23e957b4e55c19f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 13:21:05 +0530 Subject: [PATCH 2351/3167] Time: 2014 ms (20.00%), Space: 269.2 MB (80.00%) - LeetHub --- ...pecial-substring-that-occurs-thrice-ii.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp 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 From 860d1f2750748e99f65bbe725507101c59562847 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:35:15 +0530 Subject: [PATCH 2352/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1624-largest-substring-between-two-equal-characters/README.md 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 From f39049a7cdd2f606fcff0eab9e611ae3760147d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:35:16 +0530 Subject: [PATCH 2353/3167] Attach NOTES - LeetHub --- 1624-largest-substring-between-two-equal-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1624-largest-substring-between-two-equal-characters/NOTES.md 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 From 994b30d530d7e95013154e2b1ce55dd6cd540a39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:35:19 +0530 Subject: [PATCH 2354/3167] Time: 6 ms (5.74%), Space: 6.9 MB (11.97%) - LeetHub --- ...substring-between-two-equal-characters.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp 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 From 133514fce84ca0997a90fa0d49af130f3a1cfe98 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jan 2024 10:39:49 +0530 Subject: [PATCH 2355/3167] Create README - LeetHub --- 0455-assign-cookies/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0455-assign-cookies/README.md 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 From 3ffbc7f5f80ebbaf0216c52c71377c58d5672740 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jan 2024 10:39:53 +0530 Subject: [PATCH 2356/3167] Time: 23 ms (61.11%), Space: 17.7 MB (98.96%) - LeetHub --- 0455-assign-cookies/0455-assign-cookies.cpp | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0455-assign-cookies/0455-assign-cookies.cpp 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 From 8733ff398d8793979d56d2234be84bbfc6c290e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:46:17 +0530 Subject: [PATCH 2357/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2610-convert-an-array-into-a-2d-array-with-conditions/README.md 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 From 07cf7bf021df7f13444b4e3db25c91e703aea9be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:46:17 +0530 Subject: [PATCH 2358/3167] Attach NOTES - LeetHub --- 2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md 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 From 7e579e35dccfbb72688c9748a33bdd6a60080a3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:46:23 +0530 Subject: [PATCH 2359/3167] Time: 23 ms (9.84%), Space: 30.5 MB (26.85%) - LeetHub --- ...-array-into-a-2d-array-with-conditions.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp 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 From 460a64ac992ef0536a3f585dfc823f84859c422d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:47:14 +0530 Subject: [PATCH 2360/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2125-number-of-laser-beams-in-a-bank/README.md 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 From baf38edae18c48bbd7cd4fd4bbdf92ffec18e5e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:47:18 +0530 Subject: [PATCH 2361/3167] Time: 91 ms (54.49%), Space: 23.3 MB (96.49%) - LeetHub --- .../2125-number-of-laser-beams-in-a-bank.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp 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 From d921074ab76f8638cccc844524da4ea291b17d06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:15 +0530 Subject: [PATCH 2362/3167] Attach NOTES - LeetHub --- 2125-number-of-laser-beams-in-a-bank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2125-number-of-laser-beams-in-a-bank/NOTES.md 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 From f8b88d3530bf92bf925e400075c8adb05bd7e56a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:19 +0530 Subject: [PATCH 2363/3167] Time: 91 ms (54.49%), Space: 23.3 MB (96.49%) - LeetHub From 03fd7d5e33f856b51984bf4d91136cb1f524b43e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:43 +0530 Subject: [PATCH 2364/3167] Attach NOTES - LeetHub From dcc311ba9751b496c2b30b53e812d5ddf019be0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:47 +0530 Subject: [PATCH 2365/3167] Time: 91 ms (54.49%), Space: 23.3 MB (96.49%) - LeetHub From c1e2dde3df19dd70c88fc7688197bef4cbbf4527 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jan 2024 22:48:11 +0530 Subject: [PATCH 2366/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2870-minimum-number-of-operations-to-make-array-empty/README.md 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 From 18173da209fa9dd392bd9147d4e23c9c3f5ff54a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jan 2024 22:48:14 +0530 Subject: [PATCH 2367/3167] Time: 136 ms (24.86%), Space: 85.5 MB (31.49%) - LeetHub --- ...mber-of-operations-to-make-array-empty.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp 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 From 46f0cc853c4be6c037ccb102c1aa3efff021a774 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:31:24 +0530 Subject: [PATCH 2368/3167] Create README - LeetHub --- 0324-wiggle-sort-ii/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0324-wiggle-sort-ii/README.md 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 From 7161944ff926f1d49edbf2bb7ae24ff3e0bb2c19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:31:24 +0530 Subject: [PATCH 2369/3167] Attach NOTES - LeetHub --- 0324-wiggle-sort-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0324-wiggle-sort-ii/NOTES.md 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 From a5044e7d0e15fb340e0aaa5ab91db1b32a819de1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:31:27 +0530 Subject: [PATCH 2370/3167] Time: 20 ms (38.64%), Space: 18.2 MB (29.97%) - LeetHub --- 0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp 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 From cd12829e48a0ede3e8402bad32dce3e7f4364a16 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:37:35 +0530 Subject: [PATCH 2371/3167] Attach NOTES - LeetHub From 38ab38572c95c982a87538392cb5f348300ff761 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:37:38 +0530 Subject: [PATCH 2372/3167] Time: 20 ms (38.64%), Space: 18.2 MB (29.97%) - LeetHub From 778fbe1f65fd98d9ccfdc82cefe9af7971ba8b1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 10:58:27 +0530 Subject: [PATCH 2373/3167] Attach NOTES - LeetHub From d601e510a99f576a01fae200608078fbabba4add Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 10:58:31 +0530 Subject: [PATCH 2374/3167] Time: 20 ms (38.64%), Space: 18.2 MB (29.97%) - LeetHub From cab06b751450c27300f00201427243c4a19f5d55 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:30:13 +0530 Subject: [PATCH 2375/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1235-maximum-profit-in-job-scheduling/README.md 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 From b9e2d23cd395d7fa57c379819f14dad2070e9d6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:30:17 +0530 Subject: [PATCH 2376/3167] Time: 196 ms (48.75%), Space: 83 MB (31.69%) - LeetHub --- .../1235-maximum-profit-in-job-scheduling.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp 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 From bdcf78f6fae2a117636de924d9721cfa5d8a86bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jan 2024 20:28:00 +0530 Subject: [PATCH 2377/3167] Attach NOTES - LeetHub From 6e429967fb1cd9bf96de832ef0e7345b3c210a4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jan 2024 21:46:18 +0530 Subject: [PATCH 2378/3167] Attach NOTES - LeetHub --- 0698-partition-to-k-equal-sum-subsets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0698-partition-to-k-equal-sum-subsets/NOTES.md 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 From 7b528db9c6d7e7ed426bbe8a6b66e990d5220da7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Jan 2024 21:30:30 +0530 Subject: [PATCH 2379/3167] Attach NOTES - LeetHub --- 0004-median-of-two-sorted-arrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0004-median-of-two-sorted-arrays/NOTES.md 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 From c73705d4c79d02fb62a3164ec4181355b98beaa2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:34:43 +0530 Subject: [PATCH 2380/3167] Attach NOTES - LeetHub From bea8877ce110c3d0d801b1d4622fc6df0787b12a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 08:31:37 +0530 Subject: [PATCH 2381/3167] Attach NOTES - LeetHub --- 1539-kth-missing-positive-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1539-kth-missing-positive-number/NOTES.md 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 From 557fc29278d23d11b6e7e1b300dbe89cd142d6a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:40:46 +0530 Subject: [PATCH 2382/3167] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1482-minimum-number-of-days-to-make-m-bouquets/README.md 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 From 60a45d4895d72d4d43c4eaed6570c313db2f7868 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:40:46 +0530 Subject: [PATCH 2383/3167] Attach NOTES - LeetHub --- 1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md 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 From 36d8619ddb95109e9973115fe8ab2b68e35eda93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:40:55 +0530 Subject: [PATCH 2384/3167] Time: 159 ms (23.17%), Space: 66.8 MB (76.25%) - LeetHub --- ...imum-number-of-days-to-make-m-bouquets.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp 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..b9454582 --- /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,53 @@ +class Solution { +public: + int minDays(vector& bloomDay, int m, int k) { + + int n = bloomDay.size(); + + if((long long)k * m > n) + return -1; + + int low = *min_element(bloomDay.begin(), bloomDay.end()); + int high = *max_element(bloomDay.begin(), bloomDay.end()); + + auto canBloom = [&](int mid) + { + int bloom = 0; + int cnt = 0; + for(int i = 0; i < n; ++i) + { + if(bloomDay[i] <= mid) + { + ++cnt; + } + else + { + bloom += (cnt / k); + cnt = 0; + } + } + + bloom += (cnt / k); + + return bloom; + }; + + int ans = -1; + + while(low <= high) + { + int mid = low + (high - low) / 2; + + if(canBloom(mid) >= m) + { + ans = mid; + high = mid-1; + } + else + low = mid+1; + } + + return ans; + + } +}; \ No newline at end of file From 4bef233a9416b9dcb8d06d94df335e4314ec8426 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:01:22 +0530 Subject: [PATCH 2385/3167] Create README - LeetHub --- 0162-find-peak-element/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0162-find-peak-element/README.md 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 From 6d3d78de0b98b4614791e334c794f83bcb1e1854 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:01:23 +0530 Subject: [PATCH 2386/3167] Attach NOTES - LeetHub --- 0162-find-peak-element/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0162-find-peak-element/NOTES.md 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 From 43af40764102c1fd02b363535e80cbde761273ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:01:26 +0530 Subject: [PATCH 2387/3167] Time: 3 ms (70.23%), Space: 9.3 MB (35.78%) - LeetHub --- .../0162-find-peak-element.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0162-find-peak-element/0162-find-peak-element.cpp 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 From 135631649c9a43d4ce28f8952e5f48ce8d306588 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:02:32 +0530 Subject: [PATCH 2388/3167] Create README - LeetHub --- 0872-leaf-similar-trees/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0872-leaf-similar-trees/README.md 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 From 384fca62a7d2b5b0c419296851fb3cc18775008c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:23:19 +0530 Subject: [PATCH 2389/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2385-amount-of-time-for-binary-tree-to-be-infected/README.md 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 From 2c1712c52582a2459a36ca4e956c16c02c527420 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:23:23 +0530 Subject: [PATCH 2390/3167] Time: 763 ms (10.98%), Space: 185.8 MB (25.66%) - LeetHub --- ...of-time-for-binary-tree-to-be-infected.cpp | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp 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 From 205f22fdb8002bbce33a4be738ff7036425ba0fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Jan 2024 22:09:49 +0530 Subject: [PATCH 2391/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1026-maximum-difference-between-node-and-ancestor/README.md 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 From 7c41b2dd14774b0e06d075a784b9fd9fdb1df078 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Jan 2024 22:09:52 +0530 Subject: [PATCH 2392/3167] Time: 3 ms (85.41%), Space: 10.1 MB (59.95%) - LeetHub --- ...m-difference-between-node-and-ancestor.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp 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 From f23a787447527e62fd45d8cd6e1932a81eb52e8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:19:15 +0530 Subject: [PATCH 2393/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md 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 From 5c131870428e178f580be406fec17c793d501833 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:19:18 +0530 Subject: [PATCH 2394/3167] Time: 145 ms (11.64%), Space: 17.1 MB (19.43%) - LeetHub --- ...r-of-steps-to-make-two-strings-anagram.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp 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 From 689688c70a1e0ecf1a0fde5f48ceb14b39371a6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:20:02 +0530 Subject: [PATCH 2395/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md 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 From 84a6b7cd0ff2fd34423277bc1c1768affbeddd2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:20:05 +0530 Subject: [PATCH 2396/3167] Time: 145 ms (11.64%), Space: 17.1 MB (19.43%) - LeetHub From 15c7259c1ea9a639099c8b5a10c37294e977bba6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:21:48 +0530 Subject: [PATCH 2397/3167] Attach NOTES - LeetHub From 2f8dc68d9631ced74db6331cf50e4491b8f25eab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:21:51 +0530 Subject: [PATCH 2398/3167] Time: 145 ms (11.64%), Space: 17.1 MB (19.43%) - LeetHub From 944fa9c262ce494cc042a18cecab1eb945bdfc91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 Jan 2024 21:37:59 +0530 Subject: [PATCH 2399/3167] Attach NOTES - LeetHub --- 1657-determine-if-two-strings-are-close/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1657-determine-if-two-strings-are-close/NOTES.md 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 From 0839393e0ca8ca8a17c76b65183168da25edd030 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:42:43 +0530 Subject: [PATCH 2400/3167] Attach NOTES - LeetHub --- 0380-insert-delete-getrandom-o1/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0380-insert-delete-getrandom-o1/NOTES.md 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 From 185c4502ff1bda2da25f023e4820eab9647af220 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:43:43 +0530 Subject: [PATCH 2401/3167] Attach NOTES - LeetHub From 3cbce3ed511e57bd39f9396070a75d13c111bcb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jan 2024 08:28:01 +0530 Subject: [PATCH 2402/3167] Attach NOTES - LeetHub --- 1207-unique-number-of-occurrences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1207-unique-number-of-occurrences/NOTES.md 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 From a3702da2ae61784a0eae3b9d86065e65195ab7fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:56:39 +0530 Subject: [PATCH 2403/3167] Create README - LeetHub --- 0070-climbing-stairs/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0070-climbing-stairs/README.md 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 From d94abc6377dbbd47f1ed4bd668a0331eb913fc66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:56:43 +0530 Subject: [PATCH 2404/3167] Time: 0 ms (100.00%), Space: 6.4 MB (59.54%) - LeetHub --- 0070-climbing-stairs/0070-climbing-stairs.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0070-climbing-stairs/0070-climbing-stairs.cpp 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 From 5684bfab4a433c66e05e01a59975c0d5349ae419 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:41:55 +0530 Subject: [PATCH 2405/3167] Create README - LeetHub --- 0931-minimum-falling-path-sum/README.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0931-minimum-falling-path-sum/README.md diff --git a/0931-minimum-falling-path-sum/README.md b/0931-minimum-falling-path-sum/README.md new file mode 100644 index 00000000..64228ea5 --- /dev/null +++ b/0931-minimum-falling-path-sum/README.md @@ -0,0 +1,28 @@ +

931. 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 From 7b9b9d69cbfafe0b57c4747ccf9b26beaecde5ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:41:55 +0530 Subject: [PATCH 2406/3167] Attach NOTES - LeetHub --- 0931-minimum-falling-path-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0931-minimum-falling-path-sum/NOTES.md diff --git a/0931-minimum-falling-path-sum/NOTES.md b/0931-minimum-falling-path-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0931-minimum-falling-path-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4532228220a08e2a300433ff7208c257299341a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:41:59 +0530 Subject: [PATCH 2407/3167] Time: 11 ms (80.62%), Space: 13.2 MB (5.08%) - LeetHub --- .../0931-minimum-falling-path-sum.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp 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; col Date: Sun, 21 Jan 2024 21:38:53 +0530 Subject: [PATCH 2408/3167] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0987-vertical-order-traversal-of-a-binary-tree/README.md 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 From 399cd5de495e9761bd47b135df6742a790ccfba0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jan 2024 21:38:57 +0530 Subject: [PATCH 2409/3167] Time: 9 ms (16.40%), Space: 14.8 MB (14.12%) - LeetHub --- ...tical-order-traversal-of-a-binary-tree.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp 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 From a091e0ab3f818b170d26de5b84ecb1ec004b75a2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:29 +0530 Subject: [PATCH 2410/3167] Create README - LeetHub --- 0645-set-mismatch/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0645-set-mismatch/README.md 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 From 333c97e57b81642c53a90c5b2ceab0253b337f01 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:33 +0530 Subject: [PATCH 2411/3167] Time: 19 ms (89.24%), Space: 23.7 MB (36.81%) - LeetHub --- 0645-set-mismatch/0645-set-mismatch.cpp | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0645-set-mismatch/0645-set-mismatch.cpp 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 From 7f65b82893fe8ba8e27db89b75d02c118f46efe6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:39 +0530 Subject: [PATCH 2412/3167] Attach NOTES - LeetHub --- 0645-set-mismatch/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0645-set-mismatch/NOTES.md 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 From a05cf7300458fce66fbcc4cac3b8798cb0d14aea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:32:30 +0530 Subject: [PATCH 2413/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md 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 From 43858fd8257472fc4be6c27c345fd135149522b4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:32:34 +0530 Subject: [PATCH 2414/3167] Time: 166 ms (35.99%), Space: 62.5 MB (35.27%) - LeetHub --- ...concatenated-string-with-unique-characters.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp 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..081c955c --- /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,15 @@ +class Solution { +public: + int maxLength(vector& arr, string str = "", int index = 0) { + + unordered_sets(str.begin(), str.end()); + if (s.size() != ((int)str.length())) + return 0; + + int ret = str.length(); + for (int i = index; i < arr.size(); i++) + ret = max(ret, maxLength(arr, str+arr[i], i+1)); + + return ret; + } +}; \ No newline at end of file From 6d0a1bd1fdc622360c61656566dc312e71d380ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:48:02 +0530 Subject: [PATCH 2415/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1457-pseudo-palindromic-paths-in-a-binary-tree/README.md 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 From 8b512b8c4fb6ca2277d696d8f5214772b527fc17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:48:08 +0530 Subject: [PATCH 2416/3167] Time: 1988 ms (5.09%), Space: 448.5 MB (8.04%) - LeetHub --- ...udo-palindromic-paths-in-a-binary-tree.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp 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 From 0e323e5928c632e487ffce7e028fc20820f6ed3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jan 2024 19:21:19 +0530 Subject: [PATCH 2417/3167] Attach NOTES - LeetHub --- 1143-longest-common-subsequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1143-longest-common-subsequence/NOTES.md 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 From a15d0d194685f9e971748306eaa3a2cd847c341d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:24:05 +0530 Subject: [PATCH 2418/3167] Create README - LeetHub --- 0576-out-of-boundary-paths/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0576-out-of-boundary-paths/README.md 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 From 72bc031e708131dd498dce808505fea1073cf352 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:24:06 +0530 Subject: [PATCH 2419/3167] Attach NOTES - LeetHub --- 0576-out-of-boundary-paths/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0576-out-of-boundary-paths/NOTES.md 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 From 7f00936f3f616f903a65de2a270c51547b6c6445 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:24:09 +0530 Subject: [PATCH 2420/3167] Time: 5 ms (77.51%), Space: 7.8 MB (57.43%) - LeetHub --- .../0576-out-of-boundary-paths.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp 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 From c2722da4f22ab60c10078b395433173312dc10e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 21:24:49 +0530 Subject: [PATCH 2421/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md 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 From 9d00210669032aaf39b4bbbce181e178f0f2cc49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 21:24:53 +0530 Subject: [PATCH 2422/3167] Time: 862 ms (5.01%), Space: 540.5 MB (5.01%) - LeetHub --- ...atenated-string-with-unique-characters.cpp | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) 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 index 081c955c..dfde3d51 100644 --- 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 @@ -1,15 +1,58 @@ class Solution { public: - int maxLength(vector& arr, string str = "", int index = 0) { - - unordered_sets(str.begin(), str.end()); - if (s.size() != ((int)str.length())) - return 0; - - int ret = str.length(); - for (int i = index; i < arr.size(); i++) - ret = max(ret, maxLength(arr, str+arr[i], i+1)); + + 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); - return ret; + 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 From 791db53f7625c121f391a3cc1a91624ebae764e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:19:00 +0530 Subject: [PATCH 2423/3167] Create README - LeetHub --- 0629-k-inverse-pairs-array/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0629-k-inverse-pairs-array/README.md 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 From 7dab5db81ffb164f290cbc02e21d13cb13a139e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:19:04 +0530 Subject: [PATCH 2424/3167] Time: 15 ms (66.48%), Space: 17.5 MB (13.19%) - LeetHub --- .../0629-k-inverse-pairs-array.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp 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]; + } +}; From fa5ea8838a4a7d3a5657b9adb992d24977144d3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:18:47 +0530 Subject: [PATCH 2425/3167] Attach NOTES - LeetHub --- 1074-number-of-submatrices-that-sum-to-target/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1074-number-of-submatrices-that-sum-to-target/NOTES.md 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 From 61830fea462114c7bcc127ccbedfe481853af7e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:18:52 +0530 Subject: [PATCH 2426/3167] Time: 678 ms (49.88%), Space: 257 MB (18.43%) - LeetHub --- ...mber-of-submatrices-that-sum-to-target.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp 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 From c7ac455f3bd688bb9e4beede8be8c448e72930a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 Jan 2024 09:40:30 +0530 Subject: [PATCH 2427/3167] Attach NOTES - LeetHub --- 0150-evaluate-reverse-polish-notation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0150-evaluate-reverse-polish-notation/NOTES.md 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 From b07f91cc877a017fb09c8c2f4ee2d1491ff43b23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:12:52 +0530 Subject: [PATCH 2428/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2966-divide-array-into-arrays-with-max-difference/README.md 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 From 64567becb732b9842814719449a0f8d22314d25e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:12:56 +0530 Subject: [PATCH 2429/3167] Time: 232 ms (23.66%), Space: 142.3 MB (7.82%) - LeetHub --- ...-array-into-arrays-with-max-difference.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp 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 From 4e0a66b17acf519cd890ec0449c00c3e5b6cce85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:14:06 +0530 Subject: [PATCH 2430/3167] Attach NOTES - LeetHub --- 2966-divide-array-into-arrays-with-max-difference/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2966-divide-array-into-arrays-with-max-difference/NOTES.md 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 From 8ebb66c3683dd626e6082876cbc221dac00eb282 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:14:10 +0530 Subject: [PATCH 2431/3167] Time: 232 ms (23.66%), Space: 142.3 MB (7.82%) - LeetHub From 5e61ed21d0510eae65f87e0b3e755f1ddada6574 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Feb 2024 20:30:28 +0530 Subject: [PATCH 2432/3167] Attach NOTES - LeetHub --- 1291-sequential-digits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1291-sequential-digits/NOTES.md 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 From be9c5f472b26a70fcfeb035e57fafbbca9fe123a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Feb 2024 20:30:31 +0530 Subject: [PATCH 2433/3167] Time: 0 ms (100.00%), Space: 7.4 MB (9.62%) - LeetHub --- .../1291-sequential-digits.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1291-sequential-digits/1291-sequential-digits.cpp 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 From 1c3b775d660dfdbdbc7aef635d2e413e4543d4d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:29:29 +0530 Subject: [PATCH 2434/3167] Create README - LeetHub --- 0076-minimum-window-substring/README.md | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0076-minimum-window-substring/README.md 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 From cbbf5953b713c0c6eada741de398f83bda2258dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:29:32 +0530 Subject: [PATCH 2435/3167] Time: 222 ms (5.03%), Space: 372.4 MB (5.28%) - LeetHub --- .../0076-minimum-window-substring.cpp | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 0076-minimum-window-substring/0076-minimum-window-substring.cpp 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 From 7ea41d29a696ae9ecb89b8a8fa082608e83a85a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:04 +0530 Subject: [PATCH 2436/3167] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0387-first-unique-character-in-a-string/README.md 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 From 8593544cf15064f0bb66a5db71ecffd9da2eff14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:05 +0530 Subject: [PATCH 2437/3167] Attach NOTES - LeetHub --- 0387-first-unique-character-in-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0387-first-unique-character-in-a-string/NOTES.md 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 From c29f5bae408800cdaeeb95dcbcfe4e5cafb0f198 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:08 +0530 Subject: [PATCH 2438/3167] Time: 56 ms (17.72%), Space: 13.2 MB (10.77%) - LeetHub --- ...387-first-unique-character-in-a-string.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp 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 From 9d726dcec9eac6d7b972198dff71c73025b4ccb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:58 +0530 Subject: [PATCH 2439/3167] Attach NOTES - LeetHub From f49757eb0be568fe6f1bc43a4d1b26e215371dfe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:43:28 +0530 Subject: [PATCH 2440/3167] Create README - LeetHub --- 0049-group-anagrams/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0049-group-anagrams/README.md 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 From c61c8d7e607c19b61e795a831d7bdc3bdf8a2342 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Feb 2024 22:16:28 +0530 Subject: [PATCH 2441/3167] Create README - LeetHub --- 0279-perfect-squares/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0279-perfect-squares/README.md 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 From 3897f76e3c61bdc4b7d3cc4f237e7ba9bd882c2f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:26:40 +0530 Subject: [PATCH 2442/3167] Create README - LeetHub --- 0368-largest-divisible-subset/README.md | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0368-largest-divisible-subset/README.md 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 From e984f0bb2ab0db5062101d13ae8d99eb35635b4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:26:44 +0530 Subject: [PATCH 2443/3167] Time: 18 ms (71.92%), Space: 11.3 MB (40.54%) - LeetHub --- .../0368-largest-divisible-subset.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0368-largest-divisible-subset/0368-largest-divisible-subset.cpp 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 From d91f5ea5477b4357b5d8aaa7de615531e7e65be5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:27:27 +0530 Subject: [PATCH 2444/3167] Create README - LeetHub --- 0647-palindromic-substrings/README.md | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0647-palindromic-substrings/README.md 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 From beac8a8c2f71f1c41a3c7ea762cca531328f63c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:27:31 +0530 Subject: [PATCH 2445/3167] Time: 16 ms (35.55%), Space: 21.6 MB (12.92%) - LeetHub --- .../0647-palindromic-substrings.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0647-palindromic-substrings/0647-palindromic-substrings.cpp 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 From e6c26d1c74afd68d9b3ec8f90072ecc57dfb4adb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:38:47 +0530 Subject: [PATCH 2446/3167] Create README - LeetHub --- .../README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 3035-maximum-palindromes-after-operations/README.md 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 From 115a9bc7a3fb760a087085092b89c2253941c6ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:38:47 +0530 Subject: [PATCH 2447/3167] Attach NOTES - LeetHub --- 3035-maximum-palindromes-after-operations/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3035-maximum-palindromes-after-operations/NOTES.md 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 From df1d1a36cb08586817e46b505d9aeda795842fcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:38:51 +0530 Subject: [PATCH 2448/3167] Time: 81 ms (25.00%), Space: 28.3 MB (37.50%) - LeetHub --- ...5-maximum-palindromes-after-operations.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp 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..9fd03360 --- /dev/null +++ b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp @@ -0,0 +1,64 @@ +class Solution { +public: + + int maxPalindromesAfterOperations(vector& words) { + + int n = words.size(); + + map mp; + + for(auto& word : words) + { + for(auto& ch : word) + ++mp[ch]; + } + + long long ev = 0, odd = 0; + + for(auto& [f,e] : mp) + { + if(e & 1) + { + ev += (e-1); + ++odd; + } + else + ev += e; + } + + int cnt = 0; + + sort(words.begin(), words.end(), [&](const auto& a, const auto& b){ + return a.size() < b.size(); + }); + + for(int i = 0; i < words.size(); ++i) + { + int sz = words[i].size(); + + if(sz & 1) + { + if(odd > 0) + { + --odd; + } + else if(ev >= 2) + { + ++odd; + ev -= 2; + } + --sz; + } + + if(ev >= sz) + { + ev -= sz; + ++cnt; + } + + + } + + return cnt; + } +}; \ No newline at end of file From a5a6fd5e7f1f0cc129c7127f1d19c9d38dd5accc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:48:02 +0530 Subject: [PATCH 2449/3167] Attach NOTES - LeetHub From 9bb974b2a28a7d5119e928962c7f201cef3cc1b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:48:06 +0530 Subject: [PATCH 2450/3167] Time: 67 ms (25.00%), Space: 28.8 MB (37.50%) - LeetHub --- ...5-maximum-palindromes-after-operations.cpp | 51 ++++--------------- 1 file changed, 10 insertions(+), 41 deletions(-) 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 index 9fd03360..da125d45 100644 --- a/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp +++ b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp @@ -1,62 +1,31 @@ class Solution { public: - int maxPalindromesAfterOperations(vector& words) { - int n = words.size(); - - map mp; + vector length; + map mp; for(auto& word : words) { for(auto& ch : word) ++mp[ch]; + length.push_back((int)word.size()/2); } - long long ev = 0, odd = 0; - - for(auto& [f,e] : mp) - { - if(e & 1) - { - ev += (e-1); - ++odd; - } - else - ev += e; - } + int matching = 0, cnt = 0; - int cnt = 0; + for(auto&[f, e] : mp) + matching += e/2; - sort(words.begin(), words.end(), [&](const auto& a, const auto& b){ - return a.size() < b.size(); - }); + sort(length.begin(), length.end()); - for(int i = 0; i < words.size(); ++i) + for(int i = 0; i < length.size(); ++i) { - int sz = words[i].size(); - - if(sz & 1) - { - if(odd > 0) - { - --odd; - } - else if(ev >= 2) - { - ++odd; - ev -= 2; - } - --sz; - } - - if(ev >= sz) + if(matching >= length[i]) { - ev -= sz; + matching -= length[i]; ++cnt; } - - } return cnt; From b8298f8fa39ed0fdfce240c67b265ed51c87417b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 00:43:16 +0530 Subject: [PATCH 2451/3167] Attach NOTES - LeetHub --- 1463-cherry-pickup-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1463-cherry-pickup-ii/NOTES.md 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 From 2bbbfc7d729f64a8e7b162c9096763b7332f0dd1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:03:47 +0530 Subject: [PATCH 2452/3167] Create README - LeetHub --- 0169-majority-element/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0169-majority-element/README.md 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 From 5fd05ed6de1a5b806bc81a2f20f4770dc323e8bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:03:47 +0530 Subject: [PATCH 2453/3167] Attach NOTES - LeetHub --- 0169-majority-element/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0169-majority-element/NOTES.md 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 From 794ea06d80a246f3751f7ec37f03f9e3d0de5e1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:03:50 +0530 Subject: [PATCH 2454/3167] Time: 7 ms (97.54%), Space: 22 MB (33.81%) - LeetHub --- .../0169-majority-element.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0169-majority-element/0169-majority-element.cpp diff --git a/0169-majority-element/0169-majority-element.cpp b/0169-majority-element/0169-majority-element.cpp new file mode 100644 index 00000000..9b85e7db --- /dev/null +++ b/0169-majority-element/0169-majority-element.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int majorityElement(vector& nums) { + + int cnt = 0, num = -1; + + for(auto& ele : nums) + { + if(cnt == 0) + { + num = ele; + cnt = 1; + } + else if(ele == num) + ++cnt; + else + --cnt; + } + + return num; + } +}; \ No newline at end of file From 11eeb540b2c75f222460f347b65235e6580f5928 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Feb 2024 08:47:43 +0530 Subject: [PATCH 2455/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2108-find-first-palindromic-string-in-the-array/README.md 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 From dffbc0c2436549a5392d4d5f57205b41764e3e1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Feb 2024 08:47:46 +0530 Subject: [PATCH 2456/3167] Time: 42 ms (87.48%), Space: 23.3 MB (61.43%) - LeetHub --- ...-first-palindromic-string-in-the-array.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp 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 From 691a31a51647080a4402a0c2177bd4f6ecf41bd0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:23:18 +0530 Subject: [PATCH 2457/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2149-rearrange-array-elements-by-sign/README.md 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 From ecda9e7a432e727bab012b7b1e6e973aef257308 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:23:19 +0530 Subject: [PATCH 2458/3167] Attach NOTES - LeetHub --- 2149-rearrange-array-elements-by-sign/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2149-rearrange-array-elements-by-sign/NOTES.md 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 From b9fff462cd5c16a1d93ca226ecb7d5cf24f7b35f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:23:22 +0530 Subject: [PATCH 2459/3167] Time: 131 ms (94.75%), Space: 137.1 MB (18.51%) - LeetHub --- .../2149-rearrange-array-elements-by-sign.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp 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..0e568641 --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector rearrangeArray(vector& nums) { + + vector pos, neg; + + for(auto& ele : nums) + { + if(ele < 0) + neg.push_back(ele); + else + pos.push_back(ele); + } + + vector ans(nums.size()); + + int k = 0, i = 0, j = 0; + + while(i < pos.size() and j < pos.size()) + { + ans[k++] = pos[i++]; + ans[k++] = neg[j++]; + } + + return ans; + } +}; \ No newline at end of file From d54273d19c14488765b1bb926db6eaaba8afbf92 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:26:34 +0530 Subject: [PATCH 2460/3167] Attach NOTES - LeetHub From 62937670570d81233b34ae66c15a47cfe4c28352 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:26:39 +0530 Subject: [PATCH 2461/3167] Time: 148 ms (71.51%), Space: 126.5 MB (41.45%) - LeetHub --- .../2149-rearrange-array-elements-by-sign.cpp | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) 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 index 0e568641..156fbe4d 100644 --- 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 @@ -2,24 +2,23 @@ class Solution { public: vector rearrangeArray(vector& nums) { - vector pos, neg; + int n = nums.size(); + vector ans(n); - for(auto& ele : nums) - { - if(ele < 0) - neg.push_back(ele); - else - pos.push_back(ele); - } + int even = 0, odd = 1; - vector ans(nums.size()); - - int k = 0, i = 0, j = 0; - - while(i < pos.size() and j < pos.size()) + for(int i = 0; i < n; ++i) { - ans[k++] = pos[i++]; - ans[k++] = neg[j++]; + if(nums[i] < 0) + { + ans[odd] = nums[i]; + odd += 2; + } + else + { + ans[even] = nums[i]; + even += 2; + } } return ans; From 4ff72d74ea02aba49cdb9914332125aa6550f06f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:51:10 +0530 Subject: [PATCH 2462/3167] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2971-find-polygon-with-the-largest-perimeter/README.md 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 From 77af2e2003379757819a0c62efe3490c60f5c3a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:51:10 +0530 Subject: [PATCH 2463/3167] Attach NOTES - LeetHub --- 2971-find-polygon-with-the-largest-perimeter/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2971-find-polygon-with-the-largest-perimeter/NOTES.md 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 From 8446f70b953239be93117194b9c93a21bc7bd577 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:51:14 +0530 Subject: [PATCH 2464/3167] Time: 153 ms (27.15%), Space: 88.5 MB (7.40%) - LeetHub --- ...ind-polygon-with-the-largest-perimeter.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp 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 From ebe191940691ea32d1ee085b0789e3258b7160a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Feb 2024 08:13:55 +0530 Subject: [PATCH 2465/3167] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1481-least-number-of-unique-integers-after-k-removals/README.md 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 From dad74660b192e698105ed18fbe1e99c19cd8e4c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Feb 2024 08:13:59 +0530 Subject: [PATCH 2466/3167] Time: 187 ms (39.40%), Space: 70.1 MB (24.10%) - LeetHub --- ...er-of-unique-integers-after-k-removals.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp 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 From aa28815fad28d904d1054757e946650632305e7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:37:23 +0530 Subject: [PATCH 2467/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1642-furthest-building-you-can-reach/README.md diff --git a/1642-furthest-building-you-can-reach/README.md b/1642-furthest-building-you-can-reach/README.md new file mode 100644 index 00000000..57d2a1eb --- /dev/null +++ b/1642-furthest-building-you-can-reach/README.md @@ -0,0 +1,48 @@ +

1642. 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 From da2827bce2220fc29a858407d160bfab9fbfb6da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:37:26 +0530 Subject: [PATCH 2468/3167] Time: 82 ms (45.58%), Space: 57.5 MB (30.10%) - LeetHub --- .../1642-furthest-building-you-can-reach.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp 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; i Date: Sun, 18 Feb 2024 11:14:23 +0530 Subject: [PATCH 2469/3167] Create README - LeetHub --- 0354-russian-doll-envelopes/README.md | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0354-russian-doll-envelopes/README.md 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 From 1bd05fbd787909acf5d2a8c95ac190352f93abb9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Feb 2024 11:14:24 +0530 Subject: [PATCH 2470/3167] Attach NOTES - LeetHub --- 0354-russian-doll-envelopes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0354-russian-doll-envelopes/NOTES.md 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 From 8a54af9d20c459679fe525d67d37d7b67557ab6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:38 +0530 Subject: [PATCH 2471/3167] Create README - LeetHub --- 2402-meeting-rooms-iii/README.md | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2402-meeting-rooms-iii/README.md 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 From 2a5bcbe7a10ccd5f3c69b76a9be2499e63b2d755 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:38 +0530 Subject: [PATCH 2472/3167] Attach NOTES - LeetHub --- 2402-meeting-rooms-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2402-meeting-rooms-iii/NOTES.md 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 From b3b52795125a30165f05d84c9dbf23f651592a1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:42 +0530 Subject: [PATCH 2473/3167] Time: 302 ms (87.90%), Space: 100.5 MB (60.04%) - LeetHub --- .../2402-meeting-rooms-iii.cpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp 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 From b651b6855e5ac1275bdf5676dc45d214edef3036 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:21:30 +0530 Subject: [PATCH 2474/3167] Create README - LeetHub --- 0231-power-of-two/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0231-power-of-two/README.md 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 From d0af413e6f212d2aa8e7229936286737c2ae6ed3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:21:30 +0530 Subject: [PATCH 2475/3167] Attach NOTES - LeetHub --- 0231-power-of-two/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0231-power-of-two/NOTES.md 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 From eeaf9a7f57276de1e79380ff34aafa2e78bab4a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:21:34 +0530 Subject: [PATCH 2476/3167] Time: 2 ms (39.05%), Space: 7.3 MB (19.05%) - LeetHub --- 0231-power-of-two/0231-power-of-two.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 0231-power-of-two/0231-power-of-two.cpp 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 From 0ca9ad3417ba44d91bf096f80e440a99f107afb4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:41:12 +0530 Subject: [PATCH 2477/3167] Create README - LeetHub --- 0268-missing-number/README.md | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0268-missing-number/README.md 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 From 2bcdd4f236a769c7e3912b2110e0dd2ba99a3c33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:41:16 +0530 Subject: [PATCH 2478/3167] Time: 15 ms (58.96%), Space: 20.4 MB (18.88%) - LeetHub --- 0268-missing-number/0268-missing-number.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0268-missing-number/0268-missing-number.cpp 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 From ffb16fcd1a44db421cb7e3d036aeeee6b474dbd4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:42:28 +0530 Subject: [PATCH 2479/3167] Attach NOTES - LeetHub --- 0268-missing-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0268-missing-number/NOTES.md 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 From 62876500aef7b043cb30df8021d896fcecbda9db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:42:31 +0530 Subject: [PATCH 2480/3167] Time: 15 ms (58.96%), Space: 20.4 MB (18.88%) - LeetHub From e8bb9132be823f18919053e2a30700dbf02e4b65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:55:39 +0530 Subject: [PATCH 2481/3167] Create README - LeetHub --- 0201-bitwise-and-of-numbers-range/README.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0201-bitwise-and-of-numbers-range/README.md 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 From d369b1e927088fb808dae3eb7060db10bde697b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:55:39 +0530 Subject: [PATCH 2482/3167] Attach NOTES - LeetHub --- 0201-bitwise-and-of-numbers-range/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0201-bitwise-and-of-numbers-range/NOTES.md 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 From 1ca1b9516f347fa9ef0c71f10d64ace3b827ac9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:55:43 +0530 Subject: [PATCH 2483/3167] Time: 7 ms (64.93%), Space: 8.8 MB (46.79%) - LeetHub --- .../0201-bitwise-and-of-numbers-range.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp 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 From 350821ec7d5269fc2595737810aed7821f68c495 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:05:50 +0530 Subject: [PATCH 2484/3167] Create README - LeetHub --- 0022-generate-parentheses/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0022-generate-parentheses/README.md 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 From 2aa23b1f005ad392f900b4f2c8b5f6c2f39ed99f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:05:54 +0530 Subject: [PATCH 2485/3167] Time: 4 ms (49.30%), Space: 15 MB (65.11%) - LeetHub --- .../0022-generate-parentheses.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0022-generate-parentheses/0022-generate-parentheses.cpp 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 From b21d56f7d6ceb03b9728664e1edaa0f48c143f00 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Feb 2024 23:19:25 +0530 Subject: [PATCH 2486/3167] Attach NOTES - LeetHub --- 0787-cheapest-flights-within-k-stops/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0787-cheapest-flights-within-k-stops/NOTES.md 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 From 54a01a211e6bc1983154011ef74a31bf98aead0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:14:06 +0530 Subject: [PATCH 2487/3167] Create README - LeetHub --- 2092-find-all-people-with-secret/README.md | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2092-find-all-people-with-secret/README.md 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 From df91588e11eedcd6af2d8d4da9d65fa9f07de1df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:14:07 +0530 Subject: [PATCH 2488/3167] Attach NOTES - LeetHub --- 2092-find-all-people-with-secret/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2092-find-all-people-with-secret/NOTES.md 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 From ccbfdf97a0a214d587e51526ffa7d983d5880263 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:14:10 +0530 Subject: [PATCH 2489/3167] Time: 445 ms (75.41%), Space: 164.8 MB (77.46%) - LeetHub --- .../2092-find-all-people-with-secret.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp 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 From bb7a9cdac6032fee07086f9036571625a29b9bbe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:43:58 +0530 Subject: [PATCH 2490/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2709-greatest-common-divisor-traversal/README.md 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 From e6789154d00f8b01cee879ac9bcb8d32e6c5414e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:43:59 +0530 Subject: [PATCH 2491/3167] Attach NOTES - LeetHub --- 2709-greatest-common-divisor-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2709-greatest-common-divisor-traversal/NOTES.md 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 From 373b36be7cd361de1719c35743d7f4b2f41dc470 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:44:03 +0530 Subject: [PATCH 2492/3167] Time: 404 ms (71.86%), Space: 91.3 MB (80.84%) - LeetHub --- ...2709-greatest-common-divisor-traversal.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp 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 From f0aaff7da5ef1768dd32e22c2a294eda718c1509 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:28:34 +0530 Subject: [PATCH 2493/3167] Create README - LeetHub --- 0543-diameter-of-binary-tree/README.md | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0543-diameter-of-binary-tree/README.md 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 From 8d76f3c8f2f153932d96ab9a13d9b5419316e51f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:28:39 +0530 Subject: [PATCH 2494/3167] Time: 0 ms (100.00%), Space: 19 MB (70.17%) - LeetHub --- .../0543-diameter-of-binary-tree.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp 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..c381414a --- /dev/null +++ b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp @@ -0,0 +1,33 @@ +/** + * 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& maxi) + { + if(!root) + return 0; + + int left = helper(root->left, maxi); + int right = helper(root->right, maxi); + + maxi = max(maxi, left + right); + return 1 + max(left, right); + } + + int diameterOfBinaryTree(TreeNode* root) { + + int maxi = 0; + helper(root, maxi); + return maxi; + } +}; \ No newline at end of file From abac4eb30905749f1de9deb5e13e785c82fa9d2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:31:47 +0530 Subject: [PATCH 2495/3167] Attach NOTES - LeetHub --- 0543-diameter-of-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0543-diameter-of-binary-tree/NOTES.md 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 From f984f1c174f69d6c7d2a1104b11307562dcd163b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:54:46 +0530 Subject: [PATCH 2496/3167] Create README - LeetHub --- 0513-find-bottom-left-tree-value/README.md | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0513-find-bottom-left-tree-value/README.md 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 From bca6d4be3cf1c02c3bcc05372a3f8dad81ed2373 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:54:46 +0530 Subject: [PATCH 2497/3167] Attach NOTES - LeetHub --- 0513-find-bottom-left-tree-value/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0513-find-bottom-left-tree-value/NOTES.md 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 From 8fac285a983ce98fa6cd93512faff9d5ce68efb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:54:49 +0530 Subject: [PATCH 2498/3167] Time: 13 ms (22.41%), Space: 20.1 MB (76.07%) - LeetHub --- .../0513-find-bottom-left-tree-value.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp 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 From 9970cc774ea0064ba70119892d6db378a0679ac6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:57:02 +0530 Subject: [PATCH 2499/3167] Attach NOTES - LeetHub From 6012647329134c912cc777be3e3dfeb510d5ebdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:57:05 +0530 Subject: [PATCH 2500/3167] Time: 13 ms (22.41%), Space: 20.1 MB (76.07%) - LeetHub From 0d0e719a131e17397016ee54aca43e116a03baa3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:21:28 +0530 Subject: [PATCH 2501/3167] Create README - LeetHub --- 1609-even-odd-tree/README.md | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1609-even-odd-tree/README.md 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 From 54f718da899d2475f15650869d412a6ebad67f3f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:21:32 +0530 Subject: [PATCH 2502/3167] Time: 702 ms (5.00%), Space: 158.2 MB (21.32%) - LeetHub --- 1609-even-odd-tree/1609-even-odd-tree.cpp | 98 +++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 1609-even-odd-tree/1609-even-odd-tree.cpp 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 From 6565f8c45a2183265b75f034baef05444cb5f9f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:22:57 +0530 Subject: [PATCH 2503/3167] Attach NOTES - LeetHub --- 1609-even-odd-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1609-even-odd-tree/NOTES.md 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 From e77cd15dc5f6293cb879634eb23ace70c54279e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:23:01 +0530 Subject: [PATCH 2504/3167] Time: 702 ms (5.00%), Space: 158.2 MB (21.32%) - LeetHub From a2cd0413213c551f72a8a5c964b7db4ee62a2b06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:43:33 +0530 Subject: [PATCH 2505/3167] Create README - LeetHub --- 2864-maximum-odd-binary-number/README.md | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2864-maximum-odd-binary-number/README.md 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 From 3e38f406fd3c349f7d42134ef60304845639c811 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:43:37 +0530 Subject: [PATCH 2506/3167] Time: 0 ms (100.00%), Space: 8.3 MB (55.89%) - LeetHub --- .../2864-maximum-odd-binary-number.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp 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 From c4828db7ec8590d3b64ceb3badbbcc6778c74379 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:44:28 +0530 Subject: [PATCH 2507/3167] Attach NOTES - LeetHub --- 2864-maximum-odd-binary-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2864-maximum-odd-binary-number/NOTES.md 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 From 585ba2da91e3eeab60ac0ff4f294dee5ffc7319c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:44:31 +0530 Subject: [PATCH 2508/3167] Time: 0 ms (100.00%), Space: 8.3 MB (55.89%) - LeetHub From c201810e408fa12181e870d648bb98d655ba05be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:19:53 +0530 Subject: [PATCH 2509/3167] Attach NOTES - LeetHub --- 0977-squares-of-a-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0977-squares-of-a-sorted-array/NOTES.md 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 From dad71fb27d76ab7a8e361ee32ef94719907c80f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:19:54 +0530 Subject: [PATCH 2510/3167] Create README - LeetHub --- 0977-squares-of-a-sorted-array/README.md | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0977-squares-of-a-sorted-array/README.md 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 From 05cc6520e43a2aaede214399edb7373405ccd6df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:19:57 +0530 Subject: [PATCH 2511/3167] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub --- .../0977-squares-of-a-sorted-array.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp 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..ec7edff6 --- /dev/null +++ b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector sortedSquares(vector& nums) { + + vector squares; + + for(auto& ele : nums) + squares.push_back(ele * ele); + + sort(squares.begin(), squares.end()); + + return squares; + + } +}; \ No newline at end of file From 90be8e16b588c251503401a2ffa13102a6835da5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:20:06 +0530 Subject: [PATCH 2512/3167] Attach NOTES - LeetHub From 94cbf8ec6f114f314a15bc900b4ab5ab2e1d0f6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:20:10 +0530 Subject: [PATCH 2513/3167] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub From 4c8e32118dc32077ad8fc7843b27b4590061487e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:24:36 +0530 Subject: [PATCH 2514/3167] Attach NOTES - LeetHub From c3e70b2dfa69027b543d7c9cb75ae93d225a6be2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:24:40 +0530 Subject: [PATCH 2515/3167] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub From 248f73e56dd40e12385eb2fa7c5eb61609b898e7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:29:02 +0530 Subject: [PATCH 2516/3167] Attach NOTES - LeetHub From 2da2fd1c78e2321d12598737081d906b7b173c81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:29:05 +0530 Subject: [PATCH 2517/3167] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub From 88f994cf4751f95e56e1b4e75047e9a8cc42f38e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:32:32 +0530 Subject: [PATCH 2518/3167] Attach NOTES - LeetHub From f11750461eb9a3d9f4faa5671f230b5b25ea09c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:32:36 +0530 Subject: [PATCH 2519/3167] Time: 22 ms (69.10%), Space: 28.4 MB (58.32%) - LeetHub --- .../0977-squares-of-a-sorted-array.cpp | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) 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 index ec7edff6..b31ae1e2 100644 --- 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 @@ -2,14 +2,26 @@ class Solution { public: vector sortedSquares(vector& nums) { - vector squares; + int n = nums.size(); + vector squares(n); + + int i = 0, j = n-1, k = n-1; - for(auto& ele : nums) - squares.push_back(ele * ele); - - sort(squares.begin(), squares.end()); + 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 From 17e300e4712e9bca32d24eaf87296947eea1265f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:15 +0530 Subject: [PATCH 2520/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0019-remove-nth-node-from-end-of-list/README.md 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:

+ +
    +
  • The number of nodes in the list is sz.
  • +
  • 1 <= sz <= 30
  • +
  • 0 <= Node.val <= 100
  • +
  • 1 <= n <= sz
  • +
+ +

 

+

Follow up: Could you do this in one pass?

+
\ No newline at end of file From d912941ac3e5bdacc3b06c8446559d231076d68f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:15 +0530 Subject: [PATCH 2521/3167] Attach NOTES - LeetHub --- 0019-remove-nth-node-from-end-of-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0019-remove-nth-node-from-end-of-list/NOTES.md 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 From 30e21462f79b5768c4ea682b6324fcd060516377 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:19 +0530 Subject: [PATCH 2522/3167] Time: 7 ms (19.64%), Space: 13.2 MB (66.71%) - LeetHub --- .../0019-remove-nth-node-from-end-of-list.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp 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 From 3d5b5cd87f75332e996eddab3e9b08aaffe8f16b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:44:09 +0530 Subject: [PATCH 2523/3167] Create README - LeetHub --- 0948-bag-of-tokens/README.md | 106 +++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 0948-bag-of-tokens/README.md 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 From ed01bab98e89d8f0c509f2ad846aef73990b2b6c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:44:13 +0530 Subject: [PATCH 2524/3167] Time: 9 ms (18.89%), Space: 13.2 MB (23.53%) - LeetHub --- 0948-bag-of-tokens/0948-bag-of-tokens.cpp | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0948-bag-of-tokens/0948-bag-of-tokens.cpp 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 From 6c9bd3e02165ad621ec2946dc08638b8654d2876 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:48:40 +0530 Subject: [PATCH 2525/3167] Attach NOTES - LeetHub --- 0948-bag-of-tokens/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0948-bag-of-tokens/NOTES.md 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 From 93ef9308020301dc10dad5014ba505e7d575f673 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:48:43 +0530 Subject: [PATCH 2526/3167] Time: 9 ms (18.89%), Space: 13.2 MB (23.53%) - LeetHub From 3061cb3f5c3f1a237ff81f3ac9c816dfba6642f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:08:29 +0530 Subject: [PATCH 2527/3167] Create README - LeetHub --- 2680-maximum-or/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2680-maximum-or/README.md 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 From 9293cb2b85283c36e95262792cb7d00b02cf77e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:08:29 +0530 Subject: [PATCH 2528/3167] Attach NOTES - LeetHub --- 2680-maximum-or/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2680-maximum-or/NOTES.md 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 From 63dba817844aaa72bd0f2ed56351d68d75b05e71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:08:32 +0530 Subject: [PATCH 2529/3167] Time: 214 ms (7.02%), Space: 103 MB (10.53%) - LeetHub --- 2680-maximum-or/2680-maximum-or.cpp | 90 +++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 2680-maximum-or/2680-maximum-or.cpp diff --git a/2680-maximum-or/2680-maximum-or.cpp b/2680-maximum-or/2680-maximum-or.cpp new file mode 100644 index 00000000..45f8f9f2 --- /dev/null +++ b/2680-maximum-or/2680-maximum-or.cpp @@ -0,0 +1,90 @@ +class SEG +{ + public: + int N; + + vector tree, arr; + + 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); + + buildTree(0, 0, N-1); + } + + public: + void buildTree(int idx, int low, int high) + { + if(low == high) + { + tree[idx] = arr[low]; + return; + } + + int mid = (low + high) / 2; + buildTree(2*idx+1, low, mid); + buildTree(2*idx+2, mid+1, high); + + tree[idx] = tree[2*idx+1] | tree[2*idx+2]; + } + + int queryTree(int idx, int low, int high, int l, int r) + { + if(r < low or l > high) + return 0; + + if(low >= l and high <= r) + return tree[idx]; + + 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; + } + + int query(int l, int r) + { + return queryTree(0, 0, N-1, l, r); + } +}; + + +class Solution { +public: + long long maximumOr(vector& nums, int k) { + + int n = nums.size(); + + SEG seg(nums); + + long long res = 0; + + for(int i = 0; i < n; ++i) + { + long long left = seg.query(0, i-1); + long long right = seg.query(i+1, n-1); + + long long curr = nums[i], j = 0; + + while(j < k) + { + curr *= 2; + ++j; + } + + long long ans = curr | left | right; + + res = max(res, ans); + } + + return res; + + } +}; \ No newline at end of file From 2abca6f44108096a9258eea7c800ef8460c0288e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:00 +0530 Subject: [PATCH 2530/3167] Attach NOTES - LeetHub From 3a17fdde9a54156bc8f9ff121c6cf10529521c67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:03 +0530 Subject: [PATCH 2531/3167] Time: 214 ms (7.02%), Space: 103 MB (10.53%) - LeetHub From 20cd051f0478c41a375e2251b0e29b42f239aa53 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:34 +0530 Subject: [PATCH 2532/3167] Attach NOTES - LeetHub From 2d3388ee702acd5c76efc211a124fd941feaad76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:37 +0530 Subject: [PATCH 2533/3167] Time: 214 ms (7.02%), Space: 103 MB (10.53%) - LeetHub From b964a3947718c0f8ef9e0e7707c8d3c6941c169c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:51:51 +0530 Subject: [PATCH 2534/3167] Attach NOTES - LeetHub From 54a2121a5edf8e5898b3cbeee8c42464ff9852c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:51:54 +0530 Subject: [PATCH 2535/3167] Time: 109 ms (68.42%), Space: 93 MB (76.75%) - LeetHub --- 2680-maximum-or/2680-maximum-or.cpp | 86 +++++------------------------ 1 file changed, 14 insertions(+), 72 deletions(-) diff --git a/2680-maximum-or/2680-maximum-or.cpp b/2680-maximum-or/2680-maximum-or.cpp index 45f8f9f2..21840949 100644 --- a/2680-maximum-or/2680-maximum-or.cpp +++ b/2680-maximum-or/2680-maximum-or.cpp @@ -1,90 +1,32 @@ -class SEG -{ - public: - int N; - - vector tree, arr; - - 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); - - buildTree(0, 0, N-1); - } - - public: - void buildTree(int idx, int low, int high) - { - if(low == high) - { - tree[idx] = arr[low]; - return; - } - - int mid = (low + high) / 2; - buildTree(2*idx+1, low, mid); - buildTree(2*idx+2, mid+1, high); - - tree[idx] = tree[2*idx+1] | tree[2*idx+2]; - } - - int queryTree(int idx, int low, int high, int l, int r) - { - if(r < low or l > high) - return 0; - - if(low >= l and high <= r) - return tree[idx]; - - 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; - } - - int query(int l, int r) - { - return queryTree(0, 0, N-1, l, r); - } -}; - +using ll = long long; class Solution { public: long long maximumOr(vector& nums, int k) { int n = nums.size(); - SEG seg(nums); + vector pref(n, 0), suff(n, 0); - long long res = 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) { - long long left = seg.query(0, i-1); - long long right = seg.query(i+1, n-1); - - long long curr = nums[i], j = 0; - - while(j < k) - { - curr *= 2; - ++j; - } - - long long ans = curr | left | right; + 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 From e6bbedcb5fa0f70322708cc1e6058352f134351e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:23:16 +0530 Subject: [PATCH 2536/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1750-minimum-length-of-string-after-deleting-similar-ends/README.md 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 From f146a02b9313daabedf4acd1e6c42d6cf4763ac0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:23:19 +0530 Subject: [PATCH 2537/3167] Time: 19 ms (96.90%), Space: 14 MB (13.45%) - LeetHub --- ...-of-string-after-deleting-similar-ends.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp 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 From 547253fb1823e9dc2e8c8acc796e7439adec6b14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Mar 2024 07:58:17 +0530 Subject: [PATCH 2538/3167] Create README - LeetHub --- 0876-middle-of-the-linked-list/README.md | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0876-middle-of-the-linked-list/README.md 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 From 128ca9ce58f6de4fb6b91ecce668600bd6fece90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Mar 2024 07:58:21 +0530 Subject: [PATCH 2539/3167] Time: 4 ms (30.24%), Space: 8.6 MB (66.29%) - LeetHub --- .../0876-middle-of-the-linked-list.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp 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 From dde31514b7ad414c8f64b8c3080ab8077e174712 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Mar 2024 09:35:52 +0530 Subject: [PATCH 2540/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 3005-count-elements-with-maximum-frequency/README.md 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 From 896fe9b10165e7531974d5765235241d547f70b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Mar 2024 09:35:56 +0530 Subject: [PATCH 2541/3167] Time: 4 ms (68.91%), Space: 22 MB (14.32%) - LeetHub --- ...-count-elements-with-maximum-frequency.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp 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 From 785dee411abe66b09d699571c0613d3173e82345 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:50:48 +0530 Subject: [PATCH 2542/3167] Create README - LeetHub --- 2540-minimum-common-value/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2540-minimum-common-value/README.md 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 From cab21d82a642dced227e6a35f5bf3509d6b84186 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:50:52 +0530 Subject: [PATCH 2543/3167] Time: 72 ms (54.08%), Space: 53 MB (29.59%) - LeetHub --- .../2540-minimum-common-value.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2540-minimum-common-value/2540-minimum-common-value.cpp 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 From e20f5669919b6a2c2da97eef64f8da7fcc62c1e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:42:03 +0530 Subject: [PATCH 2544/3167] Create README - LeetHub --- 0349-intersection-of-two-arrays/README.md | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0349-intersection-of-two-arrays/README.md 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 From 1d7fbe02f78ee8acb1df9a4f49ffd7afd5b2762b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:42:06 +0530 Subject: [PATCH 2545/3167] Time: 4 ms (68.61%), Space: 12.9 MB (37.74%) - LeetHub --- .../0349-intersection-of-two-arrays.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp 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 From b8ee81d9dfd0d03171abcb7d7ae68d0c3e99bf09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:43:04 +0530 Subject: [PATCH 2546/3167] Attach NOTES - LeetHub --- 0349-intersection-of-two-arrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0349-intersection-of-two-arrays/NOTES.md 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 From b7272d3c3d920033f22d4f6be562c9a4b386c601 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:34:58 +0530 Subject: [PATCH 2547/3167] Create README - LeetHub --- 0791-custom-sort-string/README.md | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0791-custom-sort-string/README.md 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 From 7c97038136c8131ca0dd8ddd902eb4744c8919a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:35:02 +0530 Subject: [PATCH 2548/3167] Time: 0 ms (100.00%), Space: 8.6 MB (17.36%) - LeetHub --- .../0791-custom-sort-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0791-custom-sort-string/0791-custom-sort-string.cpp 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..8d7a7fdb --- /dev/null +++ b/0791-custom-sort-string/0791-custom-sort-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string customSortString(string order, string s) { + + map mp; + + int id = 0; + + for(int i = 0; i < order.size(); ++i) + { + if(mp.find(order[i]) == mp.end()) + mp[order[i]] = id++; + } + + vector> vp; + + for(auto&ch : s) + vp.push_back({mp[ch], ch}); + + sort(vp.begin(), vp.end()); + + string ans; + + for(auto& ch : vp) + ans += ch.second; + + return ans; + + } +}; \ No newline at end of file From 8c94f686c5e3271970dd417148bd0f514aef37c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:35:46 +0530 Subject: [PATCH 2549/3167] Attach NOTES - LeetHub --- 0791-custom-sort-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0791-custom-sort-string/NOTES.md 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 From 98d5cf6c4f4d212d16176f6ec8d58a683ff79d94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:35:49 +0530 Subject: [PATCH 2550/3167] Time: 0 ms (100.00%), Space: 8.6 MB (17.36%) - LeetHub From baee2061a9ac9b65315e4ed4a24a6e04532f23bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:36:49 +0530 Subject: [PATCH 2551/3167] Attach NOTES - LeetHub From a689ba380b898c723fbcf8ccdd33b9ac5fee6170 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:36:52 +0530 Subject: [PATCH 2552/3167] Time: 0 ms (100.00%), Space: 8.7 MB (17.36%) - LeetHub --- .../0791-custom-sort-string.cpp | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/0791-custom-sort-string/0791-custom-sort-string.cpp b/0791-custom-sort-string/0791-custom-sort-string.cpp index 8d7a7fdb..7124d4f8 100644 --- a/0791-custom-sort-string/0791-custom-sort-string.cpp +++ b/0791-custom-sort-string/0791-custom-sort-string.cpp @@ -3,28 +3,18 @@ class Solution { string customSortString(string order, string s) { map mp; - - int id = 0; - + for(int i = 0; i < order.size(); ++i) { if(mp.find(order[i]) == mp.end()) - mp[order[i]] = id++; + mp[order[i]] = i; } - vector> vp; - - for(auto&ch : s) - vp.push_back({mp[ch], ch}); - - sort(vp.begin(), vp.end()); - - string ans; - - for(auto& ch : vp) - ans += ch.second; + sort(s.begin(), s.end(),[&](const auto& a, const auto& b){ + return mp[a] < mp[b]; + }); - return ans; + return s; } }; \ No newline at end of file From 91b482b14e91059d64967f631d85f795de5fb9b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:28:26 +0530 Subject: [PATCH 2553/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md 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 From c8c45cdc3956c0b3d53d02b516341926e58739c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:28:26 +0530 Subject: [PATCH 2554/3167] Attach NOTES - LeetHub --- 1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md 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 From e3870b343767e5f6c401ea726fee741c828b68ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:28:29 +0530 Subject: [PATCH 2555/3167] Time: 11 ms (31.41%), Space: 14.4 MB (55.92%) - LeetHub --- ...sum-consecutive-nodes-from-linked-list.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp 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 From db6c6ef2bb37db1eb1fa59f873f997b62b4e5573 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:49:09 +0530 Subject: [PATCH 2556/3167] Create README - LeetHub --- 2485-find-the-pivot-integer/README.md | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2485-find-the-pivot-integer/README.md 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 From 27599f47c83f5c20dca0fbc1ac39332bd84093a4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:49:15 +0530 Subject: [PATCH 2557/3167] Time: 0 ms (100.00%), Space: 7.3 MB (15.68%) - LeetHub --- .../2485-find-the-pivot-integer.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp 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 From 9007d51abe8f2f214e5f233f63f4599c1f98ced3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:50:56 +0530 Subject: [PATCH 2558/3167] Attach NOTES - LeetHub --- 2485-find-the-pivot-integer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2485-find-the-pivot-integer/NOTES.md 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 From 4a7e835107e85a5c1b8f80ae6044430dfc7e9a4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:51:53 +0530 Subject: [PATCH 2559/3167] Attach NOTES - LeetHub From 68b1d56748468ab66bc3012ec97d8d68323fe434 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:51:57 +0530 Subject: [PATCH 2560/3167] Time: 0 ms (100.00%), Space: 7.3 MB (15.68%) - LeetHub From 81bf2a5f451825c81991b27ad8c47464d99a5d8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:52:33 +0530 Subject: [PATCH 2561/3167] Attach NOTES - LeetHub From 1d650ff87948c76238fff02221d5b786839c8baf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:52:37 +0530 Subject: [PATCH 2562/3167] Time: 0 ms (100.00%), Space: 7.3 MB (15.68%) - LeetHub From 33b538e3c9229b1a3dba6f2582485aad88af6dfb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:23 +0530 Subject: [PATCH 2563/3167] Create README - LeetHub --- 0930-binary-subarrays-with-sum/README.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0930-binary-subarrays-with-sum/README.md 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 From 7f911ab17c9f9fff69277364a012dcf1e463292e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:23 +0530 Subject: [PATCH 2564/3167] Attach NOTES - LeetHub --- 0930-binary-subarrays-with-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0930-binary-subarrays-with-sum/NOTES.md 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 From 30e8bc8fe02ead4833f09f2f99a7dccc2730385d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:27 +0530 Subject: [PATCH 2565/3167] Time: 66 ms (20.89%), Space: 37.8 MB (40.56%) - LeetHub --- .../0930-binary-subarrays-with-sum.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp 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; + } +}; From 96848b366c768f23c4cb3203f7d53a530abe1cdd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 00:04:51 +0530 Subject: [PATCH 2566/3167] Create README - LeetHub --- 0238-product-of-array-except-self/README.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0238-product-of-array-except-self/README.md 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 From 1310aaff6e11d8fbc9a3085271a06e123c528db4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 00:04:55 +0530 Subject: [PATCH 2567/3167] Time: 25 ms (6.72%), Space: 26.3 MB (86.12%) - LeetHub --- .../0238-product-of-array-except-self.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0238-product-of-array-except-self/0238-product-of-array-except-self.cpp 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 From bab8be5ba8b27c802126f62112147341334a2cd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 19:54:25 +0530 Subject: [PATCH 2568/3167] Attach NOTES - LeetHub --- 0525-contiguous-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0525-contiguous-array/NOTES.md 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 From 6efff499220f434313305cc82fccb995141bcc59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 19:54:28 +0530 Subject: [PATCH 2569/3167] Time: 114 ms (16.52%), Space: 87 MB (85.88%) - LeetHub --- .../0525-contiguous-array.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0525-contiguous-array/0525-contiguous-array.cpp 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 From 89af5d6691406caa75b20a1258ca0358f6f9b973 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:46:43 +0530 Subject: [PATCH 2570/3167] Attach NOTES - LeetHub --- 0057-insert-interval/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0057-insert-interval/NOTES.md 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 From 4295ba9444365cc0b63477df46374cb7e0b55e82 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:48:20 +0530 Subject: [PATCH 2571/3167] Attach NOTES - LeetHub From c12dcda0294e1c5214a95ecfb5bb51769f4f10a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:57:44 +0530 Subject: [PATCH 2572/3167] Attach NOTES - LeetHub From 71b5f6148a168e61b0a1aa662be61e85fe43ee91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:00:39 +0530 Subject: [PATCH 2573/3167] Create README - LeetHub --- 0621-task-scheduler/README.md | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 0621-task-scheduler/README.md 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 From dedc85e565b03e42f626ad498500d5ad47017363 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:00:42 +0530 Subject: [PATCH 2574/3167] Time: 57 ms (69.92%), Space: 37.8 MB (94.23%) - LeetHub --- 0621-task-scheduler/0621-task-scheduler.cpp | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0621-task-scheduler/0621-task-scheduler.cpp 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 From d1c2dc56f1f646cff26cc99df52d9e237cdbaa9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:20:50 +0530 Subject: [PATCH 2575/3167] Create README - LeetHub --- 1669-merge-in-between-linked-lists/README.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1669-merge-in-between-linked-lists/README.md 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 From 09ed672e78c545260328669f1d4cdfc0c2c47c63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:20:54 +0530 Subject: [PATCH 2576/3167] Time: 189 ms (25.82%), Space: 98 MB (44.80%) - LeetHub --- .../1669-merge-in-between-linked-lists.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp 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 From 34765298574d46e4d2cd646339b07988119d0a06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:20:27 +0530 Subject: [PATCH 2577/3167] Attach NOTES - LeetHub --- 0206-reverse-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0206-reverse-linked-list/NOTES.md 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 From d9bbb601bab0a62454a010603fe35ae9f24a5a83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:20:30 +0530 Subject: [PATCH 2578/3167] Time: 8 ms (16.90%), Space: 11.6 MB (67.59%) - LeetHub --- .../0206-reverse-linked-list.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0206-reverse-linked-list/0206-reverse-linked-list.cpp 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 From e78191d150a0efe07a9af8612900df593dff7b28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:38:11 +0530 Subject: [PATCH 2579/3167] Create README - LeetHub --- 0234-palindrome-linked-list/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0234-palindrome-linked-list/README.md 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 From 10a5df3e66091203a6b1344578062a257f673b21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:38:15 +0530 Subject: [PATCH 2580/3167] Time: 155 ms (84.20%), Space: 116.5 MB (92.96%) - LeetHub --- .../0234-palindrome-linked-list.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0234-palindrome-linked-list/0234-palindrome-linked-list.cpp 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 From 5121b754c8969d9a2939d7e43160845101a027cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Mar 2024 20:09:33 +0530 Subject: [PATCH 2581/3167] Create README - LeetHub --- 0143-reorder-list/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0143-reorder-list/README.md 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 From 06a98a536ad7220454a934bd42c94d008b741892 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Mar 2024 20:09:36 +0530 Subject: [PATCH 2582/3167] Time: 21 ms (81.42%), Space: 21.1 MB (99.47%) - LeetHub --- 0143-reorder-list/0143-reorder-list.cpp | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0143-reorder-list/0143-reorder-list.cpp 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 From 7b239337eb617bc9b8b44ec727712512ec7f0fe6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Mar 2024 18:19:56 +0530 Subject: [PATCH 2583/3167] Create README - LeetHub --- 3093-longest-common-suffix-queries/README.md | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 3093-longest-common-suffix-queries/README.md 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 From 7085ce84d89858326ac914cabf1247ae98c15e8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Mar 2024 18:19:59 +0530 Subject: [PATCH 2584/3167] Time: 447 ms (83.33%), Space: 824.3 MB (16.67%) - LeetHub --- .../3093-longest-common-suffix-queries.cpp | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp 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 From 31c5d7fadb6d6f399fb27a498332605048eef34f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:56:38 +0530 Subject: [PATCH 2585/3167] Attach NOTES - LeetHub --- 0442-find-all-duplicates-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0442-find-all-duplicates-in-an-array/NOTES.md 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 From 5dde6b98b695eab8340b11b2b8aeb797ca6d3972 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:28:52 +0530 Subject: [PATCH 2586/3167] Create README - LeetHub --- 0041-first-missing-positive/README.md | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0041-first-missing-positive/README.md 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 From 15451b7f96ae037b21b0505570fa5e4e2215e54d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:28:55 +0530 Subject: [PATCH 2587/3167] Time: 34 ms (93.31%), Space: 43.5 MB (81.44%) - LeetHub --- .../0041-first-missing-positive.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0041-first-missing-positive/0041-first-missing-positive.cpp 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 From 16879b7200c4bf86702e0a5b66bed2b975e08da4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:48:41 +0530 Subject: [PATCH 2588/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2958-length-of-longest-subarray-with-at-most-k-frequency/README.md 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 From 5af470c49ee2f4e9351fde4d982fb9edadd4c695 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:48:44 +0530 Subject: [PATCH 2589/3167] Time: 461 ms (5.02%), Space: 152 MB (5.02%) - LeetHub --- ...gest-subarray-with-at-most-k-frequency.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp 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..cd5a0c16 --- /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,30 @@ +class Solution { +public: + int maxSubarrayLength(vector& nums, int k) { + + int i = 0, j = 0, n = nums.size(); + + int maxFreq = 0, ans = 0; + + map mp; + + while(j < n) + { + ++mp[nums[j]]; + maxFreq = max(maxFreq, 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 From 0f72be57afdaa50056927e035ed89c03b78b9470 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:52:50 +0530 Subject: [PATCH 2590/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md 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 From 3a08f837c278139cca19a1bdd4fd6b90048e28d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:52:52 +0530 Subject: [PATCH 2591/3167] Time: 406 ms (6.33%), Space: 152.2 MB (5.02%) - LeetHub --- ...958-length-of-longest-subarray-with-at-most-k-frequency.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index cd5a0c16..46200095 100644 --- 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 @@ -4,14 +4,13 @@ class Solution { int i = 0, j = 0, n = nums.size(); - int maxFreq = 0, ans = 0; + int ans = 0; map mp; while(j < n) { ++mp[nums[j]]; - maxFreq = max(maxFreq, mp[nums[j]]); while(mp[nums[j]] > k) { From 15c4697bd71c064d1ee69bc1accdd97c09eac7c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:53:16 +0530 Subject: [PATCH 2592/3167] Attach NOTES - LeetHub From 18c8c4c5cea0c5da57d5853aab69b559868e95d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:54:09 +0530 Subject: [PATCH 2593/3167] Attach NOTES - LeetHub From 85d71d33d7d9aa21a1b1d05d3f8ce3caf4f22422 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:54:12 +0530 Subject: [PATCH 2594/3167] Time: 406 ms (6.33%), Space: 152.2 MB (5.02%) - LeetHub From 789fa391b8b8b175f3948a9ceda4a30dbc74fb6d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:15:35 +0530 Subject: [PATCH 2595/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md 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 From d67e8acd2da7e38347437265273c3be0bbdc902e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:15:36 +0530 Subject: [PATCH 2596/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md 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 From 0522897b6fd91f36d29e5659ab65754533b5bb83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:15:40 +0530 Subject: [PATCH 2597/3167] Time: 134 ms (55.43%), Space: 120 MB (84.30%) - LeetHub --- ...e-max-element-appears-at-least-k-times.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp 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 From 6639902b63122663b42b19f8be1dd4f4527b982f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Mar 2024 08:59:41 +0530 Subject: [PATCH 2598/3167] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0992-subarrays-with-k-different-integers/README.md 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 From 6a51a3fca00c59d113f7f771f14683c98b591baa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Mar 2024 22:31:18 +0530 Subject: [PATCH 2599/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2444-count-subarrays-with-fixed-bounds/README.md 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 From 874859d184e2b43088e827191c2597528fe880ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:23:37 +0530 Subject: [PATCH 2600/3167] Create README - LeetHub --- 0058-length-of-last-word/README.md | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0058-length-of-last-word/README.md 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 From 71c24cfe95dfd4208156e297676e634aa74fbd30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:23:40 +0530 Subject: [PATCH 2601/3167] Time: 0 ms (100.00%), Space: 7.6 MB (87.89%) - LeetHub --- .../0058-length-of-last-word.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0058-length-of-last-word/0058-length-of-last-word.cpp 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 From ba0168718b5bc9968ee54caafe342e2eb7de4f67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:24:54 +0530 Subject: [PATCH 2602/3167] Attach NOTES - LeetHub --- 0058-length-of-last-word/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0058-length-of-last-word/NOTES.md 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 From 704c572f4a1d265fbffa45b3cb9f993103bdd663 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:24:57 +0530 Subject: [PATCH 2603/3167] Time: 0 ms (100.00%), Space: 7.6 MB (87.89%) - LeetHub From 58441577c192bedcf302bace1466b396210c6211 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:06:06 +0530 Subject: [PATCH 2604/3167] Create README - LeetHub --- 0205-isomorphic-strings/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0205-isomorphic-strings/README.md 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 From a54d9aaf7a4ab49b9fc73d52986420bf7a1c50ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Apr 2024 22:56:55 +0530 Subject: [PATCH 2605/3167] Create README - LeetHub --- 0079-word-search/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0079-word-search/README.md 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 From 0115b002abccc1dcd51881b042a05d2901298097 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:31:37 +0530 Subject: [PATCH 2606/3167] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1614-maximum-nesting-depth-of-the-parentheses/README.md 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 From d21645bdaa081717fd29d069de5f9badeb2771f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:31:41 +0530 Subject: [PATCH 2607/3167] Time: 0 ms (100.00%), Space: 7.5 MB (22.85%) - LeetHub --- ...aximum-nesting-depth-of-the-parentheses.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp 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 From 2610c60a13460d4153a8454adc7b92ed1513f20c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:32:16 +0530 Subject: [PATCH 2608/3167] Attach NOTES - LeetHub --- 1614-maximum-nesting-depth-of-the-parentheses/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1614-maximum-nesting-depth-of-the-parentheses/NOTES.md 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 From 9942a4081abf354e109e79caf2bcd6a01138d392 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:32:19 +0530 Subject: [PATCH 2609/3167] Time: 0 ms (100.00%), Space: 7.5 MB (22.85%) - LeetHub From ea55707fc31040eab690556f714d3f69b915311f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:18:38 +0530 Subject: [PATCH 2610/3167] Create README - LeetHub --- 1544-make-the-string-great/README.md | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1544-make-the-string-great/README.md 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 From 2cca864da4df889fc3290748edd6c32979e7a25d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:18:39 +0530 Subject: [PATCH 2611/3167] Attach NOTES - LeetHub --- 1544-make-the-string-great/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1544-make-the-string-great/NOTES.md 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 From 655b78b9a8ca18a583ef312bc5c4feb543cd7ebc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:18:42 +0530 Subject: [PATCH 2612/3167] Time: 4 ms (42.41%), Space: 7.4 MB (98.61%) - LeetHub --- .../1544-make-the-string-great.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1544-make-the-string-great/1544-make-the-string-great.cpp 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 From d8937c7e23a747f855774a7239953a9f3851967b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Apr 2024 20:13:38 +0530 Subject: [PATCH 2613/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1249-minimum-remove-to-make-valid-parentheses/README.md 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 From 17d553e2919ea9976eef6304ea77b22936b4d211 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Apr 2024 20:13:42 +0530 Subject: [PATCH 2614/3167] Time: 27 ms (22.35%), Space: 13.2 MB (44.22%) - LeetHub --- ...nimum-remove-to-make-valid-parentheses.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp 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 From b4d292f7993cd58e7c41246f6349df6a09eff77e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Apr 2024 23:49:45 +0530 Subject: [PATCH 2615/3167] Create README - LeetHub --- 0678-valid-parenthesis-string/README.md | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0678-valid-parenthesis-string/README.md 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 From 6709998d3cb8f0ce81a805b30c18b082a29650b7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Apr 2024 23:49:45 +0530 Subject: [PATCH 2616/3167] Attach NOTES - LeetHub --- 0678-valid-parenthesis-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0678-valid-parenthesis-string/NOTES.md 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 From d81e6f746d6b65ee542194e54516875125949f4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:18:43 +0530 Subject: [PATCH 2617/3167] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1700-number-of-students-unable-to-eat-lunch/README.md 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 From fa5c75c8c5b80a7c85cf6a87066465e21af83562 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:18:48 +0530 Subject: [PATCH 2618/3167] Time: 0 ms (100.00%), Space: 10.8 MB (79.46%) - LeetHub --- ...number-of-students-unable-to-eat-lunch.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp 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 From a5aacb79efefa6f452d89311d2abcd185779d921 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:19:41 +0530 Subject: [PATCH 2619/3167] Attach NOTES - LeetHub --- 1700-number-of-students-unable-to-eat-lunch/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1700-number-of-students-unable-to-eat-lunch/NOTES.md 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 From 32a6be6d2f58b2159abeed39f25f494566c79bc0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:19:44 +0530 Subject: [PATCH 2620/3167] Time: 0 ms (100.00%), Space: 10.8 MB (79.46%) - LeetHub From 6002f743cb2d5af1ba7ad49f86e12575006b9ca7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:52:47 +0530 Subject: [PATCH 2621/3167] Create README - LeetHub --- .../README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 3108-minimum-cost-walk-in-weighted-graph/README.md 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 From 64fdfcf9602e543a9a3165dc6f668764e66c6390 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:52:50 +0530 Subject: [PATCH 2622/3167] Time: 322 ms (77.78%), Space: 151.8 MB (77.78%) - LeetHub --- ...08-minimum-cost-walk-in-weighted-graph.cpp | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp 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 From 810f698f8dbde18de1de6318681a9c0ef8e9a249 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:00:23 +0530 Subject: [PATCH 2623/3167] Attach NOTES - LeetHub --- 2073-time-needed-to-buy-tickets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2073-time-needed-to-buy-tickets/NOTES.md 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 From c26a83aa006e53e6e2174ae6c6b5bbfca70f8a08 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:00:25 +0530 Subject: [PATCH 2624/3167] Create README - LeetHub --- 2073-time-needed-to-buy-tickets/README.md | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2073-time-needed-to-buy-tickets/README.md 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 From b34befda1d709b32b0a878c8adc75e300011610a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:00:41 +0530 Subject: [PATCH 2625/3167] Time: 5 ms (22.66%), Space: 9.5 MB (46.11%) - LeetHub --- .../2073-time-needed-to-buy-tickets.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp 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 From feb610492990501e4ff01f85be5301e8db6073ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:03:52 +0530 Subject: [PATCH 2626/3167] Attach NOTES - LeetHub From dcebcb190e39f987017bce1015361a288cf2410a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:06:02 +0530 Subject: [PATCH 2627/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0950-reveal-cards-in-increasing-order/README.md 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 From e435e759087f3debcdadc4b0861854ecb9a4fd30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:06:02 +0530 Subject: [PATCH 2628/3167] Attach NOTES - LeetHub --- 0950-reveal-cards-in-increasing-order/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0950-reveal-cards-in-increasing-order/NOTES.md 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 From 2457c122204481abdce0a3867a57c020b7dbf390 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:06:05 +0530 Subject: [PATCH 2629/3167] Time: 8 ms (18.53%), Space: 12.9 MB (5.03%) - LeetHub --- .../0950-reveal-cards-in-increasing-order.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp 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 From 78670902025f42bdc8e2d717e69d7cff1979eb70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:52:37 +0530 Subject: [PATCH 2630/3167] Attach NOTES - LeetHub From 1621827d163318a5ffc816c03c5563320db55366 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:52:40 +0530 Subject: [PATCH 2631/3167] Time: 15 ms (19.57%), Space: 11.7 MB (9.05%) - LeetHub --- 0402-remove-k-digits/0402-remove-k-digits.cpp | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/0402-remove-k-digits/0402-remove-k-digits.cpp b/0402-remove-k-digits/0402-remove-k-digits.cpp index a7e3602b..3a06c361 100644 --- a/0402-remove-k-digits/0402-remove-k-digits.cpp +++ b/0402-remove-k-digits/0402-remove-k-digits.cpp @@ -2,47 +2,39 @@ class Solution { public: string removeKdigits(string num, int k) { - if(k >= num.size()) - return "0"; + stack st; - stack st; + st.push(num[0] - '0'); - int n = num.size(); - - st.push(num[0]); - - for(int i = 1; i < n; ++i) + for(int i = 1; i < num.size(); ++i) { - while(k > 0 and !st.empty() and st.top() > num[i]) + while(!st.empty() and st.top() > (num[i] - '0') and k) { - --k; st.pop(); + k--; } - st.push(num[i]); - if(st.size() == 1 and num[i] == '0') + st.push(num[i] - '0'); + if(st.size() == 1 and st.top() == 0) st.pop(); } - while(k and !st.empty()) + while(!st.empty() and k--) { - --k; st.pop(); } - - string res; + + string ans; while(!st.empty()) { - res += st.top(); + ans += (st.top() + '0'); st.pop(); } - if(res.empty()) - return "0"; + reverse(ans.begin(), ans.end()); - reverse(res.begin(), res.end()); + return ans.empty() ? "0" : ans; - return res; } }; \ No newline at end of file From 6b654bda8fd07fe48211a8c355485981fbdd0399 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 Apr 2024 22:44:55 +0530 Subject: [PATCH 2632/3167] Create README - LeetHub --- 0042-trapping-rain-water/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0042-trapping-rain-water/README.md 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 From e27b1ef5332f7940653dcbce48d97d018f171743 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 Apr 2024 22:44:58 +0530 Subject: [PATCH 2633/3167] Time: 4 ms (93.97%), Space: 22.3 MB (58.72%) - LeetHub --- .../0042-trapping-rain-water.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0042-trapping-rain-water/0042-trapping-rain-water.cpp 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..2f7a6ad3 --- /dev/null +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int trap(vector& height) { + + int start = 0,end = height.size()-1; + int ans = 0; + int maxleft = height[start], maxright = height[end]; + while(start <= end) + { + maxleft = max(maxleft,height[start]); + maxright = max(maxright,height[end]); + + ans += min(maxleft,maxright) - min(height[start],height[end]); + if(height[start] < height[end]) + ++start; + else + --end; + } + + return ans; + } +}; + From 9d802eb8f809dc46ead11b93b7b2f48749792b18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:02:47 +0530 Subject: [PATCH 2634/3167] Attach NOTES - LeetHub --- 0085-maximal-rectangle/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0085-maximal-rectangle/NOTES.md 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 From 12ff8015f70c1e774f5e1d08265211f45b9bd099 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:08:43 +0530 Subject: [PATCH 2635/3167] Attach NOTES - LeetHub From eb82674d8a3fb7c099bd17a2e30c3ed6ae36b7d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:38:08 +0530 Subject: [PATCH 2636/3167] Create README - LeetHub --- 0404-sum-of-left-leaves/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0404-sum-of-left-leaves/README.md 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 From 9838089d584e916bf71a206832e7889637cc607b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:38:13 +0530 Subject: [PATCH 2637/3167] Time: 3 ms (56.94%), Space: 14.6 MB (81.87%) - LeetHub --- .../0404-sum-of-left-leaves.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp 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 From 886fc05eddf5055de3da3276f2fb012df3140047 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:48:11 +0530 Subject: [PATCH 2638/3167] Attach NOTES - LeetHub --- 1004-max-consecutive-ones-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1004-max-consecutive-ones-iii/NOTES.md 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 From 99b5c184ad83e02e97b56949f7f153c50afc1428 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:48:15 +0530 Subject: [PATCH 2639/3167] Time: 40 ms (63.04%), Space: 57.8 MB (49.20%) - LeetHub --- .../1004-max-consecutive-ones-iii.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp 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 From 533eb3932fbe0d0a5176983859d11f61ab2b47db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:21:00 +0530 Subject: [PATCH 2640/3167] Create README - LeetHub --- 0623-add-one-row-to-tree/README.md | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0623-add-one-row-to-tree/README.md 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 From ede2ba4bb659b27fc1224a6fa2574481694e5bf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:21:05 +0530 Subject: [PATCH 2641/3167] Time: 17 ms (22.67%), Space: 24.6 MB (14.90%) - LeetHub --- .../0623-add-one-row-to-tree.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp 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 From 19be9f646349c0d97531c81c25b0776808d5da14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:35:31 +0530 Subject: [PATCH 2642/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0988-smallest-string-starting-from-leaf/README.md 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 From bf33b4dc87398435402d9414c2aa4678a78ce5bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:35:34 +0530 Subject: [PATCH 2643/3167] Time: 7 ms (80.26%), Space: 20.4 MB (67.10%) - LeetHub --- ...988-smallest-string-starting-from-leaf.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp 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 From 1796dab2294f1dfe48af278844460aa75490ac69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Apr 2024 08:57:58 +0530 Subject: [PATCH 2644/3167] Attach NOTES - LeetHub From af2467b8ec553ca698205caeb19be5fcd217b07e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Apr 2024 08:58:02 +0530 Subject: [PATCH 2645/3167] Time: 47 ms (14.06%), Space: 23.5 MB (21.40%) - LeetHub --- .../0200-number-of-islands.cpp | 101 +++++++++++++----- 1 file changed, 74 insertions(+), 27 deletions(-) diff --git a/0200-number-of-islands/0200-number-of-islands.cpp b/0200-number-of-islands/0200-number-of-islands.cpp index 7a793f47..29d1a177 100644 --- a/0200-number-of-islands/0200-number-of-islands.cpp +++ b/0200-number-of-islands/0200-number-of-islands.cpp @@ -1,52 +1,99 @@ -class Solution { -public: - - vector dy = {-1, +1, 0, 0}; - vector dx = {0, 0, -1, +1}; +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; + } - bool isValid(int i, int j, int n, int m) - { - return (i >= 0 and j >= 0 and i < n and j < m); - } + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } - void dfs(int i, int j, int n, int m, vector>& grid, vector>& visited) - { - visited[i][j] = true; - - for(int k = 0; k < 4; ++k) + void unionBySize(int u, int v) { - int newx = dx[k] + i; - int newy = dy[k] + j; + int parU = findParent(u); + int parV = findParent(v); - if(isValid(newx, newy, n, m) and !visited[newx][newy] and grid[newx][newy] == '1') + if(parU == parV) + return; + + if(size[parU] < size[parV]) { - dfs(newx, newy, n, m, grid, visited); + 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(); - int m = grid[0].size(); + 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); - int numberOfIslands = 0; + set ls; - vector> visited(n, vector(m, false)); + int island = 0; for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { - if(grid[i][j] == '1' and !visited[i][j]) + if(grid[i][j] == '1') { - ++numberOfIslands; - dfs(i, j, n, m, grid, visited); + 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); + } + } + } } } } - return numberOfIslands; + for(auto& node : ls) + { + if(dsu.findParent(node) == node) + ++island; + } + + return island; } }; \ No newline at end of file From b4217dbc39c85a9824c099d2ad247f049162b731 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Apr 2024 09:47:31 +0530 Subject: [PATCH 2646/3167] Create README - LeetHub --- 1992-find-all-groups-of-farmland/README.md | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1992-find-all-groups-of-farmland/README.md 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 From bc7e91f0348101755bc44b8f8fdf799d8c9af052 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Apr 2024 09:47:34 +0530 Subject: [PATCH 2647/3167] Time: 167 ms (34.91%), Space: 83.8 MB (40.73%) - LeetHub --- .../1992-find-all-groups-of-farmland.cpp | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp 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 From 803105da53206b86b05a38186c1ed9b22d9f326c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Apr 2024 22:27:37 +0530 Subject: [PATCH 2648/3167] Create README - LeetHub --- 1971-find-if-path-exists-in-graph/README.md | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1971-find-if-path-exists-in-graph/README.md 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 From 36b463b084d73d43f247511a4c2b921eb2d979c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:47:09 +0530 Subject: [PATCH 2649/3167] Create README - LeetHub --- 0752-open-the-lock/README.md | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0752-open-the-lock/README.md 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 From ba7c11fed6037328d1dd57c31eb92bf0cb5f0aad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:47:13 +0530 Subject: [PATCH 2650/3167] Time: 490 ms (11.35%), Space: 40.2 MB (53.65%) - LeetHub --- 0752-open-the-lock/0752-open-the-lock.cpp | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0752-open-the-lock/0752-open-the-lock.cpp 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 From 1d3efba18dc59079d1f9066c07ab1ed3a31d80e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Apr 2024 23:38:10 +0530 Subject: [PATCH 2651/3167] Create README - LeetHub --- 0310-minimum-height-trees/README.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0310-minimum-height-trees/README.md diff --git a/0310-minimum-height-trees/README.md b/0310-minimum-height-trees/README.md new file mode 100644 index 00000000..de5a228a --- /dev/null +++ b/0310-minimum-height-trees/README.md @@ -0,0 +1,34 @@ +

310. 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 From 86c6de911f2527cbee11386d474079e32bfce9e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Apr 2024 23:38:14 +0530 Subject: [PATCH 2652/3167] Time: 99 ms (86.83%), Space: 55.9 MB (72.52%) - LeetHub --- .../0310-minimum-height-trees.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0310-minimum-height-trees/0310-minimum-height-trees.cpp 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; i Date: Thu, 25 Apr 2024 22:51:07 +0530 Subject: [PATCH 2653/3167] Create README - LeetHub --- 2370-longest-ideal-subsequence/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2370-longest-ideal-subsequence/README.md 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 From 880c22761bb6a3a96714a63328e02ae09829d3cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Apr 2024 22:51:10 +0530 Subject: [PATCH 2654/3167] Time: 624 ms (6.72%), Space: 480.1 MB (6.30%) - LeetHub --- .../2370-longest-ideal-subsequence.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp 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 From f139a943b4ce004418601ef7d7d5f14e11d2fa09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:55:38 +0530 Subject: [PATCH 2655/3167] Create README - LeetHub --- 1289-minimum-falling-path-sum-ii/README.md | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1289-minimum-falling-path-sum-ii/README.md 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 From d806fb97def4b8a46e90e8945c5e0c0f367f2f13 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:55:42 +0530 Subject: [PATCH 2656/3167] Time: 355 ms (23.00%), Space: 18.3 MB (16.18%) - LeetHub --- .../1289-minimum-falling-path-sum-ii.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp 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 From 885fd3384839dfc4dd73c37f906a8521c4f71410 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:27:14 +0530 Subject: [PATCH 2657/3167] Attach NOTES - LeetHub --- 0514-freedom-trail/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0514-freedom-trail/NOTES.md 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 From 16ac2d0c5e57304fbb928aff1d2e8c685c861eab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:27:17 +0530 Subject: [PATCH 2658/3167] Time: 22 ms (55.19%), Space: 14.8 MB (58.02%) - LeetHub From b0e9ac7492c8a28d5d71f25571c03f64d4acab37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:46:23 +0530 Subject: [PATCH 2659/3167] Attach NOTES - LeetHub --- 0834-sum-of-distances-in-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0834-sum-of-distances-in-tree/NOTES.md 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 From ae9b0d8ccceb3aacbeececeb48607a0e22112fb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:52:56 +0530 Subject: [PATCH 2660/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md 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 From a21d5cfba36aa598f23dfde477e622eff2d223f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:52:57 +0530 Subject: [PATCH 2661/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md 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 From 807504cb27bc690ca7433cea670df9fc2523c9bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:52:59 +0530 Subject: [PATCH 2662/3167] Time: 181 ms (9.95%), Space: 91.5 MB (12.57%) - LeetHub --- ...perations-to-make-array-xor-equal-to-k.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 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 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 From e9cf04023cbe1826983569babcf8e2c76973ebda Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:10 +0530 Subject: [PATCH 2663/3167] Attach NOTES - LeetHub From 409aa24c3672e05d563a8bfa29655a32b1cb5a78 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:14 +0530 Subject: [PATCH 2664/3167] Time: 181 ms (9.95%), Space: 91.5 MB (12.57%) - LeetHub From 9373e7bd4d25d2a22ffc8a638fbbd04770fe9485 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:18 +0530 Subject: [PATCH 2665/3167] Attach NOTES - LeetHub From bc1f602952f6d4b3c7de4d81bcaeccb9909b4b58 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:21 +0530 Subject: [PATCH 2666/3167] Time: 181 ms (9.95%), Space: 91.5 MB (12.57%) - LeetHub From 848eb53c7a8f2a43553669c5aab60b81c0706dbc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 08:37:57 +0530 Subject: [PATCH 2667/3167] Create README - LeetHub --- 0148-sort-list/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0148-sort-list/README.md 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 From 1186f6b16297f63a0146f03d03bc0709f276317e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 08:38:01 +0530 Subject: [PATCH 2668/3167] Time: 118 ms (66.56%), Space: 56.8 MB (63.53%) - LeetHub --- 0148-sort-list/0148-sort-list.cpp | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0148-sort-list/0148-sort-list.cpp 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 From ae216a4d64a88e30c4c33473f250a05d8de5f878 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:26 +0530 Subject: [PATCH 2669/3167] Attach NOTES - LeetHub From b38698d27ab03e5b48874f670130523e0ec7f862 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:29 +0530 Subject: [PATCH 2670/3167] Time: 11 ms (65.39%), Space: 22.1 MB (40.22%) - LeetHub --- .../0543-diameter-of-binary-tree.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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 index c381414a..e64aec71 100644 --- a/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp +++ b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp @@ -12,22 +12,23 @@ class Solution { public: - int helper(TreeNode* root, int& maxi) + 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); - int left = helper(root->left, maxi); - int right = helper(root->right, maxi); - - maxi = max(maxi, left + right); - return 1 + max(left, right); + return 1 + max(left, right); } int diameterOfBinaryTree(TreeNode* root) { - int maxi = 0; - helper(root, maxi); - return maxi; + int ans = 0; + helper(root, ans); + return ans; } }; \ No newline at end of file From c7242b8735765c936418139103b7692f87e4cb30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:52 +0530 Subject: [PATCH 2671/3167] Attach NOTES - LeetHub From 7f04e1a4faa345218a386a00cf5d4614bb1c76f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:55 +0530 Subject: [PATCH 2672/3167] Time: 11 ms (65.39%), Space: 22.1 MB (40.22%) - LeetHub From 3072a32c9631610d721eaf66d7260c5a01404d19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:50:01 +0530 Subject: [PATCH 2673/3167] Create README - LeetHub --- 0912-sort-an-array/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0912-sort-an-array/README.md 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 From 4cf0a37005c73932affaeb120e698012cc6ab8c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:15:09 +0530 Subject: [PATCH 2674/3167] Create README - LeetHub --- 0009-palindrome-number/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0009-palindrome-number/README.md 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:

+ +
    +
  • -231 <= x <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without converting the integer to a string?
\ No newline at end of file From d60c320f226997d2b525ec903bbe772cb94f0992 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:15:12 +0530 Subject: [PATCH 2675/3167] Time: 4 ms (84.53%), Space: 8.1 MB (96.47%) - LeetHub --- .../0009-palindrome-number.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0009-palindrome-number/0009-palindrome-number.cpp diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp new file mode 100644 index 00000000..f6243bdb --- /dev/null +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + bool isPalindrome(int x) { + + if(x < 0) + return false; + + int curr = 0, num = x; + + while(num > 0) + { + int rem = num % 10; + num /= 10; + + if(curr > INT_MAX/10 or curr == INT_MAX/10 and rem > INT_MAX%10) + return false; + + curr = (curr * 10) + rem; + } + cout< Date: Mon, 29 Apr 2024 10:15:19 +0530 Subject: [PATCH 2676/3167] Attach NOTES - LeetHub --- 0009-palindrome-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0009-palindrome-number/NOTES.md 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 From ad2da2847292567f8703f6eae4af8246d93344d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:15:23 +0530 Subject: [PATCH 2677/3167] Time: 12 ms (34.42%), Space: 8.1 MB (79.20%) - LeetHub --- 0009-palindrome-number/0009-palindrome-number.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp index f6243bdb..a34a0ab8 100644 --- a/0009-palindrome-number/0009-palindrome-number.cpp +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -17,7 +17,7 @@ class Solution { curr = (curr * 10) + rem; } - cout< Date: Mon, 29 Apr 2024 10:17:48 +0530 Subject: [PATCH 2678/3167] Attach NOTES - LeetHub From c8b502b47b867b4283384ad6498d61bf02956f7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:17:52 +0530 Subject: [PATCH 2679/3167] Time: 12 ms (34.42%), Space: 8.1 MB (79.20%) - LeetHub From 8ac70c338061b8658309ef4825525144d2a7a959 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:51:10 +0530 Subject: [PATCH 2680/3167] Attach NOTES - LeetHub From d534925840b0f83bdefcb9b1d20ac8854d0aef9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:51:13 +0530 Subject: [PATCH 2681/3167] Time: 11 ms (42.84%), Space: 8.1 MB (79.20%) - LeetHub --- .../0009-palindrome-number.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp index a34a0ab8..0d8a867c 100644 --- a/0009-palindrome-number/0009-palindrome-number.cpp +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -2,23 +2,17 @@ class Solution { public: bool isPalindrome(int x) { - if(x < 0) + if(x < 0 or (x % 10 == 0 and x != 0)) return false; - int curr = 0, num = x; + int rev = 0; - while(num > 0) + while(rev < x) { - int rem = num % 10; - num /= 10; - - if(curr > INT_MAX/10 or curr == INT_MAX/10 and rem > INT_MAX%10) - return false; - - curr = (curr * 10) + rem; + rev = (rev * 10) + (x%10); + x /= 10; } - - return curr == x; + return (rev == x or rev/10 == x); } }; \ No newline at end of file From 50df3e309a16b531a1c59347c70314b6b57b2480 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 Apr 2024 22:55:28 +0530 Subject: [PATCH 2682/3167] Create README - LeetHub --- 1915-number-of-wonderful-substrings/README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 1915-number-of-wonderful-substrings/README.md 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 From 81e2413ca6d98f07b0924895cfbb37a6f3109a06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 Apr 2024 22:55:31 +0530 Subject: [PATCH 2683/3167] Time: 53 ms (68.86%), Space: 16.6 MB (59.88%) - LeetHub --- .../1915-number-of-wonderful-substrings.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp 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 From b2b9c731a0b1dd2213e6cd74199a4d5e760c4195 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 May 2024 19:52:40 +0530 Subject: [PATCH 2684/3167] Attach NOTES - LeetHub --- 2000-reverse-prefix-of-word/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2000-reverse-prefix-of-word/NOTES.md 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 From 7d0813cf38959fa30ae7cd88a64a9564adb22883 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 May 2024 19:52:43 +0530 Subject: [PATCH 2685/3167] Time: 3 ms (42.21%), Space: 7.6 MB (26.16%) - LeetHub --- .../2000-reverse-prefix-of-word.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp 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 From c6559e8e4e3c126dc165e4b3ef943040da997db5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 May 2024 19:54:49 +0530 Subject: [PATCH 2686/3167] Attach NOTES - LeetHub From 88bd18d6ae2b061bb8ef1aca56dde5b3639f8233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 May 2024 08:11:40 +0530 Subject: [PATCH 2687/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2441-largest-positive-integer-that-exists-with-its-negative/README.md 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 From 4885dcac83bae027693648e7d9fae5c2029db295 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 May 2024 08:11:44 +0530 Subject: [PATCH 2688/3167] Time: 25 ms (29.95%), Space: 25.6 MB (39.47%) - LeetHub --- ...-integer-that-exists-with-its-negative.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp 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 From 638881edf0911d72c7865c9692c5f7a6c897bb97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 May 2024 08:12:43 +0530 Subject: [PATCH 2689/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md 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 From 136c80e7a7f742ac8e3e9d2769373e7cde149e6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 May 2024 23:22:59 +0530 Subject: [PATCH 2690/3167] Create README - LeetHub --- 0165-compare-version-numbers/README.md | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0165-compare-version-numbers/README.md 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 From 74226ab51f406c9edf52b1cc72ae6d85abacd266 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 May 2024 23:23:03 +0530 Subject: [PATCH 2691/3167] Time: 0 ms (100.00%), Space: 7.3 MB (77.28%) - LeetHub --- .../0165-compare-version-numbers.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0165-compare-version-numbers/0165-compare-version-numbers.cpp 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 From c91a44690c91a44ad139a5a3fcae0046e34a4912 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 May 2024 23:19:38 +0530 Subject: [PATCH 2692/3167] Attach NOTES - LeetHub --- 0881-boats-to-save-people/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0881-boats-to-save-people/NOTES.md 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 From eb222f038221e6e00d51cad8e08678f3a2be58f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 May 2024 23:19:41 +0530 Subject: [PATCH 2693/3167] Time: 52 ms (82.35%), Space: 45.5 MB (46.86%) - LeetHub --- .../0881-boats-to-save-people.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0881-boats-to-save-people/0881-boats-to-save-people.cpp 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 From 220fa3f95c378a1abf85321c4ec2b61d1a15800a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:32:38 +0530 Subject: [PATCH 2694/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 3138-minimum-length-of-anagram-concatenation/README.md 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 From b9005d86e4ccb10e447c30830d5522dd9e7f5878 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:32:41 +0530 Subject: [PATCH 2695/3167] Time: 35 ms (33.33%), Space: 13.1 MB (33.33%) - LeetHub --- ...inimum-length-of-anagram-concatenation.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp 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 From 5259a030bb6a138e6cb7bb746a244642be271565 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:51:30 +0530 Subject: [PATCH 2696/3167] Attach NOTES - LeetHub --- 3138-minimum-length-of-anagram-concatenation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3138-minimum-length-of-anagram-concatenation/NOTES.md 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 From c7838ffee239c464282a45c4f50f5e13082349f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:51:33 +0530 Subject: [PATCH 2697/3167] Time: 35 ms (33.33%), Space: 13.1 MB (33.33%) - LeetHub From 866bb6d8e1b0733e1199fa9eb23c22ed52a26661 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:53:47 +0530 Subject: [PATCH 2698/3167] Attach NOTES - LeetHub From 8472cf020e2471b506f75cff50c40fecd4f021f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:53:50 +0530 Subject: [PATCH 2699/3167] Time: 35 ms (33.33%), Space: 13.1 MB (33.33%) - LeetHub From 86aa8ebd7c8273515e6fb1ca4f6a7033d5b69778 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:30:47 +0530 Subject: [PATCH 2700/3167] Create README - LeetHub --- .../README.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 3137-minimum-number-of-operations-to-make-word-k-periodic/README.md 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 From 5b726352d46025dd9390ef8ee45f4740dc8397a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:30:50 +0530 Subject: [PATCH 2701/3167] Time: 191 ms (25.00%), Space: 34.8 MB (12.50%) - LeetHub --- ...-of-operations-to-make-word-k-periodic.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp 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 From 3d314f650892ea2aec26b26ab951f126db6c3a0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:31:46 +0530 Subject: [PATCH 2702/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md 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 From e7aea0418ceb1d408b33e7f5384c7ccc4429ecb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:31:49 +0530 Subject: [PATCH 2703/3167] Time: 191 ms (25.00%), Space: 34.8 MB (12.50%) - LeetHub From cda1484bfe31c6d358ee9eabe26c6e00fcb5c3ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:33:27 +0530 Subject: [PATCH 2704/3167] Attach NOTES - LeetHub From 1c2c594486fc31ebfe381482a3eee32d999d059b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:08:08 +0530 Subject: [PATCH 2705/3167] Attach NOTES - LeetHub --- 3139-minimum-cost-to-equalize-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3139-minimum-cost-to-equalize-array/NOTES.md 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 From e84c6f8bfb24b7f88f380a800fd632b4142111f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:08:12 +0530 Subject: [PATCH 2706/3167] Time: 201 ms (75.00%), Space: 92.6 MB (75.00%) - LeetHub --- .../3139-minimum-cost-to-equalize-array.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp 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 From dcef3460a4129ea8151ef686221490d5027bf127 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:16:26 +0530 Subject: [PATCH 2707/3167] Create README - LeetHub --- 0237-delete-node-in-a-linked-list/README.md | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0237-delete-node-in-a-linked-list/README.md 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 From 35a6a19b5ea43a77c58caaa5929d07d483e4d0e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:16:30 +0530 Subject: [PATCH 2708/3167] Time: 7 ms (37.88%), Space: 11.4 MB (43.54%) - LeetHub --- .../0237-delete-node-in-a-linked-list.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp 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 From f1f336daa8cfa95ef6266382d639cb2f2e5702b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:17:24 +0530 Subject: [PATCH 2709/3167] Attach NOTES - LeetHub --- 0237-delete-node-in-a-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0237-delete-node-in-a-linked-list/NOTES.md 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 From c0ef1f8951574ea39d0cbebdc96cf883f8f152c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:17:25 +0530 Subject: [PATCH 2710/3167] Attach NOTES - LeetHub From e8386485e3f169bd1929fef7096c9c3f71da0fdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:17:27 +0530 Subject: [PATCH 2711/3167] Time: 7 ms (37.88%), Space: 11.4 MB (43.54%) - LeetHub From 7f48d7f361f9f6316e0e2e5ad3517c4d300c4fe8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:32 +0530 Subject: [PATCH 2712/3167] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2816-double-a-number-represented-as-a-linked-list/README.md 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 From e056c4f35b997855765b2df2a0ec85d6eefe53e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:36 +0530 Subject: [PATCH 2713/3167] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub --- ...-a-number-represented-as-a-linked-list.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp 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..a10cd124 --- /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,61 @@ +/** + * 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* reverse(ListNode* head) + { + ListNode* prev = nullptr; + + while(head) + { + ListNode* nex = head->next; + head->next = prev; + prev = head; + head = nex; + } + return prev; + } + + ListNode* add(ListNode* head) + { + ListNode* temp = head, *prev = nullptr; + + int carry = 0; + + while(temp) + { + prev = temp; + int sum = 0; + sum += (2 * temp->val) + carry; + temp->val = (sum%10); + carry = sum / 10; + temp = temp->next; + } + + if(carry) + { + prev->next = new ListNode (carry); + } + + return head; + } + + ListNode* doubleIt(ListNode* head) { + + head = reverse(head); + + head = add(head); + + return reverse(head); + + } +}; \ No newline at end of file From 2e118d2757b90eb0e47af43a9cae2fb8f468057a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:44 +0530 Subject: [PATCH 2714/3167] Attach NOTES - LeetHub --- 2816-double-a-number-represented-as-a-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2816-double-a-number-represented-as-a-linked-list/NOTES.md 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 From b06dd75bd6b4b87db75b13afa5bf218f58b0db14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:47 +0530 Subject: [PATCH 2715/3167] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From d6c144ff41b88f9f377e567dc87c3bc663b59045 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:32 +0530 Subject: [PATCH 2716/3167] Attach NOTES - LeetHub From a5962b2b3f5d6ff35223845619433bd3768cc5db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:33 +0530 Subject: [PATCH 2717/3167] Attach NOTES - LeetHub From 1350e3243ab56078deacf247c5a59460fe880dc5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:35 +0530 Subject: [PATCH 2718/3167] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From 853e0f67e0407486e49d8d3804649c1a45950a0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:37 +0530 Subject: [PATCH 2719/3167] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From bf8cbc18adbe307b39a47138e62cc3f28e71f5c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:38 +0530 Subject: [PATCH 2720/3167] Attach NOTES - LeetHub From c3ee43fc7660c4a9e4e2b9f52941c270bac7c51f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:41 +0530 Subject: [PATCH 2721/3167] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From 8548c1a12192c81eb06dd1ded84336edb76d7736 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:51:55 +0530 Subject: [PATCH 2722/3167] Attach NOTES - LeetHub From a3af5132e7e85b9b83f4373e4b7d1aef213aca81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 08:07:02 +0530 Subject: [PATCH 2723/3167] Attach NOTES - LeetHub From 7cae6b4af6347a36743fe0d1dcaf9957bfea20a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 08:07:05 +0530 Subject: [PATCH 2724/3167] Time: 167 ms (77.26%), Space: 121.3 MB (41.17%) - LeetHub --- ...-a-number-represented-as-a-linked-list.cpp | 45 +++++-------------- 1 file changed, 10 insertions(+), 35 deletions(-) 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 index a10cd124..d5ac5ea5 100644 --- 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 @@ -11,51 +11,26 @@ class Solution { public: - ListNode* reverse(ListNode* head) + int add(ListNode* head) { - ListNode* prev = nullptr; + if(!head) + return 0; - while(head) - { - ListNode* nex = head->next; - head->next = prev; - prev = head; - head = nex; - } - return prev; - } - - ListNode* add(ListNode* head) - { - ListNode* temp = head, *prev = nullptr; + int sum = (head->val * 2) + add(head->next); - int carry = 0; + head->val = sum % 10; - while(temp) - { - prev = temp; - int sum = 0; - sum += (2 * temp->val) + carry; - temp->val = (sum%10); - carry = sum / 10; - temp = temp->next; - } - - if(carry) - { - prev->next = new ListNode (carry); - } - - return head; + return sum / 10; } ListNode* doubleIt(ListNode* head) { - head = reverse(head); + int carry = add(head); - head = add(head); + if(carry) + head = new ListNode(carry, head); - return reverse(head); + return head; } }; \ No newline at end of file From 36ef78692f4b2d6fb524d7fdf46739abb3638016 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 May 2024 21:26:48 +0530 Subject: [PATCH 2725/3167] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 3075-maximize-happiness-of-selected-children/README.md 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 From d9e84f689545bea2cf4ecd38914d8e07ceebe177 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 May 2024 21:26:51 +0530 Subject: [PATCH 2726/3167] Time: 231 ms (10.07%), Space: 108.8 MB (23.42%) - LeetHub --- ...aximize-happiness-of-selected-children.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp 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 From b51a62167892ec8a2d55972eb05bb4faf689a7c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:02:50 +0530 Subject: [PATCH 2727/3167] Attach NOTES - LeetHub --- 0786-k-th-smallest-prime-fraction/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0786-k-th-smallest-prime-fraction/NOTES.md 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 From 3fdbf7faf4e802b4b7ed220002a7af7b60e2ac3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:02:53 +0530 Subject: [PATCH 2728/3167] Time: 618 ms (18.96%), Space: 228.2 MB (11.99%) - LeetHub --- .../0786-k-th-smallest-prime-fraction.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp 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 From ba312f377a057c7348d32ef341eb749daa8464fd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:05:08 +0530 Subject: [PATCH 2729/3167] Attach NOTES - LeetHub From dbe6f38ca784bf4b2f9d5fb96c268e04eb5affcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:05:12 +0530 Subject: [PATCH 2730/3167] Time: 618 ms (18.96%), Space: 228.2 MB (11.99%) - LeetHub From 2d5e274387c4c27185ab3a2ed4f0e46933dbe046 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 May 2024 22:58:06 +0530 Subject: [PATCH 2731/3167] Create README - LeetHub --- 0857-minimum-cost-to-hire-k-workers/README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0857-minimum-cost-to-hire-k-workers/README.md 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 From 41597f76c8cee47aa46d70afd447bd076ce9aae5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 May 2024 22:58:06 +0530 Subject: [PATCH 2732/3167] Attach NOTES - LeetHub --- 0857-minimum-cost-to-hire-k-workers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0857-minimum-cost-to-hire-k-workers/NOTES.md 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 From 883ef23e6fe4cc1abc516d5d95a832e3430ab017 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 May 2024 22:58:09 +0530 Subject: [PATCH 2733/3167] Time: 24 ms (74.02%), Space: 26.5 MB (38.83%) - LeetHub --- .../0857-minimum-cost-to-hire-k-workers.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp 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 From 2361be412c06dd0a3820563f54b4d2eac9c2f131 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 May 2024 21:32:15 +0530 Subject: [PATCH 2734/3167] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2373-largest-local-values-in-a-matrix/README.md 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 From baef01b19bde571a472171185a6a6d4a40722e0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 May 2024 21:32:19 +0530 Subject: [PATCH 2735/3167] Time: 12 ms (37.90%), Space: 14.5 MB (15.10%) - LeetHub --- .../2373-largest-local-values-in-a-matrix.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp 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 From 44a74a33decd37934bf938742c761cfa8b4ea66b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 May 2024 23:30:25 +0530 Subject: [PATCH 2736/3167] Create README - LeetHub --- 1219-path-with-maximum-gold/README.md | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1219-path-with-maximum-gold/README.md 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 From e976664449e2765741fa4899983a829cc43fb7da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 May 2024 23:30:30 +0530 Subject: [PATCH 2737/3167] Time: 288 ms (24.60%), Space: 9.9 MB (44.61%) - LeetHub --- .../1219-path-with-maximum-gold.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp 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 From e885198921def8ffc8e2b47183a5c379cfc12897 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 May 2024 23:02:15 +0530 Subject: [PATCH 2738/3167] Attach NOTES - LeetHub --- 2812-find-the-safest-path-in-a-grid/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2812-find-the-safest-path-in-a-grid/NOTES.md 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 From 7070ace99d6f408fad72164d78d6b8240b4688e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:43:27 +0530 Subject: [PATCH 2739/3167] Create README - LeetHub --- 2331-evaluate-boolean-binary-tree/README.md | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2331-evaluate-boolean-binary-tree/README.md 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 From 7e26773e6bc30dd247ccb45cbda6369d45373f05 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:43:30 +0530 Subject: [PATCH 2740/3167] Time: 7 ms (87.42%), Space: 17.3 MB (64.84%) - LeetHub --- .../2331-evaluate-boolean-binary-tree.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp 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 From 0651e9d4b21216d11251540a37e84ef266280b74 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:16 +0530 Subject: [PATCH 2741/3167] Attach NOTES - LeetHub --- 2331-evaluate-boolean-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2331-evaluate-boolean-binary-tree/NOTES.md 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 From ee22e4b66015a06124cb263d1cc457ae746db36f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:19 +0530 Subject: [PATCH 2742/3167] Time: 7 ms (87.42%), Space: 17.3 MB (64.84%) - LeetHub From 178a4ba28cb554e201fd2e6c7fba7d5abace7cf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:35 +0530 Subject: [PATCH 2743/3167] Attach NOTES - LeetHub From cb62db803f96f71985c61f6ea41fee0d032a45b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:38 +0530 Subject: [PATCH 2744/3167] Time: 7 ms (87.42%), Space: 17.3 MB (64.84%) - LeetHub From 9db98f30e461883bcb0265b544a1bbb4a5a57f93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 23:50:38 +0530 Subject: [PATCH 2745/3167] Attach NOTES - LeetHub --- 0721-accounts-merge/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0721-accounts-merge/NOTES.md 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 From e28c89f662354e53e0ff2d0220b778a472e09105 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 23:51:57 +0530 Subject: [PATCH 2746/3167] Attach NOTES - LeetHub From 7dff6fab0f7a1f01bf672b9e8595ca6db947ab44 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 May 2024 08:58:00 +0530 Subject: [PATCH 2747/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1325-delete-leaves-with-a-given-value/README.md 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 From b7b682b7a23ac5ad87daceb364509af0bc026f10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 May 2024 08:58:03 +0530 Subject: [PATCH 2748/3167] Time: 16 ms (15.19%), Space: 21.4 MB (9.62%) - LeetHub --- .../1325-delete-leaves-with-a-given-value.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp 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 From f79b48e6afbfc86d3c30985044c690bc6f8033de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 09:22:12 +0530 Subject: [PATCH 2749/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0979-distribute-coins-in-binary-tree/README.md 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 From 4be46f6d9062b293aec0a4876d8596cefeefbc63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 09:22:16 +0530 Subject: [PATCH 2750/3167] Time: 4 ms (63.21%), Space: 15.6 MB (58.63%) - LeetHub --- .../0979-distribute-coins-in-binary-tree.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp 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 From 5df6b155da3a23d3b4b911c065071f91f50290c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:56:46 +0530 Subject: [PATCH 2751/3167] Attach NOTES - LeetHub --- 1757-recyclable-and-low-fat-products/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1757-recyclable-and-low-fat-products/NOTES.md 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 From f68986ea87b4eb488db50035bb2a92402dc0399e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:56:48 +0530 Subject: [PATCH 2752/3167] Time: 1210 ms (29.77%), Space: 0B (100.00%) - LeetHub --- .../1757-recyclable-and-low-fat-products.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql 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'; From 48ae66effcf7e6fae759943f89868e7c3d8b1379 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:58:53 +0530 Subject: [PATCH 2753/3167] Attach NOTES - LeetHub From 10d412500e0e5f50e6f8d277b3abaf4ffc5c7762 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:58:56 +0530 Subject: [PATCH 2754/3167] Time: 1210 ms (29.77%), Space: 0B (100.00%) - LeetHub From f60e98b9e797093973e753bea4ec00d5ff469643 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:06 +0530 Subject: [PATCH 2755/3167] Create README - LeetHub --- 0584-find-customer-referee/README.md | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0584-find-customer-referee/README.md 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 From fadf872e7e7c632859de8db7bee500a25e35e2e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:07 +0530 Subject: [PATCH 2756/3167] Attach NOTES - LeetHub --- 0584-find-customer-referee/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0584-find-customer-referee/NOTES.md 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 From 647c7d5d318ceb247c50b4204678b44723a41bfa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:10 +0530 Subject: [PATCH 2757/3167] Time: 1239 ms (17.89%), Space: 0B (100.00%) - LeetHub --- 0584-find-customer-referee/0584-find-customer-referee.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 0584-find-customer-referee/0584-find-customer-referee.sql 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 From 7fd2131b17c7c8f80727330f2682aa9597b18829 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:22 +0530 Subject: [PATCH 2758/3167] Attach NOTES - LeetHub From 626c3600b7441e25abbe5bd85f278d1c0bfbe702 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:26 +0530 Subject: [PATCH 2759/3167] Time: 1239 ms (17.89%), Space: 0B (100.00%) - LeetHub From 43fa845ec5cfeb8f696de398904bbf9947cec243 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:20:04 +0530 Subject: [PATCH 2760/3167] Attach NOTES - LeetHub From a99a3a4537f47021df5b6784a214bd6f6af38a0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:20:06 +0530 Subject: [PATCH 2761/3167] Time: 1239 ms (17.89%), Space: 0B (100.00%) - LeetHub From 002f2e49487f85c795b7b6a68b2d9c96a5f277c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:31:42 +0530 Subject: [PATCH 2762/3167] Attach NOTES - LeetHub --- 0595-big-countries/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0595-big-countries/NOTES.md 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 From 1d1b7a47cbea600cef0147f0e9102dc3f5f2a1ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:31:45 +0530 Subject: [PATCH 2763/3167] Time: 472 ms (69.65%), Space: 0B (100.00%) - LeetHub --- 0595-big-countries/0595-big-countries.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 0595-big-countries/0595-big-countries.sql 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 From dab30d1755c35e97d489658190de1c9af29cfe08 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:32:19 +0530 Subject: [PATCH 2764/3167] Attach NOTES - LeetHub From 7844fe208902f225ecbf6895b747b47793115aac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:35:53 +0530 Subject: [PATCH 2765/3167] Create README - LeetHub --- 1148-article-views-i/README.md | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1148-article-views-i/README.md 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 From 5309c31f13b1038574fac38f69c62056a121b660 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:35:57 +0530 Subject: [PATCH 2766/3167] Time: 808 ms (52.73%), Space: 0B (100.00%) - LeetHub --- 1148-article-views-i/1148-article-views-i.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 1148-article-views-i/1148-article-views-i.sql 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 From f8ff36bcaa25e45cf0c6ac55e58f1e9da3210d96 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:36:11 +0530 Subject: [PATCH 2767/3167] Attach NOTES - LeetHub --- 1148-article-views-i/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1148-article-views-i/NOTES.md 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 From 4c5d44270ee54bef3dbbdbad44c118b16ad3ea52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:36:15 +0530 Subject: [PATCH 2768/3167] Time: 808 ms (52.73%), Space: 0B (100.00%) - LeetHub From d05423539dcf1d56693a3a60047c6e5943aad1d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:40:13 +0530 Subject: [PATCH 2769/3167] Create README - LeetHub --- 1683-invalid-tweets/README.md | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1683-invalid-tweets/README.md 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 From d1317f83e6989396a5c040923cd275e217b62a2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:40:13 +0530 Subject: [PATCH 2770/3167] Attach NOTES - LeetHub --- 1683-invalid-tweets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1683-invalid-tweets/NOTES.md 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 From 7712cd189277db815220f57f518f8c43b1e91683 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:40:17 +0530 Subject: [PATCH 2771/3167] Time: 974 ms (89.05%), Space: 0B (100.00%) - LeetHub --- 1683-invalid-tweets/1683-invalid-tweets.sql | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 1683-invalid-tweets/1683-invalid-tweets.sql diff --git a/1683-invalid-tweets/1683-invalid-tweets.sql b/1683-invalid-tweets/1683-invalid-tweets.sql new file mode 100644 index 00000000..e8274c99 --- /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 length(content) > 15; + From a0e0ec0a3a54e485b91d9b0a243217e2168c25cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:41:27 +0530 Subject: [PATCH 2772/3167] Attach NOTES - LeetHub From 3d72bcb5565c1a014ebd3ec2ce0a57a0706cff28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:41:45 +0530 Subject: [PATCH 2773/3167] Attach NOTES - LeetHub From 3c0531c4e0f077710b4c1e5aa86b21cd1ff4a3d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:41:48 +0530 Subject: [PATCH 2774/3167] Time: 1068 ms (71.40%), Space: 0B (100.00%) - LeetHub --- 1683-invalid-tweets/1683-invalid-tweets.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1683-invalid-tweets/1683-invalid-tweets.sql b/1683-invalid-tweets/1683-invalid-tweets.sql index e8274c99..122290c0 100644 --- a/1683-invalid-tweets/1683-invalid-tweets.sql +++ b/1683-invalid-tweets/1683-invalid-tweets.sql @@ -1,5 +1,5 @@ # Write your MySQL query statement below select tweet_id from Tweets -where length(content) > 15; +where char_length(content) > 15; From d0720a36ea2788c3264b58f7c1616e652b0f80f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:42:15 +0530 Subject: [PATCH 2775/3167] Attach NOTES - LeetHub From 6c436cd3c12d603090711c1743d265905a3547c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:42:19 +0530 Subject: [PATCH 2776/3167] Time: 1068 ms (71.40%), Space: 0B (100.00%) - LeetHub From aba50adffbddf6c7dc3d5e18a30184cbb7ae24b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 May 2024 21:37:35 +0530 Subject: [PATCH 2777/3167] Create README - LeetHub --- .../README.md | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 3154-find-number-of-ways-to-reach-the-k-th-stair/README.md diff --git a/3154-find-number-of-ways-to-reach-the-k-th-stair/README.md b/3154-find-number-of-ways-to-reach-the-k-th-stair/README.md new file mode 100644 index 00000000..d257801d --- /dev/null +++ b/3154-find-number-of-ways-to-reach-the-k-th-stair/README.md @@ -0,0 +1,85 @@ +

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 From ce241145d1125fd05df6753ff6ed3a376d18c4c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 May 2024 21:37:38 +0530 Subject: [PATCH 2778/3167] Time: 247 ms (20.00%), Space: 86.2 MB (20.00%) - LeetHub --- ...number-of-ways-to-reach-the-k-th-stair.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp 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< Date: Sun, 19 May 2024 22:59:04 +0530 Subject: [PATCH 2779/3167] Create README - LeetHub --- .../README.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 3068-find-the-maximum-sum-of-node-values/README.md 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 From 67f7bda3b3a0ad1dbcdb29f44e9259dcf6c411af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 May 2024 22:59:08 +0530 Subject: [PATCH 2780/3167] Time: 178 ms (86.44%), Space: 125.5 MB (88.89%) - LeetHub --- ...68-find-the-maximum-sum-of-node-values.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp 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..71123a5d --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + long long maximumValueSum(vector& nums, int k, vector>& edges) { + + long long sum = 0; + int smallest_gain = 2 * k + 1; + int smallest_lost = 2 * k + 1; + int gain_count = 0; + for (int num : nums) { + int gain = (num ^ k) - num; + if (gain > 0) { + smallest_gain = min(smallest_gain, gain); + sum += num + gain; + ++gain_count; + } else { + smallest_lost = min(smallest_lost, -gain); + sum += num; + } + } + if (gain_count % 2 == 1) { + sum -= min(smallest_gain, smallest_lost); + } + return sum; + + } +}; \ No newline at end of file From 182b2ea8a7360d3aee3fb1643c2ac38643509854 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 09:38:34 +0530 Subject: [PATCH 2781/3167] Attach NOTES - LeetHub --- 3068-find-the-maximum-sum-of-node-values/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3068-find-the-maximum-sum-of-node-values/NOTES.md 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 From 026fcf567787c76aed4238088327e57770a11411 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 09:38:37 +0530 Subject: [PATCH 2782/3167] Time: 189 ms (64.41%), Space: 125.5 MB (89.41%) - LeetHub --- ...68-find-the-maximum-sum-of-node-values.cpp | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) 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 index 71123a5d..704e11a4 100644 --- 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 @@ -2,25 +2,30 @@ class Solution { public: long long maximumValueSum(vector& nums, int k, vector>& edges) { - long long sum = 0; - int smallest_gain = 2 * k + 1; - int smallest_lost = 2 * k + 1; - int gain_count = 0; - for (int num : nums) { - int gain = (num ^ k) - num; - if (gain > 0) { - smallest_gain = min(smallest_gain, gain); - sum += num + gain; - ++gain_count; - } else { - smallest_lost = min(smallest_lost, -gain); - sum += num; - } - } - if (gain_count % 2 == 1) { - sum -= min(smallest_gain, smallest_lost); - } - return sum; + 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 From ca82af92cfc02b6a5f67451436d91ca06a3947cb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 10:16:28 +0530 Subject: [PATCH 2783/3167] Attach NOTES - LeetHub From 20e546c9fb67c42938bcb6194a5685e0d49c1a5d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 10:16:31 +0530 Subject: [PATCH 2784/3167] Time: 189 ms (64.41%), Space: 125.5 MB (89.41%) - LeetHub From 0be13dd859d4bc4152e4597248b3befaeb92d618 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:53 +0530 Subject: [PATCH 2785/3167] Create README - LeetHub --- 1863-sum-of-all-subset-xor-totals/README.md | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 1863-sum-of-all-subset-xor-totals/README.md 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 From 3fa2663857f66874919fcc6b95ae23f384f94e64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:53 +0530 Subject: [PATCH 2786/3167] Attach NOTES - LeetHub --- 1863-sum-of-all-subset-xor-totals/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1863-sum-of-all-subset-xor-totals/NOTES.md 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 From d0ae3a6182f36dd76cf25989706eb8cb3b2d2597 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:56 +0530 Subject: [PATCH 2787/3167] Create README - LeetHub From 05a29f54a838709e3349e0f15405549cad5bef3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:57 +0530 Subject: [PATCH 2788/3167] Time: 7 ms (31.51%), Space: 8.3 MB (65.21%) - LeetHub --- .../1863-sum-of-all-subset-xor-totals.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp 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 From cd8dd215912689f6b9088949dcbadbf1b8069d89 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:11:15 +0530 Subject: [PATCH 2789/3167] Attach NOTES - LeetHub From 24c3c490ed586c466af3d85716d21fc7bc02bc0d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:11:18 +0530 Subject: [PATCH 2790/3167] Time: 7 ms (31.51%), Space: 8.3 MB (65.21%) - LeetHub From d2721033a1e85090efe5acde97bfc58035448063 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:14:57 +0530 Subject: [PATCH 2791/3167] Create README - LeetHub --- 0078-subsets/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0078-subsets/README.md 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 From 79cd980a18773b8f38191c563323beaf2fd07f9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:14:57 +0530 Subject: [PATCH 2792/3167] Attach NOTES - LeetHub --- 0078-subsets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0078-subsets/NOTES.md 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 From 60474471fa2c618412fab825dad24d18714e2556 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:15:00 +0530 Subject: [PATCH 2793/3167] Time: 4 ms (39.89%), Space: 8.3 MB (62.90%) - LeetHub --- 0078-subsets/0078-subsets.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0078-subsets/0078-subsets.cpp 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 From 971a6286b9f23a4c50476f2b1d02e8d2a990cc0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:19:57 +0530 Subject: [PATCH 2794/3167] Create README - LeetHub --- 0090-subsets-ii/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0090-subsets-ii/README.md 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 From f5262777bf331525a20ef71ea0ad22d1c4ad4c67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:19:58 +0530 Subject: [PATCH 2795/3167] Attach NOTES - LeetHub --- 0090-subsets-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0090-subsets-ii/NOTES.md 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 From 02394c59eb5c14e8444c52ec39d6da005220acdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:20:01 +0530 Subject: [PATCH 2796/3167] Time: 5 ms (30.77%), Space: 9.3 MB (44.07%) - LeetHub --- 0090-subsets-ii/0090-subsets-ii.cpp | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0090-subsets-ii/0090-subsets-ii.cpp 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 From ac034f9a789fedae2bbea6d7f6662fda08c5e723 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 May 2024 22:39:15 +0530 Subject: [PATCH 2797/3167] Attach NOTES - LeetHub --- 2597-the-number-of-beautiful-subsets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2597-the-number-of-beautiful-subsets/NOTES.md 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 From 15c1f71fa64d3c80d8f6ea035c1955e712708ad4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 May 2024 22:40:44 +0530 Subject: [PATCH 2798/3167] Attach NOTES - LeetHub From 35027460c9d8da2f6bd6006446e03ed6c3739831 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 May 2024 08:56:35 +0530 Subject: [PATCH 2799/3167] Create README - LeetHub --- 0048-rotate-image/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0048-rotate-image/README.md 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 From 0f913ce22d840206c4f456927a7f8b441ef5d353 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 May 2024 23:26:49 +0530 Subject: [PATCH 2800/3167] Attach NOTES - LeetHub --- 0552-student-attendance-record-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0552-student-attendance-record-ii/NOTES.md 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 From b3cab41ebba3c96c0745ff43622ece7e0b64aa5c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 May 2024 23:26:52 +0530 Subject: [PATCH 2801/3167] Time: 1377 ms (12.88%), Space: 420.3 MB (23.44%) - LeetHub --- .../0552-student-attendance-record-ii.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp 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..250559d7 --- /dev/null +++ b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp @@ -0,0 +1,33 @@ +class Solution { +private: + static const int MOD = 1000000000 + 7; + +public: + int checkRecord(int n) { + vector> prevDP(2, vector(3, 1)); + + for (int i = 1; i <= n; i++) { + vector> newDP(2, vector(3, 0)); + for (int a = 0; a < 2; a++) { + for (int l = 0; l < 3; l++) { + // Pick P + newDP[a][l] += prevDP[a][2]; + newDP[a][l] %= MOD; + if (a > 0) { + // Pick A + newDP[a][l] += prevDP[a - 1][2]; + newDP[a][l] %= MOD; + } + if (l > 0) { + // Pick L + newDP[a][l] += prevDP[a][l - 1]; + newDP[a][l] %= MOD; + } + } + } + prevDP = newDP; + } + + return prevDP[1][2]; + } +}; \ No newline at end of file From cc6f650c6ac74281388df96776a65dc419f6a73e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 09:31:32 +0530 Subject: [PATCH 2802/3167] Attach NOTES - LeetHub From 7871837ca459ef1c2efdd1fb3c8a0d99af543fcc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 09:31:35 +0530 Subject: [PATCH 2803/3167] Time: 1184 ms (29.43%), Space: 399.1 MB (32.97%) - LeetHub --- .../0552-student-attendance-record-ii.cpp | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) 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 index 250559d7..f87965aa 100644 --- a/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp +++ b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp @@ -1,33 +1,41 @@ class Solution { -private: - static const int MOD = 1000000000 + 7; - public: - int checkRecord(int n) { - vector> prevDP(2, vector(3, 1)); - - for (int i = 1; i <= n; i++) { - vector> newDP(2, vector(3, 0)); - for (int a = 0; a < 2; a++) { - for (int l = 0; l < 3; l++) { - // Pick P - newDP[a][l] += prevDP[a][2]; - newDP[a][l] %= MOD; - if (a > 0) { - // Pick A - newDP[a][l] += prevDP[a - 1][2]; - newDP[a][l] %= MOD; - } - if (l > 0) { - // Pick L - newDP[a][l] += prevDP[a][l - 1]; - newDP[a][l] %= MOD; - } - } - } - prevDP = newDP; + + 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 prevDP[1][2]; + + 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 From 3a7c67be390e9bbe97721b4b09d60d86b75ad9ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:49:07 +0530 Subject: [PATCH 2804/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1608-special-array-with-x-elements-greater-than-or-equal-x/README.md 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 From b639cc01609c709ad4c775d6f32c923c4e5f6fcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:49:10 +0530 Subject: [PATCH 2805/3167] Time: 5 ms (33.11%), Space: 10.8 MB (5.12%) - LeetHub --- ...ith-x-elements-greater-than-or-equal-x.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp 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 From 259c7c3e5b0438d7879674d4c727f0993e0e10bb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:50:11 +0530 Subject: [PATCH 2806/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md 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 From c84ba67966fc543ecdb13ca3be8a16fc9f2dfecc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:50:35 +0530 Subject: [PATCH 2807/3167] Attach NOTES - LeetHub From 73b1430bc48cc8abb79cc5536ef519c561a3e4a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:50:39 +0530 Subject: [PATCH 2808/3167] Time: 5 ms (33.11%), Space: 10.8 MB (5.12%) - LeetHub From ba34a0c9d8d5a5b32070cbdc15497c75dc74aec6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 07:45:11 +0530 Subject: [PATCH 2809/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3164-find-the-number-of-good-pairs-ii/README.md 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 From 71556d5f47bf4e46b4ac2d9d408c390b13c5dd90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 07:45:16 +0530 Subject: [PATCH 2810/3167] Time: 1850 ms (25.30%), Space: 201.3 MB (20.19%) - LeetHub --- .../3164-find-the-number-of-good-pairs-ii.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp 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 From 57988780e9eb90b25b1981f5d5934050147fedb6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:01:18 +0530 Subject: [PATCH 2811/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1208-get-equal-substrings-within-budget/README.md 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 From 87c74afc557ca32dfbb128335a876a65908f5712 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:01:21 +0530 Subject: [PATCH 2812/3167] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub --- ...208-get-equal-substrings-within-budget.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp 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..664765f3 --- /dev/null +++ b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int equalSubstring(string s, string t, int maxCost) { + + int n = s.size(); + + map mp; + + for(int i = 0; i < n; ++i) + { + mp[i] = abs(s[i] - t[i]); + } + + int currCost = 0, len = 0; + + int i = 0, j = 0; + + while(j < n) + { + currCost += mp[j]; + + while(currCost > maxCost) + { + currCost -= mp[i]; + ++i; + } + + len = max(len, j - i + 1); + ++j; + } + + return len; + + } +}; \ No newline at end of file From a96244509cf57455bd4f09dfa5e5d856534a95bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:16 +0530 Subject: [PATCH 2813/3167] Attach NOTES - LeetHub --- 1208-get-equal-substrings-within-budget/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1208-get-equal-substrings-within-budget/NOTES.md 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 From 45e1f1e4f9045d1fc6955a8a91b1d6ccedd5668e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:17 +0530 Subject: [PATCH 2814/3167] Attach NOTES - LeetHub From 0024294c37952a4a08c9613dbc3752b12d1169a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:19 +0530 Subject: [PATCH 2815/3167] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub From e309122be54a40ad9c66fddea416296f8e139327 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:20 +0530 Subject: [PATCH 2816/3167] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub From 98e1589177bbf985648c29f1238791c39c16e140 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:03:46 +0530 Subject: [PATCH 2817/3167] Attach NOTES - LeetHub From c6e02613379a200efde2bb491512b2cdc736e9b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:03:49 +0530 Subject: [PATCH 2818/3167] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub From cbdac42d3fd8b49331a6e5581722e85c9c559a77 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:26:46 +0530 Subject: [PATCH 2819/3167] Attach NOTES - LeetHub From 5bb43889dbad8e86cedf2cb104508bcb279a1a0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:26:49 +0530 Subject: [PATCH 2820/3167] Time: 6 ms (62.23%), Space: 8.6 MB (99.71%) - LeetHub --- ...208-get-equal-substrings-within-budget.cpp | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) 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 index 664765f3..430101aa 100644 --- 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 @@ -4,32 +4,23 @@ class Solution { int n = s.size(); - map mp; - - for(int i = 0; i < n; ++i) - { - mp[i] = abs(s[i] - t[i]); - } - - int currCost = 0, len = 0; - - int i = 0, j = 0; + int i = 0, j = 0, ans = 0; while(j < n) { - currCost += mp[j]; + maxCost -= abs(s[j] - t[j]); - while(currCost > maxCost) + if(maxCost < 0) { - currCost -= mp[i]; + maxCost += abs(s[i] - t[i]); ++i; } - len = max(len, j - i + 1); + ans = max(ans, j - i + 1); + ++j; } - return len; - + return ans; } }; \ No newline at end of file From 9b5eaecd2499fba9a2bd47a3e25619bf6009174d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 May 2024 23:32:59 +0530 Subject: [PATCH 2821/3167] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md 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 From c9c41d2ded489e135a444081d5a4c1d07be08757 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 May 2024 23:33:02 +0530 Subject: [PATCH 2822/3167] Time: 3 ms (51.92%), Space: 7.4 MB (76.47%) - LeetHub --- ...number-in-binary-representation-to-one.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 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 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 From 8aa4420fa3e1b1c60fa7bb705adecfe827b17ba9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 May 2024 22:11:22 +0530 Subject: [PATCH 2823/3167] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md 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 From 623a31a533b3cb25c1b76130f4ee31a2c9cf8f1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 May 2024 22:11:25 +0530 Subject: [PATCH 2824/3167] Time: 62 ms (5.12%), Space: 9.1 MB (67.04%) - LeetHub --- ...-that-can-form-two-arrays-of-equal-xor.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp 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; + } +}; From 752000cb4087d7ca0fa6b9122487e74125539984 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 May 2024 12:11:45 +0530 Subject: [PATCH 2825/3167] Attach NOTES - LeetHub --- 0137-single-number-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0137-single-number-ii/NOTES.md 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 From a9f0fb44a495f8bfdd815560c94cba815785d61f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 May 2024 13:48:08 +0530 Subject: [PATCH 2826/3167] Create README - LeetHub --- 0260-single-number-iii/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0260-single-number-iii/README.md 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 From 4eca7552302cf08c403235f3a7bcc98758c2487a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 May 2024 13:48:11 +0530 Subject: [PATCH 2827/3167] Time: 3 ms (96.61%), Space: 12.2 MB (98.70%) - LeetHub --- .../0260-single-number-iii.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0260-single-number-iii/0260-single-number-iii.cpp 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 From f20e5f385022f3a621e6b459c703fb400edc7197 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jun 2024 07:16:57 +0530 Subject: [PATCH 2828/3167] Create README - LeetHub --- 3110-score-of-a-string/README.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 3110-score-of-a-string/README.md 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 From 887fb748b702ebe1c57055aa44bc224cc67f4752 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jun 2024 07:17:01 +0530 Subject: [PATCH 2829/3167] Time: 5 ms (27.27%), Space: 7.8 MB (78.98%) - LeetHub --- .../3110-score-of-a-string.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 3110-score-of-a-string/3110-score-of-a-string.cpp 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 From d6cb37e0c39eba68559bd371346d8403783ee92c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:26 +0530 Subject: [PATCH 2830/3167] Create README - LeetHub --- 0344-reverse-string/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0344-reverse-string/README.md 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 From 6967aca8b49cce74ca03f54f1f3e1f4ca4ef098f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:30 +0530 Subject: [PATCH 2831/3167] Create README - LeetHub From f2e4fba6720a5a63e76b2c80e4681ceb810952b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:30 +0530 Subject: [PATCH 2832/3167] Attach NOTES - LeetHub --- 0344-reverse-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0344-reverse-string/NOTES.md 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 From 8ea33e3354e78142de4318d278b9fae8552c449b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:32 +0530 Subject: [PATCH 2833/3167] Create README - LeetHub From dd635148a70f2a8dbb7e535ac69417a8f9cbc336 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:32 +0530 Subject: [PATCH 2834/3167] Attach NOTES - LeetHub From 94a011df4ab125b6abaf19da67ac07059b057cca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:34 +0530 Subject: [PATCH 2835/3167] Time: 19 ms (47.68%), Space: 26.8 MB (89.96%) - LeetHub --- 0344-reverse-string/0344-reverse-string.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0344-reverse-string/0344-reverse-string.cpp 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 From 7a0abbd236bd5b65daebf43d62debdb895635683 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:36 +0530 Subject: [PATCH 2836/3167] Time: 19 ms (47.68%), Space: 26.8 MB (89.96%) - LeetHub From 50353a74e10dda874d0c671dc0fc57c671e0bb26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:15:29 +0530 Subject: [PATCH 2837/3167] Create README - LeetHub --- 3169-count-days-without-meetings/README.md | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 3169-count-days-without-meetings/README.md 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 From 6f47c24f9f17c27c91d4b5f450455e046b092da0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:15:30 +0530 Subject: [PATCH 2838/3167] Attach NOTES - LeetHub --- 3169-count-days-without-meetings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3169-count-days-without-meetings/NOTES.md 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 From 4f498cfc62d78c587424db53fc93f557c5a5a7c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:15:33 +0530 Subject: [PATCH 2839/3167] Time: 296 ms (33.33%), Space: 141.5 MB (16.67%) - LeetHub --- .../3169-count-days-without-meetings.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3169-count-days-without-meetings/3169-count-days-without-meetings.cpp 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 From e123ccf98c25876cd6487bbd29e8240754853dd5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:32:26 +0530 Subject: [PATCH 2840/3167] Create README - LeetHub --- .../README.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 3171-find-subarray-with-bitwise-and-closest-to-k/README.md 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 From f59dbc361c8b58a4ef85e504f29215c556255fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:32:29 +0530 Subject: [PATCH 2841/3167] Time: 744 ms (100.00%), Space: 345.9 MB (8.33%) - LeetHub --- ...subarray-with-bitwise-and-closest-to-k.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp 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 From 58689e12fabf36805b6f7442a23f0e0f73baa29b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:48:45 +0530 Subject: [PATCH 2842/3167] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2486-append-characters-to-string-to-make-subsequence/README.md 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 From 0c29445cf96b68249a8cdcecead8d24180aa617f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:48:50 +0530 Subject: [PATCH 2843/3167] Time: 19 ms (59.76%), Space: 11.8 MB (83.42%) - LeetHub --- ...haracters-to-string-to-make-subsequence.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp 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 From 29ac9761651fece855352d9d9f12bcaac0d855ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jun 2024 07:56:43 +0530 Subject: [PATCH 2844/3167] Create README - LeetHub --- 0409-longest-palindrome/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0409-longest-palindrome/README.md 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 From ce17bf327ff8eda14001a29a806e0748004ca076 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jun 2024 07:56:43 +0530 Subject: [PATCH 2845/3167] Attach NOTES - LeetHub --- 0409-longest-palindrome/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0409-longest-palindrome/NOTES.md 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 From ced0d06e4ff5812e052975c4e0a5306042cbb4ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jun 2024 07:56:47 +0530 Subject: [PATCH 2846/3167] Time: 7 ms (16.33%), Space: 8.9 MB (48.94%) - LeetHub --- .../0409-longest-palindrome.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0409-longest-palindrome/0409-longest-palindrome.cpp 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 From 1c463e9a899ff9a177882fd857748bb8ae7211dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:29:49 +0530 Subject: [PATCH 2847/3167] Create README - LeetHub --- 1002-find-common-characters/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1002-find-common-characters/README.md 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 From d1e7fce091d24f8377d261e375f771ed02858907 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:29:52 +0530 Subject: [PATCH 2848/3167] Time: 5 ms (76.27%), Space: 11.3 MB (69.23%) - LeetHub --- .../1002-find-common-characters.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1002-find-common-characters/1002-find-common-characters.cpp 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 From d1c3bc2970731be73e8735b7a41dbf932b11078f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:05 +0530 Subject: [PATCH 2849/3167] Attach NOTES - LeetHub --- 1002-find-common-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1002-find-common-characters/NOTES.md 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 From 7c739409f690f9c9d58f95b95dc9d7988dafc8dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:08 +0530 Subject: [PATCH 2850/3167] Time: 5 ms (76.27%), Space: 11.3 MB (69.23%) - LeetHub From 676570b4ca90c858321f9b9f3b28cb94f53b6c5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:19 +0530 Subject: [PATCH 2851/3167] Attach NOTES - LeetHub From 21a7802ffeca1f5b6c92cc4f4ff5803079c17b78 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:23 +0530 Subject: [PATCH 2852/3167] Time: 5 ms (76.27%), Space: 11.3 MB (69.23%) - LeetHub From c02f73e1677a06895ceb006770132fd2d37e1c67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 08:44:22 +0530 Subject: [PATCH 2853/3167] Attach NOTES - LeetHub From 73383e3479e34a22afd565120baa983ba82479e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:11:47 +0530 Subject: [PATCH 2854/3167] Create README - LeetHub --- 0846-hand-of-straights/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0846-hand-of-straights/README.md 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 From 94f36f53bba3372f001e38207d8ca5fa7fed5ff8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:11:52 +0530 Subject: [PATCH 2855/3167] Time: 58 ms (62.52%), Space: 30.6 MB (69.25%) - LeetHub --- .../0846-hand-of-straights.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0846-hand-of-straights/0846-hand-of-straights.cpp 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 From 6cf94a46b737c450867ccdec281217e078dcba7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:12:50 +0530 Subject: [PATCH 2856/3167] Attach NOTES - LeetHub --- 0846-hand-of-straights/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0846-hand-of-straights/NOTES.md 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 From b1b81762f57c265b8ab7ec664aa9f5792054ff11 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:12:54 +0530 Subject: [PATCH 2857/3167] Time: 58 ms (62.52%), Space: 30.6 MB (69.25%) - LeetHub From a6e3eea67403bf6aa4f348809fbd1c4a4bea7a07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:18:38 +0530 Subject: [PATCH 2858/3167] Create README - LeetHub --- 0648-replace-words/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0648-replace-words/README.md 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 From ef0810ff78323b32df4452e74b32884c58270275 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:18:42 +0530 Subject: [PATCH 2859/3167] Time: 63 ms (91.05%), Space: 69.5 MB (77.56%) - LeetHub --- 0648-replace-words/0648-replace-words.cpp | 110 ++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 0648-replace-words/0648-replace-words.cpp 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 From 5793a1ec19645481143731883569d88ea1e498ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:20:06 +0530 Subject: [PATCH 2860/3167] Attach NOTES - LeetHub --- 0648-replace-words/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0648-replace-words/NOTES.md 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 From 2a7e1be07a41472c01c556622702ebba558e8c5a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:20:09 +0530 Subject: [PATCH 2861/3167] Time: 63 ms (91.05%), Space: 69.5 MB (77.56%) - LeetHub From f0ba1158554a6ffe5014cfeec8f907b1ba68e105 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:52:21 +0530 Subject: [PATCH 2862/3167] Create README - LeetHub --- 0523-continuous-subarray-sum/README.md | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0523-continuous-subarray-sum/README.md 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 From 7beb6d732ff73d015c757b4a0780b574c426f220 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:52:21 +0530 Subject: [PATCH 2863/3167] Attach NOTES - LeetHub --- 0523-continuous-subarray-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0523-continuous-subarray-sum/NOTES.md 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 From ac1db0e1b26380cbf85b726da4ea127ab3c29afc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:52:25 +0530 Subject: [PATCH 2864/3167] Time: 245 ms (13.66%), Space: 135.3 MB (14.02%) - LeetHub --- .../0523-continuous-subarray-sum.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp 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..02ceceb8 --- /dev/null +++ b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + + map mp; + mp.insert({0,-1}); + + int total = 0; + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + total += nums[i]; + int rem = total % 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 From 5955a0058fdb488d8a38bd8d187acb702c995339 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jun 2024 21:44:36 +0530 Subject: [PATCH 2865/3167] Attach NOTES - LeetHub From c5f8ff0d5f11e9a75ce7e0d2147a88de8fb1fac8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jun 2024 21:44:40 +0530 Subject: [PATCH 2866/3167] Time: 255 ms (9.42%), Space: 135.2 MB (19.20%) - LeetHub --- .../0523-continuous-subarray-sum.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp index 02ceceb8..47037b9c 100644 --- a/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp +++ b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp @@ -2,16 +2,19 @@ class Solution { public: bool checkSubarraySum(vector& nums, int k) { - map mp; - mp.insert({0,-1}); - - int total = 0; int n = nums.size(); + map mp; + + int sum = 0; + + mp[0] = -1; + for(int i = 0; i < n; ++i) { - total += nums[i]; - int rem = total % k; + sum += nums[i]; + + int rem = sum % k; if(mp.find(rem) != mp.end()) { @@ -19,10 +22,9 @@ class Solution { return true; } else - { mp[rem] = i; - } } + return false; } }; \ No newline at end of file From 00c54433484ba26a5f9ade85b443804567446484 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:34:19 +0530 Subject: [PATCH 2867/3167] Create README - LeetHub --- 1051-height-checker/README.md | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1051-height-checker/README.md 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 From 436ac288fe38cd262d2d71993a47114f49107016 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:34:23 +0530 Subject: [PATCH 2868/3167] Time: 2 ms (48.33%), Space: 10 MB (78.64%) - LeetHub --- 1051-height-checker/1051-height-checker.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1051-height-checker/1051-height-checker.cpp 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 From 7cbd875d13cec749b97b1e3abfc753f5d74433db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:41:37 +0530 Subject: [PATCH 2869/3167] Create README - LeetHub --- 1122-relative-sort-array/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1122-relative-sort-array/README.md 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 From b6f37dcf28be3e18cc387371e6b0fef8c0dbdd60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:41:41 +0530 Subject: [PATCH 2870/3167] Time: 0 ms (100.00%), Space: 10 MB (54.14%) - LeetHub --- .../1122-relative-sort-array.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1122-relative-sort-array/1122-relative-sort-array.cpp 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 From 96e19a9141b17d0d63d64e91b069e501d80f29a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:42:34 +0530 Subject: [PATCH 2871/3167] Attach NOTES - LeetHub --- 1122-relative-sort-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1122-relative-sort-array/NOTES.md 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 From 9e4fbef810a563e5b7f82b653ef6a7ed7a029820 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:42:37 +0530 Subject: [PATCH 2872/3167] Time: 0 ms (100.00%), Space: 10 MB (54.14%) - LeetHub From cc0d00fa37af44acf47327417e2d41fd839bf1d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:47:09 +0530 Subject: [PATCH 2873/3167] Create README - LeetHub --- .../README.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 1378-replace-employee-id-with-the-unique-identifier/README.md 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 From 6cc21e3164723307d8edd7c765b631f8c8774b26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:47:12 +0530 Subject: [PATCH 2874/3167] Time: 1187 ms (93.44%), Space: 0B (100.00%) - LeetHub --- .../1378-replace-employee-id-with-the-unique-identifier.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql 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 From 5af133da31f08b0c2eaef1dfcdd7394f68724cbe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:50:18 +0530 Subject: [PATCH 2875/3167] Attach NOTES - LeetHub --- 1378-replace-employee-id-with-the-unique-identifier/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1378-replace-employee-id-with-the-unique-identifier/NOTES.md 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 From d27f02d8322144bddf3fb2b9a1aa38f1bacb0974 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:50:21 +0530 Subject: [PATCH 2876/3167] Time: 1187 ms (93.44%), Space: 0B (100.00%) - LeetHub From 3305fa2becd3adeb2e087c2c35c620e42e71d5c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:18:10 +0530 Subject: [PATCH 2877/3167] Create README - LeetHub --- 1068-product-sales-analysis-i/README.md | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 1068-product-sales-analysis-i/README.md 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 From 4a03bfb5f63fbd9b0d8883544414742202074778 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:18:13 +0530 Subject: [PATCH 2878/3167] Time: 1460 ms (89.73%), Space: 0B (100.00%) - LeetHub --- .../1068-product-sales-analysis-i.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql 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..37c91497 --- /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 +left join Product on +Sales.product_id = Product.product_id; \ No newline at end of file From 99e94ccbe3dd88c5968df49ca448ec24a65a9b17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:19:15 +0530 Subject: [PATCH 2879/3167] Attach NOTES - LeetHub --- 1068-product-sales-analysis-i/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1068-product-sales-analysis-i/NOTES.md 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 From 808bbfe8ca371a68da95a23ac0e699029e063f81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:19:18 +0530 Subject: [PATCH 2880/3167] Time: 1460 ms (89.73%), Space: 0B (100.00%) - LeetHub From 7997bfc1f1f999e9c34874c0e3849c05439c56a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:20 +0530 Subject: [PATCH 2881/3167] Attach NOTES - LeetHub From 993f90262c34c64bf2e513596e73d844b34fa967 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:23 +0530 Subject: [PATCH 2882/3167] Time: 1460 ms (89.73%), Space: 0B (100.00%) - LeetHub From a22b1578ecae82a8074a1d2bb77490b6ff306fbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:38 +0530 Subject: [PATCH 2883/3167] Attach NOTES - LeetHub From f44d1d1be51bfaa5e512d2edc490bc3a5dbb8175 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:42 +0530 Subject: [PATCH 2884/3167] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub --- 1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 37c91497..e4ba918b 100644 --- a/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql +++ b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql @@ -2,5 +2,5 @@ select Product.product_name, Sales.year, Sales.price from Sales -left join Product on +inner join Product on Sales.product_id = Product.product_id; \ No newline at end of file From 9589ecae0a97153cd563b97686f3cb6137c28395 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:47 +0530 Subject: [PATCH 2885/3167] Attach NOTES - LeetHub From d8e3899f17b07d707078e7b6a040d535bf88c159 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:49 +0530 Subject: [PATCH 2886/3167] Attach NOTES - LeetHub From 2d4cca78e881433bd6bf387188dc8af8bfb5524f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:51 +0530 Subject: [PATCH 2887/3167] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From c4eba70367cddad18331cd9970ed10fde368bdce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:53 +0530 Subject: [PATCH 2888/3167] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From 26d79b7df9d2284704963a24842c4508ece80a1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:54 +0530 Subject: [PATCH 2889/3167] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From 17da11ae5ce675c7e4db88354d1a7dc30ed80200 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:23:17 +0530 Subject: [PATCH 2890/3167] Attach NOTES - LeetHub From 5be3b7102f109d89e2a8874d1a89e22153ac13c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:23:21 +0530 Subject: [PATCH 2891/3167] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From 3cf3132ea85961e675319884084935d295770c71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:52:01 +0530 Subject: [PATCH 2892/3167] Create README - LeetHub --- .../README.md | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2037-minimum-number-of-moves-to-seat-everyone/README.md 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 From c0467b13265a16499c6aef72e1891dbe85ca656a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:52:04 +0530 Subject: [PATCH 2893/3167] Time: 4 ms (74.53%), Space: 21 MB (17.35%) - LeetHub --- ...nimum-number-of-moves-to-seat-everyone.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp 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..6bbe6bac --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int minMovesToSeat(vector& seats, vector& students) { + + int n = seats.size(); + + sort(seats.begin(), seats.end()); + sort(students.begin(), students.end()); + + int st = 0, end = n-1; + + int moves = 0; + + while(st <= end) + { + int f = abs(seats[st] - students[st]); + int s = abs(seats[end] - students[end]); + + if(f < s) + { + moves += f; + ++st; + } + else + { + moves += s; + --end; + } + } + + return moves; + + } +}; \ No newline at end of file From dc00e736d4e4265945ba2752416c84cb2884e335 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:53:26 +0530 Subject: [PATCH 2894/3167] Attach NOTES - LeetHub --- 2037-minimum-number-of-moves-to-seat-everyone/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2037-minimum-number-of-moves-to-seat-everyone/NOTES.md 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 From a0ba302ea7de2c4d60591b5f6d0af7a277914203 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:53:28 +0530 Subject: [PATCH 2895/3167] Time: 7 ms (60.18%), Space: 21 MB (52.06%) - LeetHub --- ...inimum-number-of-moves-to-seat-everyone.cpp | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) 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 index 6bbe6bac..ffdf2002 100644 --- 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 @@ -7,25 +7,11 @@ class Solution { sort(seats.begin(), seats.end()); sort(students.begin(), students.end()); - int st = 0, end = n-1; - int moves = 0; - while(st <= end) + for(int i = 0; i < n; ++i) { - int f = abs(seats[st] - students[st]); - int s = abs(seats[end] - students[end]); - - if(f < s) - { - moves += f; - ++st; - } - else - { - moves += s; - --end; - } + moves += abs(seats[i] - students[i]); } return moves; From 08d478f987fad7bea0a18d9bf7d41a62f7557431 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:54:02 +0530 Subject: [PATCH 2896/3167] Attach NOTES - LeetHub From 1ecf17d3278ea2e467afdde2c857362bb41b5917 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:16:16 +0530 Subject: [PATCH 2897/3167] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0945-minimum-increment-to-make-array-unique/README.md 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 From 66873f7d2fb29682a10ba892c6efbc798bd7005c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:16:18 +0530 Subject: [PATCH 2898/3167] Attach NOTES - LeetHub --- 0945-minimum-increment-to-make-array-unique/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0945-minimum-increment-to-make-array-unique/NOTES.md 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 From 1b5f48bec9c7620c92f0d835fe7c54b55fea1f59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:16:21 +0530 Subject: [PATCH 2899/3167] Time: 125 ms (42.72%), Space: 69.1 MB (79.13%) - LeetHub --- ...minimum-increment-to-make-array-unique.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp 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..f03f57f6 --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minIncrementForUnique(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + int curr = nums[0], ans = 0; + + for(int i = 1; i < n; ++i) + { + if(nums[i] <= curr) + { + int val = abs(nums[i] - curr) + 1; + ans += val; + nums[i] += val; + } + curr = nums[i]; + } + + return ans; + + } +}; \ No newline at end of file From 2f36d465d4ab3e176368f141c848e62c0e443e79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:19:29 +0530 Subject: [PATCH 2900/3167] Attach NOTES - LeetHub From 6d88fbbd24d38a665b962afed3a7e3b0d552e9d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:19:31 +0530 Subject: [PATCH 2901/3167] Time: 119 ms (62.01%), Space: 69.1 MB (50.39%) - LeetHub --- .../0945-minimum-increment-to-make-array-unique.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 index f03f57f6..c46746a6 100644 --- 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 @@ -6,17 +6,15 @@ class Solution { sort(nums.begin(), nums.end()); - int curr = nums[0], ans = 0; + int ans = 0; for(int i = 1; i < n; ++i) { - if(nums[i] <= curr) + if(nums[i-1] >= nums[i]) { - int val = abs(nums[i] - curr) + 1; - ans += val; - nums[i] += val; + ans += (nums[i-1] - nums[i] + 1); + nums[i] = nums[i-1] + 1; } - curr = nums[i]; } return ans; From ab6aade5c1210b8bb53444210dca211d23a83bbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 15 Jun 2024 08:17:32 +0530 Subject: [PATCH 2902/3167] Attach NOTES - LeetHub --- 0502-ipo/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0502-ipo/NOTES.md 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 From 00282fbf024772d0b0a6bf09a48d429878bf5795 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:14:03 +0530 Subject: [PATCH 2903/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 3186-maximum-total-damage-with-spell-casting/README.md 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 From 28603df185eef832ab5d7e4bc5161ccee786be1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:14:07 +0530 Subject: [PATCH 2904/3167] Time: 627 ms (60.00%), Space: 205 MB (60.00%) - LeetHub --- ...aximum-total-damage-with-spell-casting.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp 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 From a2fff22a031139bc2fcf711e966711b989c7d70c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:18:23 +0530 Subject: [PATCH 2905/3167] Attach NOTES - LeetHub --- 3186-maximum-total-damage-with-spell-casting/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3186-maximum-total-damage-with-spell-casting/NOTES.md 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 From dedc46e698f7e4efbb890098b7f1d3933f33cd43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:18:26 +0530 Subject: [PATCH 2906/3167] Time: 627 ms (60.00%), Space: 205 MB (60.00%) - LeetHub From d32cbe77911f363c6d16ce95abd43d32954b3bde Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:06:56 +0530 Subject: [PATCH 2907/3167] Create README - LeetHub --- 0330-patching-array/README.md | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0330-patching-array/README.md 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 From 2cd7625ddac277df83c9d81ce31d43ce86249f02 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:06:59 +0530 Subject: [PATCH 2908/3167] Time: 3 ms (83.06%), Space: 13.7 MB (45.23%) - LeetHub --- 0330-patching-array/0330-patching-array.cpp | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0330-patching-array/0330-patching-array.cpp 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 From 12912d08023edae08906b4c3cdc7c9bbc2928d90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:40:21 +0530 Subject: [PATCH 2909/3167] Create README - LeetHub --- 0633-sum-of-square-numbers/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0633-sum-of-square-numbers/README.md 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 From a4c0672f409ed575beaf1a1d8dd8794ca5d87dd9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:40:25 +0530 Subject: [PATCH 2910/3167] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub --- .../0633-sum-of-square-numbers.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp 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..338c2700 --- /dev/null +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -0,0 +1,23 @@ +using ll = long long; + +class Solution { +public: + bool judgeSquareSum(int c) { + + ll low = 0, high = sqrt(c); + + while(low <= high) + { + ll mid = low * low + high * high; + + if(mid < c) + ++low; + else if(mid > c) + --high; + else + return true; + } + + return false; + } +}; \ No newline at end of file From 9599bffd7f5f89f9cb4a29ba4500fea6fb67f854 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:42:39 +0530 Subject: [PATCH 2911/3167] Attach NOTES - LeetHub --- 0633-sum-of-square-numbers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0633-sum-of-square-numbers/NOTES.md 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 From 98bf2ecc453b13d896a06f94f39302cf46333d14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:42:41 +0530 Subject: [PATCH 2912/3167] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From efd0a08fe2ce65ee012c9ddafb06ff60bf160b5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:43:26 +0530 Subject: [PATCH 2913/3167] Attach NOTES - LeetHub From 9117da2948ab3e72aad0a233b4f2981d7cd19ff0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:51:02 +0530 Subject: [PATCH 2914/3167] Attach NOTES - LeetHub From ae2634a6a0ef4fabc0eedf3fb99e0e600a85665b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:51:05 +0530 Subject: [PATCH 2915/3167] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From 5625af51b1130375a954cf6bd1a75bc8e0189114 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:54:44 +0530 Subject: [PATCH 2916/3167] Attach NOTES - LeetHub From 33beaa1a9b5494b55f2d4189c6bfe18094be46bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:54:47 +0530 Subject: [PATCH 2917/3167] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From c401e6eb9dcf0803ea580b9744c8f2b27cd0ee80 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:29 +0530 Subject: [PATCH 2918/3167] Attach NOTES - LeetHub From 2b61104f21f0c5a8c8d7b79ae72863d13f634234 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:32 +0530 Subject: [PATCH 2919/3167] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From 45b1f96d59d02b1892a9c854e26bc24a3adfb0bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:41 +0530 Subject: [PATCH 2920/3167] Attach NOTES - LeetHub From 5af58bcf621fc3caa8bd8dd0663d55c0d6cdf08f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:45 +0530 Subject: [PATCH 2921/3167] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From 3aa43b69e960a1bdf04d8317f6ba5a6e64fd0443 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:02:17 +0530 Subject: [PATCH 2922/3167] Attach NOTES - LeetHub From f6861ab1f6fafe339233f210e46dcbdcf0d84c95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:02:20 +0530 Subject: [PATCH 2923/3167] Time: 49 ms (6.20%), Space: 7 MB (91.95%) - LeetHub --- .../0633-sum-of-square-numbers.cpp | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) 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 index 338c2700..bd9769d0 100644 --- a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -2,22 +2,39 @@ using ll = long long; class Solution { public: - bool judgeSquareSum(int c) { + + bool isPerfectSquare(int num) + { + if(num == 0 or num == 1) + return true; - ll low = 0, high = sqrt(c); + ll low = 0, high = num; while(low <= high) { - ll mid = low * low + high * high; + ll mid = (low + high) / 2; - if(mid < c) - ++low; - else if(mid > c) - --high; + if(mid * mid == num) + return true; + else if(mid * mid < num) + low = mid+1; else + high = mid-1; + + } + return false; + } + + bool judgeSquareSum(int c) { + + for(ll i = 0; i * i <= c; ++i) + { + int a = i * i; + if(isPerfectSquare(c - a)) return true; } return false; + } }; \ No newline at end of file From accc5ad968303f3366108dc3bd6585180202ac28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:10:51 +0530 Subject: [PATCH 2924/3167] Attach NOTES - LeetHub From 5840e88e8605f5e7d800a0a46e18b4db01831c7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:10:54 +0530 Subject: [PATCH 2925/3167] Time: 0 ms (100.00%), Space: 7 MB (61.35%) - LeetHub --- .../0633-sum-of-square-numbers.cpp | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) 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 index bd9769d0..185bb33f 100644 --- a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -1,40 +1,22 @@ -using ll = long long; - class Solution { public: - - bool isPerfectSquare(int num) - { - if(num == 0 or num == 1) - return true; - - ll low = 0, high = num; - - while(low <= high) - { - ll mid = (low + high) / 2; - - if(mid * mid == num) - return true; - else if(mid * mid < num) - low = mid+1; - else - high = mid-1; - - } - return false; - } - bool judgeSquareSum(int c) { - for(ll i = 0; i * i <= c; ++i) + for(int i = 2; i * i <= c; ++i) { - int a = i * i; - if(isPerfectSquare(c - a)) - return true; + int cnt = 0; + + while(c % i == 0) + { + ++cnt; + c /= i; + } + + if(i % 4 == 3 and cnt % 2 != 0) + return false; } - return false; + return c%4 != 3; } }; \ No newline at end of file From 088cd4b6ac8d341df5bfd7c7b199129a1271fcf8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:09:12 +0530 Subject: [PATCH 2926/3167] Create README - LeetHub --- 3187-peaks-in-array/README.md | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 3187-peaks-in-array/README.md 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 From 0da40dfc8c4e3060710290dc73cb8d09864c434c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:09:15 +0530 Subject: [PATCH 2927/3167] Time: 656 ms (27.72%), Space: 250.2 MB (35.06%) - LeetHub --- 3187-peaks-in-array/3187-peaks-in-array.cpp | 133 ++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 3187-peaks-in-array/3187-peaks-in-array.cpp 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 From b60bdcb385afc53e6eba03e0422db8b13b22f4a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:38:31 +0530 Subject: [PATCH 2928/3167] Attach NOTES - LeetHub From b6918299d146980441b3de7e1c124ca356b4faa6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:38:34 +0530 Subject: [PATCH 2929/3167] Time: 143 ms (5.04%), Space: 68.9 MB (65.37%) - LeetHub --- ...imum-number-of-days-to-make-m-bouquets.cpp | 54 ++++++++----------- 1 file changed, 23 insertions(+), 31 deletions(-) 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 index b9454582..df9f3f33 100644 --- 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 @@ -1,53 +1,45 @@ +using ll = long long; + class Solution { public: - int minDays(vector& bloomDay, int m, int k) { + + int minimumNumberOfDays(int m, int k, int mid, vector& bloomDay) + { + ll bouquet = 0; + ll currSum = 0; - int n = bloomDay.size(); + for(auto& day : bloomDay) + { + currSum = (day <= mid ? currSum+1 : 0); + bouquet += (currSum / k); + currSum %= k; + } - if((long long)k * m > n) - return -1; + 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()); - auto canBloom = [&](int mid) - { - int bloom = 0; - int cnt = 0; - for(int i = 0; i < n; ++i) - { - if(bloomDay[i] <= mid) - { - ++cnt; - } - else - { - bloom += (cnt / k); - cnt = 0; - } - } - - bloom += (cnt / k); - - return bloom; - }; - - int ans = -1; + int ans = INT_MAX; while(low <= high) { - int mid = low + (high - low) / 2; + int mid = (low + high) / 2; - if(canBloom(mid) >= m) + if(minimumNumberOfDays(m, k, mid, bloomDay)) { ans = mid; high = mid-1; } else + { low = mid+1; + } } - - return ans; + return (ans == INT_MAX ? -1 : ans); } }; \ No newline at end of file From e87e9534765c0e4843f8947d2efae6ff2386dd03 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:16:32 +0530 Subject: [PATCH 2930/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1552-magnetic-force-between-two-balls/README.md 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 From 72994a5f6c6cc88f406928b2f3200499f4a08001 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:16:32 +0530 Subject: [PATCH 2931/3167] Attach NOTES - LeetHub --- 1552-magnetic-force-between-two-balls/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1552-magnetic-force-between-two-balls/NOTES.md 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 From 87ccf7aa3ee9277894630ccfaf4d9216f4991501 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:16:35 +0530 Subject: [PATCH 2932/3167] Time: 126 ms (57.07%), Space: 61.5 MB (35.79%) - LeetHub --- .../1552-magnetic-force-between-two-balls.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp 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..613e1044 --- /dev/null +++ b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp @@ -0,0 +1,45 @@ +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(); + int low = 0, high = *max_element(position.begin(), position.end()); + int ans = 0; + + sort(position.begin(), position.end()); + + 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 From f6c07e5402572a9b0ed5333427a46be6036ea88a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:07 +0530 Subject: [PATCH 2933/3167] Attach NOTES - LeetHub From 607444c2aae1f1df4654abc849e905e8d61d7e9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:10 +0530 Subject: [PATCH 2934/3167] Time: 126 ms (57.07%), Space: 61.5 MB (35.79%) - LeetHub From a70c63f96999890f36a94834aeba3b97121187ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:49 +0530 Subject: [PATCH 2935/3167] Attach NOTES - LeetHub From 6c9b5c2328c01aa75c7ac60e27f73865c4058aee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:52 +0530 Subject: [PATCH 2936/3167] Time: 131 ms (36.05%), Space: 61.3 MB (73.97%) - LeetHub --- .../1552-magnetic-force-between-two-balls.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 index 613e1044..b510ea76 100644 --- 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 @@ -21,11 +21,12 @@ class Solution { int maxDistance(vector& position, int m) { int n = position.size(); - int low = 0, high = *max_element(position.begin(), position.end()); - int ans = 0; sort(position.begin(), position.end()); + int low = 1, high = position.back() - position[0]; + int ans = 0; + while(low <= high) { int mid = (low + high) / 2; From 057a55263527f324db31b72d6b2c9e73dd3b72b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:13:10 +0530 Subject: [PATCH 2937/3167] Create README - LeetHub --- 1052-grumpy-bookstore-owner/README.md | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1052-grumpy-bookstore-owner/README.md 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 From 00d2a85140b527bb8c50083654ab00e66b40fcb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:13:13 +0530 Subject: [PATCH 2938/3167] Time: 43 ms (12.05%), Space: 45.8 MB (5.22%) - LeetHub --- .../1052-grumpy-bookstore-owner.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp 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 From 2758ea968772ce22a87a5db0dbe6c14018e01ac6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:14:12 +0530 Subject: [PATCH 2939/3167] Attach NOTES - LeetHub --- 1052-grumpy-bookstore-owner/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1052-grumpy-bookstore-owner/NOTES.md 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 From 2f06f70e63c883e316e3c791e24e2a93cb55c73e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:14:29 +0530 Subject: [PATCH 2940/3167] Attach NOTES - LeetHub From 4535991b49a31713a44197d690218a0d5f848150 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:14:32 +0530 Subject: [PATCH 2941/3167] Time: 43 ms (12.05%), Space: 45.8 MB (5.22%) - LeetHub From b308d869d8bfbc2e2ea2e9cda936c22ff13fed12 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:36:19 +0530 Subject: [PATCH 2942/3167] Attach NOTES - LeetHub --- 1248-count-number-of-nice-subarrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1248-count-number-of-nice-subarrays/NOTES.md 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 From 441e1fe2fb96c3397671cef8945b748f246d2c97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:36:22 +0530 Subject: [PATCH 2943/3167] Time: 129 ms (20.37%), Space: 89.9 MB (18.43%) - LeetHub --- .../1248-count-number-of-nice-subarrays.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp 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 From d1db13a1d278251feb1a987b3e30de15b41ca944 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:38:52 +0530 Subject: [PATCH 2944/3167] Attach NOTES - LeetHub From 8e31c16b604992531f5f9729cc9d193dffe7a2ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:38:55 +0530 Subject: [PATCH 2945/3167] Time: 129 ms (20.37%), Space: 89.9 MB (18.43%) - LeetHub From 9c529592097d4112153f1cbe7cdb8433ed198048 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:40:06 +0530 Subject: [PATCH 2946/3167] Attach NOTES - LeetHub From fb3a1be1b0ee7ae6c14d3a747d6f820885ee3904 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:41:37 +0530 Subject: [PATCH 2947/3167] Attach NOTES - LeetHub From 061afcdd109bd955b76918b98b09a8ca05e13306 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:41:41 +0530 Subject: [PATCH 2948/3167] Time: 129 ms (20.37%), Space: 89.9 MB (18.43%) - LeetHub From 1109ab5c3d8212ad41d323279ad8371687ca5e4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:44:56 +0530 Subject: [PATCH 2949/3167] Attach NOTES - LeetHub From 587fea5cb197fe0f8bc3565944a892cb1894c179 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 08:43:40 +0530 Subject: [PATCH 2950/3167] Attach NOTES - LeetHub --- 0189-rotate-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0189-rotate-array/NOTES.md 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 From 7fb9c3d53a79f8a02fbd06f9d524c1cefa7b2611 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 08:43:44 +0530 Subject: [PATCH 2951/3167] Time: 20 ms (61.86%), Space: 27.3 MB (55.61%) - LeetHub --- 0189-rotate-array/0189-rotate-array.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0189-rotate-array/0189-rotate-array.cpp 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 From eec391a9773e80945baac144888c7ca8d93496c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:42:50 +0530 Subject: [PATCH 2952/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md 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 From 1535f314bf399fd052e12cc7d3ccbd52a2734e2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:42:51 +0530 Subject: [PATCH 2953/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md 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 From 4029b300b11649dec4c2ccdffb3a72dd167eb5e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:42:54 +0530 Subject: [PATCH 2954/3167] Time: 151 ms (16.71%), Space: 97.9 MB (5.23%) - LeetHub --- ...olute-diff-less-than-or-equal-to-limit.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 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 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 From 8f231f74ed4e920796dcde9087250fea3fd3a1c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:53:52 +0530 Subject: [PATCH 2955/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0995-minimum-number-of-k-consecutive-bit-flips/README.md 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 From fcaf5ae9a0747802f8003ffea72849d976d9203d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:53:54 +0530 Subject: [PATCH 2956/3167] Time: 89 ms (63.93%), Space: 117.9 MB (5.12%) - LeetHub --- ...imum-number-of-k-consecutive-bit-flips.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp 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..fbafd718 --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp @@ -0,0 +1,47 @@ +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-k; ++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){ + pref[i].first = 1; + ++ans; + } + + pref[i].second += op; + } + + for(int i = n-k+1; i < n; ++i) + { + int moves = (i-1 >= 0 ? pref[i-1].second : 0); + + if(i - k >= 0 and pref[i-k].first) + --moves; + + flag &= (nums[i] & 1 and moves % 2 == 0 or nums[i] % 2 == 0 and moves & 1); + + pref[i].second = moves; + } + + return (flag ? ans : -1); + } +}; \ No newline at end of file From 911e27a81a4a4a50173cb13722d854f5ed042741 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:59:45 +0530 Subject: [PATCH 2957/3167] Attach NOTES - LeetHub --- 0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md 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 From 89acf3e5c07692b5163a15cece45137e8aac7f76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:59:48 +0530 Subject: [PATCH 2958/3167] Time: 89 ms (63.93%), Space: 117.9 MB (5.12%) - LeetHub From ad4e63160596a040c5eb9b59fd99fb19e6db94aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 23:06:12 +0530 Subject: [PATCH 2959/3167] Attach NOTES - LeetHub From eb92bf7e684065f3606f3aafc478295281b5bd18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 23:06:14 +0530 Subject: [PATCH 2960/3167] Time: 104 ms (18.85%), Space: 117.8 MB (5.12%) - LeetHub --- ...imum-number-of-k-consecutive-bit-flips.cpp | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) 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 index fbafd718..c2bc0ee2 100644 --- 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 @@ -8,7 +8,7 @@ class Solution { bool flag = true; - for(int i = 0; i <= n-k; ++i) + for(int i = 0; i < n; ++i) { pref[i].second = (i-1 >= 0 ? pref[i-1].second : 0); @@ -23,25 +23,18 @@ class Solution { int op = (nums[i] % 2 == 0 ? (moves % 2 == 0 ? 1 : 0) : (moves % 2 == 0 ? 0 : 1)); if(op){ - pref[i].first = 1; - ++ans; + if(i <= n-k) + { + pref[i].first = 1; + ++ans; + } + else + flag = false; } - + pref[i].second += op; } - for(int i = n-k+1; i < n; ++i) - { - int moves = (i-1 >= 0 ? pref[i-1].second : 0); - - if(i - k >= 0 and pref[i-k].first) - --moves; - - flag &= (nums[i] & 1 and moves % 2 == 0 or nums[i] % 2 == 0 and moves & 1); - - pref[i].second = moves; - } - return (flag ? ans : -1); } }; \ No newline at end of file From c73765792bc4d4a3652f948f2820d6e85759c66b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:03:10 +0530 Subject: [PATCH 2961/3167] Attach NOTES - LeetHub From d775fd200a98184909a7b53b36fda1576bb3e233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:11:25 +0530 Subject: [PATCH 2962/3167] Create README - LeetHub --- 0084-largest-rectangle-in-histogram/README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0084-largest-rectangle-in-histogram/README.md 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 From 67697c7de799d9db8fca5e58cca0ecf5dbf24615 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:28:16 +0530 Subject: [PATCH 2963/3167] Attach NOTES - LeetHub --- 0042-trapping-rain-water/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0042-trapping-rain-water/NOTES.md 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 From e3dba23ffe17d91c90e53ea53c0b0df1b5b73edd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:28:18 +0530 Subject: [PATCH 2964/3167] Time: 9 ms (68.10%), Space: 22.2 MB (95.57%) - LeetHub --- .../0042-trapping-rain-water.cpp | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp index 2f7a6ad3..2731cd5d 100644 --- a/0042-trapping-rain-water/0042-trapping-rain-water.cpp +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -2,22 +2,27 @@ class Solution { public: int trap(vector& height) { - int start = 0,end = height.size()-1; - int ans = 0; - int maxleft = height[start], maxright = height[end]; - while(start <= end) + int n = height.size(); + + int leftMax = 0, rightMax = 0; + + int ans = 0, i = 0, j = n-1; + + while(i < j) { - maxleft = max(maxleft,height[start]); - maxright = max(maxright,height[end]); + leftMax = max(leftMax, height[i]); + rightMax = max(rightMax, height[j]); - ans += min(maxleft,maxright) - min(height[start],height[end]); - if(height[start] < height[end]) - ++start; + if(height[i] <= height[j]) + { + ans += min(leftMax, rightMax) - height[i++]; + } else - --end; + { + ans += min(leftMax, rightMax) - height[j--]; + } } return ans; } -}; - +}; \ No newline at end of file From a215728d84850adc67142a21924805abb34a3b5f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:40:46 +0530 Subject: [PATCH 2965/3167] Attach NOTES - LeetHub From 01771bf372ee885a4c39321005d750ff2633fa9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:40:49 +0530 Subject: [PATCH 2966/3167] Time: 0 ms (100.00%), Space: 23.2 MB (28.72%) - LeetHub --- .../0042-trapping-rain-water.cpp | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp index 2731cd5d..915373f6 100644 --- a/0042-trapping-rain-water/0042-trapping-rain-water.cpp +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -4,23 +4,24 @@ class Solution { int n = height.size(); - int leftMax = 0, rightMax = 0; + vector leftMax(n, 0), rightMax(n, 0); - int ans = 0, i = 0, j = n-1; + int ans = 0; - while(i < j) + for(int i = 0; i < n; ++i) { - leftMax = max(leftMax, height[i]); - rightMax = max(rightMax, height[j]); - - if(height[i] <= height[j]) - { - ans += min(leftMax, rightMax) - height[i++]; - } - else - { - ans += min(leftMax, rightMax) - height[j--]; - } + leftMax[i] = (i == 0 ? height[i] : max(height[i], leftMax[i-1])); + } + + for(int i = n-1; i >= 0; --i) + { + rightMax[i] = (i == n-1 ? height[i] : max(height[i], rightMax[i+1])); + } + + for(int i = 0; i < n; ++i) + { + int canHold = min(leftMax[i], rightMax[i]); + ans += (canHold - height[i] > 0 ? canHold - height[i] : 0); } return ans; From 3090a09616b75d2e2adb88f2fc6879ab0748b2d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:42:28 +0530 Subject: [PATCH 2967/3167] Attach NOTES - LeetHub From 376fd26870a3437669d72a26ef1508ad10deff25 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:42:31 +0530 Subject: [PATCH 2968/3167] Time: 7 ms (87.59%), Space: 22.1 MB (99.87%) - LeetHub --- .../0042-trapping-rain-water.cpp | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp index 915373f6..6ea2c60c 100644 --- a/0042-trapping-rain-water/0042-trapping-rain-water.cpp +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -1,29 +1,27 @@ class Solution { public: int trap(vector& height) { - + int n = height.size(); - vector leftMax(n, 0), rightMax(n, 0); + int i = 0, j = n-1, ans = 0; - int ans = 0; + int leftMax = 0, rightMax = 0; - for(int i = 0; i < n; ++i) - { - leftMax[i] = (i == 0 ? height[i] : max(height[i], leftMax[i-1])); - } - - for(int i = n-1; i >= 0; --i) + while(i < j) { - rightMax[i] = (i == n-1 ? height[i] : max(height[i], rightMax[i+1])); - } - - for(int i = 0; i < n; ++i) - { - int canHold = min(leftMax[i], rightMax[i]); - ans += (canHold - height[i] > 0 ? canHold - height[i] : 0); + 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 From c80fb75d14d9c532c485149bdde2153d020a03d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 12:41:45 +0530 Subject: [PATCH 2969/3167] Create README - LeetHub From 4b898a9c77f8f14031b794c20e500329d792f5fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:15:03 +0530 Subject: [PATCH 2970/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1038-binary-search-tree-to-greater-sum-tree/README.md 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 From 50a0b2d2370a51ac5c4d81a3eefe43d0545fdb19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:15:06 +0530 Subject: [PATCH 2971/3167] Time: 2 ms (49.63%), Space: 10.1 MB (42.71%) - LeetHub --- ...binary-search-tree-to-greater-sum-tree.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp 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 From 15abb93c81e1f0e11645605a57613d7c257ac271 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:34:49 +0530 Subject: [PATCH 2972/3167] Create README - LeetHub --- 1382-balance-a-binary-search-tree/README.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1382-balance-a-binary-search-tree/README.md 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 From 47f249475057dfbb4df5b7bcb877205261bbb98d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:34:54 +0530 Subject: [PATCH 2973/3167] Time: 85 ms (70.08%), Space: 62.6 MB (60.81%) - LeetHub --- .../1382-balance-a-binary-search-tree.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp 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 From e31651e1678ea653d727156bbc3723519191fcc7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:36:18 +0530 Subject: [PATCH 2974/3167] Attach NOTES - LeetHub --- 1382-balance-a-binary-search-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1382-balance-a-binary-search-tree/NOTES.md 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 From 5e5d1dfe74eb4f6d5494d8395f4dcf988d065466 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:36:21 +0530 Subject: [PATCH 2975/3167] Time: 85 ms (70.08%), Space: 62.6 MB (60.81%) - LeetHub From 607dacd6be423f662e8c94c4b6dd39c78fadab98 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:56:26 +0530 Subject: [PATCH 2976/3167] Create README - LeetHub --- 0295-find-median-from-data-stream/README.md | 50 +++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0295-find-median-from-data-stream/README.md 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 From ffff5b67e965b3d54eb58ad80aff9cb24e794a8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:56:30 +0530 Subject: [PATCH 2977/3167] Time: 279 ms (41.98%), Space: 121.9 MB (20.74%) - LeetHub --- .../0295-find-median-from-data-stream.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp 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 From e4f7c62706791756f05974c92e291cebca89da6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:38:45 +0530 Subject: [PATCH 2978/3167] Attach NOTES - LeetHub From 8dd5b3c42e8afd4e047fd4a88380402a2b988e60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:48:49 +0530 Subject: [PATCH 2979/3167] Create README - LeetHub --- 1092-shortest-common-supersequence/README.md | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1092-shortest-common-supersequence/README.md 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 From 614d9c5f9befc8d1319b427623c96a33d324160c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:48:49 +0530 Subject: [PATCH 2980/3167] Attach NOTES - LeetHub --- 1092-shortest-common-supersequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1092-shortest-common-supersequence/NOTES.md 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 From 0ade45a03be0cac30df78989944b4c8765e25df0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:48:53 +0530 Subject: [PATCH 2981/3167] Time: 13 ms (95.11%), Space: 25.5 MB (83.03%) - LeetHub --- .../1092-shortest-common-supersequence.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp 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 From 3217eacab1eb4e4085198a6f8ee8552618bc7958 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:13:06 +0530 Subject: [PATCH 2982/3167] Create README - LeetHub --- 1791-find-center-of-star-graph/README.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1791-find-center-of-star-graph/README.md 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 From ed88f06aa560f8379a6b511fde6d729a61e094af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:13:09 +0530 Subject: [PATCH 2983/3167] Time: 118 ms (95.71%), Space: 70.7 MB (92.91%) - LeetHub --- .../1791-find-center-of-star-graph.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp 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 From 3972f5d4d014b8dca8f9d2f994c2feb519366268 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:08:21 +0530 Subject: [PATCH 2984/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2285-maximum-total-importance-of-roads/README.md 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 From 77fba1ba083e55135f7fb3588cf3f7bc2278b9e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:08:25 +0530 Subject: [PATCH 2985/3167] Time: 531 ms (8.97%), Space: 176.7 MB (15.35%) - LeetHub --- ...2285-maximum-total-importance-of-roads.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp 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 From 8ce859771c3699e5f8683f4311827affa5c907ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:13:04 +0530 Subject: [PATCH 2986/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md 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 From 01dc4296ac61c2106c09ea1c560df5546b276827 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:13:08 +0530 Subject: [PATCH 2987/3167] Time: 128 ms (60.70%), Space: 62.5 MB (99.12%) - LeetHub --- ...-of-a-node-in-a-directed-acyclic-graph.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp 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 From 44df5e93950b097ef4b07b20592137c889820f55 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:14:08 +0530 Subject: [PATCH 2988/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md 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 From 62b9d3ff6b7535470efccd6650971d256ade3c0e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:14:11 +0530 Subject: [PATCH 2989/3167] Time: 128 ms (60.70%), Space: 62.5 MB (99.12%) - LeetHub From b39f923740d09aefb835ffb345ca53e3c2b98761 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:19:57 +0530 Subject: [PATCH 2990/3167] Attach NOTES - LeetHub From b44ea023637b1021ebb91e8a9fb379891f1c2921 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:20:00 +0530 Subject: [PATCH 2991/3167] Time: 128 ms (60.70%), Space: 62.5 MB (99.12%) - LeetHub From fc16bf853b72eea322079efff74d3e0dff3d5a4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:34:46 +0530 Subject: [PATCH 2992/3167] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 3202-find-the-maximum-length-of-valid-subsequence-ii/README.md 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 From 005e6121521472f98f985fe54d76fe81ed046d1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:34:46 +0530 Subject: [PATCH 2993/3167] Attach NOTES - LeetHub --- 3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md 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 From db01fa34bdaf6dff323582bc98fb2af23a7b1387 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:34:50 +0530 Subject: [PATCH 2994/3167] Time: 107 ms (12.50%), Space: 127.1 MB (12.50%) - LeetHub --- ...maximum-length-of-valid-subsequence-ii.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp 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..30e02d3d --- /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,33 @@ +class Solution { +public: + + int helper(vector& nums, int need, int k) + { + int ans = 0; + + vector dp(k+1, 0); + + for(int i = 0; i < nums.size(); ++i) + { + int rem = nums[i] % k; + + dp[rem] = max(dp[rem], dp[(need - rem + k)%k] + 1); + + ans = max(ans, dp[rem]); + } + + return ans; + } + + int maximumLength(vector& nums, int k) { + + int ans = 0; + + for(int i = 0; i < k; ++i) + { + ans = max(ans, helper(nums, i, k)); + } + + return ans; + } +}; \ No newline at end of file From e6fc0a8ceaa519ccc49cd9de130026f74259ea20 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:53:55 +0530 Subject: [PATCH 2995/3167] Attach NOTES - LeetHub From 831b51aa41599e58b9e97c10c2fc6a9c47f38992 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:53:58 +0530 Subject: [PATCH 2996/3167] Time: 86 ms (62.50%), Space: 127.2 MB (12.50%) - LeetHub From e508e5e51a8e00e837c7a587504b4abf317c0246 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 10:19:32 +0530 Subject: [PATCH 2997/3167] Attach NOTES - LeetHub From 57e95110755d54c9ef496b1dd9039f90e80752d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 10:19:35 +0530 Subject: [PATCH 2998/3167] Time: 95 ms (62.50%), Space: 127.1 MB (12.50%) - LeetHub --- ...maximum-length-of-valid-subsequence-ii.cpp | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) 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 index 30e02d3d..88f28610 100644 --- 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 @@ -1,31 +1,21 @@ class Solution { public: - - int helper(vector& nums, int need, int k) - { - int ans = 0; - - vector dp(k+1, 0); - - for(int i = 0; i < nums.size(); ++i) - { - int rem = nums[i] % k; - - dp[rem] = max(dp[rem], dp[(need - rem + k)%k] + 1); - - ans = max(ans, dp[rem]); - } - - return ans; - } - int maximumLength(vector& nums, int k) { int ans = 0; for(int i = 0; i < k; ++i) { - ans = max(ans, helper(nums, i, k)); + 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; From 73cbd34ddc3423617b80e66ff1f3859f71b5cb7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 22:39:38 +0530 Subject: [PATCH 2999/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md 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 From 5335e5b93d44b92628692682d11f5bbffba17045 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:15:02 +0530 Subject: [PATCH 3000/3167] Attach NOTES - LeetHub From 649a58db3850707f66b8b816cb0191f7512efed6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:16:51 +0530 Subject: [PATCH 3001/3167] Create README - LeetHub --- 1550-three-consecutive-odds/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1550-three-consecutive-odds/README.md 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 From 3a67519544483fae1f8d7739d89209c9b94cc165 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:16:54 +0530 Subject: [PATCH 3002/3167] Time: 0 ms (100.00%), Space: 10.5 MB (83.42%) - LeetHub --- .../1550-three-consecutive-odds.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 1550-three-consecutive-odds/1550-three-consecutive-odds.cpp 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 From ebe57ff75ffeddf9f729cb2618c6bb2f478d7068 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:17:36 +0530 Subject: [PATCH 3003/3167] Attach NOTES - LeetHub --- 1550-three-consecutive-odds/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1550-three-consecutive-odds/NOTES.md 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 From 7cef70cbdad64178c6e30df3236a4b1b5be64090 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:17:39 +0530 Subject: [PATCH 3004/3167] Time: 0 ms (100.00%), Space: 10.5 MB (83.42%) - LeetHub From a848dc54649df27f205d087424244165ea8668bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jul 2024 07:38:58 +0530 Subject: [PATCH 3005/3167] Create README - LeetHub --- 0350-intersection-of-two-arrays-ii/README.md | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0350-intersection-of-two-arrays-ii/README.md 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 From 66ff9c88c5309f6c7aae21a8db238827e11c88e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jul 2024 07:39:02 +0530 Subject: [PATCH 3006/3167] Time: 7 ms (27.72%), Space: 13.4 MB (38.99%) - LeetHub --- .../0350-intersection-of-two-arrays-ii.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp 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 From 10540a235acef3ea990629c771c8d8aaf81a5365 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:01 +0530 Subject: [PATCH 3007/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md 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 From df0a089a6aeb0b7a51ab333fc47b5f334a6e685c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:04 +0530 Subject: [PATCH 3008/3167] Time: 66 ms (72.70%), Space: 38.7 MB (50.84%) - LeetHub --- ...gest-and-smallest-value-in-three-moves.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp 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 From 97342965b03c5bdcb0744d3c6b2b71704331ad39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:33 +0530 Subject: [PATCH 3009/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md 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 From 3a721ac8670f437fb16431a0213e117c5d1d9e1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:37 +0530 Subject: [PATCH 3010/3167] Time: 66 ms (72.70%), Space: 38.7 MB (50.84%) - LeetHub From 717efaa484b719b08360f13d7365879bb00ab20b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:51 +0530 Subject: [PATCH 3011/3167] Attach NOTES - LeetHub From b605a4404557889f968fbf01ae0a72c60f4271a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:55 +0530 Subject: [PATCH 3012/3167] Time: 66 ms (72.70%), Space: 38.7 MB (50.84%) - LeetHub From 89913d741b5618e1921d517700d1006572d43a91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 07:59:50 +0530 Subject: [PATCH 3013/3167] Create README - LeetHub --- 2181-merge-nodes-in-between-zeros/README.md | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2181-merge-nodes-in-between-zeros/README.md 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 From c616c4b8251b121e4d2cd424f25ea15dfae751eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 07:59:54 +0530 Subject: [PATCH 3014/3167] Time: 446 ms (54.18%), Space: 256.7 MB (76.60%) - LeetHub --- .../2181-merge-nodes-in-between-zeros.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp 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 From 8aefba178d05d1fbe082fb79d67f9f126999d568 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 23:04:39 +0530 Subject: [PATCH 3015/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 3203-find-minimum-diameter-after-merging-two-trees/README.md 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 From 66a91ef91b794fa59648d3e1517cf985dd5a1fe0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 23:04:43 +0530 Subject: [PATCH 3016/3167] Time: 2269 ms (7.75%), Space: 305 MB (62.16%) - LeetHub --- ...nimum-diameter-after-merging-two-trees.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp 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 From b3c96367ad1d396d3b8589349bd17b674c6d2769 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Jul 2024 08:44:20 +0530 Subject: [PATCH 3017/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md 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 From 491d8deef2465c11cde90d61bb3cc6ddb5abc69e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Jul 2024 08:44:24 +0530 Subject: [PATCH 3018/3167] Time: 170 ms (46.44%), Space: 115.8 MB (58.25%) - LeetHub --- ...umber-of-nodes-between-critical-points.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 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 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 From 2bd20ba49b184a603f49cbf27ab173d14f33f4eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:12:39 +0530 Subject: [PATCH 3019/3167] Create README - LeetHub --- 2582-pass-the-pillow/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2582-pass-the-pillow/README.md 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 From 97cf6439585f3d161fe9905adfefc9fd4d8921e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:12:43 +0530 Subject: [PATCH 3020/3167] Time: 0 ms (100.00%), Space: 7.1 MB (30.58%) - LeetHub --- 2582-pass-the-pillow/2582-pass-the-pillow.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2582-pass-the-pillow/2582-pass-the-pillow.cpp 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..4e73b840 --- /dev/null +++ b/2582-pass-the-pillow/2582-pass-the-pillow.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int passThePillow(int n, int time) { + + int dir = 1, pos = 1; + + while(time--) + { + if(dir) + { + ++pos; + if(pos == n) + { + dir = 0; + } + } + else + { + --pos; + if(pos == 1) + { + dir = 1; + } + } + } + + return pos; + } +}; \ No newline at end of file From 422214b82c37ab8ad3d7726f3c5518cff86daad0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:14:02 +0530 Subject: [PATCH 3021/3167] Attach NOTES - LeetHub --- 2582-pass-the-pillow/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2582-pass-the-pillow/NOTES.md 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 From 6e59790c4b50b699e4d85f65295519a694157230 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:14:05 +0530 Subject: [PATCH 3022/3167] Time: 0 ms (100.00%), Space: 7.1 MB (30.58%) - LeetHub From 89421d347e7352261b408d2d00d19ee08e2e39ec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:20:02 +0530 Subject: [PATCH 3023/3167] Attach NOTES - LeetHub From bd7e605ebc367a4c0b707bf3e3dc3cd617c88c28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:20:06 +0530 Subject: [PATCH 3024/3167] Time: 0 ms (100.00%), Space: 7.1 MB (30.58%) - LeetHub From 6a32324543fc51b45578bf4adf041c3cb8d831da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:31:07 +0530 Subject: [PATCH 3025/3167] Attach NOTES - LeetHub From f819b816547b99bf111470b1d0d6d0f0b75ebb71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:31:10 +0530 Subject: [PATCH 3026/3167] Time: 2 ms (54.14%), Space: 7 MB (71.18%) - LeetHub --- 2582-pass-the-pillow/2582-pass-the-pillow.cpp | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/2582-pass-the-pillow/2582-pass-the-pillow.cpp b/2582-pass-the-pillow/2582-pass-the-pillow.cpp index 4e73b840..52a89766 100644 --- a/2582-pass-the-pillow/2582-pass-the-pillow.cpp +++ b/2582-pass-the-pillow/2582-pass-the-pillow.cpp @@ -2,28 +2,10 @@ class Solution { public: int passThePillow(int n, int time) { - int dir = 1, pos = 1; + int fullChunks = time / (n-1); + int extTime = time % (n-1); - while(time--) - { - if(dir) - { - ++pos; - if(pos == n) - { - dir = 0; - } - } - else - { - --pos; - if(pos == 1) - { - dir = 1; - } - } - } + return (fullChunks & 1 ? n - extTime : extTime + 1); - return pos; } }; \ No newline at end of file From 5b7d0537a18235def977266626f9099bfa2e6813 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:00 +0530 Subject: [PATCH 3027/3167] Create README - LeetHub --- 1518-water-bottles/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1518-water-bottles/README.md 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 From 0bd9b0b4d2302ef5765762eb51d3e77e94b9a911 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:04 +0530 Subject: [PATCH 3028/3167] Time: 0 ms (100.00%), Space: 7.2 MB (30.43%) - LeetHub --- 1518-water-bottles/1518-water-bottles.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1518-water-bottles/1518-water-bottles.cpp 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 From e50a0453123320a3fc47e805011e050013ef3632 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:28 +0530 Subject: [PATCH 3029/3167] Attach NOTES - LeetHub --- 1518-water-bottles/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1518-water-bottles/NOTES.md 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 From 7cbde9916fdc99b90860a9fdf86c15ca12a6288a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:31 +0530 Subject: [PATCH 3030/3167] Time: 0 ms (100.00%), Space: 7.2 MB (30.43%) - LeetHub From 88ba925fc3bfbfae072c0b80d7a058977c4e3c69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:33:12 +0530 Subject: [PATCH 3031/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2196-create-binary-tree-from-descriptions/README.md 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 From 6c87acd8929ffd535b9c19c6cf18f80adb70e6f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:33:15 +0530 Subject: [PATCH 3032/3167] Time: 528 ms (98.04%), Space: 231 MB (98.25%) - LeetHub --- ...6-create-binary-tree-from-descriptions.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp 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 From 4795846e0b6ffeba95298f314ff8a6e242fc197c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:16:27 +0530 Subject: [PATCH 3033/3167] Create README - LeetHub --- .../README.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0026-remove-duplicates-from-sorted-array/README.md 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 From 0423d04bb087735cd02be948820a9a3be0e81034 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jul 2024 22:43:58 +0530 Subject: [PATCH 3034/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md diff --git a/2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md new file mode 100644 index 00000000..26236aaf --- /dev/null +++ b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md @@ -0,0 +1,39 @@ +

2096. 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 From f32d95b36dca2f0cfa341754aabb7bc8337a1a66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jul 2024 22:44:04 +0530 Subject: [PATCH 3035/3167] Time: 156 ms (55.35%), Space: 121.8 MB (27.68%) - LeetHub --- ...ons-from-a-binary-tree-node-to-another.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 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 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;i Date: Wed, 17 Jul 2024 08:27:08 +0530 Subject: [PATCH 3036/3167] Create README - LeetHub --- 0073-set-matrix-zeroes/README.md | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0073-set-matrix-zeroes/README.md 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 From d7cf27a2bb818ec34b6f60bd58dde17decfecf98 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:23:13 +0530 Subject: [PATCH 3037/3167] Attach NOTES - LeetHub From e4df025ecb98ea1c699d610d02fe6e9d39e14d30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:58:34 +0530 Subject: [PATCH 3038/3167] Create README - LeetHub --- 1110-delete-nodes-and-return-forest/README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1110-delete-nodes-and-return-forest/README.md 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 From e53989c8adb23228a82a76d04cb90b77ce8c357a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:58:35 +0530 Subject: [PATCH 3039/3167] Attach NOTES - LeetHub --- 1110-delete-nodes-and-return-forest/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1110-delete-nodes-and-return-forest/NOTES.md 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 From abdc21442e3b42146cad966530235136f3ae16f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:58:38 +0530 Subject: [PATCH 3040/3167] Time: 17 ms (45.72%), Space: 27 MB (25.05%) - LeetHub --- .../1110-delete-nodes-and-return-forest.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp 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 From c28e0b3195eab9aa556d7b0eb2288cf5b31e8891 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 18 Jul 2024 23:07:54 +0530 Subject: [PATCH 3041/3167] Attach NOTES - LeetHub --- 1530-number-of-good-leaf-nodes-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1530-number-of-good-leaf-nodes-pairs/NOTES.md 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 From cc3c2993e68bd2af99c83c29c7da1a7831f6736e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:29:19 +0530 Subject: [PATCH 3042/3167] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1423-maximum-points-you-can-obtain-from-cards/README.md 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 From 695b806677e9329559863bbb271af0adff0641a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:29:22 +0530 Subject: [PATCH 3043/3167] Time: 48 ms (19.65%), Space: 48.3 MB (5.58%) - LeetHub --- ...ximum-points-you-can-obtain-from-cards.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp 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 From f3c41ce35e799621fefd250149012b001d7e0b10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:33:57 +0530 Subject: [PATCH 3044/3167] Attach NOTES - LeetHub --- 1423-maximum-points-you-can-obtain-from-cards/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1423-maximum-points-you-can-obtain-from-cards/NOTES.md 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 From 368bd2146a1ef3997f168e39181667a02a41bf10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:34:00 +0530 Subject: [PATCH 3045/3167] Time: 48 ms (19.65%), Space: 48.3 MB (5.58%) - LeetHub From e63e306f4b0b1649c58de4c39676ec6101d1c57b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:18:31 +0530 Subject: [PATCH 3046/3167] Create README - LeetHub --- 0054-spiral-matrix/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0054-spiral-matrix/README.md 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 From 676893348b06e32765c9673452ed9007a98dfb63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:18:32 +0530 Subject: [PATCH 3047/3167] Attach NOTES - LeetHub --- 0054-spiral-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0054-spiral-matrix/NOTES.md 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 From 15fb02c9f214ff00fda717ec1fbc30c57df4c49f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:18:36 +0530 Subject: [PATCH 3048/3167] Time: 0 ms (100.00%), Space: 8.4 MB (9.65%) - LeetHub --- 0054-spiral-matrix/0054-spiral-matrix.cpp | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0054-spiral-matrix/0054-spiral-matrix.cpp 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 From a94cd345db832dde0ff16476f16ec88881074d09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:19:15 +0530 Subject: [PATCH 3049/3167] Attach NOTES - LeetHub From b76ab9c35a7e06def6774c7712f557fd86d50c7e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:19:18 +0530 Subject: [PATCH 3050/3167] Time: 0 ms (100.00%), Space: 8.4 MB (9.65%) - LeetHub From 4e9b0210f45b50e7add58c61a28065f7a0f37de9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:32:30 +0530 Subject: [PATCH 3051/3167] Create README - LeetHub --- 1380-lucky-numbers-in-a-matrix/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1380-lucky-numbers-in-a-matrix/README.md diff --git a/1380-lucky-numbers-in-a-matrix/README.md b/1380-lucky-numbers-in-a-matrix/README.md new file mode 100644 index 00000000..5cfd13f5 --- /dev/null +++ b/1380-lucky-numbers-in-a-matrix/README.md @@ -0,0 +1,37 @@ +

1380. 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 From 05768037181f87a0a5bd9d731c3e57b6bbbb66ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:32:33 +0530 Subject: [PATCH 3052/3167] Time: 12 ms (84.97%), Space: 14.5 MB (16.09%) - LeetHub --- .../1380-lucky-numbers-in-a-matrix.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp 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; i Date: Sat, 20 Jul 2024 22:13:21 +0530 Subject: [PATCH 3053/3167] Attach NOTES - LeetHub --- 1605-find-valid-matrix-given-row-and-column-sums/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1605-find-valid-matrix-given-row-and-column-sums/NOTES.md 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 From 6f10557408ac6752647169f71906b204117189a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jul 2024 22:13:24 +0530 Subject: [PATCH 3054/3167] Time: 31 ms (88.26%), Space: 36.1 MB (11.51%) - LeetHub --- ...valid-matrix-given-row-and-column-sums.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp 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 From ed7b6da749a0244a107f53007e0e17cfef917f3d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:04:07 +0530 Subject: [PATCH 3055/3167] Create README - LeetHub From 7c178341ae280bcc2135d2b60f958b972dc12bf6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:12:25 +0530 Subject: [PATCH 3056/3167] Create README - LeetHub --- 0088-merge-sorted-array/README.md | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0088-merge-sorted-array/README.md 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 From b19557d329652f75c696dc7e132d299795d27046 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:12:28 +0530 Subject: [PATCH 3057/3167] Time: 5 ms (28.10%), Space: 11.4 MB (11.74%) - LeetHub --- .../0088-merge-sorted-array.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0088-merge-sorted-array/0088-merge-sorted-array.cpp 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..22bbedc5 --- /dev/null +++ b/0088-merge-sorted-array/0088-merge-sorted-array.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + + vector ans; + int i = 0, j = 0; + + while(i < m and j < n) + { + if(nums1[i] < nums2[j]) + ans.push_back(nums1[i++]); + else + ans.push_back(nums2[j++]); + } + + while(i < m) + ans.push_back(nums1[i++]); + while(j < n) + ans.push_back(nums2[j++]); + + nums1 = ans; + + } +}; \ No newline at end of file From 49e76635d9af5d065f6e5412790cae0e1627a6e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:16:26 +0530 Subject: [PATCH 3058/3167] Attach NOTES - LeetHub --- 0088-merge-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0088-merge-sorted-array/NOTES.md 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 From bae62f3e80439d875129b2775fb100b7fc2e5db1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:16:28 +0530 Subject: [PATCH 3059/3167] Time: 2 ms (49.63%), Space: 11.1 MB (19.11%) - LeetHub --- .../0088-merge-sorted-array.cpp | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/0088-merge-sorted-array/0088-merge-sorted-array.cpp b/0088-merge-sorted-array/0088-merge-sorted-array.cpp index 22bbedc5..f5bd7797 100644 --- a/0088-merge-sorted-array/0088-merge-sorted-array.cpp +++ b/0088-merge-sorted-array/0088-merge-sorted-array.cpp @@ -1,24 +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; - vector ans; - int i = 0, j = 0; - - while(i < m and j < n) + while(i >= 0 and j >= 0) { - if(nums1[i] < nums2[j]) - ans.push_back(nums1[i++]); + if(nums1[i] > nums2[j]) + nums1[k--] = nums1[i--]; else - ans.push_back(nums2[j++]); + nums1[k--] = nums2[j--]; } - while(i < m) - ans.push_back(nums1[i++]); - while(j < n) - ans.push_back(nums2[j++]); - - nums1 = ans; + while(i >= 0) + nums1[k--] = nums1[i--]; + while(j >= 0) + nums1[k--] = nums2[j--]; } }; \ No newline at end of file From 6449d2c81c4ad3c3f9e41c21652a3d5c6dd829f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:45:02 +0530 Subject: [PATCH 3060/3167] Attach NOTES - LeetHub From 3bf5d938419dded5d5d0703df2579abe2010adae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:45:06 +0530 Subject: [PATCH 3061/3167] Time: 11 ms (93.77%), Space: 27 MB (42.59%) - LeetHub --- 0169-majority-element/0169-majority-element.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/0169-majority-element/0169-majority-element.cpp b/0169-majority-element/0169-majority-element.cpp index 9b85e7db..b2296cda 100644 --- a/0169-majority-element/0169-majority-element.cpp +++ b/0169-majority-element/0169-majority-element.cpp @@ -2,21 +2,22 @@ class Solution { public: int majorityElement(vector& nums) { - int cnt = 0, num = -1; + int cnt = 1, num1 = nums[0], n = nums.size(); - for(auto& ele : nums) + for(int i = 1; i < n; ++i) { + if(num1 != nums[i]) + --cnt; + else + ++cnt; + if(cnt == 0) { - num = ele; cnt = 1; + num1 = nums[i]; } - else if(ele == num) - ++cnt; - else - --cnt; } - return num; + return num1; } }; \ No newline at end of file From 899b4b2f60fb83388a7cfa2da1e56387ba4431df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:33:03 +0530 Subject: [PATCH 3062/3167] Attach NOTES - LeetHub --- 0493-reverse-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0493-reverse-pairs/NOTES.md 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 From add44adf338212f5e3144ebb0d095a79e97b94f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:41:37 +0530 Subject: [PATCH 3063/3167] Create README - LeetHub --- 0289-game-of-life/README.md | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0289-game-of-life/README.md 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 From d7601eca087cfed84735eb31922f23b62bfc4abd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:41:40 +0530 Subject: [PATCH 3064/3167] Time: 0 ms (100.00%), Space: 8.7 MB (8.42%) - LeetHub --- 0289-game-of-life/0289-game-of-life.cpp | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 0289-game-of-life/0289-game-of-life.cpp 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..bef875c7 --- /dev/null +++ b/0289-game-of-life/0289-game-of-life.cpp @@ -0,0 +1,58 @@ +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) + { + // cout< 3) + board[i][j] = 0; + } + else + { + // cout< Date: Sun, 21 Jul 2024 21:42:56 +0530 Subject: [PATCH 3065/3167] Attach NOTES - LeetHub --- 0289-game-of-life/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0289-game-of-life/NOTES.md 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 From 1b1a4d0392448a894bc80d8c3b82451078678dab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:42:59 +0530 Subject: [PATCH 3066/3167] Time: 4 ms (20.95%), Space: 8.6 MB (8.42%) - LeetHub --- 0289-game-of-life/0289-game-of-life.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/0289-game-of-life/0289-game-of-life.cpp b/0289-game-of-life/0289-game-of-life.cpp index bef875c7..eec57c7c 100644 --- a/0289-game-of-life/0289-game-of-life.cpp +++ b/0289-game-of-life/0289-game-of-life.cpp @@ -33,7 +33,6 @@ class Solution { if(newx >= 0 and newy >= 0 and newx < n and newy < m) { - // cout< Date: Mon, 22 Jul 2024 01:04:49 +0530 Subject: [PATCH 3067/3167] Create README - LeetHub --- 2392-build-a-matrix-with-conditions/README.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2392-build-a-matrix-with-conditions/README.md 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 From 1a924181ed95deb66a5ac282905d72f8eb79a0df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 01:04:53 +0530 Subject: [PATCH 3068/3167] Time: 108 ms (44.44%), Space: 65.2 MB (34.65%) - LeetHub --- .../2392-build-a-matrix-with-conditions.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp 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 From 29dd485e1cf61264e773af0e86c664d066598776 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:51:15 +0530 Subject: [PATCH 3069/3167] Create README - LeetHub --- 2418-sort-the-people/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2418-sort-the-people/README.md 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 From f2c81c48f5373f14b60473b97ddeced1a97d6179 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:51:16 +0530 Subject: [PATCH 3070/3167] Attach NOTES - LeetHub --- 2418-sort-the-people/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2418-sort-the-people/NOTES.md 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 From 14ae23618e460efe135013b94445bf5a17c3ba71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:51:20 +0530 Subject: [PATCH 3071/3167] Time: 31 ms (50.96%), Space: 27.9 MB (24.89%) - LeetHub --- 2418-sort-the-people/2418-sort-the-people.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2418-sort-the-people/2418-sort-the-people.cpp 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 From 8b8a2513acccd6cea7e7ae095152b5b6ecc172dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jul 2024 19:25:01 +0530 Subject: [PATCH 3072/3167] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1636-sort-array-by-increasing-frequency/README.md 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 From de73e81bb69d837470a1cffa8fb47c6b22873153 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:24:11 +0530 Subject: [PATCH 3073/3167] Create README - LeetHub --- .../README.md | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 3224-minimum-array-changes-to-make-differences-equal/README.md 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 From fd92e8e913a3aa87bcf359f4fb0748b42bbc4f87 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:24:15 +0530 Subject: [PATCH 3074/3167] Time: 394 ms (12.47%), Space: 136.4 MB (15.71%) - LeetHub --- ...rray-changes-to-make-differences-equal.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp 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 From 8c249b24fc6d0876fce5a51333fe799d263828f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:41:08 +0530 Subject: [PATCH 3075/3167] Create README - LeetHub --- 2191-sort-the-jumbled-numbers/README.md | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2191-sort-the-jumbled-numbers/README.md 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 From 1807a8aa06a39791dd0e0b7998ace64f65b9aba2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:41:11 +0530 Subject: [PATCH 3076/3167] Time: 296 ms (57.58%), Space: 88.8 MB (55.84%) - LeetHub --- .../2191-sort-the-jumbled-numbers.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp 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 From 1c70444081baaad8f421340e85a846538b8a0e43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:10:37 +0530 Subject: [PATCH 3077/3167] Create README - LeetHub From 177b8e7157e57d0897c5077883d95d5d4fa1809d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:54:49 +0530 Subject: [PATCH 3078/3167] Create README - LeetHub From 3d8391919d48f0f4d14c2beaa4cb0fec7e304301 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jul 2024 23:17:05 +0530 Subject: [PATCH 3079/3167] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md 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 From 0f827f90e6309af38b20d647558188b3961258d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jul 2024 22:48:41 +0530 Subject: [PATCH 3080/3167] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2976-minimum-cost-to-convert-string-i/README.md 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 From 205e5de390c2e7d6c9419f4d97abbcfe51b96493 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jul 2024 22:48:44 +0530 Subject: [PATCH 3081/3167] Time: 168 ms (70.36%), Space: 99.1 MB (47.68%) - LeetHub --- .../2976-minimum-cost-to-convert-string-i.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp 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 From 55834ef31833804abc038a7bda0c0c2739e338a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:38:52 +0530 Subject: [PATCH 3082/3167] Attach NOTES - LeetHub --- 2045-second-minimum-time-to-reach-destination/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2045-second-minimum-time-to-reach-destination/NOTES.md 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 From 38a222db6e32690360f02f8739bd242262a19e79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:38:56 +0530 Subject: [PATCH 3083/3167] Time: 666 ms (8.94%), Space: 223.2 MB (12.16%) - LeetHub --- ...cond-minimum-time-to-reach-destination.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp 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 From c0b66b698dab6f68a95750d57c33191d008ebbad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:27:38 +0530 Subject: [PATCH 3084/3167] Create README - LeetHub --- 1395-count-number-of-teams/README.md | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1395-count-number-of-teams/README.md 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 From 5a1cfca04ce1eb768bec9f3e2a2508a21448a688 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 31 Jul 2024 00:07:53 +0530 Subject: [PATCH 3085/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1653-minimum-deletions-to-make-string-balanced/README.md 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 From a8fb4e24c344398d2317254d73def9766425c5c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:11:25 +0530 Subject: [PATCH 3086/3167] Attach NOTES - LeetHub --- 1105-filling-bookcase-shelves/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1105-filling-bookcase-shelves/NOTES.md 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 From 98be600bac267bbe3409942435f26fd16bafe0c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:11:28 +0530 Subject: [PATCH 3087/3167] Time: 3 ms (87.31%), Space: 31.5 MB (5.37%) - LeetHub --- .../1105-filling-bookcase-shelves.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp 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 From 57210c0f0c4b3216c7e5fca81737750c3debbc93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:19:51 +0530 Subject: [PATCH 3088/3167] Create README - LeetHub --- 2678-number-of-senior-citizens/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2678-number-of-senior-citizens/README.md 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 From 63cb914baf463acf1fdcb1fb93d9809f2e7f708b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:19:54 +0530 Subject: [PATCH 3089/3167] Time: 8 ms (47.01%), Space: 17.6 MB (16.71%) - LeetHub --- .../2678-number-of-senior-citizens.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp 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 From 1c4b70fcf8e232ffb5a8ca3e906d21e1b609032a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:47:39 +0530 Subject: [PATCH 3090/3167] Create README - LeetHub From 74e61184918fe372f2b2e9846d11898894baea3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:06 +0530 Subject: [PATCH 3091/3167] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2134-minimum-swaps-to-group-all-1s-together-ii/README.md 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 From 5c9b070ffaa696396be920ec091b343fe5bdc141 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:09 +0530 Subject: [PATCH 3092/3167] Time: 80 ms (16.50%), Space: 91.1 MB (13.75%) - LeetHub --- ...imum-swaps-to-group-all-1s-together-ii.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp 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 From 89c0f32c851c41af667d130ad29968688cd9f7d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:12 +0530 Subject: [PATCH 3093/3167] Attach NOTES - LeetHub --- 2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md 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 From 639f7b9204ce138cba9a9818719de86c6a8ff29d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:15 +0530 Subject: [PATCH 3094/3167] Time: 80 ms (16.50%), Space: 91.1 MB (13.75%) - LeetHub From a8089845aabb71ef006654f54072515cb4a6598d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:52:46 +0530 Subject: [PATCH 3095/3167] Attach NOTES - LeetHub --- 1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md 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 From 1f69fc485453037ca326e4e700919c2b2cbd1b2d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:52:49 +0530 Subject: [PATCH 3096/3167] Time: 7 ms (84.20%), Space: 17.5 MB (57.40%) - LeetHub --- ...0-make-two-arrays-equal-by-reversing-subarrays.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp 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 From 745e8d482e0c416a02990899ed7fc9d5e77307b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:53:05 +0530 Subject: [PATCH 3097/3167] Attach NOTES - LeetHub From 74dc8f8f6957168b1b79235bff77edb35f7fe9ee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:53:08 +0530 Subject: [PATCH 3098/3167] Time: 7 ms (84.20%), Space: 17.5 MB (57.40%) - LeetHub From 162ddcc6defc886293edf653e5f3012e3e672621 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:34:09 +0530 Subject: [PATCH 3099/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1508-range-sum-of-sorted-subarray-sums/README.md 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 From 2d4d0823eaed5dedafc6429c26e2e7397b4b12ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:34:13 +0530 Subject: [PATCH 3100/3167] Time: 100 ms (22.70%), Space: 35 MB (15.96%) - LeetHub --- ...1508-range-sum-of-sorted-subarray-sums.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp 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 From 064cdde07322794c2ee2dc10d66548902dde8c3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:35:32 +0530 Subject: [PATCH 3101/3167] Attach NOTES - LeetHub --- 1508-range-sum-of-sorted-subarray-sums/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1508-range-sum-of-sorted-subarray-sums/NOTES.md 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 From 3878a2c37771170841205c371c757bc50a187d35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:52:16 +0530 Subject: [PATCH 3102/3167] Create README - LeetHub From 6e3c5f8d3753d5bd544ab3c63b65944cc05f964a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:00:03 +0530 Subject: [PATCH 3103/3167] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0462-minimum-moves-to-equal-array-elements-ii/README.md 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 From 33756b13abd1686b58c35f1469511f155ecf2b24 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:00:06 +0530 Subject: [PATCH 3104/3167] Time: 9 ms (59.93%), Space: 14.8 MB (48.57%) - LeetHub --- ...nimum-moves-to-equal-array-elements-ii.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp 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 From 1860c57bf3af6fe75f3e828fead2dc97e5852f51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:01:23 +0530 Subject: [PATCH 3105/3167] Attach NOTES - LeetHub --- 0462-minimum-moves-to-equal-array-elements-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0462-minimum-moves-to-equal-array-elements-ii/NOTES.md 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 From 03c725d99defe08d2cf8cf62b6141c567bbabc74 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:01:26 +0530 Subject: [PATCH 3106/3167] Time: 9 ms (59.93%), Space: 14.8 MB (48.57%) - LeetHub From 78c8bd492a4251e90de2bba4864e48751b667554 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:14:26 +0530 Subject: [PATCH 3107/3167] Attach NOTES - LeetHub --- 0067-add-binary/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0067-add-binary/NOTES.md 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 From 6d8ef1a18cd2fd81ab18d563f3f68b39ae6ca641 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:17:19 +0530 Subject: [PATCH 3108/3167] Attach NOTES - LeetHub From beb8ad4e07e3f7a85c1be941fd3f040b55ec5a3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:20:25 +0530 Subject: [PATCH 3109/3167] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0628-maximum-product-of-three-numbers/README.md 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 From 193b8a8a3a9210483b63ac17f1379db1107e7e4a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:20:28 +0530 Subject: [PATCH 3110/3167] Time: 37 ms (38.52%), Space: 31.5 MB (27.86%) - LeetHub --- .../0628-maximum-product-of-three-numbers.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp 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 From 403a8c1af9c40124bdc03fc8d8c3b987969a404b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:21:30 +0530 Subject: [PATCH 3111/3167] Attach NOTES - LeetHub --- 0628-maximum-product-of-three-numbers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0628-maximum-product-of-three-numbers/NOTES.md 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 From 7b8ec3179e96b8ac5702b5e8df461ace6fbe8520 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:09:53 +0530 Subject: [PATCH 3112/3167] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2053-kth-distinct-string-in-an-array/README.md 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 From 771b82c2884177aab637da815704a41ea000491c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:09:54 +0530 Subject: [PATCH 3113/3167] Attach NOTES - LeetHub --- 2053-kth-distinct-string-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2053-kth-distinct-string-in-an-array/NOTES.md 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 From 7a69d3f7a113f7865da53ab976669c3f1e77f2aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:09:58 +0530 Subject: [PATCH 3114/3167] Time: 33 ms (9.15%), Space: 20.2 MB (5.44%) - LeetHub --- .../2053-kth-distinct-string-in-an-array.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp 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..b1a31e15 --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp @@ -0,0 +1,23 @@ +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); + ++mp[str]; + } + } + + return (vec.size() < k ? "" : vec[k-1]); + + } +}; \ No newline at end of file From f75a51f5bc6357acd90ce9c128abb0c61c71cede Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:10:43 +0530 Subject: [PATCH 3115/3167] Attach NOTES - LeetHub From ae4bea2c9523f1f530225920a6b62dbb68509295 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:10:46 +0530 Subject: [PATCH 3116/3167] Time: 22 ms (27.19%), Space: 20.2 MB (5.97%) - LeetHub --- .../2053-kth-distinct-string-in-an-array.cpp | 1 - 1 file changed, 1 deletion(-) 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 index b1a31e15..fa609201 100644 --- 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 @@ -13,7 +13,6 @@ class Solution { if(mp[str] == 1) { vec.push_back(str); - ++mp[str]; } } From 6b27bda96149e8ceb221b7bcb58d19199364f453 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:11:15 +0530 Subject: [PATCH 3117/3167] Attach NOTES - LeetHub From 94fdc7426ede891c4469637fd58c80f437f5480c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:37:14 +0530 Subject: [PATCH 3118/3167] Create README - LeetHub --- 0273-integer-to-english-words/README.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0273-integer-to-english-words/README.md 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 From cd6b587d8e1c461dd3662b387681882eb3a01bdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:37:18 +0530 Subject: [PATCH 3119/3167] Time: 4 ms (49.19%), Space: 12.4 MB (28.62%) - LeetHub --- .../0273-integer-to-english-words.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0273-integer-to-english-words/0273-integer-to-english-words.cpp 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 From eb89a74ab3a07af55f501ba26b4be740aa9e673f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Aug 2024 23:45:05 +0530 Subject: [PATCH 3120/3167] Create README - LeetHub --- 0885-spiral-matrix-iii/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0885-spiral-matrix-iii/README.md 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 From 8a4f254c7adde2f794ae29d09e184eef96b79dd7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Aug 2024 23:45:08 +0530 Subject: [PATCH 3121/3167] Time: 11 ms (42.57%), Space: 13.9 MB (23.17%) - LeetHub --- .../0885-spiral-matrix-iii.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp 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 From fc928e4faa0dbcf01ed14e56ad757eff1018408b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:36:12 +0530 Subject: [PATCH 3122/3167] Create README - LeetHub --- 0840-magic-squares-in-grid/README.md | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0840-magic-squares-in-grid/README.md 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 From 33fe7b6e326749685b019c87e200487971519290 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:36:16 +0530 Subject: [PATCH 3123/3167] Time: 11 ms (5.88%), Space: 15.3 MB (7.14%) - LeetHub --- .../0840-magic-squares-in-grid.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp 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 From 37078e2d5df16db4669f559c0c0f1c44fffff8b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:37:05 +0530 Subject: [PATCH 3124/3167] Attach NOTES - LeetHub --- 0840-magic-squares-in-grid/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0840-magic-squares-in-grid/NOTES.md 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 From e16569c4e008ef4e314cb3f0e5d374ee3630466d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:37:08 +0530 Subject: [PATCH 3125/3167] Time: 11 ms (5.88%), Space: 15.3 MB (7.14%) - LeetHub From ae53d915e5e3281c3330124d63af6fa2f556a00f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:16 +0530 Subject: [PATCH 3126/3167] Create README - LeetHub --- 0959-regions-cut-by-slashes/README.md | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0959-regions-cut-by-slashes/README.md 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 From bf8019f434c26995745e6ab1741f6163d53311d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:20 +0530 Subject: [PATCH 3127/3167] Time: 33 ms (10.57%), Space: 16.8 MB (13.01%) - LeetHub --- .../0959-regions-cut-by-slashes.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp 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 From f088809c12db10d62cd1fee27dd60b306df28086 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:47 +0530 Subject: [PATCH 3128/3167] Attach NOTES - LeetHub --- 0959-regions-cut-by-slashes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0959-regions-cut-by-slashes/NOTES.md 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 From fdb7c9c966839ecf75603036931dfd369a7ef8c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:50 +0530 Subject: [PATCH 3129/3167] Time: 33 ms (10.57%), Space: 16.8 MB (13.01%) - LeetHub From 371d31850ebb828de31d8dac780f0b64363c2924 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:11:37 +0530 Subject: [PATCH 3130/3167] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1568-minimum-number-of-days-to-disconnect-island/README.md 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 From 66aef824a5dfaebbff473b4e9d60934064f6b074 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:11:42 +0530 Subject: [PATCH 3131/3167] Time: 84 ms (52.59%), Space: 25.5 MB (49.71%) - LeetHub --- ...um-number-of-days-to-disconnect-island.cpp | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp 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 From e6763ba9014b2f3f27e201fbdbb9505ca7f55134 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Aug 2024 08:51:31 +0530 Subject: [PATCH 3132/3167] Create README - LeetHub --- 0040-combination-sum-ii/README.md | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0040-combination-sum-ii/README.md 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 From 58fd0d0b5b8ce03d8053436e5781cf8421681560 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Aug 2024 08:51:35 +0530 Subject: [PATCH 3133/3167] Time: 0 ms (100.00%), Space: 14.2 MB (21.77%) - LeetHub --- .../0040-combination-sum-ii.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0040-combination-sum-ii/0040-combination-sum-ii.cpp 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 From 5866cd6b03499fd697140607661233ae1ad877b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:18:07 +0530 Subject: [PATCH 3134/3167] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0719-find-k-th-smallest-pair-distance/README.md 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 From 620684b453d05ba1bfdce3ed92748d91c403140e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:18:07 +0530 Subject: [PATCH 3135/3167] Attach NOTES - LeetHub --- 0719-find-k-th-smallest-pair-distance/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0719-find-k-th-smallest-pair-distance/NOTES.md 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 From 35434784dee0edbfc89ec99f6ae558025dc8bbe9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:18:10 +0530 Subject: [PATCH 3136/3167] Time: 522 ms (8.41%), Space: 96.5 MB (5.05%) - LeetHub --- .../0719-find-k-th-smallest-pair-distance.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp 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 From 445d8b61df4a2c00297d0d4052c112b9cc3b6d4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:17:58 +0530 Subject: [PATCH 3137/3167] Create README - LeetHub --- 0860-lemonade-change/README.md | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0860-lemonade-change/README.md 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 From b80851320d42ee2e4cd8aea755b5470f714f278c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:18:01 +0530 Subject: [PATCH 3138/3167] Time: 67 ms (77.92%), Space: 86.1 MB (32.71%) - LeetHub --- 0860-lemonade-change/0860-lemonade-change.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0860-lemonade-change/0860-lemonade-change.cpp 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 From 3d210b8fe1b52245f04b4d87aa61e93ba026246d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:18:41 +0530 Subject: [PATCH 3139/3167] Attach NOTES - LeetHub --- 0860-lemonade-change/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0860-lemonade-change/NOTES.md 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 From 9588e1485a96c7ca6f83683303a827cb3569fac1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:18:44 +0530 Subject: [PATCH 3140/3167] Time: 67 ms (77.92%), Space: 86.1 MB (32.71%) - LeetHub From 4602a5a9d5974444c761416382ca12d7a3753172 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:16:53 +0530 Subject: [PATCH 3141/3167] Create README - LeetHub --- 0624-maximum-distance-in-arrays/README.md | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0624-maximum-distance-in-arrays/README.md 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 From b9ebf93aeddf1cfa59531075dbff8f9775fdc6a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:16:56 +0530 Subject: [PATCH 3142/3167] Time: 235 ms (49.06%), Space: 107.8 MB (69.81%) - LeetHub --- .../0624-maximum-distance-in-arrays.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp 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 From 86180dace4573f9490192c6dabdacfd36c241d3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Aug 2024 23:57:37 +0530 Subject: [PATCH 3143/3167] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1937-maximum-number-of-points-with-cost/README.md 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 From ad62b6816981b853f858badf064e40716a0cccdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Aug 2024 20:55:27 +0530 Subject: [PATCH 3144/3167] Create README - LeetHub --- 0264-ugly-number-ii/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0264-ugly-number-ii/README.md 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 From fa838d608420553d6b3535595064781a144b9153 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Aug 2024 20:55:30 +0530 Subject: [PATCH 3145/3167] Time: 3 ms (76.61%), Space: 9.7 MB (72.38%) - LeetHub --- 0264-ugly-number-ii/0264-ugly-number-ii.cpp | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0264-ugly-number-ii/0264-ugly-number-ii.cpp 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 From 75d0be16f04fb7f0e46422472f33d81629680145 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:22:48 +0530 Subject: [PATCH 3146/3167] Create README - LeetHub --- 0650-2-keys-keyboard/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0650-2-keys-keyboard/README.md 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 From 98787384617c52655f96a1dce799678cfeada80a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:22:51 +0530 Subject: [PATCH 3147/3167] Time: 26 ms (19.81%), Space: 157.3 MB (8.56%) - LeetHub --- 0650-2-keys-keyboard/0650-2-keys-keyboard.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0650-2-keys-keyboard/0650-2-keys-keyboard.cpp 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 From 2d5b4ac0d42400b0406cfaf08c0be08fd9d30f70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:24:03 +0530 Subject: [PATCH 3148/3167] Attach NOTES - LeetHub --- 0650-2-keys-keyboard/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0650-2-keys-keyboard/NOTES.md 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 From 6f155c8206492b0c0232e62ef8a348a5678df152 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:24:06 +0530 Subject: [PATCH 3149/3167] Time: 26 ms (19.81%), Space: 157.3 MB (8.56%) - LeetHub From 60459ff6ef7e4ec148b98f72193c890ff6691b8c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:45:08 +0530 Subject: [PATCH 3150/3167] Attach NOTES - LeetHub --- 1140-stone-game-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1140-stone-game-ii/NOTES.md 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 From 1f9efafcad18ab57895a69e112175363ec0e2cb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:32:50 +0530 Subject: [PATCH 3151/3167] Attach NOTES - LeetHub --- 0664-strange-printer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0664-strange-printer/NOTES.md 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 From 87114d097ecb6cce68b6f2a15defecfd95a27e37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:45:45 +0530 Subject: [PATCH 3152/3167] Create README - LeetHub --- 0476-number-complement/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0476-number-complement/README.md 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 From a596f65bed1faf880defdede492f3a10bad8e30a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:45:49 +0530 Subject: [PATCH 3153/3167] Time: 3 ms (41.07%), Space: 7.6 MB (33.08%) - LeetHub --- .../0476-number-complement.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0476-number-complement/0476-number-complement.cpp 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 From a3a5265fa8ab2f8aaf81ff442b7369573fa8977a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:28:18 +0530 Subject: [PATCH 3154/3167] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0592-fraction-addition-and-subtraction/README.md 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 From 9d0a5ea92df9b7d020ebcc2cc4040cf6a4a71833 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:28:21 +0530 Subject: [PATCH 3155/3167] Time: 3 ms (50.39%), Space: 7.7 MB (58.91%) - LeetHub --- ...0592-fraction-addition-and-subtraction.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp 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 From ebabe8276ba152993921a703151f1f842170e21d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:25 +0530 Subject: [PATCH 3156/3167] Create README - LeetHub --- 0564-find-the-closest-palindrome/README.md | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0564-find-the-closest-palindrome/README.md 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 From 3e226f1b9b29fe00fa2068e510a42e6d15d6679d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:28 +0530 Subject: [PATCH 3157/3167] Time: 0 ms (100.00%), Space: 8.3 MB (77.93%) - LeetHub --- .../0564-find-the-closest-palindrome.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp 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 From 54589be19d702fb408316e1d0fb2495d33710458 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:38 +0530 Subject: [PATCH 3158/3167] Attach NOTES - LeetHub --- 0564-find-the-closest-palindrome/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0564-find-the-closest-palindrome/NOTES.md 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 From 902a5917c19585f82df1931bf5ea0bd270e6d7bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:41 +0530 Subject: [PATCH 3159/3167] Time: 0 ms (100.00%), Space: 8.3 MB (77.93%) - LeetHub From aab27573fd0c12dce1ba52bcb2effc0c7a5f1014 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:59:47 +0530 Subject: [PATCH 3160/3167] Create README - LeetHub --- 0590-n-ary-tree-postorder-traversal/README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0590-n-ary-tree-postorder-traversal/README.md 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 From 627eef7c7f9de705355ece74b8b06575fa3d23af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:59:51 +0530 Subject: [PATCH 3161/3167] Time: 18 ms (22.62%), Space: 15.3 MB (49.47%) - LeetHub --- .../0590-n-ary-tree-postorder-traversal.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp 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..8267c9b4 --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp @@ -0,0 +1,47 @@ +/* +// 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(chlNode->val); + } + } + } + + vector postorder(Node* root) { + + vector ans; + + postorder(root, ans); + if(root) + ans.push_back(root->val); + + return ans; + + } +}; \ No newline at end of file From ff5e9be3885d29529448831df81c16863eb85fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:00:34 +0530 Subject: [PATCH 3162/3167] Attach NOTES - LeetHub --- 0590-n-ary-tree-postorder-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0590-n-ary-tree-postorder-traversal/NOTES.md 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 From ec0bd5381cecced2b68baf36c83c5a41875ada97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:00:37 +0530 Subject: [PATCH 3163/3167] Time: 14 ms (51.70%), Space: 15.2 MB (49.47%) - LeetHub --- .../0590-n-ary-tree-postorder-traversal.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 index 8267c9b4..fb8c1b94 100644 --- 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 @@ -28,8 +28,8 @@ class Solution { for(auto& chlNode : root->children) { postorder(chlNode, ans); - ans.push_back(chlNode->val); } + ans.push_back(root->val); } } @@ -38,9 +38,7 @@ class Solution { vector ans; postorder(root, ans); - if(root) - ans.push_back(root->val); - + return ans; } From 64b28717a2fb8a1c554fe7ab0872ec4bc0338221 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:36:37 +0530 Subject: [PATCH 3164/3167] Create README - LeetHub --- 1905-count-sub-islands/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1905-count-sub-islands/README.md 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 From 67abafeb5a9c9f31d41361b6e1860f31e7af827c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:36:41 +0530 Subject: [PATCH 3165/3167] Time: 2756 ms (5.07%), Space: 118.6 MB (26.09%) - LeetHub --- .../1905-count-sub-islands.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 1905-count-sub-islands/1905-count-sub-islands.cpp 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..20251889 --- /dev/null +++ b/1905-count-sub-islands/1905-count-sub-islands.cpp @@ -0,0 +1,69 @@ +class Solution { +public: + int countSubIslands(vector>& grid1, vector>& grid2) { + + int n = grid1.size(); + int m = grid1[0].size(); + + vector> visited(n, vector(m, false)), visited2(n, vector(m, false)); + + function dfs = [&](int i, int j) + { + if(i < 0 or j < 0 or i >= n or j >= m or visited[i][j] or !grid1[i][j]) + 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; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited[i][j] and grid1[i][j]) + dfs(i, j); + } + } + + function dfs2 = [&](int i, int j) -> bool{ + if(i < 0 or j < 0 or i >= n or j >= m or visited2[i][j] or !grid2[i][j]) + return true; + + if(!visited[i][j]) return false; + + visited2[i][j] = true; + + bool up, left, right, down; + up = left = right = down = true; + + up = dfs2(i-1, j); + left = dfs2(i, j-1); + down = dfs2(i+1, j); + right = dfs2(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(!visited2[i][j] and grid2[i][j]) + { + if(dfs2(i, j)) + { + ++subIsland; + } + } + } + } + + return subIsland; + } +}; \ No newline at end of file From 19280e2de1e68d6485bd939cae84476e037106d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:38:46 +0530 Subject: [PATCH 3166/3167] Attach NOTES - LeetHub --- 1905-count-sub-islands/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1905-count-sub-islands/NOTES.md 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 From 15a212cae2f0d824a4201777e399822daa4ec374 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:38:50 +0530 Subject: [PATCH 3167/3167] Time: 604 ms (16.65%), Space: 115.6 MB (26.66%) - LeetHub --- .../1905-count-sub-islands.cpp | 44 +++++-------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/1905-count-sub-islands/1905-count-sub-islands.cpp b/1905-count-sub-islands/1905-count-sub-islands.cpp index 20251889..bd5ccebd 100644 --- a/1905-count-sub-islands/1905-count-sub-islands.cpp +++ b/1905-count-sub-islands/1905-count-sub-islands.cpp @@ -5,45 +5,23 @@ class Solution { int n = grid1.size(); int m = grid1[0].size(); - vector> visited(n, vector(m, false)), visited2(n, vector(m, false)); + vector> visited(n, vector(m, false)); - function dfs = [&](int i, int j) - { - if(i < 0 or j < 0 or i >= n or j >= m or visited[i][j] or !grid1[i][j]) - 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; ++i) - { - for(int j = 0; j < m; ++j) - { - if(!visited[i][j] and grid1[i][j]) - dfs(i, j); - } - } - - function dfs2 = [&](int i, int j) -> bool{ - if(i < 0 or j < 0 or i >= n or j >= m or visited2[i][j] or !grid2[i][j]) + 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(!visited[i][j]) return false; + if(!grid1[i][j]) return false; - visited2[i][j] = true; + visited[i][j] = true; bool up, left, right, down; up = left = right = down = true; - up = dfs2(i-1, j); - left = dfs2(i, j-1); - down = dfs2(i+1, j); - right = dfs2(i, j+1); + 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; }; @@ -54,9 +32,9 @@ class Solution { { for(int j = 0; j < m; ++j) { - if(!visited2[i][j] and grid2[i][j]) + if(!visited[i][j] and grid2[i][j]) { - if(dfs2(i, j)) + if(dfs(i, j)) { ++subIsland; }