Skip to content

Commit 183f57f

Browse files
green-studyLONGNEWBHwi
authored
Added java solution for 14 / python solution for 2420 (qiyuangong#61)
* Added java solution for 14 / python solution for 2420 * Added : 055_Jump_Game.java Co-authored-by: LONGNEW <40235475+LONGNEW@users.noreply.github.com> Co-authored-by: BHwi <bhwi0422@gmail.com>
1 parent aaec50a commit 183f57f

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
242242
| 1365 | [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.java) | 1. Sort and get position in sorted nums, O(nlogn) and O(n)<br>2. Fill count into 0-100, O(n) and O(1) |
243243
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1480_Running_Sum_of_1d_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1480_Running_Sum_of_1d_Array.java) | 1. Go through the array, O(n) and O(1)<br>2. Accumulate API |
244244
| 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1539_Kth_Missing_Positive_Number.java) | Binary search, num of missing = arr[i]-i-1 |
245+
| 1909 | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1909_Remove_One_Element_to_Make_the_Array_Strictly_Increasing.py )| Use brute-force. O( (nums.length)<sup>2</sup>) |
245246
| 1981 | [Minimize the Difference Between Target and Chosen Elements](https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/) | [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1981_Minimize_the_Difference_Between_Target_and_Chosen_Elements.java) | DP memo[row][sum] to avoid recomputing |
247+
| 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/2409_Count_Days_Spent_Together.py)| Use month as a day |
248+
| 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/2413_Smallest_Even_Multiple.py)| Check the n is multiply by 2 |
246249

247250
| # | To Understand |
248251
|---| ----- |

java/014_Longest_Common_Prefix.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//014_Longest_Common_Prefix.java
2+
class Solution {
3+
public String longestCommonPrefix(String[] strs) {
4+
String result ="";
5+
String temp = "";
6+
int c = 0; //move first point
7+
boolean check = true;
8+
while(true){
9+
for(int i = 0; i<strs.length; i++){ //move second point
10+
if(c>=strs[i].length()){
11+
check = false;
12+
break;
13+
}
14+
if(i==0){ //temp -> check same Character
15+
temp = Character.toString(strs[0].charAt(c));
16+
}
17+
if(!temp.equals(Character.toString(strs[i].charAt(c)))){
18+
check = false;
19+
break;
20+
}
21+
if(i==strs.length-1){
22+
result += temp;
23+
}
24+
}
25+
if(!check){
26+
break;
27+
}
28+
c++;
29+
}
30+
return result;
31+
32+
}
33+
}

java/055_Jump_Game.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean canJump(int[] nums) {
5+
/*
6+
* Constraints
7+
* 1 <= nums.length <= 10^4
8+
* 0 <= nums[i] <= 10^5
9+
*
10+
* Solution
11+
* 1. Use BFS Algorithm.
12+
* - reason 1 : have to ignore array which is not visited.
13+
* - reason 2 : we have to visit all possible array from array[start].
14+
*/
15+
16+
int N = nums.length;
17+
ArrayDeque<Integer> q = new ArrayDeque<>();
18+
boolean[] visited = new boolean[N];
19+
20+
// First, add first array index.
21+
// And, set visited[first_index] to true.
22+
q.add(0);
23+
visited[0] = true;
24+
25+
// Axiom : if N is 1, result is true.
26+
if(N == 1) return true;
27+
28+
// BFS algorithm
29+
while(!q.isEmpty()) {
30+
int cur = q.poll();
31+
32+
// find cur + 1 to cur + nums[cur]
33+
for(int i = 1; i <= nums[cur]; i++) {
34+
if(cur + i >= N - 1) return true;
35+
int next = Math.min(cur + i, N - 1);
36+
37+
// set visited[next] to true and add index into queue.
38+
// because of time limit(No overlap steps.)
39+
if(!visited[next]) {
40+
visited[next] = true;
41+
q.add(next);
42+
}
43+
}
44+
}
45+
46+
return false;
47+
48+
}
49+
}

python/2420_Find_All_Good_Indices.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#2420_Find_All_Good_Indices.py
2+
class Solution:
3+
def goodIndices(self, nums: List[int], k: int) -> List[int]:
4+
# posi : count the increasing idxes
5+
# nega : count the decreasing idxes
6+
posi, nega = [0], [0]
7+
8+
for i in range(1, len(nums)):
9+
diff = nums[i] - nums[i - 1]
10+
11+
posi.append(posi[i - 1])
12+
nega.append(nega[i - 1])
13+
14+
# if diff show positive or negative
15+
# then the value will updated
16+
if diff > 0:
17+
posi[i] += 1
18+
elif diff < 0:
19+
nega[i] += 1
20+
21+
# ans : count the idxes that
22+
# before k element is non increasing
23+
# after k element is non decreasing
24+
ans = []
25+
for i in range(k, len(nums) - k):
26+
if i + k >= len(nums):
27+
break
28+
29+
# check the condition with
30+
# for after, nega[i + 1], nega[i + k] is the two to check
31+
# for brfore, posi[i - 1], posi[i - k] is the two to check
32+
if nega[i + k] - nega[i + 1] > 0:
33+
continue
34+
if posi[i - 1] - posi[i - k] > 0:
35+
continue
36+
37+
ans.append(i)
38+
return ans

0 commit comments

Comments
 (0)