Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added java solution for 14 / python solution for 2420 (#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>
- Loading branch information
1 parent
aaec50a
commit 183f57f
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |