Skip to content

Commit

Permalink
solution(java): Problems 4, 151, 172, 2186, 287, and 34
Browse files Browse the repository at this point in the history
  • Loading branch information
godkingjay authored Oct 16, 2023
2 parents d29a67b + c8e465b commit 979d885
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.ArrayList;
public class Solution{
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int a = nums1.length;
int b = nums2.length;

// Calculate the total length of the merged array
int c = a + b;

// Create a new integer array to hold the merged elements
int[] arr = new int[c];

// Copy elements from nums1 into arr
for (int i = 0; i < a; i++) {
arr[i] = nums1[i];
}

// Copy elements from nums2 into arr, starting from position a
for (int i = 0; i < b; i++) {
arr[a + i] = nums2[i];
}

// Sort the merged array in ascending order
Arrays.sort(arr);

// Check if the length of the merged array is even or odd
if (c % 2 == 0) {
// Calculate the indices of the two middle elements
int mid1 = (c - 1) / 2;
int mid2 = mid1 + 1;

// Calculate the median as the average of the two middle elements
return (double) (arr[mid1] + arr[mid2]) / 2;
} else {
// Calculate the index of the middle element
int mid = (c - 1) / 2;

// Return the middle element as the median
return arr[mid];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### `findMedianSortedArrays` Function Explanation

1. Create an `ArrayList` called `list` to merge both sorted arrays into a single list.
2. Iterate through the elements of `nums1`, and for each element, add it to the `list`.
3. Iterate through the elements of `nums2`, and for each element, add it to the `list`. This effectively combines both input arrays into a single merged list.
4. Create a variable `sum` to store the sum of all elements in the merged list. Initialize it to 0.
5. Iterate through the merged `list`, and for each element, add its value to the `sum`.
6. Calculate the average (mean) of all elements in the merged list by dividing the `sum` by the total number of elements in the list.
7. Return the calculated average (mean) as the median value of the merged sorted arrays.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Arrays;
public class Solution{
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
// Get the lengths of input arrays nums1 and nums2
int a = nums1.length;
int b = nums2.length;

// Calculate the total length of the merged array
int c = a + b;

// Create a new integer array to hold the merged elements
int[] arr = new int[c];

// Copy elements from nums1 into arr
for (int i = 0; i < a; i++) {
arr[i] = nums1[i];
}

// Copy elements from nums2 into arr, starting from position a
for (int i = 0; i < b; i++) {
arr[a + i] = nums2[i];
}

// Sort the merged array in ascending order
Arrays.sort(arr);

// Check if the length of the merged array is even or odd
if (c % 2 == 0) {
// Calculate the indices of the two middle elements
int mid1 = (c - 1) / 2;
int mid2 = mid1 + 1;

// Calculate the median as the average of the two middle elements
return (double) (arr[mid1] + arr[mid2]) / 2;
} else {
// Calculate the index of the middle element
int mid = (c - 1) / 2;

// Return the middle element as the median
return arr[mid];
}
}
}
24 changes: 24 additions & 0 deletions Medium/151. Reverse Words in a String/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
public String reverseWords(String s) {
String [] words = s.split(" ");

//StringBuilder to store the result.
StringBuilder result = new StringBuilder();

int end = words.length - 1;

for(int i = 0; i<= end; i++){
// Check if the current word is not empty.
if(!words[i].isEmpty()) {
// Insert the current word at the beginning of the 'result'.
result.insert(0, words[i]);
if(i < end) {
// Add a space before the current word if it's not the last word.
result.insert(0, " ");
}
}
}

return result.toString();
}
}
24 changes: 24 additions & 0 deletions Medium/151. Reverse Words in a String/sol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Simplest and Easy Approach to Understand and solve a problem

1. Split the input string 's' into an array of words using space as the delimiter, and store them in the 'words' array.
2. Create an empty StringBuilder called 'result' to store the reversed sentence.
3. Initialize the 'end' variable to the index of the last word in the 'words' array.
4. Iterate through the 'words' array in a forward order.
5. Inside the loop, check if the current word is not empty.
6. If the current word is not empty, insert it at the beginning of the 'result' StringBuilder.
7. If the current word is not the last word, add a space character before it to separate words.
8. Repeat steps 5-7 for all words in the 'words' array.
9. Convert the 'result' StringBuilder to a string using the toString() method.
10. Return the reversed sentence as the result.

Example 1:

Input: s = "the sky is blue"

Output: "blue is sky the"

Example 2:

Input: s = " hello world "

Output: "world hello"
14 changes: 14 additions & 0 deletions Medium/172. Factorial Trailing Zeroes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public int trailingZeroes(int n) {

int zero=0;

for(int i=5 ; i<=n ; i=i*5){
zero = zero + n/i;
}

return zero;

}

}
31 changes: 31 additions & 0 deletions Medium/172. Factorial Trailing Zeroes/sol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
### Simplest, Easy and Optimized java Solution

Algorithm
1. Initialize a variable zero to 0. This variable will be used to keep track of the number of trailing zeroes.
2. Start a loop with i initialized to 5. The loop will continue as long as i is less than or equal to n.
3. Inside the loop, calculate the number of factors of 5 in n. You can do this by dividing n by i and adding the result to the zero variable. This step accounts for the number of trailing zeroes caused by the multiples of 5 in the factorial of n.
4. Multiply i by 5 in each iteration of the loop to check for the next power of 5.
5. Once the loop is finished, return the value of zero, which represents the number of trailing zeroes in the factorial of n.

Example 1:

Input: n = 3

Output: 0

Explanation: 3! = 6, no trailing zero.

Example 2:

Input: n = 5

Output: 1

Explanation: 5! = 120, one trailing zero.

Example 3:


Input: n = 0

Output: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Solution {
public int minSteps(String s, String s1) {
int[] freq1 = new int[26];
int[] freq2 = new int[26];

// Count the frequency of characters in the first string
for (char c : s.toCharArray()) {
freq1[c - 'a']++;
}

// Count the frequency of characters in the second string
for (char c : s1.toCharArray()) {
freq2[c - 'a']++;
}

int delete = 0;

// Calculate the difference in character frequencies
for (int i = 0; i < 26; i++) {
delete += Math.abs(freq1[i] - freq2[i]);
}
return delete;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Easiest solution to understand the solution

1. Create two arrays of 26 Characters
2. Count the frequency of characters in the first string
3. Count the frequency of characters in the second string
4. Calculate the difference in character frequencies
5. Return the difference Value

Example 1:

Input: s = "leetcode", t = "coats"

Output: 7

Example 2:

Input: s = "night", t = "thing"

Output: 0
13 changes: 13 additions & 0 deletions Medium/287. Find Duplicate Number/New Approach/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Arrays;
public class Solution {
public int findDuplicate(int[] nums) {
Arrays.sort(nums);

for(int i=1;i<nums.length;i++){
if(nums[i]==nums[i-1])
return nums[i];
}
return -1;
}

}
13 changes: 13 additions & 0 deletions Medium/287. Find Duplicate Number/New Approach/sol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Simplest and Easiest approach to solve

1. Sort the input array `nums` in ascending order.

2. Initialize a loop from `i` = 1 to `n` (inclusive), where `n` is the length of the sorted `nums` array.

3. Within the loop:

a. Check if `nums[i]` is equal to `nums[i-1]`.

b. If the condition is true, return `nums[i]` as it is the duplicate element.

4. If no duplicate is found after the loop completes, return -1 to indicate that there are no duplicates in the array.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
public int[] searchRange(int[] nums, int target) {

int n=nums.length;
int arr[] =new int[2];
int arr1[] =new int[]{-1,-1};
arr[0] = -1; arr[1] = -1;
for (int i = 0; i < n; i++) {
if (target != nums[i])
continue;
if (arr[0] == -1)
arr[0] = i;
arr[1] = i;
}
if (arr[0] != -1) {
return arr;

}
else
return arr1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### `searchRange` Function Explanation

1. Get the length of the input array `nums`.
2. Initialize an array to store the search range.
3. Initialize an array with [-1, -1] (default return value).
4. Initialize the start index of the search range as -1.
5. Initialize the end index of the search range as -1.
6. Start iterating through the input array.
7. If the current element is not the target, continue to the next element.
8. If the current element is not the target, continue to the next iteration.
9. If the start index of the search range is -1, set it to the current index.
10. Update the end index of the search range to the current index when a match is found.
11. If the start index is not -1, return the search range.
12. Return the search range containing the start and end indices of the target.
13. If no match was found, return the default [-1, -1] array.
14. Return the default search range [-1, -1] to indicate no match.

0 comments on commit 979d885

Please sign in to comment.