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

Let's Develop Together!
Download Telegram
image_2021-11-08_01-43-03.png
46.9 KB
#medium

#N1769. Minimum Number of Operations to Move All Balls to Each Box
problem link

#solution
class Solution {
public int[] minOperations(String boxes) {
char[] ch=boxes.toCharArray();
int[] answer=new int[boxes.length()];
int count;

for(int i=0; i<answer.length; i++){
count=0;
for(int j=0; j<answer.length; j++)
if(ch[j]=='1')
count+=Math.abs(i-j);

answer[i]=count;
}

return answer;
}
}
image_2021-11-08_01-52-44.png
26.5 KB
#medium

#N1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
problem link

#solution
class Solution {
public int minPartitions(String n) {
int max=0;

for(char num:n.toCharArray())
max=Math.max(max, num-'0');

return max;
}
}
image_2021-11-08_18-21-25.png
32.7 KB
#N944. Delete Columns to Make Sorted
problem link

#solution
class Solution {
public int minDeletionSize(String[] strs) {
int count=0;
for(int i=0; i<strs[0].length(); i++){
for(int j=0; j<strs.length-1; j++){
if(strs[j].charAt(i)>strs[j+1].charAt(i)){
count++;
break;
}
}
}

return count;
}
}
image_2021-11-09_01-19-45.png
29.1 KB
#N1502. Can Make Arithmetic Progression From Sequence
problem link

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

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

return true;
}
}
image_2021-11-09_01-40-17.png
52.3 KB
#N682. Baseball Game
problem link

#solution
class Solution {
public int calPoints(String[] ops) {
List<Integer> records=new ArrayList<Integer>();
int record=0;

for(int i=0; i<ops.length; i++){
if(ops[i].equals("+"))
records.add(records.get(records.size()-2)+records.get(records.size()-1));
else if(ops[i].equals("C"))
records.remove(records.size()-1);
else if(ops[i].equals("D"))
records.add(2*records.get(records.size()-1));
else
records.add(Integer.parseInt(ops[i]));
}

for(int i:records)
record+=i;

return record;
}
}
image_2021-11-09_01-58-29.png
50.4 KB
#N496. Next Greater Element I
problem link

#solution
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] res=new int[nums1.length];
boolean isFound;

for(int i=0; i<nums1.length; i++){
isFound=false;
for(int j=0; j<nums2.length; j++){
if(nums2[j]==nums1[i])
isFound=true;

if(isFound){
if(nums2[j]>nums1[i]){
res[i]=nums2[j];
break;
}
else if(j==nums2.length-1)
res[i]=-1;
}
}
}

return res;
}
}
image_2021-11-09_14-51-47.png
50 KB
#medium

#N1433. Check If a String Can Break Another String
problem link

#solution
class Solution {
public boolean checkIfCanBreak(String s1, String s2) {
char arr1[]=s1.toCharArray();
char arr2[]=s2.toCharArray();

Arrays.sort(arr1);
Arrays.sort(arr2);

int count1=0, count2=0, l=s1.length();

for(int i=0; i<l; i++){
if(arr1[i]>=arr2[i])
count1++;
if(arr2[i]>=arr1[i])
count2++;
}

return count1==l||count2==l;
}
}
image_2021-11-09_15-12-58.png
52.4 KB
#medium

#N791. Custom Sort String
problem link

#solution
class Solution {
public String customSortString(String order, String s) {
StringBuilder res=new StringBuilder();
char[] arr1=order.toCharArray();
char[] arr2=s.toCharArray();

for(int i=0; i<order.length(); i++){
for(int j=0; j<s.length(); j++){
if(arr1[i]==arr2[j]){
res.append(""+arr1[i]);
arr2[j]=' ';
}
}
}

for(int i=0; i<arr2.length; i++){
if(arr2[i]!=' ')
res.append(""+arr2[i]);
}

return res.toString();
}
}

p.s. runtime is O(m•n). the worst ever
Leetcode in Java && Oracle
image_2021-11-09_15-12-58.png
image_2021-11-09_15-29-43.png
57.8 KB
#updated

class Solution {
public String customSortString(String order, String s) {
StringBuilder res=new StringBuilder();
int[] arr=new int[26];

for(int i=0; i<s.length(); i++){
arr[s.charAt(i)-'a']++;
}

for(int i=0; i<order.length(); i++){
while(arr[order.charAt(i)-'a']>0){
res.append(order.charAt(i));
arr[order.charAt(i)-'a']--;
}
}

for(int i=0; i<26; i++){
while(arr[i]>0){
res.append((char) (i+'a'));
arr[i]--;
}
}

return res.toString();
}
}

p.s.: better runtime than previous one)
image_2021-11-09_18-00-09.png
60.1 KB
#N1122. Relative Sort Array
problem link

#solution
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count=new int[1001];
int[] res=new int[arr1.length];

for(int i=0; i<arr1.length; i++){
count[arr1[i]]++;
}
int index=0;
for(int i=0; i<arr2.length; i++){
while(count[arr2[i]] > 0){
res[index]=arr2[i];
index++;
count[arr2[i]]--;
}
}

for(int i=0; i<count.length; i++){
if(count[i]!=0){
while(count[i]>0){
res[index]=i;
index++;
count[i]--;
}
}
}

return res;
}
}
image_2021-11-10_13-52-56.png
19.9 KB
#N441. Arranging Coins
problem link

#solution
class Solution {
public int arrangeCoins(int n) {
return ((int)(Math.pow((1+8*(long)n), 0.5))-1)/2;
}
}

p.s. Yeah, math really helps to solve this kind of problems😅
image_2021-11-11_21-24-38.png
27.7 KB
#N1413. Minimum Value to Get Positive Step by Step Sum
problem link

#solution
class Solution {
public int minStartValue(int[] nums) {
int sum=0, minValue=0;

for(int num:nums){
sum+=num;
minValue=Math.min(minValue, sum);
}

return 1-minValue;
}
}
image_2021-11-11_21-35-59.png
23.6 KB
#N136. Single Number
problem link

#solution
class Solution {
public int singleNumber(int[] nums) {
int ans = 0;
int len = nums.length;
for(int i = 0; i != len; i++)
ans ^= nums[i];

return ans;

}
}

p.s. good problem to understand XOR operator better)
image_2021-11-12_01-47-18.png
30.7 KB
#N1925. Count Square Sum Triples
problem link

#solution
class Solution {
public int countTriples(int n) {
int count = 0;
for (int i = 1; i < n; i++){
for (int j = i + 1; j < n; j++){
int square = i * i + j * j;
int c = (int) Math.sqrt(i * i + j * j);
if (c <= n && c * c == square) count++;
}
}

return count*2;
}
}
image_2021-11-12_02-06-19.png
31.8 KB
#N1323. Maximum 69 Number
problem link

#solution
class Solution {
public int maximum69Number (int num) {
int first6=-1;
int number=num;

for(int i=1; num>0; i++){
if(num%10==6){
first6=i;
}
num/=10;
}

return number+3*(int)Math.pow(10, first6-1);
}
}
image_2021-11-12_15-55-09.png
61.3 KB
#N66. Plus One
problem link

#solution
class Solution {
public int[] plusOne(int[] digits) {
int count=0;
for(int i=0;i<digits.length; i++){
if(digits[i]==9) count++;
}
if(digits[digits.length-1]!=9){
digits[digits.length-1]=digits[digits.length-1]+1;
return digits;
}
if(count!=digits.length){
for(int i=digits.length-1; i>=0; i--){
if(digits[i]>=9){
digits[i]=0;
}else{
digits[i]=digits[i]+1;
break;
}
}
}else{
int[] arr=new int[digits.length+1];
arr[0]=1;
for(int i=1; i<arr.length; i++){
arr[i]=0;
}
return arr;
}
return digits;
}
}
image_2021-11-13_20-10-24.png
50.7 KB
#N1290. Convert Binary Number in a Linked List to Integer
problem link

#solution
class Solution {
public int getDecimalValue(ListNode head) {
int num = head.val;
while (head.next != null) {
num = num * 2 + head.next.val;
head = head.next;
}
return num;

}
}
image_2021-11-13_20-43-54.png
51.6 KB
#N876. Middle of the Linked List
problem link

#solution
class Solution {
public ListNode middleNode(ListNode head) {
ListNode temp=head;
int length=0;
while(temp!=null){
temp=temp.next;
length++;
}

for(int i=1; i<=length/2; i++)
head=head.next;

return head;
}
}
image_2021-11-14_00-07-22.png
30.1 KB
#N237. Delete Node in a Linked List
problem link

#solution
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
p.s.: but the question is totally wrong), because the head node is not given
image_2021-11-14_00-34-17.png
38.2 KB
#N206. Reverse Linked List
problem link

#solution
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null) return head;
ListNode prev=null, curr=head, after=head.next;

while(curr.next.next!=null){
curr.next=prev;
prev=curr;
curr=after;
after=after.next;
}

curr.next=prev;
after.next=curr;

return after;
}
}
image_2021-11-14_00-55-39.png
49.4 KB
#N21. Merge Two Sorted Lists
problem link

#solution
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode temp=head;

while(l1!=null&&l2!=null){
if(l1.val>=l2.val){
temp.next=l2;
l2=l2.next;
}
else{
temp.next=l1;
l1=l1.next;
}
temp=temp.next;
}

temp.next=(l1==null)?l2:l1;

return head.next;
}
}