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

Let's Develop Together!
Download Telegram
image_2021-12-06_13-34-12.png
26.8 KB

#N766. Toeplitz Matrix
problem link

#solution
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
for(int i=0; i<matrix.length-1; i++){
for(int j=0; j<matrix[i].length-1; j++){
if(matrix[i][j]!=matrix[i+1][j+1]) return false;
}
}

return true;
}
}
image_2021-12-06_13-47-38.png
25.4 KB
#N908. Smallest Range I
problem link

#solution
class Solution {
public int smallestRangeI(int[] nums, int k) {
int max=nums[0], min=nums[0];
for(int n:nums){
max = Math.max(max, n);
min = Math.min(min, n);
}
return Math.max(max-min-2*k, 0);
}
}
image_2021-12-06_14-34-59.png
51.5 KB
#N1385. Find the Distance Value Between Two Arrays
problem link

#solution
class Solution {
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
int res=0, l=arr2.length;

Arrays.sort(arr1); Arrays.sort(arr2);
for(int n:arr1){
int count=0, start=0, end=l-1;
while(start<=end){
int mid=(start+end)/2;
if(Math.abs(arr2[mid]-n)<=d) {
count++;
break;
}
if(arr2[mid]>n+d) end=mid-1;
if(arr2[mid]<n-d) start=mid+1;

}
if(count==0) res++;
}

return res;
}
}
image_2021-12-06_23-38-19.png
26.5 KB
#N575. Distribute Candies
problem link

#solution
class Solution {
public int distributeCandies(int[] candyType) {
Set<Integer> set = new HashSet<>();

for(int candy:candyType)
set.add(candy);

return Math.min(set.size(), candyType.length/2);
}
}
image_2021-12-06_23-50-19.png
51.2 KB
#N1800. Maximum Ascending Subarray Sum
problem link

#solution
class Solution {
public int maxAscendingSum(int[] nums) {
List<Integer> sumlist = new ArrayList<>();
int sum=0, i=0;

for(; i<nums.length-1; i++){
if(nums[i]<nums[i+1]){
sum+=nums[i];
}else{
sum+=nums[i];
sumlist.add(sum);
sum=0;
}
}
sum+=nums[i];
sumlist.add(sum);

int max=sumlist.get(0);
for(int n:sumlist)
max=Math.max(max, n);

return max;
}
}
image_2021-12-07_00-24-22.png
46.9 KB
#N1582. Special Positions in a Binary Matrix
problem link

#solution
class Solution {
public int numSpecial(int[][] mat) {
int count=0, row[]=new int[mat.length], col[]=new int[mat[0].length];

for(int i=0; i<mat.length; i++)
for(int j=0; j<mat[i].length; j++)
if(mat[i][j]==1){
row[i]++;
col[j]++;
}

for(int i=0; i<mat.length; i++)
for(int j=0; j<mat[i].length; j++)
if(mat[i][j]==1 && row[i] == 1 && col [j] == 1)
count++;

return count;
}
}
image_2021-12-07_00-39-22.png
33.5 KB
#N1619. Mean of Array After Removing Some Elements
problem link

#solution
class Solution {
public double trimMean(int[] arr) {
Arrays.sort(arr);

for(int i=0; i<arr.length/20; i++)
arr[i]=arr[arr.length-i-1]=0;

int sum=0;
for(int n:arr)
sum+=n;

return (double)sum/(arr.length*9/10);
}
}
image_2021-12-07_00-42-27.png
23.6 KB
#N1550. Three Consecutive Odds
problem link

#solution
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
for(int i=0; i<arr.length-2; i++)
if(arr[i]*arr[i+1]*arr[i+2] % 2 == 1) return true;

return false;
}
}
image_2021-12-07_00-48-42.png
27.5 KB
#N1598. Crawler Log Folder
problem link

#solution
class Solution {
public int minOperations(String[] logs) {
int res = 0;
for (String s : logs) {
if (s.equals("../")) res = Math.max(0, --res);
else if (s.equals("./")) continue;
else res++;
}
return res;
}
}
runtime.png
12.4 KB
#N706. Design HashMap
problem link

#solution
class MyHashMap {
public ListNode[] nodes = new ListNode[10000];
public MyHashMap() {

}

public void put(int key, int value) {
int index = key%nodes.length;
if(nodes[index]==null){
nodes[index] = new ListNode(-1, -1);
}
ListNode prev=find(nodes[index], key);
if(prev.next==null)
prev.next=new ListNode(key, value);
else
prev.next.val = value;
}

public int get(int key) {
int index = key%nodes.length;
if(nodes[index] == null) return -1;
ListNode prev = find(nodes[index], key);

if(prev.next == null) return -1;
return prev.next.val;
}

public void remove(int key) {
int index = key%nodes.length;
if(nodes[index] == null) return;
ListNode prev = find(nodes[index], key);
if(prev.next == null) return;
prev.next=prev.next.next;
}
CarbonNowShBot.png
327.8 KB
    ListNode find(ListNode node, int key){
ListNode head = node, prev=null;
while(head!=null && head.key !=key){
prev=head;
head=head.next;
}
return prev;
}

class ListNode{
int key, val;
ListNode next;
public ListNode(int key, int val){
this.key = key;
this.val = val;
}
}
}
image_2021-12-07_01-45-54.png
61.9 KB
#N1394. Find Lucky Integer in an Array
problem link

#solution
class Solution {
public int findLucky(int[] arr) {
int[] count = new int[501];

for(int i: arr)
count[i]++;

for(int i=count.length-1; i>=0; i--){
if(i==0) return -1;
if(count[i]==i) return i;
}

return -1;
}
}
Leetcode in Java && Oracle
image_2021-12-07_01-45-54.png
P.S.: another approach with HashMap‼️
        int res=-1;
Map<Integer, Integer> map = new HashMap<>();

for(int n: arr)
map.put(n, map.getOrDefault(n, 0) +1);

for(Map.Entry<Integer, Integer> entry: map.entrySet())
if(entry.getKey() == entry.getValue()) res = Math.max(res, entry.getValue());

return res;
image_2021-12-07_02-23-04.png
63.8 KB
#N1260. Shift 2D Grid
problem link

#solution
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
List<List<Integer>> list = new ArrayList<>();
int m=grid.length, n=grid[0].length;
while(k-- > 0){
int temp=grid[m-1][n-1];
for(int i=m-1; i>=0; i--){
for(int j=n-1; j>0; j--){
if(i==0 && j==0) break;
grid[i][j]=grid[i][j-1];
}
if(i>0) grid[i][0]=grid[i-1][n-1];
}
grid[0][0]=temp;
}

for(int i=0; i<m; i++){
List<Integer> sublist = new ArrayList<>();
for(int j=0; j<n; j++){
sublist.add(grid[i][j]);
}
list.add(sublist);
}

return list;
}
}
image_2021-12-07_11-31-49.png
26.9 KB
#N566. Reshape the Matrix
problem link

#solution
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int[][] res = new int[r][c];
int m = mat.length, n = mat[0].length;
if(r*c != m*n) return mat;
for(int i=0; i<r*c; i++)
res[i/c][i%c] = mat[i/n][i%n];

return res;
}
}
image_2021-12-07_11-40-43.png
21.3 KB
#N169. Majority Element
problem link

#solution
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
image_2021-12-07_13-28-33.png
36.4 KB
#medium

#N2079. Watering Plants
problem link

#solution
class Solution {
public int wateringPlants(int[] plants, int capacity) {
int steps=0, temp=capacity;
for(int i=0; i<plants.length; i++){
if(plants[i]<=temp){
steps++;
temp-=plants[i];
}else{
temp=capacity-plants[i];
steps+=2*i+1;
}
}

return steps;
}
}
image_2021-12-07_13-47-04.png
28.8 KB
#N2022. Convert 1D Array Into 2D Array
problem link

#solution
class Solution {
public int[][] construct2DArray(int[] original, int m, int n) {
int[][] res = new int[m][n];
if(m*n != original.length) return new int[0][0];

for(int i=0; i<m*n; i++)
res[i/n][i%n] = original[i];

return res;
}
}
image_2021-12-07_13-57-01.png
25.5 KB
#N867. Transpose Matrix
problem link

#solution
class Solution {
public int[][] transpose(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] res = new int[n][m];

for(int i=0; i<m*n; i++)
res[i%n][i/n] = matrix[i/n][i%n];

return res;
}
}
image_2021-12-07_17-12-39.png
33.8 KB
#N1437. Check If All 1's Are at Least Length K Places Away
problem link

#solution
class Solution {
public boolean kLengthApart(int[] nums, int k) {
int temp=k;

for(int i=0; i<nums.length; i++){
if(nums[i]==1){
if(temp<k) return false;
temp=0;
}else
temp++;
}

return true;
}
}