Skip to content

Commit

Permalink
Added 20 Problems
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavnirmal2001 committed Oct 10, 2022
1 parent b48f515 commit 85f9da9
Show file tree
Hide file tree
Showing 215 changed files with 5,345 additions and 25 deletions.
20 changes: 0 additions & 20 deletions .vscode/c_cpp_properties.json

This file was deleted.

5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

32 changes: 32 additions & 0 deletions 112-path-sum/112-path-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null)
return false;

targetSum -= root.val;

if (targetSum == 0 && root.left == null && root.right == null)
return true;

boolean ans = hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);

targetSum += root.val;

return ans;
}
}
38 changes: 38 additions & 0 deletions 112-path-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<h2><a href="https://leetcode.com/problems/path-sum/">112. Path Sum</a></h2><h3>Easy</h3><hr><div><p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p>

<p>A <strong>leaf</strong> is a node with no children.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;">
<pre><strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
<strong>Output:</strong> true
<strong>Explanation:</strong> The root-to-leaf path with the target sum is shown.
</pre>

<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg">
<pre><strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> false
<strong>Explanation:</strong> There two root-to-leaf paths in the tree:
(1 --&gt; 2): The sum is 3.
(1 --&gt; 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
</pre>

<p><strong>Example 3:</strong></p>

<pre><strong>Input:</strong> root = [], targetSum = 0
<strong>Output:</strong> false
<strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
<li><code>-1000 &lt;= targetSum &lt;= 1000</code></li>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
int mod = 1000000007;
public int numRollsToTarget(int n, int k, int target) {
int[][] dp = new int[n+1][target+1];
for(int i=0;i<dp.length;i++){
Arrays.fill(dp[i], -1);
}
return roll(n, k, target, dp);
}

public int roll(int n, int k, int t, int[][] dp){
if(t<0) return 0;
if(n==0)
return t==0 ? 1 : 0;
if(dp[n][t] ==-1) {
int res = 0;
for(int i=1;i<=k;i++){
res= (res+roll(n-1, k, t-i, dp))%mod;
}
dp[n][t] = res;
}
return dp[n][t];
}
}
36 changes: 36 additions & 0 deletions 1155-number-of-dice-rolls-with-target-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h2><a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">1155. Number of Dice Rolls With Target Sum</a></h2><h3>Medium</h3><hr><div><p>You have <code>n</code> dice and each die has <code>k</code> faces numbered from <code>1</code> to <code>k</code>.</p>

<p>Given three integers <code>n</code>, <code>k</code>, and <code>target</code>, return <em>the number of possible ways (out of the </em><code>k<sup>n</sup></code><em> total ways) </em><em>to roll the dice so the sum of the face-up numbers equals </em><code>target</code>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre><strong>Input:</strong> n = 1, k = 6, target = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> You throw one die with 6 faces.
There is only one way to get a sum of 3.
</pre>

<p><strong>Example 2:</strong></p>

<pre><strong>Input:</strong> n = 2, k = 6, target = 7
<strong>Output:</strong> 6
<strong>Explanation:</strong> You throw two dice, each with 6 faces.
There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
</pre>

<p><strong>Example 3:</strong></p>

<pre><strong>Input:</strong> n = 30, k = 30, target = 500
<strong>Output:</strong> 222616187
<strong>Explanation:</strong> The answer must be returned modulo 10<sup>9</sup> + 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n, k &lt;= 30</code></li>
<li><code>1 &lt;= target &lt;= 1000</code></li>
</ul>
</div>
1 change: 1 addition & 0 deletions 1202-smallest-string-with-swaps/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
45 changes: 45 additions & 0 deletions 1202-smallest-string-with-swaps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h2><a href="https://leetcode.com/problems/smallest-string-with-swaps/">1202. Smallest String With Swaps</a></h2><h3>Medium</h3><hr><div><p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p>

<p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p>

<p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre><strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
<strong>Output:</strong> "bacd"
<strong>Explaination:</strong>
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"
</pre>

<p><strong>Example 2:</strong></p>

<pre><strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> "abcd"
<strong>Explaination: </strong>
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"</pre>

<p><strong>Example 3:</strong></p>

<pre><strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
<strong>Output:</strong> "abc"
<strong>Explaination: </strong>
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
<li><code>0 &lt;= pairs.length &lt;= 10^5</code></li>
<li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li>
<li><code>s</code>&nbsp;only contains lower case English letters.</li>
</ul>
</div>
21 changes: 21 additions & 0 deletions 1328-break-a-palindrome/1328-break-a-palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public String breakPalindrome(String palindrome) {
int len = palindrome.length();
if(len ==1) return "";
StringBuilder sb = new StringBuilder(palindrome);
//let's traverse the string
// System.out.printf("Before:%s\n",sb.toString());
int i=0;
for( i =0;i<len;i++)
{
if(sb.toString().charAt(i)!='a'&&len-i-1!=i)
{ sb.replace(i,i+1,"a");break;}

}
if(i==len)sb.replace(i-1,i,"b");
// System.out.printf("After:%s",sb.toString());

return sb.toString();

}
}
1 change: 1 addition & 0 deletions 1328-break-a-palindrome/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
30 changes: 30 additions & 0 deletions 1328-break-a-palindrome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h2><a href="https://leetcode.com/problems/break-a-palindrome/">1328. Break a Palindrome</a></h2><h3>Medium</h3><hr><div><p>Given a palindromic string of lowercase English letters <code>palindrome</code>, replace <strong>exactly one</strong> character with any lowercase English letter so that the resulting string is <strong>not</strong> a palindrome and that it is the <strong>lexicographically smallest</strong> one possible.</p>

<p>Return <em>the resulting string. If there is no way to replace a character to make it not a palindrome, return an <strong>empty string</strong>.</em></p>

<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly smaller than the corresponding character in <code>b</code>. For example, <code>"abcc"</code> is lexicographically smaller than <code>"abcd"</code> because the first position they differ is at the fourth character, and <code>'c'</code> is smaller than <code>'d'</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre><strong>Input:</strong> palindrome = "abccba"
<strong>Output:</strong> "aaccba"
<strong>Explanation:</strong> There are many ways to make "abccba" not a palindrome, such as "<u>z</u>bccba", "a<u>a</u>ccba", and "ab<u>a</u>cba".
Of all the ways, "aaccba" is the lexicographically smallest.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre><strong>Input:</strong> palindrome = "a"
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= palindrome.length &lt;= 1000</code></li>
<li><code>palindrome</code> consists of only lowercase English letters.</li>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int numberOfSteps(int num) {
return count(num,0);
}
public int count(int num, int steps){
if(num==0) return steps;
if(num==1) return steps+1;

if(num%2==0){
steps++;
return count((num/2), steps);
}else{
steps++;
return count((num-1), steps);
}
}
}
1 change: 1 addition & 0 deletions 1342-number-of-steps-to-reduce-a-number-to-zero/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42 changes: 42 additions & 0 deletions 1342-number-of-steps-to-reduce-a-number-to-zero/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<h2><a href="https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/">1342. Number of Steps to Reduce a Number to Zero</a></h2><h3>Easy</h3><hr><div><p>Given an integer <code>num</code>, return <em>the number of steps to reduce it to zero</em>.</p>

<p>In one step, if the current number is even, you have to divide it by <code>2</code>, otherwise, you have to subtract <code>1</code> from it.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre><strong>Input:</strong> num = 14
<strong>Output:</strong> 6
<strong>Explanation:</strong>&nbsp;
Step 1) 14 is even; divide by 2 and obtain 7.&nbsp;
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3.&nbsp;
Step 4) 3 is odd; subtract 1 and obtain 2.&nbsp;
Step 5) 2 is even; divide by 2 and obtain 1.&nbsp;
Step 6) 1 is odd; subtract 1 and obtain 0.
</pre>

<p><strong>Example 2:</strong></p>

<pre><strong>Input:</strong> num = 8
<strong>Output:</strong> 4
<strong>Explanation:</strong>&nbsp;
Step 1) 8 is even; divide by 2 and obtain 4.&nbsp;
Step 2) 4 is even; divide by 2 and obtain 2.&nbsp;
Step 3) 2 is even; divide by 2 and obtain 1.&nbsp;
Step 4) 1 is odd; subtract 1 and obtain 0.
</pre>

<p><strong>Example 3:</strong></p>

<pre><strong>Input:</strong> num = 123
<strong>Output:</strong> 12
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= num &lt;= 10<sup>6</sup></code></li>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int minCost(String colors, int[] neededTime) {
int groupSum=0;
int maxSum=0;
int res=0;

for(int i=0;i<colors.length();i++){
if(i>0 &&colors.charAt(i)!=colors.charAt(i-1)){
res+=groupSum-maxSum;
groupSum=0;
maxSum=0;
}

groupSum+=neededTime[i];
maxSum=Math.max(maxSum,neededTime[i]);
}
res+=groupSum-maxSum;
return res;
}
}
1 change: 1 addition & 0 deletions 1578-minimum-time-to-make-rope-colorful/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Loading

0 comments on commit 85f9da9

Please sign in to comment.