Equal Beauty CodeChef SnackDown 2021 Round 1A Question The beauty of an (non-empty) array of integers is defined as the difference between its largest and smallest element. For example, the beauty of the array [2,3,4,4,6] is 6−2=4. An array A is said to be good if it is possible to partition the elements of A into two non-empty arrays B1 and B2 such that B1 and B2 have the same beauty. Each element of array A should be in exactly one array: either in B1 or in B2. For example, the array [6,2,4,4,4] is good because its elements can be partitioned into two arrays B1=[6,4,4] and B2=[2,4], where both B1 and B2 have the same beauty (6−4=4−2=2). You are given an array A of length N. In one move you can: Select an index i (1≤i≤N) and either increase Ai by 1 or decrease Ai by 1. Find the minimum number of moves required to make the array A good. Input Format The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follow. Each test
Island Perimeter LeetCode Solution
Question
You are given row x col
grid
representing a map where grid[i][j] = 1
represents land and grid[i][j] = 0
represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid
is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]] Output: 4
Example 3:
Input: grid = [[1,0]] Output: 4
Constraints:
row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j]
is0
or1
.- There is exactly one island in
grid
.
Explanation
The problem is quite simple we just need to develop the logic and that is most time consuming in this case since the after developing the logic the code is too easy. The problem can be solved by multiple approaches, namely Iterative method, Depth First Search, Breadth First Search.
First we discuss the iterative way. The main idea is to traverse the matrix and as soon as we find 1 we start checking all it's neighbors and if any of those neighbors are 1s then subtract the number of 1s from 4 and add it to the perimeter variable. The time complexity of this approach is O(4n) that is O(n) and the space complexity is O(1).
Second we move on to the Breadth First Search. This problem to can be solved like a typical BFS problem by calculating the difference of checking for neighbors whenever we find 1. The time complexity of this approach will be O(4*h + b) where h is the height of the matrix and b is the width of the matrix. The space complexity will be O(1).
Third we move on to the Depth First Search. This problem to can be solved like a typical DFS problem but with an extra loop for searching for neighbors. The time complexity of this approach will be O(4*h * b) where h is the height of the matrix and b is the width of the matrix. The space complexity will be O(1).
That's all now you need to code it using any of the three approaches. Hope you understood the explanation. I recommend first to try using the above explanation but if you are stuck then you can visit the solution given below in Java, C++, Python.
More information about LeetCode Daily Challenge
More Information About LeetCode Daily Challenge
LeetCode is a great coding platform which provides daily problems to solve and maintain a streak of coding. This platform helps in improvement of coding skills and attempting the daily challenge makes our practice of coding regular. With its bunch of questions available it is one of the best coding platforms where you can have self improvement. LeetCode also holds weekly and biweekly contests which is a great place to implement you knowledge and coding skills which you learnt through out the week by performing the daily challenge. It also provides badges if a coder solves all the daily problems of a month. For some lucky winners T-shirts are also given and that too for free. If you get the pro version you can get more from LeetCode like questions that are asked in interviews, detailed explanation of each chapter and each concept and much more. LeetCode challenges participants with a problem from our carefully curated collection of interview problems every 24 hours.
All users with all levels of coding background are welcome to join!
@PREMIUM USERS will have an extra carefully curated premium challenge weekly and earn extra LeetCoin rewards. The premium challenge will be available together with the first October challenge of the week and closed together with the November challenge of the week.
Starting from 2021, users who completed all Daily LeetCoding Challenge non-premium problems for the month (with or without using Time Travel Tickets) will win a badge to recognize their consistency! Your badges will be displayed on your profile page. Completing each non-premium daily challenge. (+10 LeetCoins)
Completing 25 to 30 non-premium daily challenges without using Time Travel Ticket will be eligible for an additional 25 LeetCoins. (Total = 275 to 325 LeetCoins)
Completing all 31 non-premium daily challenges without using Time Travel Ticket will be eligible for an additional 50 LeetCoins, plus a chance to win a secret prize ( Total = 385 LeetCoins + *Lucky Draw)!
Lucky Draw: Those who complete all 31 non-premium daily challenges will be automatically entered into a Lucky Draw, where LeetCode staff will randomly select 3 lucky participants to each receive one LeetCode Polo Shirt on top of their rewards! Do you have experience trying to keep a streak but failed because you missed one of the challenges?
Hoping you can travel back to the missed deadline to complete the challenge but that's not possible......or is it?
With the new Time Travel Ticket , it's possible! In order to time travel, you will need to redeem a Time Travel Ticket with your LeetCoins. You can redeem up to 3 tickets per month and the tickets are only usable through the end of the month in which they are redeemed but you can use the tickets to make up any invalid or late submission of the current year. To join, just start solving the daily problem on the calendar of the problem page. You can also find the daily problem listed on the top of the problem page. No registration is required. The daily problem will be updated on every 12 a.m. Coordinated Universal Time (UTC) and you will have 24 hours to solve that challenge. Thar's all you needed to know about Daily LeetCode Challenge.
Program Code
Java
class Solution {
public int islandPerimeter(int[][] grid) {
int perimeter = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == 1) {
if (col == 0) {
++perimeter;
} else if (grid[row][col - 1] == 0) {
++perimeter;
}
if (col == grid[row].length - 1) {
++perimeter;
} else if (grid[row][col + 1] == 0) {
++perimeter;
}
if (row == 0) {
++perimeter;
} else if (grid[row - 1][col] == 0) {
++perimeter;
}
if (row == grid.length - 1) {
++perimeter;
} else if (grid[row + 1][col] == 0) {
++perimeter;
}
}
}
}
return perimeter;
}
}
C++
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int n=grid.size(),m=grid[0].size();
int ans=0;
for(int i=0; i<n; i++)
{
int check_box=0,total_box=0,upper_box=0,flag=1;
for(int j=0; j<m; j++)
{
if(check_box==1 && grid[i][j]==0 && total_box-1>=0)
{
ans+=total_box*4-((total_box-1)*2+upper_box*2);
total_box=0,upper_box=0;
flag=0;
}
if(grid[i][j]==1)
{
flag=1;
check_box=1;
total_box++;
if(i>0 && grid[i-1][j]==1)
upper_box++;
}
}
if(flag==1 && total_box-1>=0)
ans+=total_box*4-((total_box-1)*2+upper_box*2);
}
return ans;
}
};
Python
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
row = len(grid)
col = len(grid[0])
count = 0
for i in range(row):
for j in range(col):
if grid[i][j] == 1:
count += 4
if i > 0:
count -= grid[i - 1][j]*2
if j > 0:
count -= grid[i][j - 1]*2
return count
Comments
Post a Comment