Skip to content

Commit eee3338

Browse files
added 2 more questions (codedecks-in#18)
* added 2 more questions * Update README.md Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 774174f commit eee3338

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

Diff for: C++/Degree-of-an-Array.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
*
4+
* Runtime : 38 ms
5+
* Memory : 53.3 MB
6+
*
7+
*
8+
*/
9+
10+
class Solution {
11+
public int findShortestSubArray(int[] nums) {
12+
Map<Integer, Integer> left = new HashMap(), right = new HashMap(), count = new HashMap();
13+
14+
//find the leftmost and rightmost index of each characters
15+
for(int i= 0;i<nums.length; i++)
16+
{
17+
int x =nums[i];
18+
if(left.get(x) == null)
19+
left.put(x, i);
20+
21+
right.put(x, i);
22+
count.put(x, count.getOrDefault(x, 0) + 1);
23+
}
24+
25+
int ans = nums.length;
26+
int degree = Collections.max(count.values());
27+
28+
//find min distance between subarrays with same degree
29+
for(int x: count.keySet())
30+
{
31+
if(count.get(x) == degree)
32+
{
33+
ans = Math.min(ans , right.get(x) - left.get(x) + 1);
34+
}
35+
}
36+
return ans;
37+
}
38+
}

Diff for: C++/Partition-Equal-Subset-Sum.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
*
3+
*
4+
* Runtime : 80ms
5+
* Memory : 9.6 MB
6+
* Logic : 0/1 knapsack concept used to calculate subsetSum
7+
*/
8+
class Solution {
9+
public:
10+
11+
// using 0/1 knapsack top down approach
12+
bool subsetSum(vector<int>& nums, int sum)
13+
{
14+
// size = nums+1, sum+1
15+
bool t[nums.size()+1][sum+1];
16+
17+
for(int i= 0;i<=nums.size();i++)
18+
{
19+
t[i][0] = true;
20+
}
21+
22+
for(int i= 0;i<=sum;i++)
23+
{
24+
t[0][i] = false;
25+
}
26+
27+
t[0][0] = true;
28+
29+
for(int i= 1;i<nums.size();i++)
30+
{
31+
for(int j=1;j<=sum;j++)
32+
{
33+
if(nums[i-1] <= j)
34+
{
35+
//get max by including number in the subset || by not including in subset sum
36+
t[i][j] = t[i-1][j - nums[i-1]] || t[i-1][j];
37+
}
38+
else{
39+
t[i][j] = t[i-1][j];
40+
}
41+
}
42+
}
43+
return t[nums.size()-1][sum];
44+
}
45+
46+
bool canPartition(vector<int>& nums) {
47+
48+
int n = nums.size();
49+
50+
int sum = 0;
51+
for(int i = 0;i<n;i++)
52+
{
53+
sum+=nums[i];
54+
}
55+
56+
if(sum%2 != 0 )
57+
return false;
58+
59+
return subsetSum(nums, sum/2);
60+
61+
}
62+
};

Diff for: README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8383
| 56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals) | [Python](./Python/56_MergeIntervals.py) | _O(nlogn)_ | _O(n)_ | Medium | Intervals | |
8484
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [Java](./Java/Degree-of-an-Array) | _O(n)_ | _O(n)_ | Easy | Array | |
8585

86-
8786
<br/>
8887
<div align="right">
8988
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -208,6 +207,20 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
208207
<br/>
209208

210209

210+
## Dynamic Programming
211+
212+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
213+
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
214+
| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)| [C++](./C++/Partition-Equal-Subset-Sum.cpp)| _O(n^2)_ | _O(n^2)_ | Medium | DP | |
215+
216+
<br/>
217+
<div align="right">
218+
<b><a href="#algorithms">⬆️ Back to Top</a></b>
219+
</div>
220+
<br/>
221+
222+
223+
211224
## Binary Search
212225

213226
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |

0 commit comments

Comments
 (0)