Skip to content

Instantly share code, notes, and snippets.

View vrat28's full-sized avatar
💭
do { programming() } while !success

Varun vrat28

💭
do { programming() } while !success
View GitHub Profile
@vrat28
vrat28 / min_cost_climbing_stairs.swift
Created June 8, 2021 06:29
Min cost climbing stairs (Swift| LC 746)
class Solution {
func minCostClimbingStairs(_ cost: [Int]) -> Int {
var dp = [Int](repeating:0, count: cost.count+1)
for i in 2..<dp.count{
let oneStepCost = dp[i - 1] + cost[i - 1]
let twoStepCost = dp[i - 2] + cost[i - 2]
dp[i] = min(oneStepCost, twoStepCost)
}
return dp[cost.count]
@vrat28
vrat28 / min_cost_climbing_stairs.py
Created June 8, 2021 06:26
Min cost climbing stairs
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
# The array's length should be 1 longer than the length of cost
# This is because we can treat the "top floor" as a step to reach
minimum_cost = [0] * (len(cost) + 1)
# Start iteration from step 2, since the minimum cost of reaching
# step 0 and step 1 is 0
for i in range(2, len(cost) + 1):
take_one_step = minimum_cost[i - 1] + cost[i - 1]
@vrat28
vrat28 / min_cost_climbing_stairs.java
Created June 8, 2021 06:25
Min Cost climbing stairs(DP | Bottom-up )
class Solution {
public int minCostClimbingStairs(int[] cost) {
// The array's length should be 1 longer than the length of cost
// This is because we can treat the "top floor" as a step to reach
int minimumCost[] = new int[cost.length + 1];
// Start iteration from step 2, since the minimum cost of reaching
// step 0 and step 1 is 0
for (int i = 2; i < minimumCost.length; i++) {
int takeOneStep = minimumCost[i - 1] + cost[i - 1];
@vrat28
vrat28 / loadEntityAsync.swift
Created June 6, 2021 14:53 — forked from eospi/loadEntityAsync.swift
RealityKit Asynchronous Loading
import Combine
func loadEntityAsync() {
// Create an world anchor at the origin and add it to the scene
let anchor = AnchorEntity(world: [0,0,0])
arView.scene.addAnchor(anchor)
let usdzPath = "path/to/usdz/asset"
// Load the asset asynchronously
@vrat28
vrat28 / max_area_cake.py
Created June 4, 2021 04:49
Max Area of cake (LC 1465)
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# Start by sorting the inputs
horizontalCuts.sort()
verticalCuts.sort()
# Consider the edges first
max_height = max(horizontalCuts[0], h - horizontalCuts[-1])
for i in range(1, len(horizontalCuts)):
# horizontalCuts[i] - horizontalCuts[i - 1] represents the distance between
@vrat28
vrat28 / max_area_cake.java
Created June 4, 2021 04:48
Max Area of Cake after cuts (LC 1465 | Java)
class Solution {
// We will use long instead of int to prevent overflow
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
// Start by sorting the inputs
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
int n = horizontalCuts.length;
int m = verticalCuts.length;
// Consider the edges first
@vrat28
vrat28 / max_area_cake.swift
Last active June 4, 2021 04:44
Max Area Cake after cuts(Leetcode 1465)
//Link: https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
class Solution {
func maxArea(_ h: Int, _ w: Int, _ horizontalCuts: [Int], _ verticalCuts: [Int]) -> Int {
// first sort the cuts
let hcuts = horizontalCuts.sorted()
let vcuts = verticalCuts.sorted()
var maxWidth:Int = 0, maxHeight:Int = 0
// Find max gap b/w the vertical lines = Width
@vrat28
vrat28 / max_area_island.py
Created June 2, 2021 03:58
Max Area (DFS,Recursive,Py)
class Solution(object):
def maxAreaOfIsland(self, grid):
seen = set()
def area(r, c):
if not (0 <= r < len(grid) and 0 <= c < len(grid[0])
and (r, c) not in seen and grid[r][c]):
return 0
seen.add((r, c))
return (1 + area(r+1, c) + area(r-1, c) +
area(r, c-1) + area(r, c+1))
@vrat28
vrat28 / max_area_island.java
Created June 2, 2021 03:57
Max Area Island (DFS+Recursive)
class Solution {
int[][] grid;
boolean[][] seen;
public int area(int r, int c) {
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length ||
seen[r][c] || grid[r][c] == 0)
return 0;
seen[r][c] = true;
return (1 + area(r+1, c) + area(r-1, c)
@vrat28
vrat28 / max_area_island.swift
Created June 2, 2021 03:55
Max Area Island (DFS)
class Solution {
func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
var maxArea = 0 , grid = grid
for i in 0..<grid.count{
for j in 0..<grid[0].count{
if grid[i][j] == 0 {
continue
}
let area = dfs(i,j,&grid)
maxArea = max(area,maxArea)