Skip to content

Instantly share code, notes, and snippets.

View rungxanh1995's full-sized avatar
🇨🇦

Joe Pham rungxanh1995

🇨🇦
View GitHub Profile
@rungxanh1995
rungxanh1995 / solution.swift
Created April 12, 2023 17:27
Relative "Today" string instead of value from RelativeDateTimeFormatter
// This is what I have in my project for relativeDueDate string
struct TaskItem {
...
var relativeDueDate: String {
let formatter = RelativeDateTimeFormatter()
formatter.dateTimeStyle = .named
return formatter.localizedString(for: dueDate, relativeTo: .now).capitalizingFirstLetter()
}
}
@rungxanh1995
rungxanh1995 / Merge 2 Sorted Lists.md
Last active May 14, 2022 13:17
Given 2 sorted linked lists, merge them into one in sorted order

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.


Example 1:

@rungxanh1995
rungxanh1995 / Palindrome Linked List.md
Created May 12, 2022 20:43
Judge if a singly linked list is a palindrome (reversed is similar to the original one)

Given the head of a singly linked list, return true if it is a palindrome.

Example 1:

Input: head = [1,2,2,1]
Output: true

Example 2:

@rungxanh1995
rungxanh1995 / Buy and Sell stock.md
Last active January 3, 2023 22:33
Though the title of this problem is about the "time" to buy and sell stock, the required answer turned out to be the maximum profit possible. So beware!

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Example 1:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]

Example 2:

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama"
@rungxanh1995
rungxanh1995 / Binary Search Examples.md
Last active April 22, 2022 01:16
Binary Search algorithm in Java, Kotlin, Swift, C#, Python3

Problem

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

Example 1: