Challenge: shapeArea
def gcd(a, b): | |
"""Compute the GCD of two numbers using the Euclidean algorithm.""" | |
while b != 0: | |
a, b = b, a % b | |
return a |
#! /usr/bin/env python3 | |
import json | |
import hashlib | |
with open('export.json') as f: | |
pws = json.load(f) | |
def build_radix_index(i, offset, lchar): | |
radix = {} |
Logic: The approach to this question differs to that of the classic Two Sum problem in that we have some direction with how we want to search for our target.
Since the array is sorted, we can make some general observations:
Smaller sums would come from the left half of the array Larger sums would come from the right half of the array Therefore, using two pointers starting at the end points of the array, we can choose to increase or decrease our current sum however we like. Pay attention to the example below:
// SPDX-License-Identifier: MIT | |
pragma solidity >= 0.6.0 <0.9.0; | |
contract SimpleStorage { | |
// this will get initialized as 0 | |
uint256 favoriteNumber; | |
struct People { | |
uint256 favoriteNumber; | |
string name; |
The official Leetcode page talks about four following approaches:
- Approach 1: Brute Force
- Approach 2: Using Extra Array
- Approach 3: Using Cyclic Replacements
- Approach 4: Using Reverse
The second approach can be done like this: (but only works in irb and not on leetcode somehow)
The Optimized implementation of Bubble Sort from GeeksforGeeks failed the below test case:
Code:
# @param {Integer[]} nums
# @return {Integer[]}
def sorted_squares(nums)
nums = nums.map { |n| n*n } # square the numbers
# now lets sort with bubble sort
=begin | |
Codility Challange: PrefixSet | |
https://app.codility.com/programmers/task/prefix_set/ | |
Solution: https://codility.com/media/train/solution-prefix-set.pdf | |
=end | |
def prefixSetGolden(arr) | |
n = arr.size | |
occur = {} |