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;
}
}image_2022-04-14_23-25-21.png
18.4 KB
#N584. Find Customer Referee
problem link
#solution
problem link
#solution
select name from customer
where referee_id <> 2 or referee_id is null
image_2022-04-18_14-42-50.png
28.4 KB
#N2220. Minimum Bit Flips to Convert Number
problem link
#solution
problem link
#solution
class Solution {
public int minBitFlips(int start, int goal) {
int ans, count=0;
ans=start^goal;
while(ans>0){
if((ans&1)==1) count++;
ans>>=1;
}
return count;
}
}image_2022-04-18_15-08-58.png
32.2 KB
#N1965. Employees With Missing Information
problem link
#solution
problem link
#solution
select employee_id from employees
where employee_id not in (select employee_id from salaries)
union
select employee_id from salaries
where employee_id not in (select employee_id from employees)
order by employee_id;
image_2022-04-19_23-53-18.png
52.2 KB
#medium
#N2130. Maximum Twin Sum of a Linked List
problem link
#solution
#N2130. Maximum Twin Sum of a Linked List
problem link
#solution
class Solution {
public int pairSum(ListNode head) {
int l=length(head), max=0, index;
int[] arr = new int[l/2];
for(int i=0; i<l; i++){
index=i<l/2 ? i : l-i-1;
arr[index] += head.val;
head=head.next;
max=Math.max(max, arr[index]);
}
return max;
}
public int length(ListNode node){
int count=0;
while(node!=null){
count++;
node=node.next;
}
return count;
}
}image_2022-04-20_00-23-21.png
38.7 KB
#medium
#N1817. Finding the Users Active Minutes
problem link
#solution
#N1817. Finding the Users Active Minutes
problem link
#solution
class Solution {
public int[] findingUsersActiveMinutes(int[][] logs, int k) {
int[] ans = new int[k];
Map<Integer, Set<Integer>> map = new HashMap<>();
for(int[] log: logs){
map.putIfAbsent(log[0], new HashSet<>());
map.get(log[0]).add(log[1]);
}
for(Map.Entry<Integer, Set<Integer>> entry : map.entrySet()){
ans[entry.getValue().size()-1]++;
}
return ans;
}
}👍1
image_2022-04-20_00-33-59.png
19.2 KB
#N511. Game Play Analysis I
problem link
#solution
problem link
#solution
select player_id, min(event_date) as first_login from activity group by player_id;
image_2022-04-20_22-40-56.png
26.3 KB
#medium
#N2221. Find Triangular Sum of an Array
problem link
#solution
#N2221. Find Triangular Sum of an Array
problem link
#solution
class Solution {
public int triangularSum(int[] nums) {
for(int i=nums.length-1; i>0; i--){
for(int j=0; j<i; j++){
nums[j] = (nums[j] + nums[j+1])%10;
}
}
return nums[0];
}
}image_2022-04-23_15-16-31.png
20 KB
#N1667. Fix Names in a Table
problem link
#solution
problem link
#solution
select user_id, concat(upper(substr(name, 1, 1)), lower(substr(name, 2)))
as name from users
order by user_id;
image_2022-04-23_15-28-05.png
19.6 KB
#N1527. Patients With a Condition
problem link
#solution
problem link
#solution
select * from patients
where conditions like '% diab1%' or conditions like 'diab1%';
image_2022-04-26_16-45-13.png
47.9 KB
#N459. Repeated Substring Pattern
problem link
#solution
problem link
#solution
class Solution {
public boolean repeatedSubstringPattern(String s) {
for(int i=0; i<s.length()/2; i++){
if(s.length()%(i+1)==0 && check(i, s))
return true;
}
return false;
}
public boolean check(int i, String s){
StringBuilder temp = new StringBuilder(s.substring(0, i+1));
int count=s.length()/(i+1);
while(--count >0){
temp.append(s.substring(0, i+1));
}
if(temp.toString().equals(s)) return true;
return false;
}
}image_2022-04-26_17-22-43.png
60.4 KB
#medium
#N150. Evaluate Reverse Polish Notation
problem link
#solution
#N150. Evaluate Reverse Polish Notation
problem link
#solution
class Solution {
public int evalRPN(String[] tokens) {
Stack<String> stack = new Stack<>();
for(String t: tokens){
if(!stack.isEmpty() && t.charAt(0)<48 && t.length()==1){
int num2=Integer.parseInt(stack.pop());
int num1=Integer.parseInt(stack.pop());
stack.push(String.valueOf(calculate(num1, num2, t.charAt(0))));
}else{
stack.push(t);
}
}
return Integer.parseInt(stack.peek());
}
public int calculate(int num1, int num2, char ch){
switch(ch){
case '+': return num1+num2;
case '-': return num1-num2;
case '*': return num1*num2;
case '/': return num1/num2;
}
return 0;
}
}