image_2022-04-03_13-33-53.png
26.2 KB
#N104. Maximum Depth of Binary Tree
problem link
#solution
problem link
#solution
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
}image_2022-04-03_13-50-35.png
59.6 KB
#N1356. Sort Integers by The Number of 1 Bits
problem link
#solution
problem link
#solution
class Solution {
public int[] sortByBits(int[] arr) {
int n=arr.length;
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (func(arr[j]) > func(arr[j+1]) || (arr[j] > arr[j+1] && func(arr[j]) == func(arr[j+1])))
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
public int func(int n) {
int count=0;
while(n!=0){
if((n&1)==1)
count++;
n=n>>>1;
}
return count;
}
}🔥2
image_2022-04-03_14-25-13.png
57 KB
#medium
#N1357. Apply Discount Every n Orders
problem link
#solution
#N1357. Apply Discount Every n Orders
problem link
#solution
class Cashier {
int count=0, order;
double discountProduct;
Map<Integer, Integer> map = new HashMap<>();
public Cashier(int n, int discount, int[] products, int[] prices) {
this.order=n;
this.discountProduct=1-(double)discount/100;
for(int i=0; i<prices.length; i++){
map.put(products[i], prices[i]);
}
}
public double getBill(int[] product, int[] amount) {
count++;
int total=0;
for(int i=0; i<amount.length; i++){
total+=map.get(product[i])*amount[i];
}
return count%order==0? total*discountProduct : total;
}
}image_2022-04-03_14-42-37.png
67.9 KB
#medium
#N2043. Simple Bank System
problem link
#solution
#N2043. Simple Bank System
problem link
#solution
class Bank {
long [] balance;
public Bank(long[] balance) {this.balance=balance;}
public boolean transfer(int account1, int account2, long money) {
if(account1>balance.length || account2>balance.length || balance[--account1]<money)
return false;
balance[account1]-=money;
balance[account2-1]+=money;
return true;
}
public boolean deposit(int account, long money) {
if(account>balance.length)
return false;
balance[account-1]+=money;
return true;
}
public boolean withdraw(int account, long money) {
if(account>balance.length || balance[--account]<money)
return false;
balance[account]-=money;
return true;
}
}🔥3❤1
#N1795. Rearrange Products Table
problem link
#solution
problem link
#solution
select product_id, 'store1' as store, store1 as price from products where store1 is not null
union
select product_id, 'store2' as store, store2 as price from products where store2 is not null
union
select product_id, 'store3' as store, store3 as price from products where store3 is not null
image_2022-04-06_11-13-29.png
30.5 KB
#N2073. Time Needed to Buy Tickets
problem link
#solution
problem link
#solution
class Solution {
public int timeRequiredToBuy(int[] tickets, int k) {
int time=0;
while(tickets[k]!=0){
for(int i=0; i<tickets.length; i++){
if(tickets[i]-- > 0) time++;
if(tickets[k]==0) break;
}
}
return time;
}
}image_2022-04-06_11-26-11.png
36.2 KB
#N387. First Unique Character in a String
problem link
#solution
problem link
#solution
class Solution {
public int firstUniqChar(String s) {
int arr[] = new int[26];
for(Character ch: s.toCharArray()){
arr[ch-'a']++;
}
for(Character ch: s.toCharArray()){
if(arr[ch-'a']==1)
return s.indexOf(ch);
}
return -1;
}
}image_2022-04-06_11-50-48.png
47.7 KB
#N225. Implement Stack using Queues
problem link
#solution
problem link
#solution
class MyStack {
Queue<Integer> q = new LinkedList<>();
Queue<Integer> temp = new LinkedList<>();
public MyStack() {}
public void push(int x) {
q.add(x);
for(int i=0;i<q.size()-1;i++)
{
q.add(q.poll());
}
}
public int pop() {
return q.remove();
}
public int top() {
return q.peek();
}
public boolean empty() {
return q.isEmpty();
}
}image_2022-04-11_23-33-26.png
43.6 KB
#medium
#N11. Container With Most Water
problem link
#solution
#N11. Container With Most Water
problem link
#solution
class Solution {
public int maxArea(int[] height) {
int area=0, temp;
int left=0, right=height.length-1;
while(left<right){
temp=(right-left)*Math.min(height[left], height[right]);
area=Math.max(area, temp);
if(height[left]>height[right]) {
right--;
} else if(height[left]<height[right]) {
left++;
} else {
left++;
right--;
}
}
return area;
}
}image_2022-04-12_00-23-56.png
52.4 KB
#N1260. Shift 2D Grid
problem link
#solution
problem link
#solution
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int m=grid.length, n=grid[0].length, counter=m*n-k%(m*n);
List<Integer> list = new ArrayList<>();
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
list.add(grid[i][j]);
}
}
List<Integer> piece;
List<List<Integer>> ans = new ArrayList<>();
for(int i=0; i<m; i++){
piece = new ArrayList<>();
for(int j=0; j<n; j++){
piece.add(list.get(counter++ % (m*n)));
}
ans.add(piece);
}
return ans;
}
}👍2
image_2022-04-12_12-41-42.png
26.2 KB
#N1581. Customer Who Visited but Did Not Make Any Transactions
problem link
#solution
problem link
#solution
select v.customer_id, count(*) as count_no_trans
from visits v
left join transactions t
on v.visit_id=t.visit_id
where t.transaction_id is null
group by customer_id
image_2022-04-12_13-03-59.png
40.4 KB
#medium
#N2181. Merge Nodes in Between Zeros
problem link
#solution
#N2181. Merge Nodes in Between Zeros
problem link
#solution
class Solution {
public ListNode mergeNodes(ListNode head) {
ListNode slow = head.next;
ListNode fast = head.next;
int sum=0;
while(fast!=null){
sum+=fast.val;
if(fast.val==0){
slow.val=sum;
slow.next=fast.next;
slow=slow.next;
sum=0;
}
fast=fast.next;
}
return head.next;
}
}image_2022-04-12_13-28-44.png
49.7 KB
#medium
#N1315. Sum of Nodes with Even-Valued Grandparent
problem link
#solution
#N1315. Sum of Nodes with Even-Valued Grandparent
problem link
#solution
class Solution {
int sum=0;
public int sumEvenGrandparent(TreeNode root) {
if(root==null) return 0;
helper(root, null, null);
return sum;
}
public void helper(TreeNode node, TreeNode p, TreeNode gp){
if(node == null) return;
if(gp!=null && gp.val%2==0){
sum+=node.val;
}
helper(node.left, node, p);
helper(node.right, node, p);
}
}image_2022-04-12_13-40-26.png
23.9 KB
#N1484. Group Sold Products By The Date
problem link
#solution
problem link
#solution
select sell_date,
count(distinct product) as num_sold,
group_concat(distinct product order by product) as products
from activities
group by sell_date
image_2022-04-12_23-28-23.png
29.9 KB
#medium
#N1038. Binary Search Tree to Greater Sum Tree
problem link
#solution
#N1038. Binary Search Tree to Greater Sum Tree
problem link
#solution
class Solution {
int sum=0;
public TreeNode bstToGst(TreeNode root) {
if(root==null) return null;
bstToGst(root.right);
sum+=root.val;
root.val=sum;
bstToGst(root.left);
return root;
}
}image_2022-04-12_23-30-41.png
30.1 KB
#medium
#N538. Convert BST to Greater Tree
problem link
#solution
#N538. Convert BST to Greater Tree
problem link
#solution
class Solution {
int sum=0;
public TreeNode convertBST(TreeNode root) {
if(root==null) return null;
convertBST(root.right);
sum+=root.val;
root.val=sum;
convertBST(root.left);
return root;
}
}image_2022-04-12_23-43-07.png
21.4 KB
#N1890. The Latest Login in 2020
problem link
#solution
problem link
#solution
select user_id,
max(time_stamp) as last_stamp
from logins
where year(time_stamp)=2020
group by user_id
image_2022-04-13_16-20-52.png
23.5 KB
#N1407. Top Travellers
problem link
#solution
problem link
#solution
select u.name, ifnull(sum(r.distance), 0) as travelled_distance
from users u
left join rides r on u.id=r.user_id
group by u.name
order by travelled_distance desc, u.name asc;
image_2022-04-13_16-34-50.png
40.1 KB
#medium
#N1409. Queries on a Permutation With Key
problem link
#solution
#N1409. Queries on a Permutation With Key
problem link
#solution
class Solution {
public int[] processQueries(int[] queries, int m) {
List<Integer> list = new ArrayList<>();
for(int i=1; i<=m; i++)
list.add(i);
int[] ans = new int[queries.length];
for(int i=0; i<ans.length; i++){
ans[i]=list.indexOf(queries[i]);
list.remove(ans[i]);
list.add(0, queries[i]);
}
return ans;
}
}image_2022-04-14_23-15-58.png
34.3 KB
#N28. Implement strStr()
problem link
#solution
problem link
#solution
class Solution {
public int strStr(String haystack, String needle) {
if(haystack.length()<needle.length()) return -1;
for(int i=0; i<haystack.length(); i++){
for(int j=0; ; j++){
if(i+j==haystack.length()) return -1;
if(haystack.charAt(i+j) != needle.charAt(j)) break;
if(j==needle.length()-1) return i;
}
}
return -1;
}
}