This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// radius (largest row of stars) must be an odd number | |
function makeDiamondWithStars(radius) { // let's say radius is 9 | |
let starsOrder = []; // we list down the number of stars needed to put on each line | |
for (let i = 1; i <= radius; i+=2) { | |
starsOrder.push(i); | |
} | |
// after this loop starsOrder array is [1, 3, 5, 7, 9] | |
starsOrder = starsOrder.concat(starsOrder.slice(0,-1).reverse()); // now starsOrder is [1, 3, 5, 7, 9, 7, 5, 3, 1] | |
const midOfTarget = Math.floor(radius/2); // 4 | |
for (let j=0; j<starsOrder.length; j++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumOfFirstN(n) { | |
return (n*(n+1)) / 2; | |
} | |
// Time complexity - O(1) | |
// Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def twoSum(nums, target): | |
dict = {} | |
for i in range(len(nums)): | |
if ((target-nums[i]) in dict): | |
return [dict[target - nums[i]], i] | |
dict[nums[i]] = i | |
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def removeNthFromEnd(head,n): | |
# O(2n) || O(1) | |
count = 0 | |
cur = head | |
while cur: | |
count += 1 | |
cur = cur.next | |
m = count - n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hasCycle(head): | |
fast, slow = head, head | |
while fast and fast.next: | |
fast = fast.next.next | |
slow = slow.next | |
if fast == slow: | |
return True | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isPalindrome(head): | |
if head == None or head.next == None: | |
return True | |
# find mid | |
mid, fast = head, head | |
prevOfMid = None | |
while fast and fast.next: | |
prevOfMid = mid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | |
if head == None or head.next == None: | |
return head | |
curNode = head | |
prevNode = None | |
while curNode: | |
nextNode = curNode.next | |
curNode.next = prevNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deleteMiddle(head): | |
if head.next: | |
slow = head | |
fast = head | |
beforeMid = None | |
while fast and fast.next: | |
fast = fast.next.next | |
beforeMid = slow | |
slow = slow.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def middleNode(head): | |
#find total length | |
count = 1 | |
cur = head | |
while cur.next: | |
count += 1 | |
cur = cur.next | |
#find mid | |
mid = int(count/2 + 1) | |
#find mid node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isPowerOfTwo(n): | |
left = 0 | |
right = 31 | |
result = False | |
while left <= right: | |
mid = (left + right) // 2 | |
if pow(2, mid) == n: | |
result = True |
NewerOlder