-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #769 from thesuaveguy/leetcode
issue #421 added leetcode weekly contest 315 cpp solution
- Loading branch information
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*2441. Largest Positive Integer That Exists With Its Negative */ | ||
|
||
/*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.*/ | ||
|
||
// Test cases | ||
/* | ||
1.) | ||
Input: nums = [-1,2,-3,3] | ||
Output: 3 | ||
2.) | ||
Input: nums = [-1,10,6,7,-7,1] | ||
Output: 7 | ||
3.) | ||
Input: nums = [-10,8,6,7,-2,-3] | ||
Output: -1 | ||
*/ | ||
|
||
class Solution | ||
{ | ||
public: | ||
int findMaxK(vector<int> &nums) | ||
{ | ||
set<int> s; | ||
int ans = -1; | ||
for (int i = 0; i < nums.size(); i++) | ||
s.insert(nums[i]); | ||
sort(nums.begin(), nums.end()); | ||
for (int i = nums.size() - 1; i >= 0; i--) | ||
{ | ||
if (s.find(-1 * nums[i]) != s.end()) | ||
{ | ||
|
||
ans = nums[i]; | ||
break; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*2442. Count Number of Distinct Integers After Reverse Operations*/ | ||
|
||
/*You are given an array nums consisting of positive integers. | ||
You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums. | ||
Return the number of distinct integers in the final array.*/ | ||
|
||
// Test cases | ||
/* | ||
1.) | ||
Input: nums = [1,13,10,12,31] | ||
Output: 6 | ||
2.) | ||
Input: nums = [2,2,2] | ||
Output: 1 | ||
*/ | ||
|
||
class Solution | ||
{ | ||
public: | ||
int countDistinctIntegers(vector<int> &nums) | ||
{ | ||
set<int> s; | ||
for (int i = 0; i < nums.size(); i++) | ||
s.insert(nums[i]); | ||
int n = nums.size(); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int c = 0; | ||
int d = nums[i]; | ||
while (d != 0) | ||
{ | ||
int l = d % 10; | ||
c = c * 10 + l; | ||
d /= 10; | ||
} | ||
s.insert(c); | ||
} | ||
return s.size(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*2443. Sum of Number and Its Reverse*/ | ||
|
||
/*Given a non-negative integer num, return true if num can be expressed as the sum of any non-negative integer and its reverse, or false otherwise. | ||
*/ | ||
|
||
// Test cases | ||
/* | ||
1.) | ||
Input: num = 443 | ||
Output: true | ||
2.) | ||
Input: num = 63 | ||
Output: false | ||
3.) | ||
Input: num = 181 | ||
Output: true | ||
*/ | ||
class Solution | ||
{ | ||
public: | ||
int rev(int d) | ||
{ | ||
int c = 0; | ||
while (d != 0) | ||
{ | ||
int l = d % 10; | ||
c = c * 10 + l; | ||
d /= 10; | ||
} | ||
return c; | ||
} | ||
bool sumOfNumberAndReverse(int num) | ||
{ | ||
for (int i = 0; i <= num; i++) | ||
{ | ||
if (i + rev(i) == num) | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*2444. Count Subarrays With Fixed Bounds*/ | ||
|
||
/*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.*/ | ||
|
||
// Test cases | ||
/* | ||
1.) | ||
Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5 | ||
Output: 2 | ||
2.) | ||
Input: nums = [1,1,1,1], minK = 1, maxK = 1 | ||
Output: 10 | ||
*/ | ||
|
||
class Solution | ||
{ | ||
public: | ||
long long countSubarrays(vector<int> &nums, int minK, int maxK) | ||
{ | ||
vector<int> newvec; | ||
int size = nums.size(); | ||
vector<int> v2; | ||
vector<int> v1; | ||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
if (nums[i] == minK) | ||
newvec.push_back(i); | ||
} | ||
newvec.push_back(size); | ||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
if (nums[i] == maxK) | ||
v1.push_back(i); | ||
} | ||
v1.push_back(size); | ||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
if (nums[i] < minK || nums[i] > maxK) | ||
v2.push_back(i); | ||
} | ||
v2.push_back(size); | ||
long long int ans = 0; | ||
for (int i = 0; i < size; i++) | ||
{ | ||
int os2 = *lower_bound(newvec.begin(), newvec.end(), i); | ||
int os3 = *lower_bound(v1.begin(), v1.end(), i); | ||
int os1 = *lower_bound(v2.begin(), v2.end(), i); | ||
int ann = max(os2, os3); | ||
int temp = 0; | ||
if (ann < os1) | ||
temp = os1 - ann; | ||
|
||
ans += temp; | ||
} | ||
return ans; | ||
} | ||
}; |