image_2021-10-28_03-22-49.png
26 KB
#N70. Climbing Stairs
problem link=>https://leetcode.com/problems/climbing-stairs/
#solution
problem link=>https://leetcode.com/problems/climbing-stairs/
#solution
class Solution {
public int climbStairs(int n) {
int fib=0, x=1, y=1;
for(int i=1; i<n; i++){
fib=y;
y=x+y;
x=fib;
}
return y;
}
}image_2021-10-29_01-38-15.png
30.7 KB
#N2037. Minimum Number of Moves to Seat Everyone
problem link=>https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
#solution
problem link=>https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
#solution
class Solution {
public int minMovesToSeat(int[] seats, int[] students) {
Arrays.sort(seats);
Arrays.sort(students);
int balance=0;
for(int i=0; i<seats.length; i++){
balance+=Math.abs(students[i]-seats[i]);
}
return balance;
}
}image_2021-10-29_01-40-43.png
37.5 KB
#N905. Sort Array By Parity
problem link=>https://leetcode.com/problems/sort-array-by-parity/
#solution
problem link=>https://leetcode.com/problems/sort-array-by-parity/
#solution
class Solution {
public int[] sortArrayByParity(int[] nums) {
int temp=0;
for(int i=0; i<nums.length-1; i++){
for(int j=0; j<nums.length-i-1; j++){
if(nums[j]%2==1&&nums[j+1]%2==0){
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
return nums;
}
}image_2021-10-29_01-46-19.png
24.2 KB
#N561. Array Partition I
problem link=>https://leetcode.com/problems/array-partition-i/
#solution
problem link=>https://leetcode.com/problems/array-partition-i/
#solution
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum=0;
for(int i=0; i<nums.length; i=i+2){
sum+=nums[i];
}
return sum;
}
}image_2021-10-29_14-53-36.png
39.9 KB
#N704. Binary Search
problem link=>https://leetcode.com/problems/binary-search/
#solution
problem link=>https://leetcode.com/problems/binary-search/
#solution
class Solution {
public int search(int[] nums, int target) {
int left=0, right=nums.length-1, mid;
while(left<=right){
mid=(left+right)/2;
if(nums[mid]<target){
left=mid+1;
}
if(nums[mid]>target){
right=mid-1;
}
if(nums[mid]==target){
return mid;
}
}
return -1;
}
}
‼️Implementation of Bineary Search to the problemsimage_2021-10-29_15-02-21.png
33.8 KB
#N1346. Check If N and Its Double Exist
problem link=>https://leetcode.com/problems/check-if-n-and-its-double-exist/
#solution
problem link=>https://leetcode.com/problems/check-if-n-and-its-double-exist/
#solution
class Solution {
public boolean checkIfExist(int[] arr) {
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr.length; j++){
if(arr[i]==2*arr[j]&&i!=j)
return true;
}
}
return false;
}
}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
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
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
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
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;
}
}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
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
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
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
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
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
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
#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
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
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
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;
}
}