From 05d5cce006e1884ef75edfab491be95900f28e67 Mon Sep 17 00:00:00 2001 From: green-study Date: Fri, 30 Sep 2022 19:25:45 +0900 Subject: [PATCH] Added java solution for 14 / python solution for 2420 --- java/014_Longest_Common_Prefix.java | 7 ++--- python/2420_Find_All_Good_Indices.py | 38 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 python/2420_Find_All_Good_Indices.py diff --git a/java/014_Longest_Common_Prefix.java b/java/014_Longest_Common_Prefix.java index dd92fcd..4ff7134 100644 --- a/java/014_Longest_Common_Prefix.java +++ b/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[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)))){ diff --git a/python/2420_Find_All_Good_Indices.py b/python/2420_Find_All_Good_Indices.py new file mode 100644 index 0000000..44498cc --- /dev/null +++ b/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 \ No newline at end of file