-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'SnowScriptWinterOfCode:main' into Q2--Day-18
- Loading branch information
Showing
40 changed files
with
930 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
``` | ||
class Solution { | ||
public: | ||
string longestPalindrome(string s) { | ||
if (s == string(s.rbegin(), s.rend())) { | ||
return s; | ||
} | ||
string left = longestPalindrome(s.substr(1)); | ||
string right = longestPalindrome(s.substr(0, s.size() - 1)); | ||
if (left.length() > right.length()) { | ||
return left; | ||
} else { | ||
return right; | ||
} | ||
} | ||
}; | ||
``` |
7 changes: 6 additions & 1 deletion
7
Day 13/Q2: Longest Palindromic Substring → ...Longest Palindromic Substring/question.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
Given a string s, return the longest palindromic substring in s. | ||
|
||
``` | ||
Example 1: | ||
Input: s = "babad" | ||
Output: "bab" | ||
Example 2: | ||
Input: s = "cbbd" | ||
Output: "bb" | ||
|
||
``` | ||
|
||
``` | ||
Constraints: | ||
1 <= s.length <= 1000 | ||
s consist of only digits and English letters. | ||
``` |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
``` | ||
class Solution { | ||
public: | ||
string longestPalindrome(string s) { | ||
if (s == string(s.rbegin(), s.rend())) { | ||
return s; | ||
} | ||
string left = longestPalindrome(s.substr(1)); | ||
string right = longestPalindrome(s.substr(0, s.size() - 1)); | ||
if (left.length() > right.length()) { | ||
return left; | ||
} else { | ||
return right; | ||
} | ||
} | ||
}; | ||
``` |
33 changes: 0 additions & 33 deletions
33
Day 18/q1: Maximum Difference Between Node and Ancestor/question.md
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
Day-15/q1-Lowest Common Ancestor of a Binary Tree/Akansha--C.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { | ||
if (!root || root == p || root == q) return root; | ||
TreeNode* left = lowestCommonAncestor(root->left, p, q); | ||
TreeNode* right = lowestCommonAncestor(root->right, p, q); | ||
return !left ? right : !right ? left : root; | ||
} | ||
}; |
28 changes: 28 additions & 0 deletions
28
Day-17/Q1:Determine if two strings are close/namita0210_java.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class namita0210_java { | ||
public boolean closeStrings(String word1, String word2) { | ||
if (word1.length() != word2.length()) { | ||
return false; | ||
} | ||
|
||
Map<Character, Integer> count1 = new HashMap<>(); | ||
Map<Character, Integer> count2 = new HashMap<>(); | ||
|
||
// Count occurrences of characters in word1 | ||
for (char ch : word1.toCharArray()) { | ||
count1.put(ch, count1.getOrDefault(ch, 0) + 1); | ||
} | ||
|
||
// Count occurrences of characters in word2 | ||
for (char ch : word2.toCharArray()) { | ||
count2.put(ch, count2.getOrDefault(ch, 0) + 1); | ||
} | ||
|
||
// Check if the character counts are the same | ||
return count1.keySet().equals(count2.keySet()) && count1.values().equals(count2.values()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
bool canJump(int* nums, int numsSize){ | ||
int zero=0; | ||
int len=1; | ||
int mark=1; | ||
for(int i=numsSize-1;i>=0;i--){ | ||
if(nums[i]==0){ | ||
if(i==numsSize-1) mark=0; | ||
zero++; | ||
len++; | ||
} | ||
else if(zero>0){ | ||
if(mark==0 &&nums[i]>=len-1){ | ||
zero=0; | ||
len=1; | ||
mark=1; | ||
} | ||
else if(nums[i]>=len){ | ||
zero=0; | ||
len=1; | ||
} | ||
else{ | ||
len++; | ||
} | ||
} | ||
|
||
|
||
} | ||
if(numsSize==1) return true; | ||
else if(zero>0 ) return false; | ||
|
||
else return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def can_jump(nums): | ||
n = len(nums) | ||
max_reach = 0 | ||
|
||
for i in range(n): | ||
# If the current index is beyond the furthest position reachable, return false | ||
if i > max_reach: | ||
return False | ||
|
||
# Update the furthest position reachable from the current index | ||
max_reach = max(max_reach, i + nums[i]) | ||
|
||
# If the furthest position is greater than or equal to the last index, return true | ||
if max_reach >= n - 1: | ||
return True | ||
|
||
return False | ||
|
||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Day-18/q1: Maximum Difference Between Node and Ancestor/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
``` | ||
class Solution { | ||
public: | ||
void inorder(TreeNode* root, int mini, int maxi, int &diff) { | ||
if(root==NULL) | ||
return; | ||
int tmp = root->val; | ||
diff = max(diff, max(abs(mini-tmp), abs(maxi-tmp))); | ||
if(root->val < mini) { | ||
inorder(root->left, tmp, maxi, diff); | ||
inorder(root->right, tmp, maxi, diff); | ||
} | ||
else if(root->val > maxi) { | ||
inorder(root->left, mini, tmp, diff); | ||
inorder(root->right, mini, tmp, diff); | ||
} | ||
else { | ||
inorder(root->left, mini, maxi, diff); | ||
inorder(root->right, mini, maxi, diff); | ||
} | ||
} | ||
int maxAncestorDiff(TreeNode* root) { | ||
int mini = root->val; | ||
int maxi = mini; | ||
int diff = 0; | ||
inorder(root,mini, maxi, diff); | ||
return diff; | ||
} | ||
}; | ||
``` |
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
Day-18/q2: Search in Rotated Sorted Array/Karnankita04--c.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
``` | ||
class Solution { | ||
public: | ||
int search(vector<int>& nums, int target) { | ||
int low=0, high=nums.size()-1,mid = low+(high-low)/2 ; | ||
while(low<=high) | ||
{ | ||
if(nums[mid] == target) | ||
return mid ; | ||
else if(nums[low]<= nums[mid]) // left is sorted | ||
{ | ||
if(target>=nums[low] && target<=nums[mid]) // target lies b/w left half/sorted half | ||
high = mid-1 ; | ||
else // target lies b/w right half | ||
low = mid+1 ; | ||
} | ||
else // right half is sorted | ||
{ | ||
if(target>=nums[mid] && target<=nums[high]) // target lies b/w right half | ||
low = mid+1 ; | ||
else // target lies b/w left half | ||
high = mid-1 ; | ||
} | ||
mid = low+(high-low)/2 ; | ||
} | ||
return -1 ; | ||
} | ||
}; | ||
``` |
44 changes: 44 additions & 0 deletions
44
Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
|
||
int search(const vector<int>& nums, int target) { | ||
|
||
if (nums.empty()) { | ||
return -1; | ||
} | ||
|
||
int left = 0, right = nums.size() - 1; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
|
||
if (nums[mid] > nums[right]) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
|
||
int pivot = left; | ||
|
||
left = 0; | ||
right = nums.size() - 1; | ||
|
||
while (left <= right) { | ||
int mid = (left + right) / 2; | ||
int midVal = nums[(mid + pivot) % nums.size()]; | ||
|
||
if (midVal == target) { | ||
return (mid + pivot) % nums.size(); | ||
} else if (midVal < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
}; |
File renamed without changes.
Oops, something went wrong.