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

Let's Develop Together!
Download Telegram
image_2022-02-26_13-01-47.png
33 KB
#medium

#N739. Daily Temperatures
problem link

#solution
        int[] res=new int[temp.length];
for(int i=0; i<temp.length-1; i++){
for(int j=i+1; j<temp.length; j++){
if(temp[i]<temp[j]){
res[i]=j-i;
break;
}
}
}
return res;
🤩1
image_2022-03-18_14-52-30.png
43.1 KB
#N2200. Find All K-Distant Indices in an Array
problem link

#solution
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
Set<Integer> set = new TreeSet<>();
int l=nums.length;

for(int i=0; i<l; i++){
if(nums[i]==key){
set.add(i);
for(int j=i-k; j<=i+k; j++){
if(j>-1 && j<l)
set.add(j);
}
}
}

return new ArrayList<Integer>(set);
}
}
👍1
image_2022-03-18_15-03-51.png
43.8 KB
#N2194. Cells in a Range on an Excel Sheet
problem link

#solution
class Solution {
public List<String> cellsInRange(String s) {
ArrayList<String> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();

for(int i=(int)(s.charAt(0)); i<=(int)(s.charAt(3)); i++){
for(int j=Integer.valueOf(s.charAt(1)-'0'); j<=Integer.valueOf(s.charAt(4)-'0'); j++){
sb = new StringBuilder(""+(char)i);
sb.append(j);
list.add(sb.toString());
}
}

return list;
}
}
image_2022-03-18_15-15-20.png
45 KB
#medium

#N1329. Sort the Matrix Diagonally
problem link

#solution
class Solution {
public int[][] diagonalSort(int[][] mat) {
int m = mat.length; //3
int n = mat[0].length; //4

for(int k = 0; k < Math.min(m, n) ; k++){
for(int i = 1; i < m; ++i){
for(int j = 1; j < n; ++j){
if(mat[i][j] < mat[i-1][j-1]){
int temp = mat[i-1][j-1];
mat[i-1][j-1] = mat[i][j];
mat[i][j] = temp;
}
}
}
}

return mat;
}
}
image_2022-03-18_19-34-33.png
63 KB
#medium

#N2161. Partition Array According to Given Pivot
problem link

#solution
class Solution {
public int[] pivotArray(int[] nums, int pivot) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
int count=0, temp=0;
for(int n: nums){
if(n<pivot){
list1.add(n);
}else if(n>pivot){
list2.add(n);
}else
count++;
}
temp=count;
int res[] = new int[nums.length];
for(int i=0; i<res.length; i++){
if(i<list1.size()){
res[i]=list1.get(i);
}else if(i>=list1.size() &&i<list1.size()+count){
while(count-- >0)
res[i++]=pivot;
i--;
}else
res[i]=list2.get(i-list1.size()-temp);
}
return res;
}
}
image_2022-03-18_19-39-07.png
30.6 KB
#N2176. Count Equal and Divisible Pairs in an Array
problem link

#solution
class Solution {
public int countPairs(int[] nums, int k) {
int count=0;
for(int i=0; i<nums.length-1; i++){
for(int j=i+1; j<nums.length; j++){
if(nums[i]==nums[j] && i*j%k==0)
count++;
}
}

return count;
}
}
image_2022-03-19_01-08-21.png
51.2 KB
#medium

#N1630. Arithmetic Subarrays
problem link

#solution
class Solution {
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
List<Boolean> list = new ArrayList<>();

for(int i=0; i<l.length; i++){
int temp[] = new int[r[i]-l[i]+1];
for(int j=l[i]; j<=r[i]; j++){
temp[j-l[i]]=nums[j];
}
Arrays.sort(temp);
list.add(check(temp));
}

return list;
}
public boolean check(int[] arr){
for(int i=1; i<arr.length-1; i++){
if(arr[i]*2!=arr[i-1]+arr[i+1])
return false;
}

return true;
}
}
👍2
image_2022-03-19_01-13-18.png
27.8 KB
#N2185. Counting Words With a Given Prefix
problem link

#solution
class Solution {
public int prefixCount(String[] words, String pref) {
int count=0;

for(String word: words){
if(word.startsWith(pref))
count++;
}

return count;
}
}
🔥2
image_2022-03-19_15-14-53.png
30 KB
#N2169. Count Operations to Obtain Zero
problem link

#solution
class Solution {
public int countOperations(int num1, int num2) {
int count=0;
while(num1*num2!=0){
if(num1<num2)
num2-=num1;
else
num1-=num2;

count++;
}

return count;
}
}
image_2022-03-22_14-30-43.png
23.4 KB
#N1523. Count Odd Numbers in an Interval Range
problem link

#solution
class Solution {
public int countOdds(int low, int high) {
return low % 2 == 0 && high % 2 == 0 ? (high-low)/2: (high-low)/2+1;
}
}
image_2022-03-22_14-39-24.png
30 KB
#N191. Number of 1 Bits
problem link

#solution
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count=0;
while(n!=0){
if((n&1)==1)
count++;

n=n>>>1;
}

return count;
}
}
image_2022-03-22_15-00-24.png
41.4 KB
#N202. Happy Number
problem link

#solution
class Solution {
public boolean isHappy(int n) {
int count=6;
while(count-- >0){
if(convert(n)!=1)
n=convert(n);
else
return true;
}
return false;
}
public int convert(int num){
int sum=0;
while(num>0){
sum+=Math.pow(num%10, 2);
num/=10;
}

return sum;
}
}
image_2022-03-22_15-25-51.png
39.5 KB
#N1790. Check if One String Swap Can Make Strings Equal
problem link

#solution
class Solution {
public boolean areAlmostEqual(String s1, String s2) {
List<Integer> l = new ArrayList<>();
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) l.add(i);
if (l.size() > 2) return false;
}
return l.size() == 0 || (l.size() == 2 && s1.charAt(l.get(0)) == s2.charAt(l.get(1)) && s1.charAt(l.get(1)) == s2.charAt(l.get(0)));
}
}
image_2022-03-22_16-13-42.png
32.9 KB
#N303. Range Sum Query - Immutable
problem link

#solution
class NumArray {
int nums[];
public NumArray(int[] nums) {
this.nums=nums;
}

public int sumRange(int left, int right) {
int sum=0;
for(int i=left; i<=right; i++)
sum+=nums[i];

return sum;
}
}
image_2022-03-25_16-11-14.png
21.9 KB
#N1757. Recyclable and Low Fat Products
problem link

#solution
select product_id from products
where low_fats='Y' and recyclable='Y';
image_2022-03-28_17-12-32.png
22.1 KB
#N1693. Daily Leads and Partners
problem link

#solution
SELECT date_id, make_name, COUNT(DISTINCT lead_id) AS unique_leads, COUNT(DISTINCT partner_id) AS unique_partners
FROM DailySales group by date_id, make_name;
image_2022-03-28_17-18-13.png
21.5 KB
#N1741. Find Total Time Spent by Each Employee
problem link

#solution
select event_day as day, emp_id, (sum(out_time) - sum(in_time)) as total_time
from employees group by emp_id, day;
image_2022-03-28_17-43-22.png
21 KB
#N1873. Calculate Special Bonus
problem link

#solution
SELECT employee_id, (
CASE
WHEN employee_id%2=1 AND name NOT LIKE 'M%' THEN salary
ELSE 0
END
) AS bonus FROM employees;
image_2022-03-28_19-56-30.png
22.4 KB
#medium

#N1393. Capital Gain/Loss
problem link

#solution
select stock_name, sum(
case
when operation = 'Buy' then -price
else price
end
) as capital_gain_loss from stocks
group by stock_name
image_2022-03-29_17-58-36.png
27 KB
#medium

#N287. Find the Duplicate Number
problem link

#solution
class Solution {
public int findDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int n: nums){
if(!set.contains(n))
set.add(n);
else
return n;
}

return -1;
}
}
image_2022-03-30_15-50-07.png
42 KB
#medium

#N74. Search a 2D Matrix
problem link

#solution
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r=matrix.length, c = matrix[0].length;
int start=0, end=r*c-1, mid=(start+end)/2;

while(start<=end){
if(matrix[mid/c][mid%c]>target)
end=mid-1;
else if(matrix[mid/c][mid%c]<target)
start=mid+1;
else
return true;

mid=(start+end)/2;
}

return false;
}
}