Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a40960d

Browse files
Merge pull request #19 from mihirs16/master
added leetcode questions
2 parents b633a48 + 82defae commit a40960d

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# three sum | leetcode 15 | https://leetcode.com/problems/3sum/
2+
# - sorted; nested loop; outer loop for first element
3+
# - inner loop for two sum on rest of list
4+
# - avoid duplicates by shifting window till last occurrence
5+
6+
class Solution:
7+
def threeSum(self, nums: list[int]) -> list[list[int]]:
8+
nums.sort()
9+
N = len(nums)
10+
triplets = []
11+
for i in range(N):
12+
if i > 0 and nums[i] == nums[i - 1]:
13+
continue
14+
15+
ptrL = i + 1
16+
ptrR = N - 1
17+
while ptrL < ptrR:
18+
s = nums[i] + nums[ptrL] + nums[ptrR]
19+
if s > 0:
20+
ptrR -= 1
21+
elif s < 0:
22+
ptrL += 1
23+
else:
24+
triplets.append([nums[i], nums[ptrL], nums[ptrR]])
25+
ptrL += 1
26+
while nums[ptrL] == nums[ptrL - 1] and ptrL < ptrR:
27+
ptrL += 1
28+
29+
return triplets
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# two sum II - input array is sorted | leetcode 167 | https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
2+
# use two pointers on sorted array; if sum > target slide window left, else slide window right
3+
4+
class Solution:
5+
def twoSum(self, numbers: list[int], target: int) -> list[int]:
6+
ptrL = 0
7+
ptrR = 1
8+
N = len(numbers)
9+
10+
while ptrR < N:
11+
s = numbers[ptrR] + numbers[ptrL]
12+
if s == target:
13+
return [ptrL + 1, ptrR + 1]
14+
elif s < target:
15+
ptrL += 1
16+
ptrR += 1
17+
else:
18+
ptrL -= 1
19+
20+
# unreachable for testcases with exactly one solution
21+
return [-1, -1]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# naming a company | leetcode 2306 | https://leetcode.com/problems/naming-a-company
2+
# bucket by starting character to make it n(26^2.n) and compare each set with each other
3+
4+
class Solution:
5+
def distinctNames(self, ideas: list[str]) -> int:
6+
buckets = dict()
7+
num_distinct = 0
8+
9+
for idea in ideas:
10+
if buckets.get(idea[0]) is None:
11+
buckets[idea[0]] = {idea[1:]}
12+
else:
13+
buckets[idea[0]].add(idea[1:])
14+
15+
for prefix_i, suffix_i in buckets.items():
16+
for prefix_j, suffix_j in buckets.items():
17+
if prefix_i == prefix_j:
18+
continue
19+
common = len(suffix_i & suffix_j)
20+
common_i = len(suffix_i) - common
21+
common_j = len(suffix_j) - common
22+
num_distinct += common_i * common_j
23+
24+
return num_distinct

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /