Skip to content

Commit

Permalink
Added java solution for 14 / python solution for 2420 (#61)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Oct 2, 2022
1 parent aaec50a commit 183f57f
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -242,7 +242,10 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
| 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) |
| 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 |
| 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 |
| 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>) |
| 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 |
| 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 |
| 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 |

| # | To Understand |
|---| ----- |
Expand Down
33 changes: 33 additions & 0 deletions java/014_Longest_Common_Prefix.java
@@ -0,0 +1,33 @@
//014_Longest_Common_Prefix.java
class Solution {
public String longestCommonPrefix(String[] strs) {
String result ="";
String temp = "";
int c = 0; //move first point
boolean check = true;
while(true){
for(int i = 0; i<strs.length; i++){ //move second point
if(c>=strs[i].length()){
check = false;
break;
}
if(i==0){ //temp -> check same Character
temp = Character.toString(strs[0].charAt(c));
}
if(!temp.equals(Character.toString(strs[i].charAt(c)))){
check = false;
break;
}
if(i==strs.length-1){
result += temp;
}
}
if(!check){
break;
}
c++;
}
return result;

}
}
49 changes: 49 additions & 0 deletions java/055_Jump_Game.java
@@ -0,0 +1,49 @@
import java.util.*;

class Solution {
public boolean canJump(int[] nums) {
/*
* Constraints
* 1 <= nums.length <= 10^4
* 0 <= nums[i] <= 10^5
*
* Solution
* 1. Use BFS Algorithm.
* - reason 1 : have to ignore array which is not visited.
* - reason 2 : we have to visit all possible array from array[start].
*/

int N = nums.length;
ArrayDeque<Integer> q = new ArrayDeque<>();
boolean[] visited = new boolean[N];

// First, add first array index.
// And, set visited[first_index] to true.
q.add(0);
visited[0] = true;

// Axiom : if N is 1, result is true.
if(N == 1) return true;

// BFS algorithm
while(!q.isEmpty()) {
int cur = q.poll();

// find cur + 1 to cur + nums[cur]
for(int i = 1; i <= nums[cur]; i++) {
if(cur + i >= N - 1) return true;
int next = Math.min(cur + i, N - 1);

// set visited[next] to true and add index into queue.
// because of time limit(No overlap steps.)
if(!visited[next]) {
visited[next] = true;
q.add(next);
}
}
}

return false;

}
}
38 changes: 38 additions & 0 deletions python/2420_Find_All_Good_Indices.py
@@ -0,0 +1,38 @@
#2420_Find_All_Good_Indices.py
class Solution:
def goodIndices(self, nums: List[int], k: int) -> List[int]:
# posi : count the increasing idxes
# nega : count the decreasing idxes
posi, nega = [0], [0]

for i in range(1, len(nums)):
diff = nums[i] - nums[i - 1]

posi.append(posi[i - 1])
nega.append(nega[i - 1])

# if diff show positive or negative
# then the value will updated
if diff > 0:
posi[i] += 1
elif diff < 0:
nega[i] += 1

# ans : count the idxes that
# before k element is non increasing
# after k element is non decreasing
ans = []
for i in range(k, len(nums) - k):
if i + k >= len(nums):
break

# check the condition with
# for after, nega[i + 1], nega[i + k] is the two to check
# for brfore, posi[i - 1], posi[i - k] is the two to check
if nega[i + k] - nega[i + 1] > 0:
continue
if posi[i - 1] - posi[i - k] > 0:
continue

ans.append(i)
return ans

0 comments on commit 183f57f

Please sign in to comment.