Skip to content

Commit a9a0e5a

Browse files
committed
Adds every code till 8th Jan '25
1 parent 86a4c21 commit a9a0e5a

File tree

15 files changed

+451
-0
lines changed

15 files changed

+451
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# String Matching in an Array
2+
3+
**Question ID**: 1408
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 5 ms
7+
**Memory**: 12 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/string-matching-in-an-array
12+
class Solution {
13+
public:
14+
vector<string> stringMatching(vector<string>& words) {
15+
16+
vector<string> ans;
17+
18+
for(auto i : words){
19+
for(auto j:words){
20+
if(i!=j){
21+
if(j.find(i)!=string:: npos){
22+
ans.push_back(i);
23+
break;
24+
}
25+
}
26+
}
27+
}
28+
29+
30+
return ans;
31+
32+
}
33+
};
34+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1501165727,
3+
"question_id": 1408,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1738761555,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "5 ms",
10+
"url": "https://leetcode.com/submissions/detail/1501165727/",
11+
"is_pending": "Not Pending",
12+
"title": "String Matching in an Array",
13+
"memory": "12 MB",
14+
"title_slug": "string-matching-in-an-array",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/string-matching-in-an-array
5+
class Solution {
6+
public:
7+
vector<string> stringMatching(vector<string>& words) {
8+
9+
vector<string> ans;
10+
11+
for(auto i : words){
12+
for(auto j:words){
13+
if(i!=j){
14+
if(j.find(i)!=string:: npos){
15+
ans.push_back(i);
16+
break;
17+
}
18+
}
19+
}
20+
}
21+
22+
23+
return ans;
24+
25+
}
26+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Two Sum II - Input Array Is Sorted
2+
3+
**Question ID**: 167
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 19.41 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/two-sum-ii---input-array-is-sorted
12+
class Solution
13+
{
14+
public:
15+
16+
vector<int> twoSum(vector<int> &numbers, int target)
17+
{
18+
19+
vector<int> ans(2);
20+
21+
int low=0,high=numbers.size()-1;
22+
23+
while(low<high){
24+
int sum=numbers[low]+numbers[high];
25+
if(sum==target)
26+
return {++low,++high};
27+
else if(sum>target){
28+
high--;
29+
}else
30+
low++;
31+
}
32+
33+
return {0,0};
34+
}
35+
};
36+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1501978337,
3+
"question_id": 167,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1738761686,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1501978337/",
11+
"is_pending": "Not Pending",
12+
"title": "Two Sum II - Input Array Is Sorted",
13+
"memory": "19.41 MB",
14+
"title_slug": "two-sum-ii---input-array-is-sorted",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/two-sum-ii---input-array-is-sorted
5+
6+
class Solution
7+
{
8+
public:
9+
10+
vector<int> twoSum(vector<int> &numbers, int target)
11+
{
12+
13+
vector<int> ans(2);
14+
15+
int low=0,high=numbers.size()-1;
16+
17+
while(low<high){
18+
int sum=numbers[low]+numbers[high];
19+
if(sum==target)
20+
return {++low,++high};
21+
else if(sum>target){
22+
high--;
23+
}else
24+
low++;
25+
}
26+
27+
return {0,0};
28+
}
29+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Minimum Number of Operations to Move All Balls to Each Box
2+
3+
**Question ID**: 1769
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 148 ms
7+
**Memory**: 14.5 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box
12+
class Solution {
13+
public:
14+
vector<int> minOperations(string boxes)
15+
{
16+
set<int> s;
17+
vector<int> ans;
18+
for (int i = 0; i < boxes.size(); i++)
19+
{
20+
if (boxes[i] == '1')
21+
{
22+
23+
s.insert(i);
24+
}
25+
}
26+
27+
for (int i = 0; i < boxes.size(); i++)
28+
{
29+
int sum = 0;
30+
31+
for (auto it : s)
32+
{
33+
if (it == i)
34+
continue;
35+
36+
sum += (abs(i-it));
37+
}
38+
39+
ans.push_back(sum);
40+
}
41+
42+
return ans;
43+
}
44+
};
45+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1499380439,
3+
"question_id": 1769,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1738761440,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "148 ms",
10+
"url": "https://leetcode.com/submissions/detail/1499380439/",
11+
"is_pending": "Not Pending",
12+
"title": "Minimum Number of Operations to Move All Balls to Each Box",
13+
"memory": "14.5 MB",
14+
"title_slug": "minimum-number-of-operations-to-move-all-balls-to-each-box",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box
5+
class Solution {
6+
public:
7+
vector<int> minOperations(string boxes)
8+
{
9+
set<int> s;
10+
vector<int> ans;
11+
for (int i = 0; i < boxes.size(); i++)
12+
{
13+
if (boxes[i] == '1')
14+
{
15+
16+
s.insert(i);
17+
}
18+
}
19+
20+
for (int i = 0; i < boxes.size(); i++)
21+
{
22+
int sum = 0;
23+
24+
for (auto it : s)
25+
{
26+
if (it == i)
27+
continue;
28+
29+
sum += (abs(i-it));
30+
}
31+
32+
ans.push_back(sum);
33+
}
34+
35+
return ans;
36+
}
37+
};
38+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Minimum Size Subarray Sum
2+
3+
**Question ID**: 209
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 32.29 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/minimum-size-subarray-sum
12+
class Solution
13+
{
14+
public:
15+
bool check(int num, vector<int> &nums, int target)
16+
{
17+
for (int i = 0; i + num - 1 < nums.size(); i++)
18+
{
19+
int sum = nums[i + num - 1] - (i > 0 ? nums[i - 1] : 0);
20+
if (sum >= target)
21+
return true;
22+
}
23+
24+
return false;
25+
}
26+
int minSubArrayLen(int target, vector<int> &nums)
27+
{
28+
for (int i = 1; i < nums.size(); i++)
29+
nums[i] += nums[i - 1];
30+
31+
int low = 1, high = nums.size(), ans = 0, mid;
32+
33+
while (low <= high)
34+
{
35+
mid = low + (high - low) / 2;
36+
if (check(mid, nums, target))
37+
{
38+
ans = mid;
39+
high = mid - 1;
40+
}
41+
else
42+
low = mid + 1;
43+
}
44+
45+
return ans;
46+
}
47+
};
48+
```

0 commit comments

Comments
 (0)