Skip to content

Commit 0487e3d

Browse files
4-LeetCodeQs
1 parent 15edf7a commit 0487e3d

File tree

12 files changed

+255
-0
lines changed

12 files changed

+255
-0
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+
}

1048-LeetCode/README.md

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class NumMatrix {
2+
private int[][] dp;
3+
public NumMatrix(int[][] matrix) {
4+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
5+
return;
6+
int m = matrix.length;
7+
int n = matrix[0].length;
8+
9+
dp = new int[m+1][n+1];
10+
11+
for(int i=1;i<=m;i++)
12+
for(int j=1;j<=n;j++)
13+
dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + matrix[i-1][j-1];
14+
}
15+
16+
public int sumRegion(int row1, int col1, int row2, int col2) {
17+
return dp[row2+1][col2+1] - dp[row1][col2+1] - dp[row2+1][col1] + dp[row1][col1];
18+
}
19+
}
20+
21+
/**
22+
* Your NumMatrix object will be instantiated and called as such:
23+
* NumMatrix obj = new NumMatrix(matrix);
24+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
25+
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<h2><a href="https://leetcode.com/problems/range-sum-query-2d-immutable/">304. Range Sum Query 2D - Immutable</a></h2><h3>Medium</h3><hr><div><p>Given a 2D matrix <code>matrix</code>, handle multiple queries of the following type:</p>
2+
3+
<ul>
4+
<li>Calculate the <strong>sum</strong> of the elements of <code>matrix</code> inside the rectangle defined by its <strong>upper left corner</strong> <code>(row1, col1)</code> and <strong>lower right corner</strong> <code>(row2, col2)</code>.</li>
5+
</ul>
6+
7+
<p>Implement the NumMatrix class:</p>
8+
9+
<ul>
10+
<li><code>NumMatrix(int[][] matrix)</code> Initializes the object with the integer matrix <code>matrix</code>.</li>
11+
<li><code>int sumRegion(int row1, int col1, int row2, int col2)</code> Returns the <strong>sum</strong> of the elements of <code>matrix</code> inside the rectangle defined by its <strong>upper left corner</strong> <code>(row1, col1)</code> and <strong>lower right corner</strong> <code>(row2, col2)</code>.</li>
12+
</ul>
13+
14+
<p>&nbsp;</p>
15+
<p><strong>Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg" style="width: 415px; height: 415px;">
17+
<pre><strong>Input</strong>
18+
["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
19+
[[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]
20+
<strong>Output</strong>
21+
[null, 8, 11, 12]
22+
23+
<strong>Explanation</strong>
24+
NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]);
25+
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
26+
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
27+
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>m == matrix.length</code></li>
35+
<li><code>n == matrix[i].length</code></li>
36+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
37+
<li><code>-10<sup>5</sup> &lt;= matrix[i][j] &lt;= 10<sup>5</sup></code></li>
38+
<li><code>0 &lt;= row1 &lt;= row2 &lt; m</code></li>
39+
<li><code>0 &lt;= col1 &lt;= col2 &lt; n</code></li>
40+
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>sumRegion</code>.</li>
41+
</ul>
42+
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minMoves2(vector<int>& nums) {
4+
sort(nums.begin(),nums.end());
5+
int i=0,j=nums.size()-1;
6+
int ans = 0;
7+
while(i<j)
8+
{
9+
ans += nums[j]-nums[i];
10+
i++;
11+
j--;
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int minMoves2(int[] nums) {
3+
int mean = 0;
4+
int ans = 0;
5+
Arrays.sort(nums);
6+
mean = nums[nums.length/2];
7+
for(int num : nums)
8+
ans+=Math.abs(mean-num);
9+
return ans;
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h2><a href="https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/">462. Minimum Moves to Equal Array Elements II</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
2+
3+
<p>In one move, you can increment or decrement an element of the array by <code>1</code>.</p>
4+
5+
<p>Test cases are designed so that the answer will fit in a <strong>32-bit</strong> integer.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong>Example 1:</strong></p>
9+
10+
<pre><strong>Input:</strong> nums = [1,2,3]
11+
<strong>Output:</strong> 2
12+
<strong>Explanation:</strong>
13+
Only two moves are needed (remember each move increments or decrements one element):
14+
[<u>1</u>,2,3] =&gt; [2,2,<u>3</u>] =&gt; [2,2,2]
15+
</pre>
16+
17+
<p><strong>Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> nums = [1,10,2,9]
20+
<strong>Output:</strong> 16
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
<p><strong>Constraints:</strong></p>
25+
26+
<ul>
27+
<li><code>n == nums.length</code></li>
28+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
29+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
30+
</ul>
31+
</div>
Lines changed: 38 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)