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

Let's Develop Together!
Download Telegram
image_2021-11-25_13-24-04.png
40.7 KB
#N897. Increasing Order Search Tree
problem link

#solution
class Solution {
TreeNode res = new TreeNode();
TreeNode curr=res;
public TreeNode increasingBST(TreeNode root) {
inOrder(root);
return res.right;
}

public void inOrder(TreeNode root){
if(root == null) return;
inOrder(root.left);
curr.right = new TreeNode(root.val);
curr = curr.right;
inOrder(root.right);
}
}
image_2021-11-25_13-25-08.png
35.9 KB
#N700. Search in a Binary Search Tree
problem link

#solution
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null) return null;
if(root.val>val){
root = searchBST(root.left, val);
}
else if(root.val<val){
root = searchBST(root.right, val);
}
return root;
}
}
image_2021-11-27_01-37-20.png
32.3 KB
#N35. Search Insert Position
problem link

#solution
class Solution {
public int searchInsert(int[] nums, int target) {
int start=0, end=nums.length-1, mid;

while(start<=end){
mid=(start+end)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]<target) start=mid+1;
else if(nums[mid]>target) end=mid-1;
}

return start;
}
}
image_2021-11-27_02-12-57.png
16.5 KB
#N2057. Smallest Index With Equal Value
problem link

#solution
class Solution {
public int smallestEqual(int[] nums) {
for(int i=0; i<nums.length; i++)
if((i-nums[i])%10==0) return i;
return -1;
}
}
image_2021-11-27_02-27-01.png
39.4 KB
#N2078. Two Furthest Houses With Different Colors
problem link

#solution
class Solution {
public int maxDistance(int[] colors) {
int x1=0, x2=0;
for(int i=colors.length-1; i>0; i--)
if(colors[i]!=colors[0]){
x1=i;
break;
}

for(int i=0; i<colors.length-1; i++)
if(colors[i]!=colors[colors.length-1]) {
x2=colors.length-1-i;
break;
}
return Math.max(x1, x2);
}
}
#statistics
Till this time, 144 in total,
133 easy;
11 medium;
type of problem solutions have been posted
image_2021-11-29_17-37-18.png
29.5 KB
#N2089. Find Target Indices After Sorting Array
problem link

#solution
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();

for(int i=0; i<nums.length; i++)
if(nums[i]==target) list.add(i);

return list;
}
}
image_2021-11-29_18-29-48.png
40.7 KB
#N1991. Find the Middle Index in Array
problem link

#solution
class Solution {
public int findMiddleIndex(int[] nums) {
int sum = 0, preSum=0, index=-1;

for(int n:nums)
sum+=n;

for(int i=0; i<nums.length; i++){
if(sum-nums[i]==preSum){
return i;
}
else{
preSum+=nums[i];
sum-=nums[i];
}
}

return index;
}
}
image_2021-11-29_18-31-35.png
36.8 KB
#N724. Find Pivot Index
problem link

#solution
class Solution {
public int pivotIndex(int[] nums) {
int sum = 0, preSum=0, index=-1;

for(int n:nums)
sum+=n;

for(int i=0; i<nums.length; i++){
if(sum-nums[i]==preSum){
return i;
}
else{
preSum+=nums[i];
sum-=nums[i];
}
}

return index;
}
}
image_2021-11-30_11-05-51.png
39.7 KB
#N1822. Sign of the Product of an Array
problem link

#solution
class Solution {
public int arraySign(int[] nums) {
long product=1;
for(int n:nums){
if(n<0) product = -product;
if(n==0) return 0;
}

return signFunc(product);
}

int signFunc(long product){
if(product > 0) return 1;
else if(product < 0) return -1;
else return 0;
}
}
image_2021-11-30_11-23-41.png
55.2 KB
#medium

#N807. Max Increase to Keep City Skyline
problem link

#solution
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int len=grid.length, sum=0;
int[] rowMax = new int[len];
int[] colMax = new int[len];

for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
rowMax[i] = Math.max(rowMax[i], grid[i][j]);

for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
colMax[i] = Math.max(colMax[i], grid[j][i]);

for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
sum += Math.min(rowMax[i], colMax[j])-grid[i][j];

return sum;
}
}
Leetcode in Java && Oracle
image_2021-11-30_11-05-51.png
image_2021-11-30_11-30-57.png
25.8 KB
#python
#N1822. Sign of the Product of an Array

#anotherSolution
class Solution:
def arraySign(self, nums: List[int]) -> int:
counter = 0
for i in nums:
if i == 0:
return 0
if i < 0:
counter +=1
return 1 if counter % 2 == 0 else -1
image_2021-11-30_16-14-54.png
36.1 KB
#N617. Merge Two Binary Trees
problem link

#solution
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null) return null;
else if(root1 == null) return root2;
else if(root2 == null) return root1;

TreeNode res = new TreeNode(root1.val + root2.val);
res.left = mergeTrees(root1.left, root2.left);
res.right = mergeTrees(root1.right, root2.right);

return res;
}
}
image_2021-12-03_18-30-06.png
41.6 KB
#N2053. Kth Distinct String in an Array
problem link

#solution
class Solution {
public String kthDistinct(String[] arr, int k) {
Map<String, Integer> map = new HashMap<>();

for(String s:arr){
if(map.containsKey(s)) map.put(s, map.get(s)+1);
else map.put(s, 1);
}

for(String s:arr){
if(map.get(s)==1)
if(k-- == 1)
return s;
}

return "";
}
}
#recommendation
Advent of Code is a annually virtual event taking place in every December 1th-25th. Every day features new programming exercises, which can be solved in your programming language of choice.
It is full of interesting algorithmic problems, i recommend it)👍🏻

https://adventofcode.com/2021
image_2021-12-04_14-43-10.png
73.3 KB
#N1160. Find Words That Can Be Formed by Characters
problem link

#solution
class Solution {
public int countCharacters(String[] words, String chars) {
int sum=0;
Map<Character, Integer> map = new HashMap<>();
for(char ch:chars.toCharArray()){
if(map.containsKey(ch))
map.put(ch, map.get(ch)+1);
else
map.put(ch, 1);
}
for(String word:words){
Map<Character, Integer> temp = new HashMap<>(map);
boolean isGood = true;

for(char ch:word.toCharArray()){
temp.put(ch, temp.getOrDefault(ch, 0)-1);
if(temp.get(ch)<0) {
isGood=false;
break;
}
}
if(isGood) {
sum+=word.length();
}
}

return sum;
}
}
image_2021-12-04_16-17-14.png
56.5 KB
#N1002. Find Common Characters
problem link

#solution
class Solution {
public List<String> commonChars(String[] words) {
int[][] arr = new int[words.length][26];
int n=0;

for(String word:words){
for(char ch:word.toCharArray()){
arr[n][ch-'a']++;
}
n++;
}

List<String> list = new ArrayList<>();

for(int i=0; i<26; i++){
int k=100;
for(int j=0; j<arr.length; j++){
k=Math.min(k, arr[j][i]);
}

while(k-- >0){
list.add(Character.toString(i+'a'));
}
}

return list;
}
}
image_2021-12-04_17-19-07.png
49.5 KB
#N349. Intersection of Two Arrays
problem link

#solution
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[][] arr = new int[2][1001];
List<Integer> list = new ArrayList<>();

for(int i:nums1)
arr[0][i]++;

for(int i:nums2)
arr[1][i]++;

for(int i=0; i<1001; i++){
if(arr[0][i]*arr[1][i]>0) list.add(i);
}

int[] res = new int[list.size()];
for(int i=0; i<list.size(); i++){
res[i] = list.get(i);
}
return res;
}
}
image_2021-12-05_22-41-41.png
53.5 KB
#N1200. Minimum Absolute Difference
problem link

#solution
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
int diff = Integer.MAX_VALUE;

for(int i=0; i<arr.length-1; i++)
diff = Math.min(diff, arr[i+1] - arr[i]);

List<List<Integer>> list = new ArrayList<>();

for(int i=0; i<arr.length-1; i++){
if(diff == arr[i+1] - arr[i]){
List<Integer> sublist = new ArrayList<>();
sublist.add(arr[i]);
sublist.add(arr[i+1]);
list.add(sublist);
}
}

return list;
}
}
👍1
image_2021-12-06_01-28-46.png
38.4 KB
#N1779. Find Nearest Point That Has the Same X or Y Coordinate
problem link

#solution
class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int min=Integer.MAX_VALUE, index=-1;;
for(int i=points.length-1; i>=0; i--){
if(points[i][0]==x||points[i][1]==y){
if(min>=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y)){
min=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);
index=i;
}
}
}

return index;
}
}