Skip to content

Commit 721b977

Browse files
author
aryaveer
committed
added new solution to DP, Array, Linked List, String and Matrix
1 parent e82341b commit 721b977

7 files changed

+659
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Problem statement:
3+
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
4+
5+
If target is not found in the array, return [-1, -1].
6+
7+
You must write an algorithm with O(log n) runtime complexity.
8+
9+
10+
11+
Example 1:
12+
13+
Input: nums = [5,7,7,8,8,10], target = 8
14+
Output: [3,4]
15+
Example 2:
16+
17+
Input: nums = [5,7,7,8,8,10], target = 6
18+
Output: [-1,-1]
19+
Example 3:
20+
21+
Input: nums = [], target = 0
22+
Output: [-1,-1]
23+
24+
25+
Constraints:
26+
27+
0 <= nums.length <= 105
28+
-109 <= nums[i] <= 109
29+
nums is a non-decreasing array.
30+
-109 <= target <= 109
31+
32+
33+
*/
34+
#include "bits/stdc++.h"
35+
#define endl "\n"
36+
37+
using namespace std;
38+
39+
40+
vector<int> searchRange(vector<int>& nums, int target) {
41+
int start = -1;
42+
int end = -1;
43+
int low = 0, high = int(nums.size()) - 1;
44+
while (low <= high) {
45+
46+
int mid = (high + low) / 2;
47+
48+
if (nums[mid] == target) {
49+
// intially consider the mid is the starting and end position
50+
start = mid;
51+
end = mid;
52+
53+
// now we will search in left and right search for new start and end positions
54+
int tempLow = low;
55+
int tempHigh = mid - 1;
56+
57+
// find " new START " in left subarray
58+
while (tempLow <= tempHigh) {
59+
60+
int tempMid = (tempLow + tempHigh) / 2;
61+
62+
if (nums[tempMid] == target) {
63+
64+
start = tempMid;
65+
tempHigh = tempMid - 1;
66+
67+
} else {
68+
69+
tempLow = tempMid + 1;
70+
71+
}
72+
}
73+
74+
// find " new END " in right subarray
75+
tempLow = mid + 1;
76+
tempHigh = high;
77+
while (tempLow <= tempHigh) {
78+
int tempMid = (tempLow + tempHigh) / 2;
79+
if (nums[tempMid] == target) {
80+
end = tempMid;
81+
tempLow = tempMid + 1;
82+
} else {
83+
tempHigh = tempMid - 1;
84+
}
85+
}
86+
87+
// important step to follow
88+
// =========
89+
break;
90+
// =========
91+
}
92+
93+
if (nums[mid] < target) {
94+
low = mid + 1;
95+
} else {
96+
high = mid - 1;
97+
}
98+
}
99+
100+
nums.clear();
101+
102+
return {start, end};
103+
}
104+
105+
106+
// { Driver Code Starts.
107+
int main()
108+
{
109+
int t;
110+
cin >> t;
111+
while (t--)
112+
{
113+
int N;
114+
cin >> N;
115+
vector<int> nums(N);
116+
for (int i = 0; i < N; i++) {
117+
int x;
118+
cin >> x;
119+
nums.push_back(x);
120+
}
121+
int target;
122+
cin >> target;
123+
vector<int> ans = searchRange(nums, target);
124+
cout << "[";
125+
for (auto x : ans) {
126+
cout << x << ", ";
127+
}
128+
cout << "]" << endl;
129+
130+
}
131+
} // } Driver Code Ends

04. Arrays/13_Combination Sum.cpp

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Problem statement:
3+
4+
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
5+
6+
Only numbers 1 through 9 are used.
7+
Each number is used at most once.
8+
Return a list of all possible valid combinations.
9+
The list must not contain the same combination twice, and the combinations may be returned in any order.
10+
11+
12+
Example 1:
13+
14+
Input: k = 3, n = 7
15+
Output: [[1,2,4]]
16+
Explanation:
17+
1 + 2 + 4 = 7
18+
There are no other valid combinations.
19+
Example 2:
20+
21+
Input: k = 3, n = 9
22+
Output: [[1,2,6],[1,3,5],[2,3,4]]
23+
Explanation:
24+
1 + 2 + 6 = 9
25+
1 + 3 + 5 = 9
26+
2 + 3 + 4 = 9
27+
There are no other valid combinations.
28+
Example 3:
29+
30+
Input: k = 4, n = 1
31+
Output: []
32+
Explanation: There are no valid combinations.
33+
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
34+
Example 4:
35+
36+
Input: k = 3, n = 2
37+
Output: []
38+
Explanation: There are no valid combinations.
39+
Example 5:
40+
41+
Input: k = 9, n = 45
42+
Output: [[1,2,3,4,5,6,7,8,9]]
43+
Explanation:
44+
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
45+
There are no other valid combinations.
46+
47+
48+
Constraints:
49+
50+
2 <= k <= 9
51+
1 <= n <= 60
52+
53+
*/
54+
55+
56+
#include <iostream>
57+
#include <vector>
58+
59+
60+
vector<vector<int>> ans; // declared globally
61+
62+
void makesum(vector<int>& v, int tar, int ssf, vector<int> &temp, int idx, int cnt) {
63+
if (ssf > tar) {
64+
return;
65+
}
66+
if (ssf == tar) {
67+
if (cnt == 0) {
68+
ans.push_back(temp);
69+
}
70+
return;
71+
}
72+
73+
for (int i = idx; i < v.size(); i++) {
74+
temp.push_back(v[i]);
75+
makesum(v, tar, ssf + v[i], temp, i + 1, cnt - 1);
76+
temp.pop_back();
77+
}
78+
}
79+
80+
81+
vector<vector<int>> combinationSum3(int k, int n) {
82+
vector<int> temp;
83+
vector<int> candidates;
84+
for (int i = 0; i < 9; i++) {
85+
candidates.push_back(i + 1);
86+
}
87+
makesum(candidates, n, 0, temp, 0, k);
88+
return ans;
89+
}
90+
91+
int main()
92+
{
93+
std::ios::sync_with_stdio(false);
94+
int T;
95+
cin >> T;
96+
while (T--)
97+
{
98+
int n, k;
99+
cin >> n, k;
100+
101+
combinationSum3(n, k);
102+
103+
for (auto x : ans) {
104+
cout << x << " ";
105+
}
106+
107+
cout << endl;
108+
}
109+
return 0;
110+
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
3+
Problem Statement:
4+
5+
You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.
6+
7+
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.
8+
9+
Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).
10+
11+
Note: You cannot rotate an envelope.
12+
13+
14+
15+
Example 1:
16+
17+
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
18+
Output: 3
19+
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
20+
Example 2:
21+
22+
Input: envelopes = [[1,1],[1,1],[1,1]]
23+
Output: 1
24+
25+
26+
Constraints:
27+
28+
1 <= envelopes.length <= 5000
29+
envelopes[i].length == 2
30+
1 <= wi, hi <= 104
31+
32+
*/
33+
34+
#include <bits/stdc++.h>
35+
36+
using namespace std;
37+
38+
39+
static bool compare(vector<int>&a , vector<int>& b)
40+
{
41+
return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0];
42+
}
43+
44+
int maxEnvelopes(vector<vector<int>>&a) {
45+
sort(a.begin(), a.end(), compare);
46+
vector<int>dp;
47+
for (auto i : a)
48+
{
49+
auto it = lower_bound(dp.begin(), dp.end(), i[1]);
50+
if (it == dp.end())
51+
dp.push_back(i[1]);
52+
else
53+
*it = i[1];
54+
}
55+
return dp.size();
56+
}
57+
58+
int main()
59+
{
60+
std::ios::sync_with_stdio(false);
61+
int T;
62+
cin >> T;
63+
// cin.ignore(); must be there when using getline(cin, s)
64+
while (T--)
65+
{
66+
int n, m;
67+
vector<vector<int> > nums(n, vector<int>(m));
68+
69+
for (int i = 0; i < n; i++) {
70+
for (int j = 0; j < m; j++) {
71+
cin >> nums[i][j];
72+
}
73+
}
74+
75+
cout << maxEnvelopes(nums) << endl;
76+
}
77+
return 0;
78+
}

0 commit comments

Comments
 (0)