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

Let's Develop Together!
Download Telegram
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;
}
}
image_2021-12-09_02-41-10.png
41.7 KB
#N888. Fair Candy Swap
problem link

#solution
class Solution {
public int[] fairCandySwap(int[] a, int[] b) {
int sumA = 0, sumB = 0;
for(int n: a) sumA+=n;
for(int n: b) sumB+=n;

int delta = (sumB - sumA) / 2;

Set<Integer> set = new HashSet<>();
for(int n: b) set.add(n);

for(int n: a)
if(set.contains(n+delta)) return new int[]{n, n+delta};

return new int[0];
}
}
image_2021-12-09_02-58-54.png
28.4 KB
#N283. Move Zeroes
problem link

#solution
class Solution {
public void moveZeroes(int[] nums) {
int index = 0;

for (int n: nums)
if(n != 0)
nums[index++] = n;

while(index<nums.length)
nums[index++] = 0;

}
}
image_2021-12-09_03-06-43.png
28.7 KB
#N1848. Minimum Distance to the Target Element
problem link

#solution
class Solution {
public int getMinDistance(int[] nums, int target, int start) {
int ans = Integer.MAX_VALUE;

for(int i=0; i<nums.length; i++){
if(nums[i] == target)
ans = Math.min(ans, Math.abs(i - start));
}

return ans;
}
}
image_2021-12-09_03-19-58.png
27.5 KB
#N1287. Element Appearing More Than 25% In Sorted Array
problem link

#solution
class Solution {
public int findSpecialInteger(int[] arr) {
int length = arr.length, quart = length/4;

for(int i=0; i<length - quart; i++)
if(arr[i] == arr[i+quart]) return arr[i];

return -1;
}
}
image_2021-12-10_00-32-27.png
75.3 KB
#N748. Shortest Completing Word
problem link

#solution
Map<Character, Integer> map=new HashMap<>();
int minLength=Integer.MAX_VALUE;
String result = null;
for(char ch: licensePlate.toLowerCase().toCharArray())
if(Character.isLetter(ch))map.put(ch, map.getOrDefault(ch, 0) +1);
for(String word:words){
System.out.println(word);
Map<Character, Integer> temp = new HashMap<>(map);
int count = temp.size();
for(char ch:word.toCharArray())
if(temp.containsKey(ch))temp.put(ch, temp.get(ch) -1);

for(Map.Entry<Character, Integer> entry: temp.entrySet()){
if(entry.getValue()>0) break;
count--;
}
if(count<=0&&minLength>word.length()){
result=word;
minLength=word.length();
}
}
return result;
image_2021-12-10_00-33-02.png
47.5 KB
#N1629. Slowest Key
problem link

#solution
class Solution {
public char slowestKey(int[] releaseTimes, String str) {
int max=releaseTimes[0], ix=0;

for(int i=1; i<releaseTimes.length; i++){
if(max<=releaseTimes[i]-releaseTimes[i-1]){
if(max==releaseTimes[i]-releaseTimes[i-1]&&str.charAt(i)>str.charAt(ix)){
ix=i;
//break;
}else if(max<releaseTimes[i]-releaseTimes[i-1]){
max=releaseTimes[i]-releaseTimes[i-1];
ix=i;}
}
}

return str.charAt(ix);
}
}
image_2021-12-10_00-33-29.png
38.1 KB
#N217. Contains Duplicate
problem link

#solution
class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null){
return false;
}

int len = nums.length;
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0; i < len; i++){
if(!set.add(nums[i])){
return true;
}
}
return false;

}
}
image_2021-12-10_00-33-55.png
37.7 KB
#N896. Monotonic Array
problem link

#solution
class Solution {
public boolean isMonotonic(int[] nums) {
int countI=0, countD=0, l=nums.length;
for(int i=0; i<l-1; i++){
if(nums[i] == nums[i+1]){
countI++;
countD++;
}
else if(nums[i] < nums[i+1]) countI++;
else countD++;
}

return countI == l-1 || countD == l-1;
}
}
image_2021-12-10_00-49-07.png
39.1 KB
#N1331. Rank Transform of an Array
problem link

#solution
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] helper = Arrays.copyOf(arr, arr.length);
Arrays.sort(helper);
Map<Integer, Integer> rank = new HashMap<>();
int pos=1;
for(int n: helper)
if(rank.getOrDefault(n, 0) == 0) rank.put(n, rank.size()+1);

for(int i=0; i<arr.length; i++)
arr[i] = rank.get(arr[i]);

return arr;
}
}
image_2021-12-10_01-25-15.png
43.5 KB
#N1560. Most Visited Sector in a Circular Track
problem link

#solution
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
int l=rounds.length;
List<Integer> list = new ArrayList<>();

for(int i=rounds[0]; i<=rounds[l-1]; i++)
list.add(i);

if(list.size()>0) return list;

for(int i=1; i<=rounds[l-1]; i++)
list.add(i);
for(int i=rounds[0]; i<=n; i++)
list.add(i);

return list;
}
}
image_2021-12-10_01-26-42.png
30.4 KB
#N167. Two Sum II - Input Array Is Sorted
problem link

#solution
class Solution {
public int[] twoSum(int[] numbers, int target) {
int start = 0, end = numbers.length - 1;
while(start < end){
if(numbers[start] + numbers[end] == target) break;
if(numbers[start] + numbers[end] < target) start++;
else end--;
}
return new int[]{start + 1, end + 1};
}
}
image_2021-12-10_01-56-28.png
57.3 KB
#N1370. Increasing Decreasing String
problem link

#solution
class Solution {
public String sortString(String s) {
int count[] = new int[26];
StringBuilder sb=new StringBuilder();
for(char ch: s.toCharArray())
count[ch-'a']++;

while(sb.toString().length()<s.length()){
for(int i=0; i<26; i++){
if(count[i]>0){
sb.append((char) (i+'a'));
count[i]--;
}
}

for(int i=25; i>=0; i--){
if(count[i]>0){
sb.append((char) (i+'a'));
count[i]--;
}
}
}

return sb.toString();
}
}