Skip to content

Commit e7548cd

Browse files
Merge branch 'main' into main
2 parents a199d05 + f256b14 commit e7548cd

File tree

1,095 files changed

+37414
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,095 files changed

+37414
-314
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int longestStrChain(String[] words) {
3+
Map<String,Integer> map = new HashMap<>();
4+
Arrays.sort(words, (a,b)->a.length() - b.length());
5+
int res = 0;
6+
for(String word : words)
7+
{
8+
int best = 0;
9+
for(int i=0;i<word.length();i++)
10+
{
11+
String prev = word.substring(0,i) + word.substring(i+1);
12+
best = Math.max(best,map.getOrDefault(prev,0)+1);
13+
}
14+
map.put(word,best);
15+
res = Math.max(res,best);
16+
}
17+
return res;
18+
}
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<h2><a href="https://leetcode.com/problems/longest-string-chain/">1048. Longest String Chain</a></h2><h3>Medium</h3><hr><div><p>You are given an array of <code>words</code> where each word consists of lowercase English letters.</p>
2+
3+
<p><code>word<sub>A</sub></code> is a <strong>predecessor</strong> of <code>word<sub>B</sub></code> if and only if we can insert <strong>exactly one</strong> letter anywhere in <code>word<sub>A</sub></code> <strong>without changing the order of the other characters</strong> to make it equal to <code>word<sub>B</sub></code>.</p>
4+
5+
<ul>
6+
<li>For example, <code>"abc"</code> is a <strong>predecessor</strong> of <code>"ab<u>a</u>c"</code>, while <code>"cba"</code> is not a <strong>predecessor</strong> of <code>"bcad"</code>.</li>
7+
</ul>
8+
9+
<p>A <strong>word chain</strong><em> </em>is a sequence of words <code>[word<sub>1</sub>, word<sub>2</sub>, ..., word<sub>k</sub>]</code> with <code>k &gt;= 1</code>, where <code>word<sub>1</sub></code> is a <strong>predecessor</strong> of <code>word<sub>2</sub></code>, <code>word<sub>2</sub></code> is a <strong>predecessor</strong> of <code>word<sub>3</sub></code>, and so on. A single word is trivially a <strong>word chain</strong> with <code>k == 1</code>.</p>
10+
11+
<p>Return <em>the <strong>length</strong> of the <strong>longest possible word chain</strong> with words chosen from the given list of </em><code>words</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre><strong>Input:</strong> words = ["a","b","ba","bca","bda","bdca"]
17+
<strong>Output:</strong> 4
18+
<strong>Explanation</strong>: One of the longest word chains is ["a","<u>b</u>a","b<u>d</u>a","bd<u>c</u>a"].
19+
</pre>
20+
21+
<p><strong>Example 2:</strong></p>
22+
23+
<pre><strong>Input:</strong> words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
24+
<strong>Output:</strong> 5
25+
<strong>Explanation:</strong> All the words can be put in a word chain ["xb", "xb<u>c</u>", "<u>c</u>xbc", "<u>p</u>cxbc", "pcxbc<u>f</u>"].
26+
</pre>
27+
28+
<p><strong>Example 3:</strong></p>
29+
30+
<pre><strong>Input:</strong> words = ["abcd","dbqca"]
31+
<strong>Output:</strong> 1
32+
<strong>Explanation:</strong> The trivial word chain ["abcd"] is one of the longest word chains.
33+
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
41+
<li><code>1 &lt;= words[i].length &lt;= 16</code></li>
42+
<li><code>words[i]</code> only consists of lowercase English letters.</li>
43+
</ul>
44+
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
void flatten(TreeNode* root) {
4+
if(!root) return;
5+
while(root)
6+
{
7+
TreeNode* temp = root->right;
8+
root->right = root->left;
9+
root->left = NULL;
10+
TreeNode* node = root;
11+
while(node->right)
12+
{
13+
node = node->right;
14+
}
15+
node->right = temp;
16+
root = root->right;
17+
}
18+
}
19+
};

121 Leetcode/121 leetcode.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
maxProfit = 0
4+
currentMax = 0
5+
6+
for i in reversed(prices):
7+
currentMax = max(currentMax, i)
8+
profit = currentMax - i
9+
maxProfit = max(profit, maxProfit)
10+
return maxProfit

121 Leetcode/word-ladder-ii.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// BFS gives TLE if we store path while traversing because whenever we find a better visit time for a word, we have to clear/make a new path vector everytime.
2+
// The idea is to first use BFS to search from beginWord to endWord and generate the word-to-children mapping at the same time.
3+
// Then, use DFS (backtracking) to generate the transformation sequences according to the mapping.
4+
// The reverse DFS allows us to only make the shortest paths, never having to clear a whole sequence when we encounter better result in BFS
5+
// No string operations are done, by dealing with indices instead.
6+
7+
8+
9+
class Solution {
10+
public:
11+
bool able(string s,string t){
12+
int c=0;
13+
for(int i=0;i<s.length();i++)
14+
c+=(s[i]!=t[i]);
15+
return c==1;
16+
}
17+
void bfs(vector<vector<int>> &g,vector<int> parent[],int n,int start,int end){
18+
vector <int> dist(n,1005);
19+
queue <int> q;
20+
q.push(start);
21+
parent[start]={-1};
22+
dist[start]=0;
23+
while(!q.empty()){
24+
int x=q.front();
25+
q.pop();
26+
for(int u:g[x]){
27+
if(dist[u]>dist[x]+1){
28+
dist[u]=dist[x]+1;
29+
q.push(u);
30+
parent[u].clear();
31+
parent[u].push_back(x);
32+
}
33+
else if(dist[u]==dist[x]+1)
34+
parent[u].push_back(x);
35+
}
36+
}
37+
}
38+
void shortestPaths(vector<vector<int>> &Paths, vector<int> &path, vector<int> parent[],int node){
39+
if(node==-1){
40+
// as parent of start was -1, we've completed the backtrack
41+
Paths.push_back(path);
42+
return ;
43+
}
44+
for(auto u:parent[node]){
45+
path.push_back(u);
46+
shortestPaths(Paths,path,parent,u);
47+
path.pop_back();
48+
}
49+
}
50+
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
51+
// start and end are indices of beginWord and endWord
52+
int n=wordList.size(),start=-1,end=-1;
53+
vector<vector<string>> ANS;
54+
for(int i=0;i<n;i++){
55+
if(wordList[i]==beginWord)
56+
start=i;
57+
if(wordList[i]==endWord)
58+
end=i;
59+
}
60+
61+
// if endWord doesn't exist, return empty list
62+
if(end==-1)
63+
return ANS;
64+
65+
// if beginWord doesn't exist, add it in start of WordList
66+
if(start==-1){
67+
wordList.emplace(wordList.begin(),beginWord);
68+
start=0;
69+
end++;
70+
n++;
71+
}
72+
// for each word, we're making adjency list of neighbour words (words that can be made with one letter change)
73+
// Paths will store all the shortest paths (formed later by backtracking)
74+
vector<vector<int>> g(n,vector<int>()),Paths;
75+
76+
// storing possible parents for each word (to backtrack later), path is the current sequence (while backtracking)
77+
vector<int> parent[n],path;
78+
79+
// creating adjency list for each pair of words in the wordList (including beginword)
80+
for(int i=0;i<n-1;i++)
81+
for(int j=i+1;j<n;j++)
82+
if(able(wordList[i],wordList[j])){
83+
g[i].push_back(j);
84+
g[j].push_back(i);
85+
}
86+
87+
bfs(g,parent,n,start,end);
88+
89+
// backtracking to make shortestpaths
90+
shortestPaths(Paths,path,parent,end);
91+
for(auto u:Paths){
92+
vector <string> now;
93+
for(int i=0;i<u.size()-1;i++)
94+
now.push_back(wordList[u[i]]);
95+
reverse(now.begin(),now.end());
96+
now.push_back(wordList[end]);
97+
ANS.push_back(now);
98+
}
99+
return ANS;
100+
}
101+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
vector<int> runningSum(vector<int>& nums) {
4+
for(int i=1;i<nums.size();i++)
5+
nums[i]+= nums[i-1];
6+
return nums;
7+
}
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int[] runningSum(int[] nums) {
3+
for(int i=1;i<nums.length;i++)
4+
nums[i]+=nums[i-1];
5+
return nums;
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<h2><a href="https://leetcode.com/problems/running-sum-of-1d-array/">1480. Running Sum of 1d Array</a></h2><h3>Easy</h3><hr><div><p>Given an array <code>nums</code>. We define a running sum of an array as&nbsp;<code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
2+
3+
<p>Return the running sum of <code>nums</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong>Example 1:</strong></p>
7+
8+
<pre><strong>Input:</strong> nums = [1,2,3,4]
9+
<strong>Output:</strong> [1,3,6,10]
10+
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
11+
12+
<p><strong>Example 2:</strong></p>
13+
14+
<pre><strong>Input:</strong> nums = [1,1,1,1,1]
15+
<strong>Output:</strong> [1,2,3,4,5]
16+
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
17+
18+
<p><strong>Example 3:</strong></p>
19+
20+
<pre><strong>Input:</strong> nums = [3,1,2,10,1]
21+
<strong>Output:</strong> [3,4,6,16,17]
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
29+
<li><code>-10^6&nbsp;&lt;= nums[i] &lt;=&nbsp;10^6</code></li>
30+
</ul></div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int furthestBuilding(int[] heights, int bricks, int ladders) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>();
4+
for(int i=0;i<heights.length-1;i++)
5+
{
6+
int distance = heights[i+1] - heights[i];
7+
if(distance>0)
8+
pq.add(distance);
9+
if(pq.size()>ladders)
10+
bricks -= pq.poll();
11+
if(bricks<0)
12+
return i;
13+
}
14+
return heights.length-1;
15+
}
16+
}

0 commit comments

Comments
 (0)