Skip to content

Commit 3dbb9cd

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 9bc6361 commit 3dbb9cd

File tree

5 files changed

+283
-0
lines changed

5 files changed

+283
-0
lines changed

leetcode/412. Fizz Buzz.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## 412. Fizz Buzz
2+
3+
### Question
4+
Write a program that outputs the string representation of numbers from 1 to n.
5+
6+
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
7+
8+
```
9+
Example:
10+
11+
n = 15,
12+
13+
Return:
14+
[
15+
"1",
16+
"2",
17+
"Fizz",
18+
"4",
19+
"Buzz",
20+
"Fizz",
21+
"7",
22+
"8",
23+
"Fizz",
24+
"Buzz",
25+
"11",
26+
"Fizz",
27+
"13",
28+
"14",
29+
"FizzBuzz"
30+
]
31+
```
32+
33+
### Thinking:
34+
* Method 1:
35+
```Java
36+
class Solution {
37+
public List<String> fizzBuzz(int n) {
38+
List<String> result = new ArrayList<>();
39+
for(int i = 1; i <= n; i++){
40+
if(i % 15 == 0){
41+
result.add("FizzBuzz");
42+
}else if(i % 3 == 0){
43+
result.add("Fizz");
44+
}else if(i % 5 == 0){
45+
result.add("Buzz");
46+
}else{result.add("" + i);}
47+
}
48+
return result;
49+
}
50+
}
51+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## 560. Subarray Sum Equals K
2+
3+
### Question
4+
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
5+
6+
```
7+
Example 1:
8+
9+
Input:nums = [1,1,1], k = 2
10+
Output: 2
11+
```
12+
13+
Note:
14+
* The length of the array is in range [1, 20,000].
15+
* The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
16+
17+
18+
### Thinking:
19+
* Method 1: Prefix sum O(n^2)
20+
```Java
21+
class Solution {
22+
public int subarraySum(int[] nums, int k) {
23+
if(nums == null || nums.length == 0) return 0;
24+
int[] sum = new int[nums.length];
25+
sum[0] = nums[0];
26+
for(int i = 1; i < nums.length; i++){
27+
sum[i] = sum[i - 1] + nums[i];
28+
}
29+
int res = 0;
30+
for(int i = 0; i < nums.length; i++){
31+
if(sum[i] == k) res++;
32+
for(int j = 0; j < i; j++){
33+
int temp = sum[i] - sum[j];
34+
if(temp == k) res++;
35+
}
36+
}
37+
return res;
38+
}
39+
}
40+
```
41+
42+
* Method 2: HashMap + prefix sum
43+
1. record the appearance frequency of the prefix sum.
44+
2. record all of the sum.
45+
3. for each sum, check how many times (sum - k) exists. | sum - k | k | => total is sum.
46+
```Java
47+
class Solution {
48+
public int subarraySum(int[] nums, int k) {
49+
if(nums == null || nums.length == 0) return 0;
50+
int sum = 0, res = 0;
51+
Map<Integer, Integer> map = new HashMap<>(); //key is prefix sum, value is frequency
52+
map.put(0, 1);
53+
for(int i = 0; i < nums.length; i++){
54+
sum += nums[i];
55+
Integer freq = map.get(sum - k);
56+
if(freq != null) res += freq;
57+
map.put(sum, map.getOrDefault(sum, 0) + 1);
58+
}
59+
return res;
60+
}
61+
}
62+
```

leetcode/621. Task Scheduler.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## 621. Task Scheduler
2+
3+
### Question
4+
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
5+
6+
However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.
7+
8+
You need to return the least number of intervals the CPU will take to finish all the given tasks.
9+
10+
```
11+
Example:
12+
13+
Input: tasks = ["A","A","A","B","B","B"], n = 2
14+
Output: 8
15+
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
16+
```
17+
18+
Note:
19+
* The number of tasks is in the range [1, 10000].
20+
* The integer n is in the range [0, 100].
21+
22+
### Thinking:
23+
* Method 1: Greedy
24+
```Java
25+
class Solution {
26+
public int leastInterval(char[] tasks, int n) {
27+
int[] freq = new int[26];
28+
for(char c : tasks){
29+
freq[c - 'A']++;
30+
}
31+
Arrays.sort(freq);
32+
int i = 25;
33+
while(i >= 0 && freq[i] == freq[25]){i--;}
34+
return Math.max((n + 1) * (freq[25] - 1) + (25 - i), tasks.length);
35+
}
36+
}
37+
```

leetcode/683. K Empty Slots.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## 683. K Empty Slots
2+
3+
### Question
4+
There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will be in the status of blooming since then.
5+
6+
Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.
7+
8+
For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, where i and x will be in the range from 1 to N.
9+
10+
Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is k and these flowers are not blooming.
11+
12+
If there isn't such day, output -1.
13+
14+
```
15+
Example 1:
16+
17+
Input:
18+
flowers: [1,3,2]
19+
k: 1
20+
Output: 2
21+
Explanation: In the second day, the first and the third flower have become blooming.
22+
23+
Example 2:
24+
25+
Input:
26+
flowers: [1,2,3]
27+
k: 1
28+
Output: -1
29+
```
30+
31+
Note:
32+
* The given array will be in the range [1, 20000].
33+
34+
### Thinking:
35+
* Method 1: TreeSet
36+
```Java
37+
class Solution {
38+
public int kEmptySlots(int[] flowers, int k) {
39+
if(flowers == null || flowers.length == 0) return -1;
40+
TreeSet<Integer> set = new TreeSet<>();
41+
for(int i = 0; i < flowers.length; i++){
42+
Integer higher = set.higher(flowers[i]);
43+
if(higher != null && higher - flowers[i] == k + 1) return i + 1;
44+
Integer lower = set.lower(flowers[i]);
45+
if(lower != null && flowers[i] - lower == k + 1) return i + 1;
46+
set.add(flowers[i]);
47+
}
48+
return -1;
49+
}
50+
}
51+
```
52+

leetcode/975. Odd Even Jump.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## 975. Odd Even Jump
2+
3+
### Question
4+
You are given an integer array A. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps.
5+
6+
You may from index i jump forward to index j (with i < j) in the following way:
7+
8+
1. During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value. If there are multiple such indexes j, you can only jump to the smallest such index j.
9+
2. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value. If there are multiple such indexes j, you can only jump to the smallest such index j.
10+
3. (It may be the case that for some index i, there are no legal jumps.)
11+
12+
A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.)
13+
14+
Return the number of good starting indexes.
15+
16+
```
17+
Example 1:
18+
19+
Input: [10,13,12,14,15]
20+
Output: 2
21+
Explanation:
22+
From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more.
23+
From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more.
24+
From starting index i = 3, we can jump to i = 4, so we've reached the end.
25+
From starting index i = 4, we've reached the end already.
26+
In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps.
27+
28+
Example 2:
29+
30+
Input: [2,3,1,1,4]
31+
Output: 3
32+
Explanation:
33+
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
34+
35+
During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0].
36+
37+
During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3.
38+
39+
During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2].
40+
41+
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good.
42+
43+
In a similar manner, we can deduce that:
44+
From starting index i = 1, we jump to i = 4, so we reach the end.
45+
From starting index i = 2, we jump to i = 3, and then we can't jump anymore.
46+
From starting index i = 3, we jump to i = 4, so we reach the end.
47+
From starting index i = 4, we are already at the end.
48+
In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps.
49+
50+
Example 3:
51+
52+
Input: [5,1,3,4,2]
53+
Output: 3
54+
Explanation:
55+
We can reach the end from starting indexes 1, 2, and 4.
56+
```
57+
58+
Note:
59+
* 1 <= A.length <= 20000
60+
* 0 <= A[i] < 100000
61+
62+
63+
### Thinking:
64+
* Method 1: TreeSet
65+
```Java
66+
class Solution {
67+
public int kEmptySlots(int[] flowers, int k) {
68+
if(flowers == null || flowers.length == 0) return -1;
69+
TreeSet<Integer> set = new TreeSet<>();
70+
for(int i = 0; i < flowers.length; i++){
71+
Integer higher = set.higher(flowers[i]);
72+
if(higher != null && higher - flowers[i] == k + 1) return i + 1;
73+
Integer lower = set.lower(flowers[i]);
74+
if(lower != null && flowers[i] - lower == k + 1) return i + 1;
75+
set.add(flowers[i]);
76+
}
77+
return -1;
78+
}
79+
}
80+
```
81+

0 commit comments

Comments
 (0)