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 2349dca

Browse files
Added Leetcode easy and medium problems
1 parent df8b948 commit 2349dca

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
# LOWEST COMMON ANCESTOR OF A BINARY SEARCH TREE
3+
4+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
5+
6+
According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself)."
7+
8+
Example 1:
9+
10+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
11+
Output: 6
12+
Explanation: The LCA of nodes 2 and 8 is 6.
13+
14+
Example 2:
15+
16+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
17+
Output: 2
18+
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
19+
20+
Example 3:
21+
22+
Input: root = [2,1], p = 2, q = 1
23+
Output: 2
24+
25+
Constraints:
26+
27+
The number of nodes in the tree is in the range [2, 105].
28+
-109 <= Node.val <= 109
29+
All Node.val are unique.
30+
p != q
31+
p and q will exist in the BST.
32+
"""
33+
34+
# Definition for a binary tree node.
35+
class TreeNode:
36+
def __init__(self, x):
37+
self.val = x
38+
self.left = None
39+
self.right = None
40+
41+
class Solution:
42+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
43+
44+
if root.val == p.val or root.val == q.val:
45+
return root
46+
47+
if p.val < root.val < q.val or q.val < root.val < p.val:
48+
return root
49+
50+
if p.val > root.val and q.val > root.val:
51+
return self.lowestCommonAncestor(root.right, p, q)
52+
53+
else:
54+
return self.lowestCommonAncestor(root.left, p, q)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
# PALINDROME LINKED LIST
3+
4+
Given a singly linked list, determine if it is a palindrome.
5+
6+
Example 1:
7+
8+
Input: 1->2
9+
Output: false
10+
11+
Example 2:
12+
13+
Input: 1->2->2->1
14+
Output: true
15+
16+
Follow up:
17+
Could you do it in O(n) time and O(1) space?
18+
"""
19+
20+
# Definition for singly-linked list.
21+
class ListNode:
22+
def __init__(self, val=0, next=None):
23+
self.val = val
24+
self.next = next
25+
26+
class Solution:
27+
def isPalindrome(self, head: ListNode) -> bool:
28+
29+
slow = head
30+
fast = head
31+
while fast and fast.next:
32+
slow = slow.next
33+
fast = fast.next.next
34+
35+
parent = None
36+
cur = head
37+
while cur != slow:
38+
temp = cur.next
39+
cur.next = parent
40+
parent = cur
41+
cur = temp
42+
43+
if fast and not fast.next:
44+
slow = slow.next
45+
46+
while parent:
47+
if parent.val != slow.val:
48+
return False
49+
parent = parent.next
50+
slow = slow.next
51+
52+
return True
53+
54+
55+

‎Leetcode/medium/elimination-game.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
# ELIMINATION GAME
3+
4+
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
5+
6+
Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.
7+
8+
We keep repeating the steps again, alternating left to right and right to left, until a single number remains.
9+
10+
Find the last number that remains starting with a list of length n.
11+
12+
Example:
13+
14+
Input:
15+
n = 9,
16+
1 2 3 4 5 6 7 8 9
17+
2 4 6 8
18+
2 6
19+
6
20+
21+
Output:
22+
6
23+
"""
24+
25+
class Solution:
26+
def lastRemaining(self, n: int) -> int:
27+
28+
k = 0
29+
start = 1
30+
left = True
31+
while n > 1:
32+
# print("k:",k,"start:",start,"n:",n)
33+
if left:
34+
start = start + pow(2, k)
35+
left = False
36+
else:
37+
if n % 2 != 0:
38+
start = start + pow(2, k)
39+
left = True
40+
41+
k += 1
42+
n = n // 2
43+
44+
return start

0 commit comments

Comments
(0)

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