Open in app

Sign In

Write

Sign In

Aman Agarwal
Aman Agarwal

16 Followers

Home

Lists

About

Published in 100-days-of-leetcode

·Mar 26, 2020

Day 34 — Unique Paths II

https://leetcode.com/problems/unique-paths-ii/ — class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; int n = obstacleGrid[0].length; int[][] pathCount = new int[m][n]; pathCount[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1; for(int i = 1; i < n; i++) { pathCount[0][i] = obstacleGrid[0][i] == 0 ? pathCount[0][i-1] : 0; } for(int i = 1; i < m; i++){ if(obstacleGrid[i][0] == 0) { pathCount[i][0] = pathCount[i-1][0]; } else { pathCount[i][0] = 0; } } for(int i = 1; i < m; i++) { for(int j = 1; j < n; j++) { if(obstacleGrid[i][j] == 1) { pathCount[i][j] =0; continue; } if(obstacleGrid[i-1][j] == 0){ pathCount[i][j] += pathCount[i-1][j]; } if(obstacleGrid[i][j-1] == 0){ pathCount[i][j] += pathCount[i][j-1]; } } } return pathCount[m-1][n-1]; } }

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 25, 2020

Day 33–01 Matrix (Medium)

https://leetcode.com/problems/01-matrix/ — class Solution { public int[][] updateMatrix(int[][] matrix) { if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return matrix; int m = matrix.length; int n = matrix[0].length; int[][] dist = new int[m][n]; for(int[] a : dist) { Arrays.fill(a, 10001); } for(int i = 0; i < m; i++) { for(int j = 0; j< n; j++) { if(matrix[i][j] == 0) { dist[i][j] = 0; } else { if(i > 0) { dist[i][j] = Math.min(dist[i-1][j] + 1, dist[i][j]); } if(j > 0) { dist[i][j] = Math.min(dist[i][j-1] + 1, dist[i][j]); } } } } for(int i = m-1; i >= 0; i--) { for(int j = n-1; j >= 0; j--) { if(i < m-1) { dist[i][j] = Math.min(dist[i+1][j] + 1, dist[i][j]); } if(j < n-1){ dist[i][j] = Math.min(dist[i][j+1] + 1, dist[i][j]); } } } return dist; } }

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 25, 2020

Day 32 — Palindromic Substrings

https://leetcode.com/problems/palindromic-substrings/ — Solution 1 class Solution { public int countSubstrings(String s) { int n = s.length(); int ans = n…

Palindrome

1 min read

Palindrome

1 min read


Published in 100-days-of-leetcode

·Mar 24, 2020

Day 31 — Longest Common Subsequence

https://leetcode.com/problems/longest-common-subsequence/ — class Solution { public int longestCommonSubsequence(String text1, String text2) { int n = text1.length(); int m = text2.length(); char[] st1 = text1.toCharArray(); char[] st2 = text2.toCharArray(); int[][] dp = new int[n+1][m+1]; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (st1[i-1] == st2[j-1]) { dp[i][j] = dp[i-1][j-1] + 1; } else { dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); } } } return dp[n][m]; } }

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 24, 2020

Day 30 — Last Stone Weight

https://leetcode.com/problems/last-stone-weight/ — class Solution { public int lastStoneWeight(int[] stones) { if(stones == null || stones.length == 0) return 0; PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)-> b-a); for(int stone: stones) { pq.offer(stone); } while(!pq.isEmpty()) { int s1 = pq.poll(); if(!pq.isEmpty()){ int s2 = pq.poll(); if (s1 > s2) pq.offer(s1 - s2); } else { return s1; } } return 0; } }

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 21, 2020

Day 29 -Minimum Falling Path Sum II

https://leetcode.com/problems/minimum-falling-path-sum-ii/ — class Solution { public int minFallingPathSum(int[][] arr) { if (arr == null || arr.length == 0 || arr[0].length == 0) return 0; int n = arr.length; int min1, min2, minCol = 0; for (int i = n - 2; i >= 0; i--) { min1 = Integer.MAX_VALUE; min2 = Integer.MAX_VALUE; for (int j = 0; j < n; j++) { if (arr[i + 1][j] < min1) { min2 = min1; min1 = arr[i + 1][j]; minCol = j; } else if (arr[i + 1][j] < min2) { min2 = arr[i + 1][j]; } } for (int j = 0; j < n; j++) { if (j == minCol) arr[i][j] += min2; else arr[i][j] += min1; } } int result = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { result = Math.min(result, arr[0][i]); } return result; } }

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 20, 2020

Day 28 — Minimum Falling Path Sum

https://leetcode.com/problems/minimum-falling-path-sum/ — class MinFallingPathSum{ public int minFallingPathSumMethod(int[][] A) { if(A == null || A.length == 0 || A[0].length == 0) return 0; int n = A.length; for(int i = 1; i < n; i++) { for(int j = 0; j < n; j++) { int min = A[i-1][j]; if(j > 0) min = Math.min(min, A[i-1][j-1]); if(j < n-1) min = Math.min(min, A[i-1][j+1]); A[i][j] += min; } } int result = Integer.MAX_VALUE; for(int i : A[n-1]) { result = Math.min(i, result); } return result; } }

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 19, 2020

Day 27 — Unique Paths III

https://leetcode.com/problems/unique-paths-iii/ — class Solution { int rows = 0; int cols = 0; int count_zero = 0; public int uniquePathsIII(int[][] grid) {…

Leetcode

1 min read

Leetcode

1 min read


Published in 100-days-of-leetcode

·Mar 18, 2020

Day 26 — Unique Paths

https://leetcode.com/problems/unique-paths/ — class Solution { public int uniquePaths(int m, int n) { if(m == 0 || n == 0) return 0; int[][] grid = new int[m][n]; //fill all cells with 1. This will eventually be updated for(int[] ar: grid) { Arrays.fill(ar, 1); } for(int i = 1 ; i < m; i++){ for(int j = 1 ; j < n ; j++) { grid[i][j] = grid[i-1][j] + grid [i][j-1]; } } return grid[m-1][n-1]; } }

Leetcode Medium

1 min read

Leetcode Medium

1 min read


Published in 100-days-of-leetcode

·Mar 17, 2020

Day 25 — Min Cost Climbing Stairs

https://leetcode.com/problems/min-cost-climbing-stairs/ — class Solution { public int minCostClimbingStairs(int[] cost) { if(cost == null) return 0; if(cost.length ==1 ) return cost[0]; int t0 = cost[0]; int t1 = cost[1]; for(int i=2; i < cost.length ; i++ ) { int t2 = Math.min(t0, t1) +cost[i]; t0=t1; t1=t2; } return Math.min(t0, t1); } }

Leetcode

1 min read

Leetcode

1 min read

Aman Agarwal

Aman Agarwal

16 Followers

Software developer by profession, computer gamer by hobby and a foodie by heart!!

Following
  • Eric Elliott

    Eric Elliott

  • Netflix Technology Blog

    Netflix Technology Blog

  • Gaurav Goel

    Gaurav Goel

  • Abhijith Chandradas

    Abhijith Chandradas

  • Kevin Werbach

    Kevin Werbach

See all (18)

Help

Status

Writers

Blog

Careers

Privacy

Terms

About

Text to speech