Skip to content

Commit 4e8ae6a

Browse files
Merge pull request #2099 from CodingTestStudy2/최원준
[최원준] Day29
2 parents c3b232d + 7923799 commit 4e8ae6a

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def majorityElement(self, nums: list[int]) -> int:
3+
n = len(nums)
4+
majority = n/2
5+
6+
counter = Counter(nums)
7+
for num, freq in counter.items():
8+
if freq >= majority:
9+
return num
10+
11+
return -1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(t) != len(s):
4+
return False
5+
6+
counter1 = Counter(s)
7+
counter2 = Counter(t)
8+
9+
for char1, freq in counter1.items():
10+
if counter2[char1] != freq:
11+
return False
12+
return True
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
3+
'''
4+
1. 아이디어 :
5+
diff가 다를때마다 자른다.
6+
자른 길이를 재귀함수를 통해 경우의 수를 구한다
7+
8+
2. 시간복잡도 :
9+
O(n + n)
10+
11+
3. 자료구조/알고리즘 :
12+
dfs
13+
14+
'''
15+
class Solution:
16+
def numberOfArithmeticSlices(self, nums: list[int]) -> int:
17+
if len(nums) <3:
18+
return 0
19+
20+
@cache
21+
def calc(n):
22+
return n // 3 if n <= 3 else n-2 + calc(n-1)
23+
24+
ans = 0
25+
prevs = [nums[0], nums[1]]
26+
diff = nums[1] - nums[0]
27+
28+
for num in nums[2:]:
29+
if num - prevs[-1] == diff:
30+
prevs.append(num)
31+
32+
elif num - prevs[-1] != diff:
33+
if len(prevs) >= 3:
34+
ans += calc(len(prevs))
35+
36+
diff = num - prevs[-1]
37+
prevs = [prevs[-1], num]
38+
ans += calc(len(prevs))
39+
return ans
40+

0 commit comments

Comments
 (0)