-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b48f515
commit 85f9da9
Showing
215 changed files
with
5,345 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </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 --> 2): The sum is 3. | ||
(1 --> 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> </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 <= Node.val <= 1000</code></li> | ||
<li><code>-1000 <= targetSum <= 1000</code></li> | ||
</ul> | ||
</div> |
24 changes: 24 additions & 0 deletions
24
1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </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> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n, k <= 30</code></li> | ||
<li><code>1 <= target <= 1000</code></li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p> | ||
|
||
<p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p> | ||
|
||
<p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p> | ||
|
||
<p> </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> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 10^5</code></li> | ||
<li><code>0 <= pairs.length <= 10^5</code></li> | ||
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li> | ||
<li><code>s</code> only contains lower case English letters.</li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </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> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= palindrome.length <= 1000</code></li> | ||
<li><code>palindrome</code> consists of only lowercase English letters.</li> | ||
</ul> | ||
</div> |
17 changes: 17 additions & 0 deletions
17
...-of-steps-to-reduce-a-number-to-zero/1342-number-of-steps-to-reduce-a-number-to-zero.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> num = 14 | ||
<strong>Output:</strong> 6 | ||
<strong>Explanation:</strong> | ||
Step 1) 14 is even; divide by 2 and obtain 7. | ||
Step 2) 7 is odd; subtract 1 and obtain 6. | ||
Step 3) 6 is even; divide by 2 and obtain 3. | ||
Step 4) 3 is odd; subtract 1 and obtain 2. | ||
Step 5) 2 is even; divide by 2 and obtain 1. | ||
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> | ||
Step 1) 8 is even; divide by 2 and obtain 4. | ||
Step 2) 4 is even; divide by 2 and obtain 2. | ||
Step 3) 2 is even; divide by 2 and obtain 1. | ||
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> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>0 <= num <= 10<sup>6</sup></code></li> | ||
</ul> | ||
</div> |
20 changes: 20 additions & 0 deletions
20
1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Oops, something went wrong.