Skip to content

Commit aaec50a

Browse files
BHwiLONGNEW
andauthored
Added Java solutions for 17, 22 / Python solutions for 2413, 2409, 1909 (qiyuangong#60)
* 2413_Smallest_Even_Multiple * 2409_Count_Days_Spent_Together * 1909_Remove_One_Element_to_Make_the_Array_Strictly_Increasing In a brute-force way. * Add 17_Letter_Combinations_of_a_Phone_Number.java * Add 22_Generate_Parentheses.java Contributed by @BHwi BHwi Co-authored-by: LONGNEW <[email protected]>
1 parent d8ab69f commit aaec50a

5 files changed

+156
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
// make list for return.
3+
public ArrayList<String> list = new ArrayList<>();
4+
// make array for get phone number's characters.
5+
public char[][] arr = {
6+
{'0', '0', '0', '-'},
7+
{'0', '0', '0', '-'},
8+
{'a', 'b', 'c', '-'},
9+
{'d', 'e', 'f', '-'},
10+
{'g', 'h', 'i', '-'},
11+
{'j', 'k', 'l', '-'},
12+
{'m', 'n', 'o', '-'},
13+
{'p', 'q', 'r', 's'},
14+
{'t', 'u', 'v', '-'},
15+
{'w', 'x', 'y', 'z'},
16+
};
17+
18+
// main function
19+
public List<String> letterCombinations(String digits) {
20+
addString(digits, 0, "");
21+
return list;
22+
}
23+
24+
// axiom : if input == "", return []
25+
// if index == digits.length(), add str in list
26+
// else do loop number's character, and function recursion.
27+
public void addString(String digits, int index, String str) {
28+
if(digits.equals("")) return;
29+
if(index == digits.length()) {
30+
list.add(str);
31+
}
32+
else {
33+
for(int i = 0; i < 4; i++) {
34+
int number = Integer.parseInt(digits.charAt(index) + "");
35+
if(arr[number][i] == '-') continue;
36+
addString(digits, index + 1, str + arr[number][i]);
37+
}
38+
}
39+
}
40+
}

java/22_Generate_Parentheses.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
// main function
3+
public List<String> generateParenthesis(int n) {
4+
ArrayList<String> list = new ArrayList<>();
5+
rec(list, "(", n - 1, n);
6+
return list;
7+
}
8+
9+
// axiom : if start == end == 0, add str in list.
10+
// IDEA :
11+
// In well-formed parentheses
12+
// close character(")") has to be bigger than open character("(")
13+
// So, we can solve this problem with recursion.
14+
public void rec(List<String> list, String str, int start, int end) {
15+
if(start == 0 && end == 0) {
16+
list.add(str);
17+
}
18+
19+
if(start > 0) {
20+
rec(list, str + "(", start - 1, end);
21+
}
22+
if(end > start) {
23+
rec(list, str + ")", start, end - 1);
24+
}
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def canBeIncreasing(self, nums: List[int]) -> bool:
3+
# bruteforcing the whole idxes.
4+
canBe = [0] * len(nums)
5+
6+
# choosing the idx that will be removed.
7+
for bannedIdx in range(len(nums)):
8+
Flag = 1
9+
10+
# if the bannedIdx is 0 than the startIdx will be 2.
11+
# when bannedIdx is 0, idx 2 is the first element that has a previous element.
12+
# In other cases, idx 1 is the one.
13+
for i in range(1 if bannedIdx != 0 else 2, len(nums)):
14+
# if i is bannedIdx than just skip it.
15+
if i == bannedIdx:
16+
continue
17+
18+
# if the previous element is banned.
19+
# compare [i] with [i - 2]
20+
if i - 1 == bannedIdx:
21+
if nums[i] <= nums[i - 2]:
22+
Flag = 0
23+
break
24+
continue
25+
26+
# compare [i] with [i - 1]
27+
if nums[i] <= nums[i - 1]:
28+
Flag = 0
29+
break
30+
31+
# end of loop we will get Flag that has a 0 or 1 value.
32+
canBe[bannedIdx] = Flag
33+
34+
if sum(canBe) > 0:
35+
return True
36+
return False
37+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
3+
# split the dates to month and day.
4+
arriveAliceMonth, arriveAliceDay = map(int, arriveAlice.split("-"))
5+
leaveAliceMonth, leaveAliceDay = map(int, leaveAlice.split("-"))
6+
arriveBobMonth, arriveBobDay = map(int, arriveBob.split("-"))
7+
leaveBobMonth, leaveBobDay = map(int, leaveBob.split("-"))
8+
9+
# prefixOfCalendar : initialize the calendar and in the past we will use this to convert month to day, index is 1 - based
10+
# spentTogether, aliceSpent : work as cache list. and index is 1 - based
11+
calendar = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
12+
prefixOfCalendar = [0] * 13
13+
totalDates = sum(calendar)
14+
spentTogether, aliceSpent = [0] * (totalDates + 1), [0] * (totalDates + 1)
15+
16+
# calculate the prefix of calendar
17+
for i in range(1, len(calendar)):
18+
prefixOfCalendar[i] = prefixOfCalendar[i - 1] + calendar[i]
19+
20+
# if the string is "01-15", it can be treat as 15 days.
21+
# if the string is "02-27", it can be treat as 58 days.
22+
# So, it can be "prefixOfCalendar[month - 1] + day"
23+
# and in the problem it includes the leaveDate so +1 need to be in .
24+
arriveAliceTotal = prefixOfCalendar[arriveAliceMonth - 1] + arriveAliceDay
25+
leaveAliceTotal = prefixOfCalendar[leaveAliceMonth - 1] + leaveAliceDay
26+
for i in range(arriveAliceTotal, leaveAliceTotal + 1):
27+
aliceSpent[i] += 1
28+
29+
# check the aliceSpent[i] is True.
30+
# if it is, they spentTogether is True too.
31+
arriveBobTotal = prefixOfCalendar[arriveBobMonth - 1] + arriveBobDay
32+
leaveBobTotal = prefixOfCalendar[leaveBobMonth - 1] + leaveBobDay
33+
for i in range(arriveBobTotal, leaveBobTotal + 1):
34+
if aliceSpent[i]:
35+
spentTogether[i] += 1
36+
37+
# I used list because of this sum function.
38+
return sum(spentTogether)

python/2413_Smallest_Even_Multiple.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def smallestEvenMultiple(self, n: int) -> int:
3+
"""
4+
n : positive integer
5+
return : smallest positive integer that is a multiple of both 2 and n
6+
"""
7+
if n % 2 == 0:
8+
# if n is alreay muliply by 2
9+
# return itself
10+
return n
11+
12+
# if previous condition is false
13+
# n * 2 is the smallest positive integer.
14+
return n * 2
15+

0 commit comments

Comments
 (0)