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
}

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

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 161. One Edit Distance
5-
*
6-
* Given two strings s and t, determine if they are both one edit distance apart.
7-
*
8-
* Note:
9-
* There are 3 possiblities to satisify one edit distance apart:
10-
* Insert a character into s to get t
11-
* Delete a character from s to get t
12-
* Replace a character of s to get t
13-
*
14-
* Example 1:
15-
* Input: s = "ab", t = "acb"
16-
* Output: true
17-
* Explanation: We can insert 'c' into s to get t.
18-
*
19-
* Example 2:
20-
* Input: s = "cab", t = "ad"
21-
* Output: false
22-
* Explanation: We cannot get t from s by only one step.
23-
*
24-
* Example 3:
25-
* Input: s = "1203", t = "1213"
26-
* Output: true
27-
* Explanation: We can replace '0' with '1' to get t.
28-
*/
293
public class _161 {
304
public static class Solution1 {
315
public boolean isOneEditDistance(String s, String t) {
@@ -48,9 +22,8 @@ public boolean isOneEditDistance(String s, String t) {
4822
j++;
4923
}
5024
}
51-
return diffCnt == 1
52-
|| diffCnt
53-
== 0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero
25+
return diffCnt == 1 || diffCnt == 0;
26+
//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero
5427
} else if (s.length() == t.length()) {
5528
int diffCnt = 0;
5629
for (int i = 0; i < s.length(); i++) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1641 {
6+
public static class Solution1 {
7+
/**
8+
* I solved this problem using Math, no DP, recursion or backtracking techniques.
9+
* Time: beat 100% submission consistently since it's O(n), essentialy it's O(1) because the contraints in the problem state: 1 <= n <= 50
10+
* After writing out from n = 1 to 3, we can see the pattern.
11+
* Detailed reasoning to be seen in my youtube video on my channel: https://www.youtube.com/fishercoder.
12+
*/
13+
public int countVowelStrings(int n) {
14+
if (n == 1) {
15+
return 5;
16+
}
17+
int[] arr = new int[]{1, 1, 1, 1, 1};
18+
int sum = 5;
19+
for (int i = 2; i <= n; i++) {
20+
int[] copy = new int[5];
21+
for (int j = 0; j < arr.length; j++) {
22+
if (j == 0) {
23+
copy[j] = sum;
24+
} else {
25+
copy[j] = copy[j - 1] - arr[j - 1];
26+
}
27+
}
28+
arr = Arrays.copyOf(copy, 5);
29+
sum = 0;
30+
for (int j = 0; j < arr.length; j++) {
31+
sum += arr[j];
32+
}
33+
}
34+
return sum;
35+
}
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1658 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936074/JavaPython-3-Sliding-window%3A-Longest-subarray-sum-to-the-target-sum(nums)-x.
7+
*/
8+
public int minOperations(int[] nums, int x) {
9+
int sum = 0;
10+
for (int n : nums) {
11+
sum += n;
12+
}
13+
int target = sum - x;
14+
int len = nums.length;
15+
int size = Integer.MIN_VALUE;
16+
for (int left = -1, right = 0, windowSum = 0; right < len; right++) {
17+
windowSum += nums[right];
18+
while (left + 1 < len && windowSum > target) {
19+
left++;
20+
windowSum -= nums[left];
21+
}
22+
if (windowSum == target) {
23+
size = Math.max(size, right - left);
24+
}
25+
}
26+
return size < 0 ? -1 : len - size;
27+
}
28+
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.TreeSet;
4+
5+
public class _1675 {
6+
public static class Solution1 {
7+
public int minimumDeviation(int[] nums) {
8+
TreeSet<Integer> treeSet = new TreeSet<>();
9+
for (int num : nums) {
10+
if (num % 2 == 1) {
11+
treeSet.add(num * 2);
12+
} else {
13+
treeSet.add(num);
14+
}
15+
}
16+
int minDev = treeSet.last() - treeSet.first();
17+
while (treeSet.last() % 2 == 0) {
18+
treeSet.add(treeSet.last() / 2);
19+
treeSet.remove(treeSet.last());
20+
minDev = Math.min(minDev, treeSet.last() - treeSet.first());
21+
}
22+
return minDev;
23+
}
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1680 {
4+
public static class Solution1 {
5+
public int concatenatedBinary(int n) {
6+
final int MOD = 1000000007;
7+
int result = 0;
8+
for (int i = 1; i <= n; i++) {
9+
String binary = Integer.toBinaryString(i);
10+
for (int j = 0; j < binary.length(); j++) {
11+
result = (result * 2 + (binary.charAt(j) - '0')) % MOD;
12+
}
13+
}
14+
return result;
15+
}
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1716 {
4+
public static class Solution1 {
5+
public int totalMoney(int n) {
6+
int mondayMoney = 1;
7+
int total = 0;
8+
while (n > 0) {
9+
int weekDays = 0;
10+
int base = mondayMoney;
11+
while (weekDays < 7 && n > 0) {
12+
total += base;
13+
base++;
14+
weekDays++;
15+
n--;
16+
}
17+
mondayMoney++;
18+
}
19+
return total;
20+
}
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
public class _1717 {
6+
public static class Solution1 {
7+
public int maximumGain(String s, int x, int y) {
8+
Stack<Character> stack1 = new Stack<>();
9+
int big = x > y ? x : y;
10+
int small = big == x ? y : x;
11+
char first = x == big ? 'a' : 'b';
12+
char second = first == 'a' ? 'b' : 'a';
13+
int maximumGain = 0;
14+
for (char c : s.toCharArray()) {
15+
if (c == second && !stack1.isEmpty() && stack1.peek() == first) {
16+
stack1.pop();
17+
maximumGain += big;
18+
} else {
19+
stack1.push(c);
20+
}
21+
}
22+
Stack<Character> stack2 = new Stack<>();
23+
while (!stack1.isEmpty()) {
24+
char c = stack1.pop();
25+
if (c == second && !stack2.isEmpty() && stack2.peek() == first) {
26+
stack2.pop();
27+
maximumGain += small;
28+
} else {
29+
stack2.push(c);
30+
}
31+
}
32+
return maximumGain;
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)