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

Let's Develop Together!
Download Telegram
image_2021-10-31_00-21-17.png
67.8 KB
#N1154. Day of the Year
problem link=>https://leetcode.com/problems/day-of-the-year/

#solution
class Solution {
public int dayOfYear(String date) {
int[] days={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year=Integer.parseInt(date.substring(0, 4));
int month=Integer.parseInt(date.substring(5, 7));
int isLeapYear=0;

int ans=0;

for(int i=0; i<month; i++){
ans+=days[i];
}

ans+=Integer.parseInt(date.substring(8, 10));

if(year%4!=0)
isLeapYear=0;
else if(year%100!=0)
isLeapYear=1;
else if(year%400!=0)
isLeapYear=0;
else
isLeapYear=1;

if(month<=2)
isLeapYear=0;

return ans+isLeapYear;
}
}
image_2021-10-31_02-54-09.png
52 KB
#N1556. Thousand Separator
problem link=>https://leetcode.com/problems/thousand-separator/

#solution
class Solution {
public String thousandSeparator(int n) {
String str=String.valueOf(n);
int xonalar=str.length();
String ans="";

if(xonalar<=3) return str;

if(xonalar>3&&xonalar%3!=0)
ans+=str.substring(0, xonalar%3)+".";

int index=xonalar%3+2;
while(index<=xonalar){
ans+=str.substring(index-2, index+1)+".";
index+=3;
}

return ans.substring(0, ans.length()-1);
}
}
image_2021-10-31_04-26-08.png
41.5 KB
#N1051. Height Checker
problem link=>https://leetcode.com/problems/height-checker/

#solution
class Solution {
public int heightChecker(int[] heights) {
int[] expected=new int[heights.length];
for(int i=0; i<expected.length; i++){
expected[i]=heights[i];
}
int count=0;

Arrays.sort(expected);

for(int i=0; i<heights.length; i++){
if(heights[i]!=expected[i])
count++;
}

return count;
}
}
image_2021-10-31_04-30-00.png
31.1 KB
#N1460. Make Two Arrays Equal by Reversing Sub-arrays
problem link=>https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/

#solution
class Solution {
public boolean canBeEqual(int[] target, int[] arr) {
Arrays.sort(target);
Arrays.sort(arr);

for(int i=0; i<arr.length; i++){
if(arr[i]!=target[i])
return false;
}
return true;
}
}
#statistics
Till this time, 54 in total,
51 easy;
3 medium;
type of problem solutions have been posted
image_2021-11-01_03-04-37.png
37.5 KB
#N1207. Unique Number of Occurrences
problem link=>https://leetcode.com/problems/unique-number-of-occurrences/

#solution
class Solution {
public boolean uniqueOccurrences(int[] arr) {
int[] occur=new int[2001];

for(int num:arr){
occur[1000+num]++;
}

Arrays.sort(occur);
for(int i=0; i<occur.length-1; i++){
if(occur[i]==occur[i+1]&&occur[i]!=0)
return false;
}

return true;
}
}
image_2021-11-01_03-24-50.png
49.5 KB
#N1337. The K Weakest Rows in a Matrix
problem link=>https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/

#solution
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
int[] soldiers=new int[mat.length];
int rows=mat.length;
int sum=0;
int[] ans=new int[k];

for(int i=0; i<rows; i++){
for(int j=0; j<mat[i].length; j++){
sum+=mat[i][j];
}

soldiers[i]=sum*rows+i;
sum=0;
}
Arrays.sort(soldiers);
int min=0;
for(int i=0; i<k; i++)
ans[i]=soldiers[i]%rows;

return ans;
}
}
image_2021-11-01_03-48-35.png
47.2 KB
#N1710. Maximum Units on a Truck
problem link=>https://leetcode.com/problems/maximum-units-on-a-truck/

#solution
class Solution {
public int maximumUnits(int[][] type, int tsize) {
Arrays.sort(type, (a, b) -> a[1] - b[1]);
int ans=0;

for(int i=type.length-1; i>=0; i--){
if(tsize>type[i][0]){
ans+=type[i][0]*type[i][1];
tsize-=type[i][0];
}
else{
ans+=tsize*type[i][1];
break;
}
}

return ans;
}
}
image_2021-11-01_03-55-22.png
40.9 KB
#N1281. Subtract the Product and Sum of Digits of an Integer
problem link=>https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

#solution
class Solution {
public int subtractProductAndSum(int n) {
int product=1;
int sum=0;
int temp=0;

while(n>0){
temp=n%10;
product*=temp;
sum+=temp;
n/=10;
}

return product-sum;

}
}
image_2021-11-01_03-58-29.png
35.9 KB
#N1342. Number of Steps to Reduce a Number to Zero
problem link=>https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/

#solution
class Solution {
public int numberOfSteps(int num) {
int steps=0;

while(num>0){
if(num%2==0)
num/=2;
else
num--;

steps++;
}

return steps;
}
}
image_2021-11-01_04-03-50.png
26.6 KB
#N1486. XOR Operation in an Array
problem link=>https://leetcode.com/problems/xor-operation-in-an-array/

#solution
class Solution {
public int xorOperation(int n, int start) {
int ans=0;

while(n-- > 0){
ans ^= start;
start+=2;
}

return ans;
}
}
image_2021-11-01_04-22-39.png
46.9 KB
#medium

#N1828. Queries on Number of Points Inside a Circle
problem link=>https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/

#solution
class Solution {
public int[] countPoints(int[][] point, int[][] circle) {
int insidePoints=0;
int[] arrOfInsidePoints=new int[circle.length];

for(int i=0; i<circle.length; i++){
for(int j=0; j<point.length; j++){
if(Math.pow(circle[i][0]-point[j][0], 2)+Math.pow(circle[i][1]-point[j][1], 2)<=Math.pow(circle[i][2], 2))
insidePoints++;
}

arrOfInsidePoints[i]=insidePoints;
insidePoints=0;
}

return arrOfInsidePoints;
}
}
image_2021-11-01_15-39-39.png
24 KB
#N1688. Count of Matches in Tournament
problem link=>https://leetcode.com/problems/count-of-matches-in-tournament/

#solution
class Solution {
public int numberOfMatches(int n) {
int matches=0;
while(n>1){
matches+=n/2;
n-=n/2;
}

return matches;
}
}
image_2021-11-01_17-03-45.png
28.1 KB
#N1812. Determine Color of a Chessboard Square
problem link=>https://leetcode.com/problems/determine-color-of-a-chessboard-square/

#solution
class Solution {
public boolean squareIsWhite(String coordinates) {
int letter=coordinates.charAt(0)-97;
int num=coordinates.charAt(1)-49;

if((letter+num)%2==0)
return false;
return true;
}
}
image_2021-11-01_17-30-46.png
34 KB
#N728. Self Dividing Numbers
problem link=>https://leetcode.com/problems/self-dividing-numbers/

#solution
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans=new ArrayList<>();

for (int i = left, num = 0; i <= right; i++) {
for (num = i; num > 0; num /= 10)
if (num % 10 == 0 || i % (num % 10) != 0)
break;

if (num == 0) ans.add(i);
}

return ans;
}
}
image_2021-11-01_18-04-03.png
48.2 KB
#N1837. Sum of Digits in Base K
problem link=>https://leetcode.com/problems/sum-of-digits-in-base-k/

#solution
class Solution {
public int sumBase(int n, int k) {
int reverseBase=convertToBaseK(n, k);
int sum=0;

for(; reverseBase>0; reverseBase/=10){
sum+=reverseBase%10;
}

return sum;
}

//function to convert to Base K
int convertToBaseK(int n, int k){
int reverseBase=0;
for(; n>0; n/=k){
reverseBase=reverseBase*10+n%k;
}

return reverseBase;
}
}
image_2021-11-01_18-31-53.png
25 KB
#N509. Fibonacci Number
problem link=>https://leetcode.com/problems/fibonacci-number/

#solution
class Solution {
public int fib(int n) {
return F(n);
}

int F(int n){
int sum=0;
if(n==0) return 0;
if(n==1) return 1;
return F(n-2)+F(n-1);
}
}
image_2021-11-02_15-23-58.png
52.8 KB
#N551. Student Attendance Record I
problem link=>https://leetcode.com/problems/student-attendance-record-i/

#solution
class Solution {
public boolean checkRecord(String s) {
char[] ch=s.toCharArray();
int isLate=1;
int isAbsent=0;

for(int i=0; i<ch.length; i++){
if(ch[i]=='A'){
isAbsent++;
}
}

for(int i=0; i<ch.length-1;i++){
if(ch[i]=='L'&&ch[i+1]=='L'){
isLate++;
if(isLate==3)
return false;
}
else if(ch[i]!='L'&&ch[i+1]=='L'){
isLate=1;
}
}

return isAbsent<2 && isLate<3;
}
}
image_2021-11-02_17-52-09.png
64.8 KB
#medium

#N969. Pancake Sorting
problem link=>https://leetcode.com/problems/pancake-sorting/

#solution
class Solution {
public List<Integer> pancakeSort(int[] arr) {
ArrayList<Integer> ans = new ArrayList<Integer>();
int indexMax=0, max=arr.length, temp=0;
for(int i=0; i<arr.length-1; i++){
for(int j=0; j<arr.length-i; j++){
if(arr[j]==max) indexMax=j+1;
}

if(arr[0]!=max){
for(int k = 0; k < indexMax / 2; k++){
temp = arr[k];
arr[k] = arr[indexMax - k - 1];
arr[indexMax - k - 1] = temp;
}
ans.add(indexMax);
}
ans.add(max);
for(int k = 0; k < max / 2; k++){
temp = arr[k];
arr[k] = arr[max - k - 1];
arr[max - k - 1] = temp;
}
max--;
}
return ans;
}
}
Leetcode in Java && Oracle
image_2021-11-02_17-52-09.png
p.s. finally, accepted😊, after 2-hour trying. even 100% runtime and the Medium type)
the best solution ever for me😅