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;
image_2022-01-24_22-29-27.png
36.1 KB
#N278. First Bad Version
problem link
#solution
problem link
#solution
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1, end = n, middle=(n+1)/2;
while(start<=end){
middle = end + (start - end) / 2;
if(isBadVersion(middle))
end=middle-1;
else
start=middle+1;
}
return start;
}
}image_2022-01-24_23-07-59.png
45.6 KB
#N2148. Count Elements With Strictly Smaller and Greater Elements
problem link
#solution
problem link
#solution
class Solution {
public int countElements(int[] nums) {
int min=Integer.MAX_VALUE, max=Integer.MIN_VALUE, mins=1, maxs=1;
for(int n: nums){
if(n == max){
maxs++;
}
if(n == min){
mins++;
}
if(n>max){
max = n;
maxs = 1;
}
if(n < min){
min = n;
mins = 1;
}
}
return Math.max(nums.length-mins-maxs, 0);
}
}image_2022-01-29_00-18-49.png
40.8 KB
#medium
#N189. Rotate Array
problem link
#solution
#N189. Rotate Array
problem link
#solution
class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);
}
public void reverse(int[] nums, int start, int end){
while(start<end){
int temp = nums[start];
nums[start]=nums[end];
nums[end] = temp;
start++;
end--;
}
}
}image_2022-02-04_14-14-04.png
44.3 KB
#N1603. Design Parking System
problem link
#solution
problem link
#solution
class ParkingSystem {
int big, medium, small;
public ParkingSystem(int big, int medium, int small) {
this.big=big;
this.medium=medium;
this.small=small;
}
public boolean addCar(int carType) {
int type = carType%3;
if(type==1)
return --big>=0;
else if(type == 2)
return --medium >= 0;
else
return --small >= 0;
}
}image_2022-02-04_18-41-26.png
61 KB
#N2103. Rings and Rods
problem link
#solution
problem link
#solution
public int countPoints(String rings) {
int n = rings.length()/2;
int arr1[] = new int[10];
int arr2[] = new int[10];
int arr3[] = new int[10];
int count = 0;
for(int i=0; i<n; i++){
if(rings.charAt(2*i) == 'R')
arr1[rings.charAt(2*i+1)-'0']++;
if(rings.charAt(2*i) == 'G')
arr2[rings.charAt(2*i+1)-'0']++;
if(rings.charAt(2*i) == 'B')
arr3[rings.charAt(2*i+1)-'0']++;
}
for(int i=0; i<10; i++){
if(arr1[i]*arr2[i]*arr3[i] != 0)
count++;
}
return count;
}image_2022-02-06_17-25-08.png
46.7 KB
#medium
#N1476. Subrectangle Queries
problem link
#solution
#N1476. Subrectangle Queries
problem link
#solution
class SubrectangleQueries {
int height, width;
int[][] rect;
public SubrectangleQueries(int[][] rectangle) {
this.rect = rectangle;
}
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for(int i=row1; i<=row2; i++){
for(int j=col1; j<=col2; j++){
rect[i][j] = newValue;
}
}
}
public int getValue(int row, int col) {
return rect[row][col];
}
}image_2022-02-06_17-35-23.png
30.8 KB
#N2160. Minimum Sum of Four Digit Number After Splitting Digits
problem link
#solution
problem link
#solution
class Solution {
public int minimumSum(int num) {
int[] arr = new int[4];
for(int i=0; i<4; i++){
arr[i] = num%10;
num/=10;
}
Arrays.sort(arr);
return 10*(arr[0]+arr[1])+arr[2]+arr[3];
}
}