image_2022-01-08_23-37-18.png
37.5 KB
#N1189. Maximum Number of Balloons
problem link
#solution
problem link
#solution
class Solution {
public int maxNumberOfBalloons(String text) {
int letters[] = new int[26];
for(char ch: text.toCharArray())
letters[ch-'a']++;
int min = letters[1];
min = Math.min(min, letters[0]);
min = Math.min(min, letters[11]/2);
min = Math.min(min, letters[14]/2);
min = Math.min(min, letters[13]);
return min;
}
}#statistics
Till this time, 223 in total,
210 easy;
13 medium;
type of problem solutions have been posted
Till this time, 223 in total,
210 easy;
13 medium;
type of problem solutions have been posted
#N1945. Sum of Digits of String After Convert
problem link
#solution
problem link
#solution
class Solution {
public int getLucky(String s, int k) {
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++)
sb.append((s.charAt(i)-'a')+1);
String result=sb.toString();
if(result.length()==1)
return Character.getNumericValue(result.charAt(0));
int sum=0;
while(k-->0 && result.length()>1)
{
sum=0;
for(int i=0;i<result.length();i++)
sum+=Character.getNumericValue(result.charAt(i));
result=String.valueOf(sum);
}
return sum;
}
}#N1507. Reformat Date
problem link
#solution
problem link
#solution
class Solution {
public String reformatDate(String date) {
StringBuilder sb = new StringBuilder();
String month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String d[] = date.split(" ");
sb.append(d[2]+"-");
for(int i=0; i<12; i++)
if(month[i].equals(d[1])){
if(i<9) sb.append(0).append(i+1);
else sb.append(i+1);
}
sb.append("-");
if(d[1].substring(0, d[0].length()-2).length()==1)
sb.append(0).append(d[0].charAt(0));
else
sb.append(d[0].substring(0, 2));
return sb.toString();
}
}image_2022-01-10_00-30-16.png
41.6 KB
#N242. Valid Anagram
problem link
#solution
problem link
#solution
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return 0==1;
Map<Character, Integer> mapS = new HashMap<>();
Map<Character, Integer> mapT = new HashMap<>();
for(int i=0; i<s.length(); i++){
mapS.put(s.charAt(i), mapS.getOrDefault(s.charAt(i), 0) +1);
mapT.put(t.charAt(i), mapT.getOrDefault(t.charAt(i), 0) +1);
}
return mapS.equals(mapT);
}
}image_2022-01-11_15-22-08.png
20.7 KB
#N620. Not Boring Movies
problem link
#solution
problem link
#solution
select * from cinema
where description <> 'boring' and id%2=1
order by rating desc;
image_2022-01-11_15-26-47.png
20 KB
#N175. Combine Two Tables
problem link
#solution
problem link
#solution
select t1.firstname, t1.lastname, t2.city, t2.state
from person t1 left join address t2
on t1.personid=t2.personid;
image_2022-01-11_15-39-20.png
19.5 KB
#N182. Duplicate Emails
problem link
#solution
problem link
#solution
select email from
(select email, count(email) as num from person group by email) as temp
where num>1;
image_2022-01-11_15-46-01.png
20.8 KB
#N183. Customers Who Never Order
problem link
#solution
problem link
#solution
select name as 'customers' from customers
where id not in
(select customerid from orders);
image_2022-01-11_15-55-09.png
18.8 KB
#N196. Delete Duplicate Emails
problem link
#solution
problem link
#solution
delete p1.*
from person p1, person p2
where p1.email=p2.email and p1.id>p2.id;
image_2022-01-11_16-14-40.png
21.1 KB
#N197. Rising Temperature
problem link
#solution
problem link
#solution
select w1.id from weather w1, weather w2
where datediff(w1.recorddate, w2.recorddate)=1 and
w1.temperature>w2.temperature;
image_2022-01-11_16-18-21.png
19.6 KB
#N596. Classes More Than 5 Students
problem link
#solution
problem link
#solution
select class from(select class, count(class) as num from courses group by class) as temp where num>4;
image_2022-01-11_16-26-45.png
40.3 KB
#N2129. Capitalize the Title
problem link
#solution
problem link
#solution
class Solution {
public String capitalizeTitle(String title) {
StringBuilder sb = new StringBuilder();
for(String s: title.split(" ")){
if(s.length()>2)
sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1).toLowerCase());
else
sb.append(s.toLowerCase());
sb.append(" ");
}
return sb.toString().trim();
}
}image_2022-01-12_14-46-28.png
47 KB
#N1869. Longer Contiguous Segments of Ones than Zeros
problem link
#solution
class Solution {
public boolean checkZeroOnes(String s) {
String[] one=s.split("0");
String[] zero=s.split("1");
int max0=0, max1=0;
for(String str: one)
max1=Math.max(max1, str.length());
for(String str: zero)
max0=Math.max(max0, str.length());
return max1>max0;
}
}image_2022-01-14_19-12-01.png
29 KB
#N171. Excel Sheet Column Number
problem link
#solution
problem link
#solution
class Solution {
public int titleToNumber(String columnTitle) {
int num=0;
char[] title = columnTitle.toCharArray();
int length=title.length;
for(char ch: title)
num = num*26+(ch-'A'+1);
return num;
}
}image_2022-01-14_19-44-50.png
30.7 KB
#N1422. Maximum Score After Splitting a String
problem link
#solution
problem link
#solution
class Solution {
public int maxScore(String s) {
int zeros = 0, ones = 0, max = Integer.MIN_VALUE;
for(int i=0;i<s.length();i++) {
if(s.charAt(i) == '0') zeros++; else ones++;
if(i != s.length()-1) max = Math.max(zeros - ones, max);
}
return max + ones;
}
}image_2022-01-17_13-11-06.png
21.8 KB
#medium
#N184. Department Highest Salary
problem link
#solution
#N184. Department Highest Salary
problem link
#solution
select d.name as 'Department', e.name as 'Employee',
e.salary as 'Salary' from employee e
join department d on e.departmentid = d.id
where (e.departmentid, e.salary) in
(select departmentid, max(salary)
from employee group by departmentid);
image_2022-01-22_21-41-11.png
34.2 KB
#medium
#N2145. Count the Hidden Sequences
problem link
#solution
#N2145. Count the Hidden Sequences
problem link
#solution
class Solution {
public int numberOfArrays(int[] differences, int lower, int upper) {
long min = 0, max = 0, curr = 0;
for(int d: differences){
curr += d;
min = Math.min(min, curr);
max = Math.max(max, curr);
}
long rangeDifference = (upper-lower) - (max-min) + 1;
return (int)Math.max(rangeDifference, 0);
}
}image_2022-01-22_21-55-33.png
28.7 KB
#N520. Detect Capital
problem link
#solution
problem link
#solution
class Solution {
public boolean detectCapitalUse(String word) {
int lower=0;
for(char ch: word.toCharArray()){
if(ch>=97 && ch<=122)
lower++;
}
return word.charAt(0)<=90 ? lower == word.length()-1 || lower == 0 : lower == word.length();
}
}image_2022-01-22_23-15-49.png
22.9 KB
#medium
#N1551. Minimum Operations to Make Array Equal
problem link
#solution
#N1551. Minimum Operations to Make Array Equal
problem link
#solution
class Solution {
public int minOperations(int n) {
return n*n/4;
}
}image_2022-01-24_17-54-04.png
21.7 KB
#medium
#N180. Consecutive Numbers
problem link
#solution
#N180. Consecutive Numbers
problem link
#solution
select distinct l1.num as ConsecutiveNums from
logs l1, logs l2, logs l3
where l1.num=l2.num && l2.num=l3.num && l1.id+1=l2.id && l2.id+1=l3.id;