str:
+ res = ""
+ while n > 0:
+ n -= 1
+ res = chr(65 + n % 26) + res
+ n //= 26
+ return res
diff --git a/python/1909_Remove_One_Element_to_Make_the_Array_Strictly_Increasing.py b/python/1909_Remove_One_Element_to_Make_the_Array_Strictly_Increasing.py
new file mode 100644
index 0000000..5ae5c6f
--- /dev/null
+++ b/python/1909_Remove_One_Element_to_Make_the_Array_Strictly_Increasing.py
@@ -0,0 +1,37 @@
+class Solution:
+ def canBeIncreasing(self, nums: List[int]) -> bool:
+ # bruteforcing the whole idxes.
+ canBe = [0] * len(nums)
+
+ # choosing the idx that will be removed.
+ for bannedIdx in range(len(nums)):
+ Flag = 1
+
+ # if the bannedIdx is 0 than the startIdx will be 2.
+ # when bannedIdx is 0, idx 2 is the first element that has a previous element.
+ # In other cases, idx 1 is the one.
+ for i in range(1 if bannedIdx != 0 else 2, len(nums)):
+ # if i is bannedIdx than just skip it.
+ if i == bannedIdx:
+ continue
+
+ # if the previous element is banned.
+ # compare [i] with [i - 2]
+ if i - 1 == bannedIdx:
+ if nums[i] <= nums[i - 2]:
+ Flag = 0
+ break
+ continue
+
+ # compare [i] with [i - 1]
+ if nums[i] <= nums[i - 1]:
+ Flag = 0
+ break
+
+ # end of loop we will get Flag that has a 0 or 1 value.
+ canBe[bannedIdx] = Flag
+
+ if sum(canBe) > 0:
+ return True
+ return False
+
diff --git a/python/207_Course_Schedule.py b/python/207_Course_Schedule.py
new file mode 100644
index 0000000..0388fd9
--- /dev/null
+++ b/python/207_Course_Schedule.py
@@ -0,0 +1,28 @@
+from collections import defaultdict
+
+class Solution(object):
+ # Adapted from https://youtu.be/yPldqMtg-So
+
+ def hasCycle(self, course, deps, visited, tracker):
+ visited.add(course)
+ tracker.add(course)
+ for n in deps[course]:
+ if n not in visited and self.hasCycle(n, deps, visited, tracker):
+ return True
+ if n in tracker:
+ return True
+ tracker.remove(course)
+ return False
+
+ def canFinish(self, numCourses, prerequisites):
+ deps = defaultdict(set)
+ for course, pre in prerequisites:
+ deps[pre].add(course)
+
+ visited = set()
+ for course in range(numCourses):
+ tracker = set()
+ if self.hasCycle(course, deps, visited, tracker):
+ return False
+
+ return True
diff --git a/python/2409_Count_Days_Spent_Together.py b/python/2409_Count_Days_Spent_Together.py
new file mode 100644
index 0000000..13747f1
--- /dev/null
+++ b/python/2409_Count_Days_Spent_Together.py
@@ -0,0 +1,38 @@
+class Solution:
+ def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
+ # split the dates to month and day.
+ arriveAliceMonth, arriveAliceDay = map(int, arriveAlice.split("-"))
+ leaveAliceMonth, leaveAliceDay = map(int, leaveAlice.split("-"))
+ arriveBobMonth, arriveBobDay = map(int, arriveBob.split("-"))
+ leaveBobMonth, leaveBobDay = map(int, leaveBob.split("-"))
+
+ # prefixOfCalendar : initialize the calendar and in the past we will use this to convert month to day, index is 1 - based
+ # spentTogether, aliceSpent : work as cache list. and index is 1 - based
+ calendar = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+ prefixOfCalendar = [0] * 13
+ totalDates = sum(calendar)
+ spentTogether, aliceSpent = [0] * (totalDates + 1), [0] * (totalDates + 1)
+
+ # calculate the prefix of calendar
+ for i in range(1, len(calendar)):
+ prefixOfCalendar[i] = prefixOfCalendar[i - 1] + calendar[i]
+
+ # if the string is "01-15", it can be treat as 15 days.
+ # if the string is "02-27", it can be treat as 58 days.
+ # So, it can be "prefixOfCalendar[month - 1] + day"
+ # and in the problem it includes the leaveDate so +1 need to be in .
+ arriveAliceTotal = prefixOfCalendar[arriveAliceMonth - 1] + arriveAliceDay
+ leaveAliceTotal = prefixOfCalendar[leaveAliceMonth - 1] + leaveAliceDay
+ for i in range(arriveAliceTotal, leaveAliceTotal + 1):
+ aliceSpent[i] += 1
+
+ # check the aliceSpent[i] is True.
+ # if it is, they spentTogether is True too.
+ arriveBobTotal = prefixOfCalendar[arriveBobMonth - 1] + arriveBobDay
+ leaveBobTotal = prefixOfCalendar[leaveBobMonth - 1] + leaveBobDay
+ for i in range(arriveBobTotal, leaveBobTotal + 1):
+ if aliceSpent[i]:
+ spentTogether[i] += 1
+
+ # I used list because of this sum function.
+ return sum(spentTogether)
diff --git a/python/2413_Smallest_Even_Multiple.py b/python/2413_Smallest_Even_Multiple.py
new file mode 100644
index 0000000..02b723f
--- /dev/null
+++ b/python/2413_Smallest_Even_Multiple.py
@@ -0,0 +1,15 @@
+class Solution:
+ def smallestEvenMultiple(self, n: int) -> int:
+ """
+ n : positive integer
+ return : smallest positive integer that is a multiple of both 2 and n
+ """
+ if n % 2 == 0:
+ # if n is alreay muliply by 2
+ # return itself
+ return n
+
+ # if previous condition is false
+ # n * 2 is the smallest positive integer.
+ return n * 2
+
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
diff --git a/python/2429_Minimize_XOR.py b/python/2429_Minimize_XOR.py
new file mode 100644
index 0000000..2405feb
--- /dev/null
+++ b/python/2429_Minimize_XOR.py
@@ -0,0 +1,44 @@
+class Solution:
+ def minimizeXor(self, num1: int, num2: int) -> int:
+ # remove "0b" in front of num1, num2
+ num1, num2 = bin(num1)[2:], bin(num2)[2:]
+ lenNum1, lenNum2 = len(num1), len(num2)
+ ones = num2.count("1")
+ maxLen = max(lenNum1, lenNum2)
+
+ # ans list have elements same as the maxLen
+ ans = []
+ for _ in range(maxLen):
+ ans.append("0")
+
+ # add "0" in front of the binary numbers to make indexing easier
+ for _ in range(maxLen - lenNum1):
+ num1 = "0" + num1
+
+ for _ in range(maxLen - lenNum2):
+ num2 = "0" + num2
+
+ # now make "x XOR num1" minimal
+ # fill the ans list from index "0"
+ # because XOR give 0 when the elements are same.
+ for i in range(len(num1)):
+ if num1[i] == "1" and ones:
+ ans[i] = "1"
+ ones -= 1
+
+ # if we still got "1" to fill in the ans list.
+ # "1" need to be fill from the back of ans list.
+ # to maintain the number small.
+ for i in range(len(ans) - 1, -1, -1):
+ if ones < 1:
+ break
+
+ if ans[i] == "1":
+ continue
+
+ ans[i] = "1"
+ ones -= 1
+
+ # make the ans in string
+ ans = "".join(ans)
+ return int(ans, 2)
diff --git a/python/380_Insert_Delete_GetRandom.py b/python/380_Insert_Delete_GetRandom.py
new file mode 100644
index 0000000..f222d7d
--- /dev/null
+++ b/python/380_Insert_Delete_GetRandom.py
@@ -0,0 +1,33 @@
+import random
+
+class RandomizedSet(object):
+
+ def __init__(self):
+ self.num_to_idx = {}
+ self.num_list = []
+
+ def insert(self, val):
+ if val in self.num_to_idx:
+ return False
+ else:
+ self.num_list.append(val)
+ self.num_to_idx[val] = len(self.num_list) - 1
+ return True
+
+ def remove(self, val):
+ if val not in self.num_to_idx:
+ return False
+
+ idx = self.num_to_idx[val]
+ last = self.num_list[-1]
+
+ # swap last elem to current spot so you can pop the end
+ self.num_list[idx] = last
+ self.num_list.pop()
+ self.num_to_idx[last] = idx
+ del self.num_to_idx[val]
+
+ return True
+
+ def getRandom(self):
+ return random.choice(self.num_list)
diff --git a/python/392_Is_Subsequence.py b/python/392_Is_Subsequence.py
new file mode 100644
index 0000000..5cf66c0
--- /dev/null
+++ b/python/392_Is_Subsequence.py
@@ -0,0 +1,11 @@
+class Solution:
+ def isSubsequence(self, s: str, t: str) -> bool:
+ for a in s:
+ if a in t:
+ for b in range(0, len(t)):
+ if a==t[b]:
+ t=t[b+1:]
+ break
+ else:
+ return(False)
+ return(True)
diff --git a/python/441_Arranging_Coins.py b/python/441_Arranging_Coins.py
new file mode 100644
index 0000000..eeeb750
--- /dev/null
+++ b/python/441_Arranging_Coins.py
@@ -0,0 +1,7 @@
+class Solution(object):
+ def arrangeCoins(self, n):
+ level = 0
+ while n > level:
+ level += 1
+ n -= level
+ return level
diff --git a/python/523_Continuous_Subarray_Sum.py b/python/523_Continuous_Subarray_Sum.py
new file mode 100644
index 0000000..d37ceeb
--- /dev/null
+++ b/python/523_Continuous_Subarray_Sum.py
@@ -0,0 +1,20 @@
+class Solution:
+ def checkSubarraySum(self, nums: List[int], k: int) -> bool:
+ # remeainders[0] = 0 is for when x == 0
+ remainders = dict()
+ remainders[0] = 0
+ pre_sum = 0
+
+ for idx, item in enumerate(nums):
+ pre_sum += item
+ remaind = pre_sum % k
+
+ # remainder doesnt exist then it has to be init
+ # if it exists, then check the prev one has the same remainder
+ if remaind not in remainders:
+ remainders[remaind] = idx + 1
+ elif remainders[remaind] < idx:
+ return True
+
+ return False
+
diff --git a/python/732_My_Calendar_III.py b/python/732_My_Calendar_III.py
new file mode 100644
index 0000000..6347108
--- /dev/null
+++ b/python/732_My_Calendar_III.py
@@ -0,0 +1,17 @@
+from sortedcontainers import SortedDict
+
+
+class MyCalendarThree:
+ def __init__(self):
+ self.timeline = SortedDict()
+
+ def book(self, start: int, end: int) -> int:
+ self.timeline[start] = self.timeline.get(start, 0) + 1
+ self.timeline[end] = self.timeline.get(end, 0) - 1
+
+ ans = 0
+ activeEvents = 0
+
+ for count in self.timeline.values():
+ activeEvents += count
+ ans = max(ans, activeEvents)
diff --git a/python/981_Time_Based_Store.py b/python/981_Time_Based_Store.py
new file mode 100644
index 0000000..3320458
--- /dev/null
+++ b/python/981_Time_Based_Store.py
@@ -0,0 +1,26 @@
+from collections import defaultdict
+
+class TimeMap(object):
+
+ def __init__(self):
+ self.store = defaultdict(list)
+
+ def set(self, key, value, timestamp):
+ self.store[key].append((value, timestamp))
+
+ def get(self, key, timestamp):
+ values = self.store.get(key, [])
+ res = ""
+
+ l = 0
+ r = len(values) - 1
+
+ while l <= r:
+ mid = (l + r) // 2
+ if values[mid][1] <= timestamp:
+ l = mid + 1
+ res = values[mid][0]
+ else:
+ r = mid - 1
+
+ return res
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy