Skip to content

Commit b47d542

Browse files
Add files via upload
1 parent eea4720 commit b47d542

6 files changed

+401
-0
lines changed

BuyTwoChocolates.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
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.
3+
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.
4+
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.
5+
6+
Example 1:
7+
Input: prices = [1,2,2], money = 3
8+
Output: 0
9+
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.
10+
11+
Example 2:
12+
Input: prices = [3,2,3], money = 3
13+
Output: 3
14+
Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.
15+
*/
16+
17+
18+
class Solution {
19+
public:
20+
int buyChoco(vector<int>& prices, int money) {
21+
sort(prices.begin(),prices.end());
22+
int l=prices[0], m=prices[1];
23+
if(l+m<=money){
24+
return (money-(l+m));
25+
}else{
26+
return money;
27+
}
28+
return money;
29+
}
30+
};

ExtraCharactersInAString.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
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.
3+
Return the minimum number of extra characters left over if you break up s optimally.
4+
5+
Example 1:
6+
Input: s = "leetscode", dictionary = ["leet","code","leetcode"]
7+
Output: 1
8+
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.
9+
10+
Example 2:
11+
Input: s = "sayhelloworld", dictionary = ["hello","world"]
12+
Output: 3
13+
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.
14+
*/
15+
16+
class Solution {
17+
public:
18+
int minExtraChar(string s, vector<string>& dictionary) {
19+
set<string> dict(dictionary.begin(),dictionary.end());
20+
int n=s.size();
21+
vector<int> dp(n+1,0);
22+
for(int i=1;i<=n;i++){
23+
dp[i]=dp[i-1]+1;
24+
for(int j=i;j>=1;j--){
25+
string str=s.substr(j-1,i-j+1);
26+
if(dict.find(str)!=dict.end()){
27+
dp[i]=min(dp[i],dp[j-1]);
28+
}
29+
}
30+
}
31+
return dp[n];
32+
}
33+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Given a positive integer n, return the punishment number of n.
3+
The punishment number of n is defined as the sum of the squares of all integers i such that:
4+
1 <= i <= n
5+
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.
6+
7+
Example 1:
8+
Input: n = 10
9+
Output: 182
10+
Explanation: There are exactly 3 integers i that satisfy the conditions in the statement:
11+
- 1 since 1 * 1 = 1
12+
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
13+
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
14+
Hence, the punishment number of 10 is 1 + 81 + 100 = 182
15+
16+
Example 2:
17+
Input: n = 37
18+
Output: 1478
19+
Explanation: There are exactly 4 integers i that satisfy the conditions in the statement:
20+
- 1 since 1 * 1 = 1.
21+
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
22+
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
23+
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
24+
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
25+
*/
26+
27+
28+
class Solution {
29+
public:
30+
bool solve(int i, int x, int total, string &str){
31+
if(i>=str.size()) return (x==total);
32+
bool ans=0;
33+
int q=0;
34+
for(int j=i;j<str.size();j++){
35+
q=10*q+int(str[j])-48;
36+
ans|=solve(j+1,x+q,total,str);
37+
if(ans) return 1;
38+
}
39+
return ans;
40+
}
41+
int punishmentNumber(int n) {
42+
long long int result=0;
43+
for(int i=1;i<=n;i++){
44+
string str=to_string(i*i);
45+
if(solve(0,0,i,str)){
46+
result+=(i*i);
47+
}
48+
}
49+
return result;
50+
}
51+
};

GreatestCommonDivisorTraversal.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
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.
3+
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.
4+
Return true if it is possible to traverse between all such pairs of indices, or false otherwise.
5+
6+
Example 1:
7+
Input: nums = [2,3,6]
8+
Output: true
9+
Explanation: In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).
10+
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.
11+
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.
12+
13+
Example 2:
14+
Input: nums = [3,9,5]
15+
Output: false
16+
Explanation: No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
17+
18+
Example 3:
19+
Input: nums = [4,3,12,8]
20+
Output: true
21+
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.
22+
*/
23+
24+
class Solution {
25+
public:
26+
class UnionFind{
27+
private:
28+
vector<int> parent, rank;
29+
public:
30+
UnionFind(int n){
31+
parent.resize(n);
32+
rank.resize(n,1);
33+
for(int i=0;i<n;i++){
34+
parent[i]=i;
35+
}
36+
}
37+
int find(int x){
38+
if(parent[x]!=x){
39+
parent[x]=find(parent[x]);
40+
}
41+
return parent[x];
42+
}
43+
void unionByRank(int x, int y){
44+
int rx=find(x);
45+
int ry=find(y);
46+
if(rx!=ry){
47+
if(rank[rx]<rank[ry]){
48+
parent[rx]=ry;
49+
}else if(rank[rx]>rank[ry]){
50+
parent[ry]=rx;
51+
}else{
52+
parent[rx]=ry;
53+
rank[ry]++;
54+
}
55+
}
56+
}
57+
};
58+
59+
bool canTraverseAllPairs(vector<int>& nums) {
60+
int n=nums.size();
61+
UnionFind u(n);
62+
unordered_map<int, vector<int>> mp;
63+
for(int i=0;i<n;i++){
64+
for(int j=2;j*j<=nums[i];j++){
65+
if(nums[i]%j==0){
66+
mp[j].push_back(i);
67+
while(nums[i]%j==0){
68+
nums[i]/=j;
69+
}
70+
}
71+
}
72+
if(nums[i]>1){
73+
mp[nums[i]].push_back(i);
74+
}
75+
}
76+
for(auto it : mp){
77+
vector<int> &ind=it.second;
78+
for(int i=1;i<ind.size();i++){
79+
u.unionByRank(ind[i-1],ind[i]);
80+
}
81+
}
82+
int root=u.find(0);
83+
for(int i=1;i<n;i++){
84+
if(u.find(i)!=root){
85+
return false;
86+
}
87+
}
88+
return true;
89+
}
90+
};

MaximumStrengthOfAGroup.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
You are given a 0-indexed integer array nums representing the score of students in an exam. The teacher would like to form one non-empty group of students with maximal strength, where the strength of a group of students of indices i0, i1, i2, ... , ik is defined as nums[i0] * nums[i1] * nums[i2] * ... * nums[ik​].
3+
Return the maximum strength of a group the teacher can create.
4+
5+
Example 1:
6+
Input: nums = [3,-1,-5,2,5,-9]
7+
Output: 1350
8+
Explanation: One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal.
9+
10+
Example 2:
11+
Input: nums = [-4,-5,-4]
12+
Output: 20
13+
Explanation: Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.
14+
*/
15+
16+
17+
class Solution {
18+
public:
19+
long long maxStrength(vector<int>& nums) {
20+
int n=nums.size();
21+
if(n==1) return nums[0];
22+
vector<int> less, great;
23+
for(int i=0;i<n;i++){
24+
if(nums[i]<0){
25+
less.push_back(nums[i]);
26+
}else if(nums[i]>0){
27+
great.push_back(nums[i]);
28+
}
29+
}
30+
long long ans=0;
31+
sort(great.begin(),great.end());
32+
sort(less.begin(),less.end());
33+
for(int i=0;i<less.size();i++){
34+
if(i==0){
35+
ans=(long long)less[i];
36+
}else{
37+
if(less.size()%2==0){
38+
long long neg=less[i];
39+
ans*=neg;
40+
}else{
41+
if(i<less.size()-1){
42+
long long neg=less[i];
43+
ans*=neg;
44+
}
45+
}
46+
}
47+
}
48+
long long ans2=0;
49+
for(int i=0;i<great.size();i++){
50+
if(ans2==0)
51+
ans2=(long long)great[i];
52+
else{
53+
long long pos=great[i];
54+
ans2*=pos;
55+
}
56+
}
57+
58+
if(ans>0 and ans2>0){
59+
return (long long)(ans*ans2);
60+
}else if(ans>0){
61+
return ans;
62+
}else if(ans2>0){
63+
return ans2;
64+
}else{
65+
return 0;
66+
}
67+
}
68+
};

0 commit comments

Comments
 (0)