Skip to content

Commit d379ea0

Browse files
Merge branch 'master' into master
2 parents 29412be + a816d0d commit d379ea0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1318
-107
lines changed

README.md

Lines changed: 30 additions & 6 deletions
Large diffs are not rendered by default.

database/_1113.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select extra as report_reason, count(distinct(post_id)) as report_count from Actions where action_date = '2019-07-04' and action = 'report' group by extra;

database/_1142.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--# Write your MySQL query statement below
2+
select ifnull(round(count(distinct session_id)/count(distinct user_id), 2), 0.00)
3+
as average_sessions_per_user
4+
from Activity
5+
where activity_date between '2019-06-28' and '2019-07-27';

database/_1371.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--# Write your MySQL query statement below
2+
select b.employee_id, b.name, count(*) as reports_count, round(avg(a.age)) as average_age
3+
from Employees a join Employees b on a.reports_to = b.employee_id
4+
group by b.employee_id
5+
order by b.employee_id

database/_1393.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
select stock_name, sum(
2+
case
3+
when operation = 'Buy' then -price
4+
else price
5+
end
6+
) as capital_gain_loss from Stocks group by stock_name;

database/_1596.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--credit: https://leetcode.com/problems/the-most-frequently-ordered-products-for-each-customer/discuss/861257/simple-and-easy-solution-using-window-function
2+
3+
select customer_id, product_id, product_name from
4+
(
5+
select o.customer_id, o.product_id, p.product_name,
6+
rank() over (partition by customer_id order by count(o.product_id) desc) as ranking
7+
from Orders o
8+
join Products p
9+
on o.product_id = p.product_id
10+
group by customer_id, product_id
11+
) tmp
12+
where ranking = 1
13+
order by customer_id, product_id

database/_1729.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--# Write your MySQL query statement below
2+
select user_id, count(follower_id) as followers_count from followers group by user_id order by user_id;

src/main/java/com/fishercoder/solutions/_127.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,6 @@
44
import java.util.List;
55
import java.util.Set;
66

7-
/**
8-
* 127. Word Ladder
9-
*
10-
* Given two words (beginWord and endWord), and a dictionary's word list,
11-
* find the length of shortest transformation sequence from beginWord to endWord, such that:
12-
* Only one letter can be changed at a time.
13-
* Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
14-
15-
For example,
16-
17-
Given:
18-
beginWord = "hit"
19-
endWord = "cog"
20-
wordList = ["hot","dot","dog","lot","log","cog"]
21-
22-
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5.
23-
24-
Note:
25-
26-
Return 0 if there is no such transformation sequence.
27-
All words have the same length.
28-
All words contain only lowercase alphabetic characters.
29-
You may assume no duplicates in the word list.
30-
You may assume beginWord and endWord are non-empty and are not the same.
31-
*/
32-
337
public class _127 {
348
public static class Solution1 {
359

src/main/java/com/fishercoder/solutions/_1437.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
public class _1437 {
74
public static class Solution1 {
85
public boolean kLengthApart(int[] nums, int k) {
9-
List<Integer> indexes = new ArrayList<>();
10-
for (int i = 0; i < nums.length; i++) {
6+
int lastOneIndex = nums[0] == 1 ? 0 : -1;
7+
for (int i = 1; i < nums.length; i++) {
118
if (nums[i] == 1) {
12-
indexes.add(i);
13-
}
14-
}
15-
for (int i = 0; i < indexes.size() - 1; i++) {
16-
if (indexes.get(i + 1) - indexes.get(i) < k + 1) {
17-
return false;
9+
if (i - lastOneIndex <= k) {
10+
return false;
11+
} else {
12+
lastOneIndex = i;
13+
}
1814
}
1915
}
2016
return true;

src/main/java/com/fishercoder/solutions/_151.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,38 @@ public String reverseWords(String s) {
2727
return stringBuilder.substring(0, stringBuilder.length() - 1).toString();
2828
}
2929
}
30+
31+
public static class Solution2 {
32+
public String reverseWords(String s) {
33+
int len = s.length();
34+
int i = 0;
35+
int j = 0;
36+
String result = "";
37+
while (i < len) {
38+
39+
// index i keeps track of the spaces and ignores them if found
40+
while (i < len && s.charAt(i) == ' ') {
41+
i++;
42+
}
43+
if (i == len) {
44+
break;
45+
}
46+
j = i + 1;
47+
48+
// index j keeps track of non-space characters and gives index of the first occurrence of space after a non-space character
49+
while (j < len && s.charAt(j) != ' ') {
50+
j++;
51+
}
52+
// word found
53+
String word = s.substring(i, j);
54+
if (result.length() == 0) {
55+
result = word;
56+
} else {
57+
result = word + " " + result;
58+
}
59+
i = j + 1;
60+
}
61+
return result;
62+
}
63+
}
3064
}

0 commit comments

Comments
 (0)