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

Let's Develop Together!
Download Telegram
image_2022-04-14_23-25-21.png
18.4 KB
#N584. Find Customer Referee
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
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
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
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
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
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
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
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
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
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
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;
}
}
image_2022-04-26_18-06-58.png
46 KB
#N989. Add to Array-Form of Integer
problem link
#solution
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
List<Integer> list = new ArrayList<>();
int qarz=0, temp=0;
for(int i=num.length-1; i>=0; i--){
temp=num[i]+k%10+qarz;
k/=10;
System.out.println(temp);
list.add(0, temp%10);
qarz=temp/10;
}
k+=qarz;
while(k!=0){
list.add(0, k%10);
k/=10;
}
return list;
}
}
image_2022-04-26_18-14-20.png
27.3 KB
#N58. Length of Last Word
problem link
#solution
class Solution {
public int lengthOfLastWord(String s) {
int count=0;
for(int i=s.length()-1; i>=0; i--){
if(s.charAt(i)==' ' && count!=0) break;
if(s.charAt(i)!=' ') count++;
}

return count;
}
}
image_2022-04-27_18-03-49.png
25.4 KB
#medium
#N973. K Closest Points to Origin
problem link
#solution
class Solution {
public int[][] kClosest(int[][] points, int k) {
Arrays.sort(points, (a, b) -> a[0]*a[0]+a[1]*a[1]-b[0]*b[0]-b[1]*b[1]);

return Arrays.copyOfRange(points, 0, k);
}
}
image_2022-04-28_15-31-26.png
54.1 KB
#medium
#N2. Add Two Numbers
problem link
#solution
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode temp = head;
int carry=0;
while(l1!=null || l2!=null){
temp.next=new ListNode();
temp=temp.next;
temp.val = (l1!=null?l1.val:0) + (l2!=null?l2.val:0)+carry;
carry=temp.val/10;
temp.val %= 10;
if(l1!=null) l1=l1.next;
if(l2!=null) l2=l2.next;
}
if(carry>0)
temp.next = new ListNode(carry);
return head.next;
}
}
image_2022-04-28_17-14-32.png
26.5 KB
#medium
#N608. Tree Node
problem link
#solution
select id, 'Root' as type from tree where p_id is null
union
select id, 'Inner' as type from tree
where id in (select distinct p_id from tree where p_id is not null) and p_id is not null
union
select id, 'Leaf' as type from tree
where id not in(select distinct p_id from tree where p_id is not null) and p_id is not null
image_2022-04-28_17-47-38.png
17.8 KB
#N1148. Article Views I
problem link
#solution
select distinct author_id as id from views where author_id=viewer_id order by author_id
image_2022-04-28_18-30-17.png
23.8 KB
#N1141. User Activity for the Past 30 Days I
problem link
#solution
select activity_date as day, count(distinct user_id) as active_users from activity
where datediff('2019-07-27', activity_date)<30
group by activity_date
image_2022-04-28_18-33-37.png
19.5 KB
#N1729. Find Followers Count
problem link
#solution
select user_id, count(distinct follower_id) as followers_count
from followers group by user_id;
image_2022-04-29_17-26-22.png
25.2 KB
#N1050. Actors and Directors Who Cooperated At Least Three Times
problem link
#solution
select actor_id, director_id 
from (select actor_id, director_id, count(*) as total from actordirector group by actor_id, director_id) as a where total>=3