Special Positions in a Binary Matrix

🏠 ⬅️ ➡️

Given an m x n binary matrix mat, return the number of special positions in mat.

A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).

Example 1:

Input: mat = [[1,0,0],[0,0,1],[1,0,0]] Output: 1 Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.

Example 2:

Input: mat = [[1,0,0],[0,1,0],[0,0,1]] Output: 3 Explanation: (0, 0), (1, 1) and (2, 2) are special positions.

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • mat[i][j] is either 0 or 1.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: m = 3, n = 3
    integer :: i, j
    logical :: is_special(m, n)
    integer :: num_special

    ! Example 1
    is_special(:, :) = .false.
    is_special(1, 2) = .true.
    write(*, '(I0)') count_special(is_special)

    ! Example 2
    is_special(:, :) = .false.
    is_special(1, 1) = .true.
    is_special(2, 2) = .true.
    write(*, '(I0)') count_special(is_special)

contains

    function count_special(mat) result(num_special)
        implicit none
        logical, intent(in) :: mat(m, n)
        integer :: i, j, num_special

        do i = 1, m
            do j = 1, n
                if (mat(i, j)) then
                    if (all(mat(i, :) == 0) .and. all(mat(:, j) == 0)) then
                        num_special = num_special + 1
                    end if
                end if
            end do
        end do
    end function count_special

end program main
Compiled
Executed
Correct
program specialPositions
      implicit none

      integer, parameter :: m = 3, n = 3
      integer :: mat(m, n)
      integer :: i, j
      integer :: specialPositions

      ! Read the matrix from the input
      read(*,*) mat

      ! Initialize the number of special positions to 0
      specialPositions = 0

      ! Loop through each row and column
      do i = 1, m
         do j = 1, n
            ! If the current element is 1 and all other elements in the row and column are 0, then it is a special position
            if (mat(i, j) == 1 .and. all(mat(i, :) == 0) .and. all(mat(:, j) == 0)) then
               specialPositions = specialPositions + 1
            end if
         end do
      end do

      ! Print the number of special positions
      write(*,*) specialPositions

      end program specialPositions

This program uses a 2D array `mat` to store the input matrix. It then loops through each row and column, and checks if the current element is 1 and all other elements in the row and column are 0. If it is, then it is a special position, and the number of special positions is incremented. Finally, the number of special positions is printed to the console.

Note that this program assumes that the input matrix is square, and that the rows and columns are 0-indexed. If the input matrix is not square, or if the rows and columns are 1-indexed, then the program will need to be modified accordingly.
🌐 Data from online sources
class BrowserHistory:

    def __init__(self, homepage: str):
        self.history = [homepage]
        self.current_index = 0

    def visit(self, url: str):
        self.history = self.history[:self.current_index + 1]
        self.history.append(url)
        self.current_index += 1

    def back(self, steps: int) -> str:
        self.current_index -= min(steps, self.current_index)
        return self.history[self.current_index]

    def forward(self, steps:int) -> str:
        self.current_index += min(steps, len(self.history) - 1 - self.current_index)
        return self.history[self.current_index]
We have a BrowserHistory class which will keep track of visited urls as well as the current index of the history.

Whenever the object of the class is instantiated, we pass the homepage and store it in the history (array, list or vector) at 0 index.

For "visit(url)" function: - We will first resize the history (if needed) so that all the urls after the current index are removed, as whenever we visit a new URL, the forward history should be cleared. After that, we will append the new url and increment the current index.

For the "back(steps)" function: - We move the current index by the minimum of steps and the current index which ensures that the current index never goes below 0. And then, the function returns the URL at the updated index.

For the "forward(steps)" function: - We move the current index by the minimum of steps and the difference between the size of history and current index-1 (to ensure it doesn't exceed the index range). And then, return the URL at the updated index.

🌐 Data from online sources
class BrowserHistory {
public:
    int currentIndex;
    vector<string> history;

    BrowserHistory(string homepage) {
        history.push_back(homepage);
        currentIndex = 0;
    }

    void visit(string url) {
        history.resize(currentIndex + 1);
        history.push_back(url);
        currentIndex++;
    }

    string back(int steps) {
        currentIndex -= min(steps, currentIndex);
        return history[currentIndex];
    }

    string forward(int steps) {
        currentIndex += min(steps, (int)(history.size() - 1 - currentIndex));
        return history[currentIndex];
    }
};
We have a BrowserHistory class which will keep track of visited urls as well as the current index of the history.

Whenever the object of the class is instantiated, we pass the homepage and store it in the history (array, list or vector) at 0 index.

For "visit(url)" function: - We will first resize the history (if needed) so that all the urls after the current index are removed, as whenever we visit a new URL, the forward history should be cleared. After that, we will append the new url and increment the current index.

For the "back(steps)" function: - We move the current index by the minimum of steps and the current index which ensures that the current index never goes below 0. And then, the function returns the URL at the updated index.

For the "forward(steps)" function: - We move the current index by the minimum of steps and the difference between the size of history and current index-1 (to ensure it doesn't exceed the index range). And then, return the URL at the updated index.