-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution for 7. Reverse Integer
- Loading branch information
Showing
8 changed files
with
173 additions
and
50 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
*.swp | ||
|
||
*.class |
35 changes: 19 additions & 16 deletions
35
leetcode/medium/1481. Least Number of Unique Integers after K Removals/question.md
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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements. | ||
Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements. | ||
|
||
|
||
|
||
Example 1: | ||
Example 1: | ||
|
||
Input: arr = [5,5,4], k = 1 | ||
Output: 1 | ||
Explanation: Remove the single 4, only 5 is left. | ||
Example 2: | ||
Input: arr = [4,3,1,1,3,3,2], k = 3 | ||
Output: 2 | ||
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left. | ||
Input: arr = [5,5,4], k = 1 | ||
Output: 1 | ||
Explanation: Remove the single 4, only 5 is left. | ||
Example 2: | ||
Input: arr = [4,3,1,1,3,3,2], k = 3 | ||
Output: 2 | ||
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left. | ||
|
||
|
||
Constraints: | ||
Constraints: | ||
|
||
1 <= arr.length <= 10^5 | ||
1 <= arr[i] <= 10^9 | ||
0 <= k <= arr.length | ||
1 <= arr.length <= 10^5 | ||
1 <= arr[i] <= 10^9 | ||
0 <= k <= arr.length | ||
|
||
|
||
## Hint | ||
Imagine you have a bunch of numbers written down in a list (that's your array of integers). Now, someone tells you to pick out some numbers from that list, but you can only take away a certain number of them (that's your k). Your task is to figure out how to take away numbers so that you end up with the fewest different numbers left in your list. So, basically, you're trying to minimize the number of different numbers you have in your list after removing some. | ||
## Hint | ||
Imagine you have a bunch of numbers written down in a list (that's your array of integers). | ||
Now, someone tells you to pick out some numbers from that list, but you can only take away a certain number of them (that's your k). | ||
Your task is to figure out how to take away numbers so that you end up with the fewest different numbers left in your list. | ||
So, basically, you're trying to minimize the number of different numbers you have in your list after removing some. | ||
|
||
In order to achieve that we have to keep the numbers with maximum duplicates | ||
In order to achieve that we have to keep the numbers with maximum duplicates |
52 changes: 24 additions & 28 deletions
52
leetcode/medium/2971. Find Polygon With the Largest Perimeter/question.md
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 |
---|---|---|
@@ -1,39 +1,35 @@ | ||
You are given an array of positive integers nums of length n. | ||
You are given an array of positive integers nums of length n. | ||
|
||
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides. | ||
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides. | ||
|
||
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak. | ||
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak. | ||
|
||
The perimeter of a polygon is the sum of lengths of its sides. | ||
The perimeter of a polygon is the sum of lengths of its sides. | ||
|
||
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon. | ||
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon. | ||
|
||
|
||
|
||
Example 1: | ||
Example 1: | ||
|
||
Input: nums = [5,5,5] | ||
Output: 15 | ||
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. | ||
Example 2: | ||
Input: nums = [5,5,5] | ||
Output: 15 | ||
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. | ||
Example 2: | ||
|
||
Input: nums = [1,12,1,2,5,50,3] | ||
Output: 12 | ||
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. | ||
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. | ||
It can be shown that the largest possible perimeter is 12. | ||
Example 3: | ||
Input: nums = [1,12,1,2,5,50,3] | ||
Output: 12 | ||
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. | ||
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. | ||
It can be shown that the largest possible perimeter is 12. | ||
Example 3: | ||
|
||
Input: nums = [5,5,50] | ||
Output: -1 | ||
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5. | ||
Input: nums = [5,5,50] | ||
Output: -1 | ||
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5. | ||
|
||
Constraints: | ||
|
||
Constraints: | ||
|
||
3 <= n <= 105 | ||
1 <= nums[i] <= 109 | ||
|
||
3 <= n <= 105 | ||
1 <= nums[i] <= 109 | ||
|
||
## Hint | ||
Only the longest side's sum should be smaller than the sum of all the other sides. So looping in reverse order will get the job done. | ||
## Hint | ||
Only the longest side's sum should be smaller than the sum of all the other sides. So looping in reverse order will get the job done. |
30 changes: 30 additions & 0 deletions
30
leetcode/medium/3. Longest Substring Without Repeating Characters/Java/Solution.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,30 @@ | ||
import java.util.HashSet; | ||
|
||
class Solution { | ||
|
||
public static void main(String[] args) { | ||
String s = "pwwkew"; | ||
System.out.println(lengthOfLongestSubstring(s)); | ||
} | ||
|
||
public static int lengthOfLongestSubstring(String s) { | ||
int pointer_one = 0; | ||
int pointer_two = 0; | ||
int max = 0; | ||
|
||
HashSet<Character> hashSet = new HashSet(); | ||
|
||
while(pointer_two < s.length()){ | ||
if(!hashSet.contains(s.charAt(pointer_two))){ | ||
hashSet.add(s.charAt(pointer_two)); | ||
pointer_two++; | ||
max = Math.max(hashSet.size(), max); | ||
}else{ | ||
hashSet.remove(s.charAt(pointer_one)); | ||
pointer_one++; | ||
} | ||
} | ||
System.out.println(hashSet); | ||
return max; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
leetcode/medium/3. Longest Substring Without Repeating Characters/question.md
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,23 @@ | ||
Given a string s, find the length of the longest substring without repeating characters. | ||
|
||
Example 1: | ||
|
||
Input: s = "abcabcbb" | ||
Output: 3 | ||
Explanation: The answer is "abc", with the length of 3. | ||
Example 2: | ||
|
||
Input: s = "bbbbb" | ||
Output: 1 | ||
Explanation: The answer is "b", with the length of 1. | ||
Example 3: | ||
|
||
Input: s = "pwwkew" | ||
Output: 3 | ||
Explanation: The answer is "wke", with the length of 3. | ||
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. | ||
|
||
Constraints: | ||
|
||
0 <= s.length <= 5 * 104 | ||
s consists of English letters, digits, symbols and spaces. |
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
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,33 @@ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
int num = 1234567890; | ||
System.out.println(reverse(num)); | ||
} | ||
|
||
public static int reverse(int x) { | ||
|
||
int reversedNum = 0; | ||
int max = Integer.MAX_VALUE; | ||
int min = Integer.MIN_VALUE; | ||
|
||
while(x != 0){ | ||
int digit = x % 10; | ||
|
||
// this is to make sure that the digit we are going to add to the reversedNum doesn't exceed 2147483647 | ||
// reversedNum > max/10 -> the current reversedNum shouldn't be greater than 214748364 | ||
// If the reversedNUm is equal to 214748364, we have to make sure that the digit is not greater than 7 | ||
if(reversedNum > max/10 || (reversedNum == max/10 && digit > max%10)) | ||
return 0; | ||
|
||
// this is to make sure that the digit we are going to add to the reversedNum doesn't go below -2147483647 | ||
if(reversedNum < min/10 || (reversedNum == min/10 && digit < min%10)) | ||
return 0; | ||
|
||
x = x/10; | ||
reversedNum = reversedNum * 10 + digit; | ||
} | ||
return reversedNum; | ||
|
||
} | ||
} |
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 @@ | ||
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. | ||
|
||
Assume the environment does not allow you to store 64-bit integers (signed or unsigned). | ||
|
||
Example 1: | ||
|
||
Input: x = 123 | ||
Output: 321 | ||
Example 2: | ||
|
||
Input: x = -123 | ||
Output: -321 | ||
Example 3: | ||
|
||
Input: x = 120 | ||
Output: 21 | ||
|
||
Constraints: | ||
|
||
-231 <= x <= 231 - 1 | ||
|
||
## Hint: | ||
Formula for reversing is | ||
|
||
Example:- | ||
``` | ||
public static int reverse(int num){ | ||
int result = 0; | ||
while(num != 0){ | ||
int digit = num % 10; | ||
result = result * 10 + digit; | ||
num = num/10; | ||
} | ||
return result; | ||
} | ||
``` |