Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
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
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
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
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
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
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
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
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
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
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
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
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;
}
}
image_2021-10-20_03-12-01.png
35.7 KB
#N1672. Richest Customer Wealth
problem link=>https://leetcode.com/problems/richest-customer-wealth/

#solution
class Solution {
public int maximumWealth(int[][] accounts) {
int max=0 , sum=0;
for(int i=0; i<accounts.length; i++){
for(int j=0; j<accounts[i].length; j++){
sum=sum+accounts[i][j];
max=Math.max(max, sum);
}
sum=0;
}
return max;
}
}
image_2021-10-20_09-13-49.png
45.9 KB
#medium

#N1347. Minimum Number of Steps to Make Two Strings Anagram
problem link=>https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/

#solution
class Solution {
public int minSteps(String s, String t) {
int[] arr=new int[26];
int sum=0;

for(int i=0; i<t.length(); i++){
arr[s.charAt(i)-'a']++;
arr[t.charAt(i)-'a']--;
}

for(int i=0; i<26; i++){
if(arr[i]>0)
sum+=arr[i];
}

return sum;
}
}
image_2021-10-23_16-26-06.png
49.8 KB
#medium

#N1605. Find Valid Matrix Given Row and Column Sums
problem link=>https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/

#solution
class Solution {
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
int rowLen = rowSum.length;
int colLen = colSum.length;

int[][] ans = new int[rowLen][colLen];

int row = 0, col = 0;

while (row < rowLen && col < colLen) {
int min = Math.min(colSum[col], rowSum[row]);
ans[row][col] = min;
colSum[col] -= min;
rowSum[row] -= min;

if (rowSum[row] == 0)
row++;
if (colSum[col] == 0)
col++;
}

return ans;
}
}
image_2021-10-23_16-35-57.png
24.1 KB
#N1470. Shuffle the Array
problem link=>https://leetcode.com/problems/shuffle-the-array/

#solution
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans=new int[n*2];
for(int i=0; i<n; i++){
ans[i*2]=nums[i];
ans[i*2+1]=nums[i+n];
}
return ans;
}
}
image_2021-10-23_16-51-13.png
28.5 KB
#N1512. Number of Good Pairs
problem link=>https://leetcode.com/problems/number-of-good-pairs/

#solution
class Solution {
public int numIdenticalPairs(int[] nums) {
int good=0;
for(int i=0; i<nums.length-1; i++){
for(int j=i+1; j<nums.length; j++){
if(nums[i]==nums[j])
good++;
}
}

return good;
}
}
image_2021-10-23_16-53-56.png
38.9 KB
#N1365. How Many Numbers Are Smaller Than the Current Number
problem link=>https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

#solution
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int[] ans=new int[nums.length];
int count=0;
for(int i=0; i<nums.length; i++){
for(int j=0; j<nums.length; j++){
if(nums[i]>nums[j]&&j!=i){
count++;
}
}
ans[i]=count;
count=0;
}
return ans;
}
}
image_2021-10-25_15-28-15.png
49.6 KB
#medium

#N1395. Count Number of Teams
problem link=>https://leetcode.com/problems/count-number-of-teams/

#solution
class Solution {
public int numTeams(int[] rating) {
int res = 0;

for (int i = 1; i < rating.length - 1; ++i) {

int less[] = new int[2], greater[] = new int[2];

for (int j = 0; j < rating.length; ++j) {

if (rating[i] < rating[j])
++less[j > i ? 1 : 0];

if (rating[i] > rating[j])
++greater[j > i ? 1 : 0];
}

res += less[0] * greater[1] + greater[0] * less[1];
}

return res;
}
}
image_2021-10-25_15-37-39.png
29.8 KB
#N1961. Check If String Is a Prefix of Array
problem link=>https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/

#solution
class Solution {
public boolean isPrefixString(String s, String[] words) {
String allWords="";

for(int i=0; i<words.length; i++){
allWords+=words[i];
if(allWords.equals(s))
return true;
}
return false;
}
}
image_2021-10-26_02-13-29.png
38.2 KB
#N1389. Create Target Array in the Given Order
problem link=>https://leetcode.com/problems/create-target-array-in-the-given-order/

#solution
class Solution {
public int[] createTargetArray(int[] nums, int[] index) {
int[] target = new int[nums.length];
int i = 0, k = 0;

while (i < index.length) {

for (k = target.length - 1; k > index[i]; k--)
target[k] = target[k - 1];

target[index[i]] = nums[i];
i++;
}

return target;
}
}
‼️P.s. Without ArrayLists. It would be simple and no-logic with ArrayLists