lek-tin:

Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1 Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2 Input: [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. Note The length of the given binary array will not exceed 50,000.

by lek tin in "algorithm" access_time 1-min read

Monthly Transactions Ii

Table: Transactions +----------------+---------+ | Column Name | Type | +----------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date | +----------------+---------+ id is the primary key of this table. The table has information about incoming transactions. The state column is an enum of type ["approved", "declined"]. Table: Chargebacks +----------------+---------+ | Column Name | Type | +----------------+---------+ | trans_id | int | | charge_date | date | +----------------+---------+ Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.

by lek tin in "database" access_time 2-min read

Monthly Transactions I

Table: Transactions +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date | +---------------+---------+ id is the primary key of this table. The table has information about incoming transactions. The state column is an enum of type ["approved", "declined"]. Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.

by lek tin in "database" access_time 2-min read

Last Stone Weight

We have a collection of stones, each stone has a positive integer weight. Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.

by lek tin in "algorithm" access_time 2-min read

Basic Calculator Ii

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. Example 1 Input: "3+2*2" Output: 7 Example 2 Input: " 3/2 " Output: 1 Example 3 Input: " 3+5 / 2 " Output: 5 Note You may assume that the given expression is always valid. Do not use the eval built-in library function.

by lek tin in "algorithm" access_time 2-min read

Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . Example 1 Input: "1 + 1" Output: 2 Example 2 Input: " 2-1 + 2 " Output: 3 Example 3 Input: "(1+(4+5+2)-3)+(6+8)" Output: 23 Note You may assume that the given expression is always valid.

by lek tin in "algorithm" access_time 1-min read

Backspace String Compare

Solution (build string ❌) Time: O(N) Space: O(N) class Solution: def backspaceCompare(self, S: str, T: str) -> bool: res_1 = self.helper(S) res_2 = self.helper(T) return len(res_1) == len(res_2) and res_1 == res_2 def helper(self, s): stack = [] for c in s: if c != "#": stack.append(c) else: if len(stack) > 0: stack.pop() return "".join(stack) s Solution (two pointers 👍🏼) Time: O(N) Space: O(1) from itertools import zip_longest class Solution: def backspaceCompare(self, S: str, T: str) -> bool: return all( a == b for a, b in zip_longest(self.

by lek tin in "algorithm" access_time 1-min read

Insertion Sort List

Algorithm of Insertion Sort Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. Example 1 Input: 4->2->1->3 Output: 1->2->3->4 Example 2 Input: -1->5->3->4->0 Output: -1->0->3->4->5 Solution # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.

by lek tin in "algorithm" access_time 1-min read

Middle of the Linked List

Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. Example 1 Input: [1,2,3,4,5] Output: Node 3 from this list (Serialization: [3,4,5]) The returned node has value 3. (The judge's serialization of this node is [3,4,5]). Note that we returned a ListNode object ans, such that: ans.val = 3, ans.next.val = 4, ans.

by lek tin in "algorithm" access_time 1-min read

Sort List

Sort a linked list in O(n log n) time using constant space complexity. Example 1 Input: 4->2->1->3 Output: 1->2->3->4 Example 2 Input: -1->5->3->4->0 Output: -1->0->3->4->5 Solution (merge sort - top down) Time: O(NlogN) Space: O(logN) Doesn’t satisfy the requirement # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def sortList(self, head: ListNode) -> ListNode: if not head or not head.

by lek tin in "algorithm" access_time 2-min read