image_2022-04-28_17-47-38.png
17.8 KB
#N1148. Article Views I
problem link
#solution
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
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
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
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
image_2022-05-06_17-41-46.png
28.1 KB
#medium
#N1561. Maximum Number of Coins You Can Get
problem link
#solution
#N1561. Maximum Number of Coins You Can Get
problem link
#solution
class Solution {
public int maxCoins(int[] piles) {
int n=piles.length/3;
int count=0;
Arrays.sort(piles);
for(int i=piles.length-2; n-- >0; i-=2){
count+=piles[i];
}
return count;
}
}image_2022-05-11_10-48-47.png
49.2 KB
#medium
#N950. Reveal Cards In Increasing Order
problem link
#solution
#N950. Reveal Cards In Increasing Order
problem link
#solution
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
Deque<Integer> deque = new ArrayDeque<>();
Arrays.sort(deck);
for(int i=deck.length-1; i>=0; i--){
if(deque.isEmpty()){
deque.offer(deck[i]);
}else{
deque.offerFirst(deque.pollLast());
deque.offerFirst(deck[i]);
}
}
int ans[] = new int[deque.size()];
for(int i=0; i<ans.length; i++){
ans[i]=deque.poll();
}
return ans;
}
}🔥2
image_2022-05-11_10-59-02.png
32.9 KB
#N2206. Divide Array Into Equal Pairs
problem link
#solution
problem link
#solution
class Solution {
public boolean divideArray(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for(int num: nums){
map.put(num, map.getOrDefault(num, 0) +1);
}
for(Map.Entry<Integer, Integer> entry: map.entrySet()){
if(entry.getValue()%2!=0) return false;
}
return true;
}
}🔥2
image_2022-05-11_12-04-32.png
45.8 KB
#medium
#N1823. Find the Winner of the Circular Game
problem link
#solution
#N1823. Find the Winner of the Circular Game
problem link
#solution
class Solution {
public int findTheWinner(int n, int k) {
int cycle=n-1, curr=0, temp;
int[] arr = new int[n];
while(cycle-- >0){
temp=0;
while(temp<k){
if(arr[curr%n]==0) temp++;
if(temp<k) curr++;
}
arr[curr%n]++;
curr++;
}
for(int i=0; i<arr.length; i++){
if(arr[i]==0) return i+1;
}
return -1;
}
}image_2022-05-17_17-29-16.png
37.8 KB
#medium
#N12. Integer to Roman
problem link
#solution
#N12. Integer to Roman
problem link
#solution
class Solution {
public String intToRoman(int num) {
StringBuffer sb = new StringBuffer();
String[] romans={"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
int[] nums={1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
for(int i=romans.length-1; i>=0; i--){
while(num>=nums[i]){
sb.append(romans[i]);
num = num-nums[i];
}
}
return sb.toString();
}
}image_2022-05-17_17-30-45.png
46.7 KB
#medium
#N137. Single Number II
problem link
#solution
#N137. Single Number II
problem link
#solution
class Solution {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
for(int i=1; i<nums.length-1; i+=3){
if(nums[i-1]!=nums[i]) return nums[i-1];
}
return nums[nums.length-1];
}
}👍2
image_2022-05-18_14-47-24.png
54.3 KB
#medium
#N260. Single Number III
problem link
#solution
#N260. Single Number III
problem link
#solution
class Solution {
public int[] singleNumber(int[] nums) {
int single1=0, single2=0;
boolean found=false;
Map<Integer, Integer> map = new HashMap<>();
for(int num: nums){
map.put(num, map.getOrDefault(num, 0) +1);
}
for(Map.Entry<Integer, Integer> entry: map.entrySet()){
if(entry.getValue()==1&&!found){
single1=entry.getKey();
found=true;
}
if(entry.getValue()==1&&found){
single2=entry.getKey();
}
}
return new int[]{single1, single2};
}
}image_2022-05-18_16-20-35.png
48.7 KB
#medium
#N1669. Merge In Between Linked Lists
problem link
#solution
#N1669. Merge In Between Linked Lists
problem link
#solution
class Solution {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode head=list1;
int diff=b-a;
while(a-- > 1){
list1=list1.next;
}
ListNode move=list1.next;
list1.next=list2;
while(diff-- >= 0){
move=move.next;
}
while(list2.next!=null){
list2=list2.next;
}
list2.next=move;
return head;
}
}image_2022-05-18_17-49-18.png
34.8 KB
#N374. Guess Number Higher or Lower
problem link
#solution
problem link
#solution
public class Solution extends GuessGame {
public int guessNumber(int n) {
int l=1, r=n, mid;
while(l<=r){
mid=l+(r-l)/2;
if(guess(mid)==0) return mid;
if(guess(mid)==1) l=mid+1;
if(guess(mid)==-1) r=mid-1;
}
return l;
}
}image_2022-05-19_16-49-35.png
49.3 KB
#medium
#N31. Next Permutation
problem link
#solution
#N31. Next Permutation
problem link
#solution
class Solution {
public void nextPermutation(int[] nums) {
int i=nums.length-2, j=nums.length-1, temp;
while(i>-1 && nums[i]>=nums[i+1])
i--;
if(i>=0){
while(nums[i]>=nums[j])
j--;
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
int k=nums.length-1;
i++;
while(i<k){
temp=nums[i];
nums[i]=nums[k];
nums[k]=temp;
i++; k--;
}
}
}image_2022-05-23_16-32-48.png
28.4 KB
#N2154. Keep Multiplying Found Values by Two
problem link
#solution
problem link
#solution
class Solution {
public int findFinalValue(int[] nums, int original) {
for(int i=0; i<nums.length; i++){
if(nums[i]==original){
return findFinalValue(nums, 2*original);
}
}
return original;
}
}👍1
image_2022-05-23_17-57-27.png
87.1 KB
#medium
#N1418. Display Table of Food Orders in a Restaurant
problem link
#solution
#N1418. Display Table of Food Orders in a Restaurant
problem link
#solution
class Solution {
public static List<List<String>> displayTable(List<List<String>> orders) {
int table; String food;
Map<Integer, TreeMap<String, Integer>> map = new TreeMap<>();
Set<String> set = new TreeSet<>();
for (List<String> order : orders) {
table = Integer.parseInt(order.get(1));food = order.get(2);
set.add(food);
TreeMap<String, Integer> foodMap;
if (map.get(table) == null) foodMap = new TreeMap<>();
else foodMap = map.get(table);
foodMap.put(food, foodMap.getOrDefault(food, 0) +1);
map.put(table, foodMap);
}
Leetcode in Java && Oracle
image_2022-05-23_17-57-27.png
List<List<String>> list = new ArrayList<>();
List<String> temp = new ArrayList<>(); temp.add("Table");
for (String s: set)
temp.add(s);
list.add(temp);
for (Map.Entry<Integer, TreeMap<String, Integer>> entry: map.entrySet()){
temp = new ArrayList<>();
temp.add(String.valueOf(entry.getKey()));
for (String s : set)
temp.add(String.valueOf(entry.getValue().getOrDefault(s, 0)));
list.add(temp);
}
return list;
}
}
image_2022-05-24_13-58-14.png
73.4 KB
#medium
#N811. Subdomain Visit Count
problem link
#solution
#N811. Subdomain Visit Count
problem link
#solution
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> map = new HashMap<>(); String website, websites[]; int number; StringBuilder sb;
for(String s: cpdomains){
sb=new StringBuilder(); number=Integer.parseInt(s.split(" ")[0]);
websites=s.split(" ")[1].split("\\.");
for(int i=websites.length-1; i>0; i--){
sb.insert(0, websites[i]);
map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) +number);
sb.insert(0, ".");
} sb.insert(0, websites[0]);
map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) +number);
} List<String> list = new ArrayList<>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
sb=new StringBuilder();
sb.append(entry.getValue()).append(" ").append(entry.getKey());
list.add(sb.toString());
} return list;}image_2022-05-24_13-59-41.png
25.6 KB
#N2278. Percentage of Letter in String
problem link
#solution
problem link
#solution
class Solution {
public int percentageLetter(String s, char letter) {
int count=0;
for(char ch: s.toCharArray())
if(ch==letter) count++;
return count*100/s.length();
}
}image_2022-05-24_17-17-22.png
35.3 KB
#medium
#N48. Rotate Image
problem link
#solution
#N48. Rotate Image
problem link
#solution
class Solution {
public void rotate(int[][] matrix) {
int n=matrix.length, temp;
for(int i=0; i<n/2; i++){
for(int j=0; j<(n+1)/2; j++){
temp=matrix[i][j];
matrix[i][j]=matrix[n-1-j][i];
matrix[n-1-j][i]=matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j]=matrix[j][n-1-i];
matrix[j][n-1-i]=temp;
}
}
}
}🔥3