Skip to content

Commit

Permalink
Merge branch 'SnowScriptWinterOfCode:main' into Q2--Day-18
Browse files Browse the repository at this point in the history
  • Loading branch information
namita0210 authored Jan 23, 2024
2 parents b08c4aa + f1ba4a2 commit 5b36ab4
Show file tree
Hide file tree
Showing 40 changed files with 930 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
```
class Solution {
public:
string longestPalindrome(string s) {
Expand Down Expand Up @@ -31,4 +32,5 @@ class Solution {
return ans;
}
};
};
```
19 changes: 19 additions & 0 deletions Day 13/q2: Longest Palindromic Substring/kalpana--cpp.md
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;
}
}
};
```
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.
33 changes: 0 additions & 33 deletions Day 13/q3: Permutation in String/kalpana--c++.cpp

This file was deleted.

19 changes: 19 additions & 0 deletions Day 13/q3: Permutation in String/kalpana--cpp.md
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;
}
}
};
```

This file was deleted.

18 changes: 18 additions & 0 deletions Day-15/q1-Lowest Common Ancestor of a Binary Tree/Akansha--C.cpp
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 Day-17/Q1:Determine if two strings are close/namita0210_java.java
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());
}


}
32 changes: 32 additions & 0 deletions Day-17/q3: Jump Game/kalpana--c.md
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;
}
19 changes: 19 additions & 0 deletions Day-17/q3: Jump Game/namita0210_python.py
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


Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ Given the root of a binary tree, find the maximum value v for which there exist

A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.



Example 1:

Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
tmp-tree

![tmp-tree](https://github.com/SnowScriptWinterOfCode/LeetCode_Q/assets/97434896/108fd5df-8e90-4f6c-bbfc-f2e1e08b4503)

Explanation: We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
Expand All @@ -19,11 +22,11 @@ Example 2:

Input: root = [1,null,2,null,0,3]
Output: 3
tmp-tree-1

Constraints:
![tmp-tree-1](https://github.com/SnowScriptWinterOfCode/LeetCode_Q/assets/97434896/bedf5d7f-cf61-40ee-bdcf-3a4a066abbc0)

The number of nodes in the tree is in the range [2, 5000].
0 <= Node.val <= 105
Problem link: https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/description/?envType=daily-question&envId=2024-01-11
Constraints:

The number of nodes in the tree is in the range [2, 5000].
0 <= Node.val <= 105
Problem link: https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/description/?envType=daily-question&envId=2024-01-11
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;
}
};
```
32 changes: 32 additions & 0 deletions Day-18/q2: Search in Rotated Sorted Array/Karnankita04--c.md
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 Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp
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;
}
};
Loading

0 comments on commit 5b36ab4

Please sign in to comment.