Skip to content

Instantly share code, notes, and snippets.

@agnel
agnel / codesignal-arcade-intro-edge-of-the-ocean-shapeArea.md
Last active June 16, 2022 09:48
Codesignal - Aracde Intro - Edge of the Ocean - shapeArea

Codesignal

Aracde > Intro > Edge of the Ocean

Challenge: shapeArea

Imgur

@agnel
agnel / day-3-two-pointers-167-two_sum_ii_input_array_is_sorted.md
Created June 10, 2022 19:01
Algorithm I Day 3 Two Pointers 167. Two Sum II - Input Array is Sorted

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:

Algorithm I Day 3 Two Pointers 167 Two Sum II Input array is sorted - Visual Explanation

@agnel
agnel / SimpleStorage.sol
Created June 10, 2022 13:42
The SimpleStorage contract in Solidity from Lesson 1: Welcome to Remix! Simple Storage on FreeCodeCamp
// 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:

  1. Approach 1: Brute Force
  2. Approach 2: Using Extra Array
  3. Approach 3: Using Cyclic Replacements
  4. 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
@agnel
agnel / prefix_set.rb
Last active June 4, 2022 16:27
Codility Challange: PrefixSet [Ruby solution]
=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 = {}
@agnel
agnel / qiskit-chapter-2-exercise-3.4-question-2.ipynb
Last active May 17, 2022 07:15
Solution to Qiskit Textbook Chapter 2 Exercise 3.4 Question 2
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@agnel
agnel / qiskit-chapter-2-exercise-3.4-question-1.ipynb
Last active May 17, 2022 06:58
Solution to Qiskit Textbook Chapter 2 Exercise 3.4 Question 1
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@agnel
agnel / min_max.js
Created May 7, 2022 10:10
Find min and max values without Math functions in js
/**
* These methods only work for an array of numbers
*/
function findMinimum(array) {
let length = array.length;
let min = Infinity;
while(length--) {
if(array[length] < min) {
@agnel
agnel / Counter.rb
Last active March 9, 2022 21:39
A base 36 incrementing Counter Model for Mongoid
module CounterIncrementor
def next(counter_name)
counter = self.find_by(name: counter_name)
counter.increment
end
end
class Counter
include Mongoid::Document
include Mongoid::Timestamps