Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 19 new problem solutions in Java. #275

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
591d8a6
Create DistributeCandiesToPeople.java
Pikeras72 Apr 21, 2022
0c35e3e
Update README.md
Pikeras72 Apr 21, 2022
bc6ae55
Create IsSubsequence.java
Pikeras72 Apr 21, 2022
6dcac29
Update README.md
Pikeras72 Apr 21, 2022
4c6f6a0
Update IsSubsequence.java
Pikeras72 Apr 21, 2022
382e4f5
Update DistributeCandiesToPeople.java
Pikeras72 Apr 21, 2022
c2c0091
Create MedianOfTwoSortedArrays.java
Pikeras72 Apr 23, 2022
7a1528c
Update README.md
Pikeras72 Apr 23, 2022
42024b4
Create IntegerToRoman.java
Pikeras72 Apr 23, 2022
5baf8fd
Create RomanToInteger.java
Pikeras72 Apr 23, 2022
3ae05ee
Update README.md
Pikeras72 Apr 23, 2022
9255f16
Create LongestCommonPrefix.java
Pikeras72 Apr 23, 2022
47ccb47
Update README.md
Pikeras72 Apr 23, 2022
7e8e9c1
Create ValidParentheses.java
Pikeras72 Apr 23, 2022
4c6227a
Create RecoverBinarySearchTree.java
Pikeras72 Apr 23, 2022
b1736c7
Update README.md
Pikeras72 Apr 23, 2022
e06d446
Update README.md
Pikeras72 Apr 23, 2022
87c9f11
Update LengthOfLastWord.java
Pikeras72 Apr 23, 2022
fa89226
Create ExcelSheetColumnNumber.java
Pikeras72 Apr 23, 2022
5f8eadf
Update ExcelSheetColumnNumber.java
Pikeras72 Apr 23, 2022
2708d72
Update README.md
Pikeras72 Apr 23, 2022
cdb8b7d
Create HappyNumber.java
Pikeras72 Apr 23, 2022
1b27532
Update README.md
Pikeras72 Apr 23, 2022
07f37f8
Update MyQueue.java
Pikeras72 Apr 24, 2022
afc502d
Update MyQueue.java
Pikeras72 Apr 24, 2022
c5e9a52
Create PowerOfTwo.java
Pikeras72 Apr 24, 2022
fb445ab
Update README.md
Pikeras72 Apr 24, 2022
0edbcf8
Create PowerOfThree.java
Pikeras72 Apr 24, 2022
18316f2
Create PowerOfFour.java
Pikeras72 Apr 24, 2022
cb9f236
Update README.md
Pikeras72 Apr 24, 2022
22ec019
Create ThirdMaximumNumber.java
Pikeras72 Apr 24, 2022
58bed68
Create MinimumRemoveToMakeValidParentheses.java
Pikeras72 Apr 24, 2022
e61a4f6
Delete algorithms/java/MinimumRemoveToMakeValidParentheses directory
Pikeras72 Apr 24, 2022
e343959
Create MinimumRemoveToMakeValidParentheses.java
Pikeras72 Apr 24, 2022
17c5e42
Update README.md
Pikeras72 Apr 24, 2022
376480d
Create SubtractTheProductAndSumOfDigitsOfAnInteger.java
Pikeras72 Apr 24, 2022
8c306eb
Update README.md
Pikeras72 Apr 24, 2022
6a748c6
Create CanMakeArithmeticProgressionFromSequence.java
Pikeras72 Apr 24, 2022
9e2be50
Update README.md
Pikeras72 Apr 24, 2022
72db063
Create SignOfTheProductOfAnArray.java
Pikeras72 Apr 24, 2022
16a6c40
Update README.md
Pikeras72 Apr 24, 2022
43bf2f6
Update LengthOfLastWord.java
Pikeras72 Apr 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Source : https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
// Author : Diego Ruiz Piqueras (Pikeras72)
// Date : 24-04-2022

/*****************************************************************************************************
*
* A sequence of numbers is called an arithmetic progression if the
* difference between any two consecutive elements is the same.
*
* Given an array of numbers arr, return true if the array can be
* rearranged to form an arithmetic progression. Otherwise, return false.
*
* Example 1:
*
* Input: arr = [3,5,1]
* Output: true
* Explanation: We can reorder the elements as [1,3,5] or [5,3,1]
* with differences 2 and -2 respectively, between each consecutive elements.
*
* Example 2:
*
* Input: arr = [1,2,4]
* Output: false
* Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
*
* Constraints:
*
* 2 <= arr.length <= 1000
* -10^6 <= arr[i] <= 10^6
*
* Explanation of the solution:
*
* 1. We sort the array 'arr' to check the distance between the first two numbers.
*
* 2. We analyze the array looking if all the numbers have the exact same distance
* between them compared to the two first numbers.
******************************************************************************************************/

class Solution {
public boolean canMakeArithmeticProgression(int[] arr) {
int dis, disBase = 0;
boolean yes = true;
Arrays.sort(arr);
for(int i = 0; i < arr.length-1; i++){
if(i == 0){
disBase = arr[i+1]-arr[i];
}else{
dis = arr[i+1]-arr[i];
if(dis != disBase){
yes = false;
break;
}
}
}
return yes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Source : https://leetcode.com/problems/distribute-candies-to-people/
// Author : Diego Ruiz Piqueras (Pikeras72)
// Date : 22-04-2022

/*****************************************************************************************************
*
* We distribute some number of candies, to a row of n = num_people people in the
* following way:
*
* We then give 1 candy to the first person, 2 candies to the second person, and so on until
* we give n candies to the last person.
*
* Then, we go back to the start of the row, giving n + 1 candies to the first person, n +
* 2 candies to the second person, and so on until we give 2 * n candies to the last
* person.
*
* This process repeats (with us giving one more candy each time, and moving to the start of
* the row after we reach the end) until we run out of candies. The last person will receive all
* of our remaining candies (not necessarily one more than the previous gift).
*
* Return an array (of length num_people and sum candies) that represents the final
* distribution of candies.
*
*
* Example 1:
*
* Input: candies = 7, num_people = 4
* Output: [1,2,3,1]
* Explanation:
* On the first turn, ans[0] += 1, and the array is [1,0,0,0].
* On the second turn, ans[1] += 2, and the array is [1,2,0,0].
* On the third turn, ans[2] += 3, and the array is [1,2,3,0].
* On the fourth turn, ans[3] += 1 (because there is only one candy
* left), and the final array is [1,2,3,1].
*
* Example 2:
*
* Input: candies = 10, num_people = 3
* Output: [5,2,3]
* Explanation:
* On the first turn, ans[0] += 1, and the array is [1,0,0].
* On the second turn, ans[1] += 2, and the array is [1,2,0].
* On the third turn, ans[2] += 3, and the array is [1,2,3].
* On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
*
*
* Constraints:
*
* 1 <= candies <= 10^9
* 1 <= num_people <= 1000
*
* Explanation of the solution:
*
* 1. While we have given less candies than the ones we had at the beginning (while(total < candies){...})
*
* 2. Use count % num_people == 0 to determine the current index of the people.
******************************************************************************************************/

class Solution {
public int[] distributeCandies(int candies, int num_people) {
int[] res = new int[num_people]; //Initialize the array of people
int toGiveCandie = 1, count = 0, total = 0;
while(total < candies){ //While the total candies given is fewer than the candies we have...
if(count % num_people == 0){ //This is to 'reset' de counter to 0 when we reach the last person.
count = 0;
}
if(total + toGiveCandie <= candies){
//If the number of candies we have already given plus the candies we are
// going to give are less or equal than the total candies...
res[count] += toGiveCandie; //We add the candies to the array (give the candie to the person)
total += toGiveCandie; //We update the candies we have given
toGiveCandie++; //To the next person we will give one more candie
}else{
//If we don't have the enough candies, we just give the candies left
res[count] += candies-total;
total += candies-total;
toGiveCandie++;
}
count++;
}
return res; //We return the filled array
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Source : https://leetcode.com/problems/excel-sheet-column-number/
// Author : Diego Ruiz Piqueras (Pikeras72)
// Date : 24-04-2022

/*****************************************************************************************************
* Given a string columnTitle that represents the column title as appear in an
* Excel sheet, return its corresponding column number.
*
* For example:
* A -> 1
* B -> 2
* C -> 3
* ...
* Z -> 26
* AA -> 27
* AB -> 28
* ...
*
* Example 1:
*
* Input: columnTitle = "A"
* Output: 1
*
* Example 2:
*
* Input: columnTitle = "AB"
* Output: 28
* Explanation:
*
* Example 3:
*
* Input: columnTitle = "ZY"
* Output: 701
*
* 1 <= columnTitle.length <= 7
* columnTitle consists only of uppercase English letters.
* columnTitle is in the range ["A", "FXSHRXW"]
******************************************************************************************************/

class Solution {
public int titleToNumber(String columnTitle) {
int res = 0, cnt = 0;
String letras = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int exponente = columnTitle.length()-1;
if(exponente == 0){
return letras.indexOf(columnTitle)+1;
}
while(exponente != -1){
res += (int) ((letras.indexOf(String.valueOf(columnTitle.charAt(cnt)))+1)*Math.pow(26, exponente));
exponente--;
cnt++;
}
return res;
}
}
55 changes: 55 additions & 0 deletions algorithms/java/src/HappyNumber/HappyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Source : https://leetcode.com/problems/happy-number/
// Author : Diego Ruiz Piqueras (Pikeras72)
// Date : 24-04-2022

/*****************************************************************************************************
*
* Write an algorithm to determine if a number n is happy.
* A happy number is a number defined by the following process:
*
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
*
* Repeat the process until the number equals 1 (where it will stay),
* or it loops endlessly in a cycle which does not include 1.
*
* Those numbers for which this process ends in 1 are happy.
*
* Return true if n is a happy number, and false if not.
*
* Example 1:
*
* Input: n = 19
* Output: true
* Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
*
* Example 2:
*
* Input: n = 2
* Output: false
*
* Constraints:
*
* 1 <= n <= 2^31 - 1
******************************************************************************************************/

class Solution {
public boolean isHappy(int n) {
while(n != 1){
if(n == 7)
return true;
else if(n <= 9)
return false;
int num = 0;
while(n > 0){
num += Math.pow(n % 10, 2);
n /= 10;
}
n=num;
}
return true;
}
}
140 changes: 140 additions & 0 deletions algorithms/java/src/IntegerToRoman/IntegerToRoman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Source : https://leetcode.com/problems/integer-to-roman/
// Author : Diego Ruiz Piqueras (Pikeras72)
// Date : 23-04-2022

/*****************************************************************************************************
* Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
*
* Symbol Value
* I 1
* V 5
* X 10
* L 50
* C 100
* D 500
* M 1000
*
* For example, 2 is written as II in Roman numeral, just two one's added
* together. 12 is written as XII, which is simply X + II. The number 27 is
* written as XXVII, which is XX + V + II.
*
* Roman numerals are usually written largest to smallest from left to right.
* However, the numeral for four is not IIII. Instead, the number four is
* written as IV. Because the one is before the five we subtract it making four.
* The same principle applies to the number nine, which is written as IX. There
* are six instances where subtraction is used:
*
* I can be placed before V (5) and X (10) to make 4 and 9.
* X can be placed before L (50) and C (100) to make 40 and 90.
* C can be placed before D (500) and M (1000) to make 400 and 900.
*
* Given an integer, convert it to a roman numeral.
*
* Example 1:
*
* Input: num = 3
* Output: "III"
* Explanation: 3 is represented as 3 ones.
*
* Example 2:
*
* Input: num = 58
* Output: "LVIII"
* Explanation: L = 50, V = 5, III = 3.
*
* Example 3:
*
* Input: num = 1994
* Output: "MCMXCIV"
* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
*
* Constraints:
*
* 1 <= num <= 3999
*
* Explanation of the solution:
*
* We start looking if the number given is bigger than 1000
* to see if there is going to be a letter 'M' (1000) in the solution.
*
* If the number > 1000 we repeat the process until a maximun of three 'M'
* substracting 1000 to the number each time.
*
* The process is the same for all the letters.
*
* We also analyze special cases such as 'CM' (num < 1000 && num > 899), 'CD' (num < 500 && num > 399),
* 'XC' (num < 100 && num > 89), 'XL' (num < 50 && num > 39), 'IX' (num == 9) and 'IV' (num == 4).
******************************************************************************************************/

class Solution {
public String intToRoman(int num) {
String res = "";
int cnt = 0;
while(num >= 1000 && cnt < 3){
res += "M";
num -= 1000;
cnt++;
}
cnt = 0;
if(num < 1000 && num > 899){
res += "CM";
num -= 900;
}
while(num >= 500 && cnt < 3){
res += "D";
num -= 500;
cnt++;
}
cnt = 0;
if(num < 500 && num > 399){
res += "CD";
num -= 400;
}
while(num >= 100 && cnt < 3){
res += "C";
num -= 100;
cnt++;
}
cnt = 0;
if(num < 100 && num > 89){
res += "XC";
num -= 90;
}
while(num >= 50 && cnt < 3){
res += "L";
num -= 50;
cnt++;
}
cnt = 0;
if(num < 50 && num > 39){
res += "XL";
num -= 40;
}
while(num >= 10 && cnt < 3){
res += "X";
num -= 10;
cnt++;
}
cnt = 0;
if(num == 9){
res += "IX";
num -= 9;
}
while(num >= 5 && cnt < 3){
res += "V";
num -= 5;
cnt++;
}
cnt = 0;
if(num == 4){
res += "IV";
num -= 4;
}
while(num >= 1 && cnt < 3){
res += "I";
num -= 1;
cnt++;
}
return res;
}
}
Loading