Skip to content

Commit 2619e67

Browse files
Merge pull request #588 from Abhishek-Mallick/leetcode4
Leetcode4
2 parents 84c50fe + e980120 commit 2619e67

File tree

12 files changed

+240
-0
lines changed

12 files changed

+240
-0
lines changed
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+
}

1048-longest-string-chain/README.md

+44
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>
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+
};
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+
}

1480-running-sum-of-1d-array/NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+30
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>
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<h2><a href="https://leetcode.com/problems/furthest-building-you-can-reach/">1642. Furthest Building You Can Reach</a></h2><h3>Medium</h3><hr><div><p>You are given an integer array <code>heights</code> representing the heights of buildings, some <code>bricks</code>, and some <code>ladders</code>.</p>
2+
3+
<p>You start your journey from building <code>0</code> and move to the next building by possibly using bricks or ladders.</p>
4+
5+
<p>While moving from building <code>i</code> to building <code>i+1</code> (<strong>0-indexed</strong>),</p>
6+
7+
<ul>
8+
<li>If the current building's height is <strong>greater than or equal</strong> to the next building's height, you do <strong>not</strong> need a ladder or bricks.</li>
9+
<li>If the current building's height is <b>less than</b> the next building's height, you can either use <strong>one ladder</strong> or <code>(h[i+1] - h[i])</code> <strong>bricks</strong>.</li>
10+
</ul>
11+
12+
<p><em>Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.</em></p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong>Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/27/q4.gif" style="width: 562px; height: 561px;">
17+
<pre><strong>Input:</strong> heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
18+
<strong>Output:</strong> 4
19+
<strong>Explanation:</strong> Starting at building 0, you can follow these steps:
20+
- Go to building 1 without using ladders nor bricks since 4 &gt;= 2.
21+
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 &lt; 7.
22+
- Go to building 3 without using ladders nor bricks since 7 &gt;= 6.
23+
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 &lt; 9.
24+
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre><strong>Input:</strong> heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
30+
<strong>Output:</strong> 7
31+
</pre>
32+
33+
<p><strong>Example 3:</strong></p>
34+
35+
<pre><strong>Input:</strong> heights = [14,3,19,3], bricks = 17, ladders = 0
36+
<strong>Output:</strong> 3
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= heights.length &lt;= 10<sup>5</sup></code></li>
44+
<li><code>1 &lt;= heights[i] &lt;= 10<sup>6</sup></code></li>
45+
<li><code>0 &lt;= bricks &lt;= 10<sup>9</sup></code></li>
46+
<li><code>0 &lt;= ladders &lt;= heights.length</code></li>
47+
</ul>
48+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public int minCameraCover(TreeNode root) {
18+
int ans[] = solve(root);
19+
return Math.min(ans[1],ans[2]);
20+
}
21+
22+
public int[] solve(TreeNode node)
23+
{
24+
if(node == null)
25+
return new int[]{0,0,9999};
26+
int l[] = solve(node.left);
27+
int r[] = solve(node.right);
28+
29+
int ml = Math.min(l[1],l[2]);
30+
int mr = Math.min(r[1],r[2]);
31+
32+
int d0 = l[1]+r[1];
33+
int d1 = Math.min(l[2]+mr , r[2]+ml);
34+
int d2 = 1+Math.min(l[0],ml) + Math.min(r[0],mr);
35+
36+
return new int[]{d0,d1,d2};
37+
}
38+
}

968-binary-tree-cameras/NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

968-binary-tree-cameras/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<h2><a href="https://leetcode.com/problems/binary-tree-cameras/solution/">968. Binary Tree Cameras</a></h2><h3>Hard</h3><hr><div><p>You are given the <code>root</code> of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.</p>
2+
3+
<p>Return <em>the minimum number of cameras needed to monitor all nodes of the tree</em>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong>Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png" style="width: 138px; height: 163px;">
8+
<pre><strong>Input:</strong> root = [0,0,null,0,0]
9+
<strong>Output:</strong> 1
10+
<strong>Explanation:</strong> One camera is enough to monitor all nodes if placed as shown.
11+
</pre>
12+
13+
<p><strong>Example 2:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png" style="width: 139px; height: 312px;">
15+
<pre><strong>Input:</strong> root = [0,0,null,0,null,0,null,null,0]
16+
<strong>Output:</strong> 2
17+
<strong>Explanation:</strong> At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
25+
<li><code>Node.val == 0</code></li>
26+
</ul>
27+
</div>

0 commit comments

Comments
 (0)