Skip to content

Commit 46c7fb1

Browse files
Merge pull request #11 from InnocentDaksh63/patch-3
Patch 3
2 parents 370d1d1 + e1bc425 commit 46c7fb1

24 files changed

+1227
-0
lines changed

112. Path sum

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean hasPathSum(TreeNode root, int targetSum) {
3+
return solve(root,targetSum,0);
4+
}
5+
public boolean solve(TreeNode root, int targetSum,int currSum) {
6+
if(root == null) return false;
7+
8+
//leaf node
9+
if(root.left == null && root.right == null) {
10+
currSum += root.val;
11+
if(targetSum == currSum) {
12+
return true;
13+
}
14+
else{
15+
return false;
16+
}
17+
}
18+
19+
20+
boolean left = solve(root.left,targetSum,currSum + root.val);
21+
boolean right = solve(root.right,targetSum,currSum + root.val);
22+
23+
return left || right;
24+
25+
}
26+
}

16. 3Sum Closest

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public int threeSumClosest(int[] nums, int target) {
3+
int n = nums.length;
4+
int res = 0;
5+
int m = Integer.MAX_VALUE;
6+
Arrays.sort(nums);
7+
for (int i = 0; i < n - 2; i++){
8+
if (i > 0 && nums[i-1] == nums[i]){
9+
continue;
10+
}
11+
if (nums[i] * 3 >= target){
12+
int s = nums[i] + nums[i+1] + nums[i+2];
13+
if (s - target < m){
14+
return s;
15+
}
16+
break;
17+
}
18+
int t = nums[i] + nums[n-1] + nums[n-2];
19+
if (t == target){
20+
return t;
21+
}
22+
if (t < target){
23+
if (t - target < m){
24+
m = target - t;
25+
res = t;
26+
}
27+
continue;
28+
}
29+
int a = i + 1;
30+
int b = n - 1;
31+
while (a < b){
32+
t = nums[i] + nums[a] + nums[b];
33+
if (t == target){
34+
return t;
35+
}
36+
if (Math.abs(t - target) < m){
37+
res = t;
38+
m = Math.abs(t - target);
39+
}
40+
if (t > target){
41+
b--;
42+
} else {
43+
a++;
44+
}
45+
}
46+
}
47+
return res;
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 1823. Find the Winner of the Circular Game
2+
// Josephus Problem
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
void solve(vector<int> &arr, int index, int k){
7+
if(arr.size() == 1)
8+
return;
9+
10+
index = (index + k) % arr.size();
11+
12+
arr.erase(arr.begin() + index);
13+
index--;
14+
15+
solve(arr, index, k);
16+
}
17+
18+
int findTheWinner(int n, int k) {
19+
vector<int> arr;
20+
for(int i = 1; i <= n; i++){
21+
arr.push_back(i);
22+
}
23+
int index = -1;
24+
solve(arr, index, k);
25+
return arr[0];
26+
}
27+
28+
int main(){
29+
// Input: n = 5, k = 2
30+
// Output: 3
31+
// Explanation: Here are the steps of the game:
32+
// 1) Start at friend 1.
33+
// 2) Count 2 friends clockwise, which are friends 1 and 2.
34+
// 3) Friend 2 leaves the circle. Next start is friend 3.
35+
// 4) Count 2 friends clockwise, which are friends 3 and 4.
36+
// 5) Friend 4 leaves the circle. Next start is friend 5.
37+
// 6) Count 2 friends clockwise, which are friends 5 and 1.
38+
// 7) Friend 1 leaves the circle. Next start is friend 3.
39+
// 8) Count 2 friends clockwise, which are friends 3 and 5.
40+
// 9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
41+
int n = 4, k = 2;
42+
cout << findTheWinner(n,k);
43+
return 0;
44+
}
45+
46+
// for(int i = 0; i < arr.size(); i++){
47+
// cout << arr[i] << ' ';
48+
// }
49+
// cout<<endl;

238.odd-even-linked-list.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 328. Odd Even Linked List
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
class ListNode {
6+
public:
7+
int data;
8+
ListNode* next;
9+
10+
public:
11+
ListNode(int data){
12+
this->data = data;
13+
this->next = NULL;
14+
}
15+
};
16+
17+
ListNode* oddEvenList(ListNode* head) {
18+
19+
if(head == NULL || head->next == NULL || head->next->next == NULL)
20+
return head;
21+
22+
ListNode* end = head;
23+
int count = 0;
24+
25+
while(end->next != NULL) {
26+
end = end->next;
27+
count++;
28+
}
29+
30+
int counter = (count & 1) ? (count/2 + 1) : (count/2);
31+
ListNode* temp = head;
32+
33+
while(counter--) {
34+
35+
end->next = temp->next;
36+
temp->next = temp->next->next;
37+
end->next->next = NULL;
38+
temp = temp->next;
39+
end = end->next;
40+
}
41+
42+
return head;
43+
}

37.Sudoku Solver.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution
2+
{
3+
public:
4+
bool check(int i, int j, int k, vector<vector<char>> &board)
5+
{
6+
for (int p = 0; p < 9; p++)
7+
{
8+
if (board[i][p] - '0' == k)
9+
return false;
10+
if (board[p][j] - '0' == k)
11+
return false;
12+
}
13+
int x = i / 3 * 3, y = j / 3 * 3;
14+
for (int p = x; p < x + 3; p++)
15+
{
16+
for (int r = y; r < y + 3; r++)
17+
{
18+
if (board[p][r] - '0' == k)
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
bool solve(vector<vector<char>> &board)
25+
{
26+
for (int i = 0; i < 9; i++)
27+
{
28+
for (int j = 0; j < 9; j++)
29+
{
30+
if (board[i][j] == '.')
31+
{
32+
for (int k = 1; k <= 9; k++)
33+
{
34+
if (check(i, j, k, board))
35+
{
36+
board[i][j] = '0' + k;
37+
if (solve(board))
38+
return true;
39+
board[i][j] = '.';
40+
}
41+
}
42+
return false;
43+
}
44+
}
45+
}
46+
return true;
47+
}
48+
void solveSudoku(vector<vector<char>> &board)
49+
{
50+
solve(board);
51+
}
52+
};

392.is-subsequence.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 392. Is Subsequence
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
void solve(string &t, vector<string> &ans, int index, string &temp) {
6+
7+
if(index == t.size()) {
8+
ans.push_back(temp);
9+
return;
10+
}
11+
12+
// exclude
13+
solve(t, ans, index+1, temp);
14+
15+
// inclusion
16+
temp.push_back(t[index]);
17+
solve(t, ans, index+1, temp);
18+
temp.pop_back();
19+
}
20+
21+
bool isSubsequence(string s, string t) {
22+
23+
vector<string> ans;
24+
string temp = "";
25+
26+
solve(t, ans, 0, temp);
27+
28+
for(int i=0; i<ans.size(); i++) {
29+
if(ans[i] == s) {
30+
return true;
31+
}
32+
}
33+
34+
return false;
35+
}
36+
37+
bool solve2(string s, string t, int i, int j) {
38+
39+
if(i >= s.size())
40+
return true;
41+
42+
if(i < s.size() && j >= t.size())
43+
return false;
44+
45+
if(s[i] == t[j])
46+
return solve(s, t, i+1, j+1);
47+
48+
else
49+
return solve(s, t, i, j+1);
50+
}
51+
52+
bool isSubsequence1(string s, string t) {
53+
return solve2(s, t, 0, 0);
54+
}
55+
int main() {
56+
57+
string s = "abc";
58+
string t = "ahbgdc";
59+
60+
cout << isSubsequence(s, t);
61+
62+
return 0;
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
typedef long long ll;
3+
4+
public:
5+
bool isPossible(vector<int> &nums)
6+
{
7+
8+
map<int, int> mp, mp2;
9+
for (ll i = 0; i < nums.size(); i++)
10+
{
11+
mp[nums[i]] = mp[nums[i]] + 1;
12+
}
13+
14+
for (ll i = 0; i < nums.size(); i++)
15+
{
16+
// cout << nums[i] << " ";
17+
if (mp2[nums[i]] && mp[nums[i]])
18+
{
19+
mp[nums[i]] = mp[nums[i]] - 1;
20+
mp2[nums[i]] = mp2[nums[i]] - 1;
21+
mp2[nums[i] + 1] = mp2[nums[i] + 1] + 1;
22+
}
23+
else
24+
{
25+
if (mp[nums[i]])
26+
{
27+
mp[nums[i]] = mp[nums[i]] - 1;
28+
if (mp[nums[i] + 1])
29+
mp[nums[i] + 1] = mp[nums[i] + 1] - 1;
30+
else
31+
return false;
32+
if (mp[nums[i] + 2])
33+
mp[nums[i] + 2] = mp[nums[i] + 2] - 1;
34+
else
35+
return false;
36+
mp2[nums[i] + 3] = mp2[nums[i] + 3] + 1;
37+
}
38+
}
39+
// for (ll i = 0; i < nums.size(); i++)
40+
// {
41+
// cout << mp[nums[i]] << " ";
42+
// // if(mp[nums[i]])return false;
43+
// }
44+
}
45+
for (ll i = 0; i < nums.size(); i++)
46+
{
47+
// cout<<mp[nums[i]]<<" ";
48+
if (mp[nums[i]])
49+
return false;
50+
}
51+
return true;
52+
}
53+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<h2><a href="https://leetcode.com/problems/split-array-into-consecutive-subsequences/">659. Split Array into Consecutive Subsequences</a></h2><h3>Medium</h3><hr><div><p>You are given an integer array <code>nums</code> that is <strong>sorted in non-decreasing order</strong>.</p>
2+
3+
<p>Determine if it is possible to split <code>nums</code> into <strong>one or more subsequences</strong> such that <strong>both</strong> of the following conditions are true:</p>
4+
5+
<ul>
6+
<li>Each subsequence is a <strong>consecutive increasing sequence</strong> (i.e. each integer is <strong>exactly one</strong> more than the previous integer).</li>
7+
<li>All subsequences have a length of <code>3</code><strong> or more</strong>.</li>
8+
</ul>
9+
10+
<p>Return <code>true</code><em> if you can split </em><code>nums</code><em> according to the above conditions, or </em><code>false</code><em> otherwise</em>.</p>
11+
12+
<p>A <strong>subsequence</strong> of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., <code>[1,3,5]</code> is a subsequence of <code>[<u>1</u>,2,<u>3</u>,4,<u>5</u>]</code> while <code>[1,3,2]</code> is not).</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong>Example 1:</strong></p>
16+
17+
<pre><strong>Input:</strong> nums = [1,2,3,3,4,5]
18+
<strong>Output:</strong> true
19+
<strong>Explanation:</strong> nums can be split into the following subsequences:
20+
[<strong><u>1</u></strong>,<strong><u>2</u></strong>,<strong><u>3</u></strong>,3,4,5] --&gt; 1, 2, 3
21+
[1,2,3,<strong><u>3</u></strong>,<strong><u>4</u></strong>,<strong><u>5</u></strong>] --&gt; 3, 4, 5
22+
</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre><strong>Input:</strong> nums = [1,2,3,3,4,4,5,5]
27+
<strong>Output:</strong> true
28+
<strong>Explanation:</strong> nums can be split into the following subsequences:
29+
[<strong><u>1</u></strong>,<strong><u>2</u></strong>,<strong><u>3</u></strong>,3,<strong><u>4</u></strong>,4,<strong><u>5</u></strong>,5] --&gt; 1, 2, 3, 4, 5
30+
[1,2,3,<strong><u>3</u></strong>,4,<strong><u>4</u></strong>,5,<strong><u>5</u></strong>] --&gt; 3, 4, 5
31+
</pre>
32+
33+
<p><strong>Example 3:</strong></p>
34+
35+
<pre><strong>Input:</strong> nums = [1,2,3,4,4,5]
36+
<strong>Output:</strong> false
37+
<strong>Explanation:</strong> It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
45+
<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
46+
<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
47+
</ul>
48+
</div>

0 commit comments

Comments
 (0)