image_2021-10-15_22-58-01.png
40.5 KB
#N1588 Sum of All Odd Length Subarrays
problem link=>https://leetcode.com/problems/sum-of-all-odd-length-subarrays/
#solution
problem link=>https://leetcode.com/problems/sum-of-all-odd-length-subarrays/
#solution
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int res=0;
for(int i=0; i<arr.length; i++){
int start=arr.length-i;
int end=i+1;
if(start*end%2==1)
res+=(start*end+1)/2 * arr[i];
else
res+=(start*end)/2 * arr[i];
}
return res;
}
}image_2021-10-15_23-08-17.png
43 KB
#N1662 Check If Two String Arrays are Equivalent
problem link=>https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
#solution
problem link=>https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
#solution
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
String str1="", str2="";
for(int i=0; i<Math.max(word1.length, word2.length); i++){
if(i<word1.length)
str1+=word1[i];
if(i<word2.length)
str2+=word2[i];
}
return str1.equals(str2);
}
}image_2021-10-15_23-44-42.png
47.2 KB
#N1684 Count the Number of Consistent Strings
problem link=>https://leetcode.com/problems/count-the-number-of-consistent-strings/
#solution
problem link=>https://leetcode.com/problems/count-the-number-of-consistent-strings/
#solution
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int count = 0;
for (String s : words) {
boolean isValid = true;
for (char ch : s.toCharArray()) {
if (!allowed.contains(String.valueOf(ch))) {
isValid = false;
}
}
if (isValid) count++;
}
return count;
}
}image_2021-10-15_23-57-39.png
55.5 KB
#N1913 Maximum Product Difference Between Two Pairs
problem link=>https://leetcode.com/problems/maximum-product-difference-between-two-pairs/
#solution
problem link=>https://leetcode.com/problems/maximum-product-difference-between-two-pairs/
#solution
class Solution {
public int maxProductDifference(int[] nums) {
int largest = 0, largest2 = 0, smallest = Integer.MAX_VALUE, smallest2 = Integer.MAX_VALUE;
for(int i=0;i<nums.length;i++) {
if(nums[i]>=largest) {
largest2 = largest;
largest = nums[i];
} else if(nums[i]>largest2) largest2 = nums[i];
if(nums[i]<=smallest) {
smallest2 = smallest;
smallest = nums[i];
} else if(nums[i]<smallest2) smallest2 = nums[i];
}
return largest * largest2 - smallest * smallest2;
}
}image_2021-10-16_00-24-46.png
39.4 KB
#N1816 Truncate Sentence
problem link=>https://leetcode.com/problems/truncate-sentence/
#solution
problem link=>https://leetcode.com/problems/truncate-sentence/
#solution
class Solution {
public String truncateSentence(String s, int k) {
s=s+" ";
String res="";
int start=0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)==' '&&k>0){
res+=s.substring(start, i);
start=i;
k--;
}
}
return res;
}
}image_2021-10-16_01-22-40.png
43 KB
#N1534 Count Good Triplets
problem link=>https://leetcode.com/problems/count-good-triplets/
#solution
problem link=>https://leetcode.com/problems/count-good-triplets/
#solution
class Solution {
public int countGoodTriplets(int[] arr, int a, int b, int c) {
int count=0;
for(int i=0; i<arr.length-2; i++){
for(int j=i+1; j<arr.length-1; j++){
if(Math.abs(arr[i]-arr[j])>a) continue;
for(int k=j+1; k<arr.length; k++){
if(Math.abs(arr[j]-arr[k])<=b&&Math.abs(arr[i]-arr[k])<=c)
count ++;
}
}
}
return count;
}
}image_2021-10-16_01-33-18.png
37.7 KB
#N1979 Find Greatest Common Divisor of Array
problem link=>https://leetcode.com/problems/find-greatest-common-divisor-of-array/
#solution
problem link=>https://leetcode.com/problems/find-greatest-common-divisor-of-array/
#solution
class Solution {
public int findGCD(int[] nums) {
int min=1000, max=1, gcd=1;
for(int eachNums: nums){
max=Math.max(max, eachNums);
min=Math.min(min, eachNums);
}
for(int i=1; i<=min; i++){
if(min%i==0&&max%i==0)
gcd=i;
}
return gcd;
}
}image_2021-10-16_17-04-24.png
60.9 KB
#N804 Unique Morse Code Words
problem link=>https://leetcode.com/problems/unique-morse-code-words/
#solution
problem link=>https://leetcode.com/problems/unique-morse-code-words/
#solution
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] morseAlphabet={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
String morse="";
for(int i=0; i<words.length; i++){
morse="";
for(char ch:words[i].toCharArray()){
morse+=morseAlphabet[ch-'a'];
}
words[i]=morse;
}
Arrays.sort(words);
int count=words.length;
for(int i=0; i<words.length-1; i++){
if(words[i].equals(words[i+1]))
count--;
}
return count;
}
}image_2021-10-16_17-33-46.png
33.2 KB
#N1266 Minimum Time Visiting All Points
problem link=>https://leetcode.com/problems/minimum-time-visiting-all-points/
#solution
problem link=>https://leetcode.com/problems/minimum-time-visiting-all-points/
#solution
class Solution {
public int minTimeToVisitAllPoints(int[][] points) {
int time=0;
for(int i=0; i<points.length-1; i++){
int max=Math.max(Math.abs(points[i][0]-points[i+1][0]), Math.abs(points[i][1]-points[i+1][1]));
time+=max;
}
return time;
}
}image_2021-10-16_17-37-39.png
30.3 KB
#N832 Flipping an Image
problem link=>https://leetcode.com/problems/flipping-an-image/
#solution
problem link=>https://leetcode.com/problems/flipping-an-image/
#solution
class Solution {
public int[][] flipAndInvertImage(int[][] image) {
int n = image.length;
for (int[] row : image)
for (int i = 0; i * 2 < n; i++)
if (row[i] == row[n - i - 1])
row[i] = row[n - i - 1] ^= 1;
return image;
}
}image_2021-10-16_18-07-09.png
32.6 KB
#N1732 Find the Highest Altitude
problem link=>https://leetcode.com/problems/find-the-highest-altitude/
#solution
problem link=>https://leetcode.com/problems/find-the-highest-altitude/
#solution
class Solution {
public int largestAltitude(int[] gain) {
int max=0, altitude=0;
for(int i=0; i<gain.length; i++){
altitude+=gain[i];
gain[i]=altitude;
max=Math.max(max, gain[i]);
}
return max;
}
}image_2021-10-17_00-01-49.png
44.3 KB
#N1725 Number Of Rectangles That Can Form The Largest Square
problem link=>https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/
#solution
problem link=>https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/
#solution
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int max=0, count=0, side;
for(int i=0; i<rectangles.length; i++){
side=Math.min(rectangles[i][0], rectangles[i][1]);
if(side>max){
count=1;
max=side;
}else if(side==max)
count++;
}
return count;
}
}image_2021-10-17_00-31-15.png
64.1 KB
#N1252. Cells with Odd Values in a Matrix
problem link=>https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/
#solution
problem link=>https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/
#solution
class Solution {
public int oddCells(int m, int n, int[][] indices) {
int[][] matrix=new int[m][n];
int row, col, count=0;
for(int k=0; k<indices.length; k++){
row=indices[k][0];
col=indices[k][1];
for(int i=0; i<n; i++){
matrix[row][i]++;
}
for(int j=0; j<m; j++){
matrix[j][col]++;
}
}
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++){
if(matrix[i][j]%2==1) count++;
}
}
return count;
}
}image_2021-10-17_00-54-15.png
33.6 KB
#N1572 Matrix Diagonal Sum
problem link=>https://leetcode.com/problems/matrix-diagonal-sum/
#solution
problem link=>https://leetcode.com/problems/matrix-diagonal-sum/
#solution
class Solution {
public int diagonalSum(int[][] mat) {
int sum=0;
for(int i=0, j=0; i<mat.length; i++, j++){
sum+=mat[i][j]+mat[i][mat.length-j-1];
}
if(mat.length%2==1){
sum-=mat[(mat.length-1)/2][(mat.length-1)/2];
}
return sum;
}
}image_2021-10-18_14-33-42.png
51.8 KB
#N1295. Find Numbers with Even Number of Digits
problem link=>https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
#solution
problem link=>https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
#solution
class Solution {
public int findNumbers(int[] nums) {
int count=0;
int x=0;
for(int i=0; i<nums.length; i++){
x=divide100(nums[i]);
if(x==0)
count++;
}
return count;
}
static int divide100(int x){
for(; x>=0; x=x/100){
if(x<=9)
return x;
}
return 1;
}
}image_2021-10-18_14-59-46.png
45.2 KB
#N1464. Maximum Product of Two Elements in an Array
problem link=>https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
#solution
P.s. The code of Bubble sort is just written in the comments for note)
problem link=>https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
#solution
class Solution {
public int maxProduct(int[] nums) {
/*int temp, n=nums.length; //Bubble Sort
for(int i=1; i<n; i++){
for(int j=0; j<n-i; j++){
if(nums[j]>nums[j+1]){
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}*/
Arrays.sort(nums);
return (nums[nums.length-2]-1)*(nums[nums.length-1]-1);
}
}
‼️P.s. I simply used the sort() method of the Arrays class to sort array rather than using Bubble sort. Because I checked that sort() method is more efficent than Bubble sort.P.s. The code of Bubble sort is just written in the comments for note)
image_2021-10-18_15-20-14.png
32.8 KB
#N1450. Number of Students Doing Homework at a Given Time
problem link=>https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/
#solution
problem link=>https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/
#solution
class Solution {
public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
int count=0;
for(int i=0; i<startTime.length; i++){
if(startTime[i]<=queryTime&&endTime[i]>=queryTime)
count++;
}
return count;
}
}image_2021-10-18_15-30-00.png
28.4 KB
#N1304. Find N Unique Integers Sum up to Zero
problem link=>https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/
#solution
problem link=>https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/
#solution
class Solution {
public int[] sumZero(int n) {
int[] arr=new int[n];
for(int i=0; i<n/2; i++){
arr[i]=i+1;
arr[n-i-1]=-i-1;
}
return arr;
}
}image_2021-10-20_03-07-25.png
36.8 KB
#N2011. Final Value of Variable After Performing Operations
problem link=>https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
#solution
problem link=>https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
#solution
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x=0;
for(int i=0; i<operations.length; i++){
if(operations[i].contains("++")){
x++;
}
if(operations[i].contains("--")){
x--;
}
}
return x;
}
}image_2021-10-20_03-09-19.png
29 KB
#N1480. Running Sum of 1d Array
problem link=>https://leetcode.com/problems/running-sum-of-1d-array/
#solution
problem link=>https://leetcode.com/problems/running-sum-of-1d-array/
#solution
class Solution {
public int[] runningSum(int[] nums) {
int[] ans=new int[nums.length];
int sum=0;
for(int i=0; i<nums.length; i++){
sum=sum+nums[i];
ans[i]=sum;
}
return ans;
}
}