Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added java solution for 14 / python solution for 2420 #2

Merged
merged 1 commit into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions java/014_Longest_Common_Prefix.java
@@ -1,16 +1,17 @@
//014_Longest_Common_Prefix.java
class Solution {
public String longestCommonPrefix(String[] strs) {
String result ="";
String temp = "";
int c = 0;
int c = 0; //move first point
boolean check = true;
while(true){
for(int i = 0; i<strs.length; i++){
for(int i = 0; i<strs.length; i++){ //move second point
if(c>=strs[i].length()){
check = false;
break;
}
if(i==0){
if(i==0){ //temp -> check same Character
temp = Character.toString(strs[0].charAt(c));
}
if(!temp.equals(Character.toString(strs[i].charAt(c)))){
Expand Down
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