Skip to content

Instantly share code, notes, and snippets.

@thekhairul
thekhairul / starDiamond.js
Last active August 2, 2023 04:00
Make star diamond with js
// 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++) {
@thekhairul
thekhairul / firstNsum.js
Created July 31, 2023 14:30
a function to calculate the sum of the first N natural numbers
function sumOfFirstN(n) {
return (n*(n+1)) / 2;
}
// Time complexity - O(1)
// Space complexity - O(1)
@thekhairul
thekhairul / twoSum.py
Created May 27, 2022 05:34
Leetcode 1: Two sum - python
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 []
@thekhairul
thekhairul / removeNthEndNode.py
Last active May 23, 2022 05:03
Leetcode 19: Remove Nth Node From End of List - python
def removeNthFromEnd(head,n):
# O(2n) || O(1)
count = 0
cur = head
while cur:
count += 1
cur = cur.next
m = count - n
@thekhairul
thekhairul / listCycle.py
Created May 21, 2022 04:42
Leetcode 141: Linked List Cycle - python
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
@thekhairul
thekhairul / linkedListPalindrome.py
Created May 21, 2022 04:39
Leetcode 234: Palindrome Linked List - Python
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
@thekhairul
thekhairul / reverseLinkedList.py
Last active May 20, 2022 09:39
Leetcode 206: Reverse linked list - python
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
@thekhairul
thekhairul / deleteMidNode.py
Created May 19, 2022 05:11
Leetcode 2095: Delete the Middle Node of a Linked List - python
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
@thekhairul
thekhairul / midNode.py
Created May 18, 2022 04:45
Leetcode 876: Middle of a linked list - python
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
@thekhairul
thekhairul / isPoweOfTwo.py
Last active May 3, 2022 11:58
Leetcode 231: Power of Two - python3
def isPowerOfTwo(n):
left = 0
right = 31
result = False
while left <= right:
mid = (left + right) // 2
if pow(2, mid) == n:
result = True